KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > Scriptlet


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

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 /**
23  * Script Model
24  *
25  * @author Jorg Janke
26  * @version $Id: Scriptlet.java,v 1.3 2002/08/12 01:55:12 danb Exp $
27  */

28 public class Scriptlet
29 {
30     /**
31      * Run Script
32      * @param variable
33      * @param script
34      * @param ctx
35      * @param WindowNo Included Window variables
36      * @return result
37      */

38     static Object JavaDoc run (String JavaDoc variable, String JavaDoc script, Properties ctx, int WindowNo)
39     {
40         Scriptlet scr = new Scriptlet (variable, script, ctx, WindowNo);
41         scr.execute();
42         return scr.getResult(false);
43     } // run
44

45     /**
46      * Constructor
47      */

48     public Scriptlet()
49     {
50         this(VARIABLE, "", Env.getCtx(), 0);
51     } // Scriptlet
52

53     /** Default Result Variable Name */
54     public static final String JavaDoc VARIABLE = "result";
55
56
57     /**
58      * Full Constructor
59      *
60      * @param variable Variable Name
61      * @param script The Script
62      * @param prop Environment
63      * @param WindowNo Included Window variables
64      */

65     public Scriptlet (String JavaDoc variable, String JavaDoc script, Properties prop, int WindowNo)
66     {
67         setVariable(variable);
68         setScript(script);
69         setEnvironment(prop, WindowNo);
70     } // Scriptlet
71

72     /**
73      * Full Constructor
74      *
75      * @param variable Variable Name
76      * @param script The Script
77      * @param ctx Environment
78      */

79     public Scriptlet (String JavaDoc variable, String JavaDoc script, HashMap ctx)
80     {
81         setVariable(variable);
82         setScript(script);
83         setEnvironment(ctx);
84     } // Scriptlet
85

86     private String JavaDoc m_variable;
87     private String JavaDoc m_script;
88     private HashMap m_ctx;
89     private Object JavaDoc m_result;
90
91     /*************************************************************************/
92
93     /**
94      * Execute Script
95      * Loads environment and saves result
96      * @return null or Exception
97      */

98     public Exception JavaDoc 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 JavaDoc e = new IllegalArgumentException JavaDoc("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 JavaDoc 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 JavaDoc e)
125         {
126             Log.trace(Log.l3_Util, "Scriptlet.execute Result", e);
127             if (e instanceof NullPointerException JavaDoc)
128                 e = new IllegalArgumentException JavaDoc("Result Variable not found - " + m_variable);
129             return e;
130         }
131         return null;
132     } // execute
133

134     /**
135      * Set Environment for Interpreter
136      *
137      * @param i Interpreter
138      */

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 JavaDoc key = (String JavaDoc)it.next();
147             Object JavaDoc value = m_ctx.get(key);
148             try
149             {
150                 if (value instanceof Boolean JavaDoc)
151                     i.set(key, ((Boolean JavaDoc)value).booleanValue());
152                 else if (value instanceof Integer JavaDoc)
153                     i.set(key, ((Integer JavaDoc)value).intValue());
154                 else if (value instanceof Double JavaDoc)
155                     i.set(key, ((Double JavaDoc)value).doubleValue());
156                 else
157                     i.set(key, value);
158             }
159             catch (EvalError ee)
160             {
161                 Log.error("Scriptlet.setEnvironment", ee);
162             }
163         }
164     } // setEnvironment
165

166     /*************************************************************************/
167
168     /**
169      * Get Variable
170      * @return variable
171      */

172     public String JavaDoc getVariable()
173     {
174         return m_variable;
175     } // getVariable
176

177     /**
178      * Set Variable
179      * @param variable - if null set to VARIABLE
180      * @see VARIABLE
181      */

182     public void setVariable(String JavaDoc variable)
183     {
184         if (variable == null || variable.length() == 0)
185             m_variable = VARIABLE;
186         else
187             m_variable = variable;
188     }
189
190     /**
191      * Set Script
192      * @param script
193      */

194     public void setScript(String JavaDoc script)
195     {
196         if (script == null)
197             m_script = "";
198         else
199             m_script = script;
200     } // setScript
201

202     /**
203      * Get Script
204      * @return script
205      */

206     public String JavaDoc getScript()
207     {
208         return m_script;
209     } // getScript
210

211     /**
212      * Set Environment
213      * @param prop
214      * @param WindowNo included Window variables
215      */

