001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created
014: * @author
015: */
016: package org.pentaho.core.solution;
017:
018: import java.util.Iterator;
019: import java.util.Map;
020:
021: import org.apache.commons.logging.Log;
022: import org.mozilla.javascript.Context;
023: import org.mozilla.javascript.Scriptable;
024: import org.mozilla.javascript.ScriptableObject;
025: import org.pentaho.commons.connection.IPentahoResultSet;
026: import org.pentaho.core.runtime.IActionParameter;
027: import org.pentaho.messages.Messages;
028: import org.pentaho.data.connection.javascript.JavaScriptResultSet;
029: import org.pentaho.data.connection.javascript.RhinoScriptable;
030:
031: public class ConditionalExecution {
032:
033: private String script;
034:
035: public ConditionalExecution() {
036: super ();
037: }
038:
039: public String getScript() {
040: return script;
041: }
042:
043: public void setScript(String script) {
044: this .script = script;
045: }
046:
047: public boolean shouldExecute(Map currentInputs, Log logger)
048: throws Exception {
049: boolean shouldExecute = true;
050: Context cx = Context.enter();
051: try {
052: ScriptableObject scriptable = new RhinoScriptable();
053: // initialize the standard javascript objects
054: Scriptable scope = cx.initStandardObjects(scriptable);
055: ScriptableObject.defineClass(scope,
056: JavaScriptResultSet.class);
057: Object inputValue;
058: IActionParameter inputParameter;
059: String inputName;
060: Iterator inputs = currentInputs.entrySet().iterator();
061: Map.Entry mapEntry;
062: while (inputs.hasNext()) {
063: mapEntry = (Map.Entry) inputs.next();
064: inputName = (String) mapEntry.getKey();
065: if (inputName.indexOf('-') >= 0) {
066: logger
067: .info(Messages
068: .getString(
069: "CONDITIONAL_EXECUTION.INFO_IGNORING_INPUT", inputName)); //$NON-NLS-1$
070: continue;
071: }
072: inputParameter = (IActionParameter) mapEntry.getValue();
073: inputValue = inputParameter.getValue();
074:
075: Object wrapper;
076: if (inputValue instanceof IPentahoResultSet) {
077: JavaScriptResultSet results = new JavaScriptResultSet();
078: results
079: .setResultSet((IPentahoResultSet) inputValue);
080: wrapper = Context.javaToJS(inputValue, results);
081: } else {
082: wrapper = Context.javaToJS(inputValue, scope);
083: }
084: ScriptableObject.putProperty(scope, inputName, wrapper);
085: }
086: Object wrappedOut = Context.javaToJS(System.out, scope);
087: Object wrappedThis = Context.javaToJS(this , scope);
088: ScriptableObject.putProperty(scope, "out", wrappedOut); //$NON-NLS-1$
089: ScriptableObject.putProperty(scope, "rule", wrappedThis); //$NON-NLS-1$
090:
091: // evaluate the script
092: Object resultObject = cx.evaluateString(scope, script,
093: "<cmd>", 1, null); //$NON-NLS-1$
094:
095: Object actualObject = null;
096: if (resultObject instanceof org.mozilla.javascript.NativeJavaObject) {
097: actualObject = ((org.mozilla.javascript.NativeJavaObject) resultObject)
098: .unwrap();
099: } else {
100: actualObject = resultObject;
101: }
102: if (actualObject instanceof Boolean) {
103: return ((Boolean) actualObject).booleanValue();
104: } else if (actualObject instanceof String) {
105: return ("true".equalsIgnoreCase(actualObject.toString())) || ("yes".equalsIgnoreCase(actualObject.toString())); //$NON-NLS-1$ //$NON-NLS-2$
106: } else if (actualObject instanceof Number) {
107: return ((Number) actualObject).intValue() > 0;
108: } else if (actualObject instanceof IPentahoResultSet) {
109: return ((IPentahoResultSet) actualObject).getRowCount() > 0;
110: }
111:
112: // } catch (Exception e) {
113: // logger.error("Error executing conditional execution script.", e);
114: } finally {
115: Context.exit();
116: }
117: return shouldExecute;
118: }
119:
120: }
|