KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > core > solution > ConditionalExecution


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created
14  * @author
15  */

16 package org.pentaho.core.solution;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.commons.logging.Log;
22 import org.mozilla.javascript.Context;
23 import org.mozilla.javascript.Scriptable;
24 import org.mozilla.javascript.ScriptableObject;
25 import org.pentaho.core.connection.IPentahoResultSet;
26 import org.pentaho.core.connection.javascript.JavaScriptResultSet;
27 import org.pentaho.core.connection.javascript.RhinoScriptable;
28 import org.pentaho.core.runtime.IActionParameter;
29 import org.pentaho.messages.Messages;
30
31 public class ConditionalExecution {
32
33   private String JavaDoc script;
34   
35   public ConditionalExecution() {
36     super();
37   }
38
39   public String JavaDoc getScript() {
40     return script;
41   }
42
43   public void setScript(String JavaDoc script) {
44     this.script = script;
45   }
46   
47   public boolean shouldExecute(Map JavaDoc currentInputs, Log logger) throws Exception JavaDoc {
48     boolean shouldExecute = true;
49     Context cx = Context.enter();
50     try {
51       ScriptableObject scriptable = new RhinoScriptable();
52       // initialize the standard javascript objects
53
Scriptable scope = cx.initStandardObjects(scriptable);
54       ScriptableObject.defineClass(scope, JavaScriptResultSet.class);
55       Object JavaDoc inputValue;
56       IActionParameter inputParameter;
57       String JavaDoc inputName;
58       Iterator JavaDoc inputs = currentInputs.entrySet().iterator();
59       Map.Entry JavaDoc mapEntry;
60       while (inputs.hasNext()) {
61         mapEntry = (Map.Entry JavaDoc)inputs.next();
62         inputName = (String JavaDoc)mapEntry.getKey();
63         if (inputName.indexOf('-')>=0) {
64           logger.info(Messages.getString("CONDITIONAL_EXECUTION.INFO_IGNORING_INPUT", inputName)); //$NON-NLS-1$
65
continue;
66         }
67         inputParameter = (IActionParameter)mapEntry.getValue();
68         inputValue = inputParameter.getValue();
69         
70         Object JavaDoc wrapper;
71         if (inputValue instanceof IPentahoResultSet) {
72             JavaScriptResultSet results = new JavaScriptResultSet();
73             results.setResultSet((IPentahoResultSet) inputValue);
74             wrapper = Context.javaToJS(inputValue, results);
75         } else {
76             wrapper = Context.javaToJS(inputValue, scope);
77         }
78         ScriptableObject.putProperty(scope, inputName, wrapper);
79       }
80       Object JavaDoc wrappedOut = Context.javaToJS(System.out, scope);
81       Object JavaDoc wrappedThis = Context.javaToJS(this, scope);
82       ScriptableObject.putProperty(scope, "out", wrappedOut); //$NON-NLS-1$
83
ScriptableObject.putProperty(scope, "rule", wrappedThis); //$NON-NLS-1$
84

85       // evaluate the script
86
Object JavaDoc resultObject = cx.evaluateString(scope, script, "<cmd>", 1, null); //$NON-NLS-1$
87

88       Object JavaDoc actualObject = null;
89       if (resultObject instanceof org.mozilla.javascript.NativeJavaObject) {
90         actualObject = ((org.mozilla.javascript.NativeJavaObject) resultObject).unwrap();
91       } else {
92         actualObject = resultObject;
93       }
94       if (actualObject instanceof Boolean JavaDoc) {
95         return ((Boolean JavaDoc)actualObject).booleanValue();
96       } else if (actualObject instanceof String JavaDoc) {
97         return ("true".equalsIgnoreCase(actualObject.toString())) || ("yes".equalsIgnoreCase(actualObject.toString())); //$NON-NLS-1$ //$NON-NLS-2$
98
} else if (actualObject instanceof Number JavaDoc) {
99         return ((Number JavaDoc)actualObject).intValue() > 0;
100       } else if (actualObject instanceof IPentahoResultSet) {
101         return ((IPentahoResultSet)actualObject).getRowCount()>0;
102       }
103       
104 // } catch (Exception e) {
105
// logger.error("Error executing conditional execution script.", e);
106
} finally {
107       Context.exit();
108     }
109     return shouldExecute;
110   }
111   
112 }
113
114
115
Popular Tags