216     public void setEnvironment (Properties prop, int WindowNo)
217     {
218         if (prop == null)
219             prop = Env.getCtx();
220
221         m_ctx = new HashMap();
222         // Convert properties to HashMap
223
Enumeration en = prop.keys();
224         while (en.hasMoreElements())
225         {
226             String JavaDoc key = en.nextElement().toString();
227             // filter
228
if (key == null || key.length() == 0
229                 || key.startsWith("P") // Preferences
230
|| (key.indexOf("|") != -1 && !key.startsWith(String.valueOf(WindowNo))) // other Window Settings
231
)
232                 continue;
233
234             String JavaDoc value = prop.getProperty(key);
235             setEnvironment (key, value);
236         }
237
238     } // setEnvironment
239

240     /**
241      * Set Environment key to value
242      *
243      * @param key variable name ('#' will be converted to '_')
244      * @param stringValue try to convert to Object
245      */

246     public void setEnvironment (String JavaDoc key, String JavaDoc stringValue)
247     {
248         if (key == null || key.length() == 0)
249             return;
250     // Log.trace(Log.l5_DData, "Scriptlet.setEnvironment " + key, stringValue);
251
if (stringValue == null)
252         {
253             m_ctx.remove(key);
254             return;
255         }
256
257         // Boolean
258
if (stringValue.equals("Y"))
259         {
260             m_ctx.put(convertKey(key), new Boolean JavaDoc(true));
261             return;
262         }
263         if (stringValue.equals("N"))
264         {
265             m_ctx.put(convertKey(key), new Boolean JavaDoc(false));
266             return;
267         }
268
269         // Timestamp
270
Timestamp timeValue = null;
271         try
272         {
273             timeValue = Timestamp.valueOf(stringValue);
274             m_ctx.put(convertKey(key), timeValue);
275             return;
276         }
277         catch (Exception JavaDoc e) {}
278
279         // Numeric
280
Integer JavaDoc intValue = null;
281         try {
282             intValue = Integer.valueOf(stringValue);
283         } catch (Exception JavaDoc e) {}
284         Double JavaDoc doubleValue = null;
285         try {
286             doubleValue = Double.valueOf(stringValue);
287         } catch (Exception JavaDoc e) {}
288         if (doubleValue != null)
289         {
290             if (intValue != null)
291             {
292                 double di = Double.parseDouble(intValue.toString());
293                 // the numbers are the same -> integer
294
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     } // SetEnvironment
310

311     /**
312      * Set Environment key to value
313      *
314      * @param key variable name ('#' will be vonverted to '_')
315      * @param value
316      */

317     public void setEnvironment (String JavaDoc key, Object JavaDoc value)
318     {
319         if (key != null && key.length() > 0)
320         {
321         // Log.trace(Log.l5_DData, "Scriptlet.setEnvironment " + key, value);
322
if (value == null)
323                 m_ctx.remove(key);
324             else
325                 m_ctx.put(convertKey(key), value);
326         }
327     } // SetEnvironment
328

329     /**
330      * Convert Key
331      * # -> _
332      * @param key
333      * @return converted key
334      */

335     private String JavaDoc convertKey (String JavaDoc key)
336     {
337         String JavaDoc retValue = Util.replace(key, "#", "_");
338         return retValue;
339     } // convertKey
340

341     /**
342      * Set Environment
343      * @param ctx
344      */

345     public void setEnvironment (HashMap ctx)
346     {
347         if (ctx == null)
348             m_ctx = new HashMap();
349         else
350             m_ctx = ctx;
351     } // setEnvironment
352

353     /**
354      * Get Environment
355      * @return environment
356      */

357     public HashMap getEnvironment()
358     {
359         return m_ctx;
360     } // getEnvironment
361

362     /*************************************************************************/
363
364     /**
365      * Get Result
366      * @param runIt if true, execute script
367      * @return result or null
368      */

369     public Object JavaDoc getResult (boolean runIt)
370     {
371         if (runIt)
372             execute();
373         return m_result;
374     } // getResult
375

376     /**
377      * String Representation incl. Result
378      * @return Scipt
379      */

380     public String JavaDoc toString()
381     {
382         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(m_variable);
383         sb.append(" { ").append(m_script).append(" } = ").append(getResult(true));
384         return sb.toString();
385     } // toString
386

387 } // Scriptlet
388
Popular Tags