1 14 package org.compiere.model; 15 16 import java.util.*; 17 import java.sql.*; 18 import bsh.*; 19 20 import org.compiere.util.*; 21 22 28 public class Scriptlet 29 { 30 38 static Object run (String variable, String script, Properties ctx, int WindowNo) 39 { 40 Scriptlet scr = new Scriptlet (variable, script, ctx, WindowNo); 41 scr.execute(); 42 return scr.getResult(false); 43 } 45 48 public Scriptlet() 49 { 50 this(VARIABLE, "", Env.getCtx(), 0); 51 } 53 54 public static final String VARIABLE = "result"; 55 56 57 65 public Scriptlet (String variable, String script, Properties prop, int WindowNo) 66 { 67 setVariable(variable); 68 setScript(script); 69 setEnvironment(prop, WindowNo); 70 } 72 79 public Scriptlet (String variable, String script, HashMap ctx) 80 { 81 setVariable(variable); 82 setScript(script); 83 setEnvironment(ctx); 84 } 86 private String m_variable; 87 private String m_script; 88 private HashMap m_ctx; 89 private Object m_result; 90 91 92 93 98 public Exception execute() 99 { 100 m_result = null; 101 if (m_variable == null || m_variable.length() == 0 || m_script == null || m_script.length() == 0) 102 { 103 IllegalArgumentException e = new IllegalArgumentException ("No variable/script"); 104 Log.trace(Log.l3_Util, "Scriptlet.execute", e); 105 return e; 106 } 107 Interpreter i = new Interpreter(); 108 loadEnvironment(i); 109 try 110 { 111 Log.trace(Log.l3_Util, "Scriptlet.execute Script", m_script); 112 i.eval(m_script); 113 } 114 catch (Exception e) 115 { 116 Log.trace(Log.l3_Util, "Scriptlet.execute Error", e); 117 return e; 118 } 119 try 120 { 121 m_result = i.get (m_variable); 122 Log.trace(Log.l3_Util, "Scriptlet.execute Result (" + m_result.getClass().getName() + ")", m_result); 123 } 124 catch (Exception e) 125 { 126 Log.trace(Log.l3_Util, "Scriptlet.execute Result", e); 127 if (e instanceof NullPointerException ) 128 e = new IllegalArgumentException ("Result Variable not found - " + m_variable); 129 return e; 130 } 131 return null; 132 } 134 139 private void loadEnvironment (Interpreter i) 140 { 141 if (m_ctx == null) 142 return; 143 Iterator it = m_ctx.keySet().iterator(); 144 while (it.hasNext()) 145 { 146 String key = (String )it.next(); 147 Object value = m_ctx.get(key); 148 try 149 { 150 if (value instanceof Boolean ) 151 i.set(key, ((Boolean )value).booleanValue()); 152 else if (value instanceof Integer ) 153 i.set(key, ((Integer )value).intValue()); 154 else if (value instanceof Double ) 155 i.set(key, ((Double )value).doubleValue()); 156 else 157 i.set(key, value); 158 } 159 catch (EvalError ee) 160 { 161 Log.error("Scriptlet.setEnvironment", ee); 162 } 163 } 164 } 166 167 168 172 public String getVariable() 173 { 174 return m_variable; 175 } 177 182 public void setVariable(String variable) 183 { 184 if (variable == null || variable.length() == 0) 185 m_variable = VARIABLE; 186 else 187 m_variable = variable; 188 } 189 190 194 public void setScript(String script) 195 { 196 if (script == null) 197 m_script = ""; 198 else 199 m_script = script; 200 } 202 206 public String getScript() 207 { 208 return m_script; 209 } 211 216 public void setEnvironment (Properties prop, int WindowNo) 217 { 218 if (prop == null) 219 prop = Env.getCtx(); 220 221 m_ctx = new HashMap(); 222 Enumeration en = prop.keys(); 224 while (en.hasMoreElements()) 225 { 226 String key = en.nextElement().toString(); 227 if (key == null || key.length() == 0 229 || key.startsWith("P") || (key.indexOf("|") != -1 && !key.startsWith(String.valueOf(WindowNo))) ) 232 continue; 233 234 String value = prop.getProperty(key); 235 setEnvironment (key, value); 236 } 237 238 } 240 246 public void setEnvironment (String key, String stringValue) 247 { 248 if (key == null || key.length() == 0) 249 return; 250 if (stringValue == null) 252 { 253 m_ctx.remove(key); 254 return; 255 } 256 257 if (stringValue.equals("Y")) 259 { 260 m_ctx.put(convertKey(key), new Boolean (true)); 261 return; 262 } 263 if (stringValue.equals("N")) 264 { 265 m_ctx.put(convertKey(key), new Boolean (false)); 266 return; 267 } 268 269 Timestamp timeValue = null; 271 try 272 { 273 timeValue = Timestamp.valueOf(stringValue); 274 m_ctx.put(convertKey(key), timeValue); 275 return; 276 } 277 catch (Exception e) {} 278 279 Integer intValue = null; 281 try { 282 intValue = Integer.valueOf(stringValue); 283 } catch (Exception e) {} 284 Double doubleValue = null; 285 try { 286 doubleValue = Double.valueOf(stringValue); 287 } catch (Exception e) {} 288 if (doubleValue != null) 289 { 290 if (intValue != null) 291 { 292 double di = Double.parseDouble(intValue.toString()); 293 if (Double.compare(di, doubleValue.doubleValue()) == 0) 295 { 296 m_ctx.put(convertKey(key), intValue); 297 return; 298 } 299 } 300 m_ctx.put(convertKey(key), doubleValue); 301 return; 302 } 303 if (intValue != null) 304 { 305 m_ctx.put(convertKey(key), intValue); 306 return; 307 } 308 m_ctx.put(convertKey(key), stringValue); 309 } 311 317 public void setEnvironment (String key, Object value) 318 { 319 if (key != null && key.length() > 0) 320 { 321 if (value == null) 323 m_ctx.remove(key); 324 else 325 m_ctx.put(convertKey(key), value); 326 } 327 } 329 335 private String convertKey (String key) 336 { 337 String retValue = Util.replace(key, "#", "_"); 338 return retValue; 339 } 341 345 public void setEnvironment (HashMap ctx) 346 { 347 if (ctx == null) 348 m_ctx = new HashMap(); 349 else 350 m_ctx = ctx; 351 } 353 357 public HashMap getEnvironment() 358 { 359 return m_ctx; 360 } 362 363 364 369 public Object getResult (boolean runIt) 370 { 371 if (runIt) 372 execute(); 373 return m_result; 374 } 376 380 public String toString() 381 { 382 StringBuffer sb = new StringBuffer (m_variable); 383 sb.append(" { ").append(m_script).append(" } = ").append(getResult(true)); 384 return sb.toString(); 385 } 387 } | Popular Tags |