| 1 16 package org.pentaho.core.solution; 17 18 import java.util.Iterator ; 19 import java.util.Map ; 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 script; 34 35 public ConditionalExecution() { 36 super(); 37 } 38 39 public String getScript() { 40 return script; 41 } 42 43 public void setScript(String script) { 44 this.script = script; 45 } 46 47 public boolean shouldExecute(Map currentInputs, Log logger) throws Exception { 48 boolean shouldExecute = true; 49 Context cx = Context.enter(); 50 try { 51 ScriptableObject scriptable = new RhinoScriptable(); 52 Scriptable scope = cx.initStandardObjects(scriptable); 54 ScriptableObject.defineClass(scope, JavaScriptResultSet.class); 55 Object inputValue; 56 IActionParameter inputParameter; 57 String inputName; 58 Iterator inputs = currentInputs.entrySet().iterator(); 59 Map.Entry mapEntry; 60 while (inputs.hasNext()) { 61 mapEntry = (Map.Entry )inputs.next(); 62 inputName = (String )mapEntry.getKey(); 63 if (inputName.indexOf('-')>=0) { 64 logger.info(Messages.getString("CONDITIONAL_EXECUTION.INFO_IGNORING_INPUT", inputName)); continue; 66 } 67 inputParameter = (IActionParameter)mapEntry.getValue(); 68 inputValue = inputParameter.getValue(); 69 70 Object 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 wrappedOut = Context.javaToJS(System.out, scope); 81 Object wrappedThis = Context.javaToJS(this, scope); 82 ScriptableObject.putProperty(scope, "out", wrappedOut); ScriptableObject.putProperty(scope, "rule", wrappedThis); 85 Object resultObject = cx.evaluateString(scope, script, "<cmd>", 1, null); 88 Object 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 ) { 95 return ((Boolean )actualObject).booleanValue(); 96 } else if (actualObject instanceof String ) { 97 return ("true".equalsIgnoreCase(actualObject.toString())) || ("yes".equalsIgnoreCase(actualObject.toString())); } else if (actualObject instanceof Number ) { 99 return ((Number )actualObject).intValue() > 0; 100 } else if (actualObject instanceof IPentahoResultSet) { 101 return ((IPentahoResultSet)actualObject).getRowCount()>0; 102 } 103 104 } finally { 107 Context.exit(); 108 } 109 return shouldExecute; 110 } 111 112 } 113 114 115 | Popular Tags |