Search in sources :

Example 1 with JavascriptAction

use of org.pentaho.actionsequence.dom.actions.JavascriptAction in project pentaho-platform by pentaho.

the class JavascriptRule method validateAction.

@Override
protected boolean validateAction() {
    boolean actionValidated = true;
    JavascriptAction jscriptAction = null;
    if (getActionDefinition() instanceof JavascriptAction) {
        jscriptAction = (JavascriptAction) getActionDefinition();
        // get report connection setting
        if (jscriptAction.getScript() == ActionInputConstant.NULL_INPUT) {
            // $NON-NLS-1$
            error(Messages.getInstance().getErrorString("JSRULE.ERROR_0001_SCRIPT_NOT_DEFINED", getActionName()));
            actionValidated = false;
        }
        if (actionValidated) {
            if (jscriptAction.getOutputs().length <= 0) {
                // $NON-NLS-1$
                error(Messages.getInstance().getString("Template.ERROR_0002_OUTPUT_COUNT_WRONG"));
                actionValidated = false;
            }
        }
        if (actionValidated) {
            // getOutputNames();
            IActionOutput[] outputs = jscriptAction.getOutputs();
            /*
         * if the number of action def outputs is more than 1 and if the fist output var defined in the input section is
         * named output1 then the xaction is defined oldstyle and we should check if there is corresponding o/p variable
         * defined in the input section for each output var defined in the output section. NOTE: With the old style you
         * can only have output defined as "output#" where # is a number.
         */
            if ((outputs.length > 1) && (jscriptAction.getInput("output1") != ActionInputConstant.NULL_INPUT)) {
                // $NON-NLS-1$
                oldStyleOutputs = true;
                for (int i = 1; i <= outputs.length; ++i) {
                    if (jscriptAction.getInput("output" + i) == ActionInputConstant.NULL_INPUT) {
                        // $NON-NLS-1$
                        error(Messages.getInstance().getErrorString("JavascriptRule.ERROR_0006_NO_MAPPED_OUTPUTS", String.valueOf(outputs.length), // $NON-NLS-1$
                        String.valueOf(i)));
                        actionValidated = false;
                        break;
                    }
                }
            }
        }
    } else {
        actionValidated = false;
        error(Messages.getInstance().getErrorString("ComponentBase.ERROR_0001_UNKNOWN_ACTION_TYPE", // $NON-NLS-1$
        getActionDefinition().getElement().asXML()));
    }
    return actionValidated;
}
Also used : IActionOutput(org.pentaho.actionsequence.dom.IActionOutput) JavascriptAction(org.pentaho.actionsequence.dom.actions.JavascriptAction)

Example 2 with JavascriptAction

use of org.pentaho.actionsequence.dom.actions.JavascriptAction in project pentaho-platform by pentaho.

the class JavascriptRule method executeAction.

/*
   * (non-Javadoc)
   * 
   * @see org.pentaho.component.ComponentBase#execute()
   */
@Override
protected boolean executeAction() {
    Context cx = ContextFactory.getGlobal().enterContext();
    StringBuffer buffer = new StringBuffer();
    @SuppressWarnings("unchecked") Iterator<String> iter = getResourceNames().iterator();
    while (iter.hasNext()) {
        IActionSequenceResource resource = getResource(iter.next().toString());
        // If this is a javascript resource then append it to the script string
        if ("text/javascript".equalsIgnoreCase(resource.getMimeType())) {
            // $NON-NLS-1$
            buffer.append(getResourceAsString(resource));
        }
    }
    List<String> outputNames = new ArrayList<String>();
    JavascriptAction jscriptAction = (JavascriptAction) getActionDefinition();
    IActionOutput[] actionOutputs = jscriptAction.getOutputs();
    if (actionOutputs.length == 1) {
        String outputName = actionOutputs[0].getName();
        outputNames.add(outputName);
    } else {
        if (oldStyleOutputs) {
            int i = 1;
            while (true) {
                if (jscriptAction.getInput("output" + i) != ActionInputConstant.NULL_INPUT) {
                    // $NON-NLS-1$
                    // $NON-NLS-1$
                    outputNames.add(jscriptAction.getInput("output" + i).getStringValue());
                } else {
                    break;
                }
                i++;
            }
        } else {
            for (IActionOutput element : actionOutputs) {
                outputNames.add(element.getName());
            }
        }
    }
    boolean success = false;
    try {
        String script = jscriptAction.getScript().getStringValue();
        if (script == null) {
            // $NON-NLS-1$
            error(Messages.getInstance().getErrorString("JSRULE.ERROR_0001_SCRIPT_NOT_DEFINED", getActionName()));
        } else {
            buffer.append(script);
            script = buffer.toString();
            if (ComponentBase.debug) {
                // $NON-NLS-1$
                debug("script=" + script);
            }
            try {
                ScriptableObject scriptable = new RhinoScriptable();
                // initialize the standard javascript objects
                Scriptable scope = cx.initStandardObjects(scriptable);
                Object resultObject = executeScript(scriptable, scope, script, cx);
                if (oldStyleOutputs) {
                    if (resultObject instanceof org.mozilla.javascript.NativeArray) {
                        // we need to convert this to an ArrayList
                        NativeArray jsArray = (NativeArray) resultObject;
                        int length = (int) jsArray.getLength();
                        for (int i = 0; i < length; i++) {
                            Object value = jsArray.get(i, scriptable);
                            if (i < outputNames.size()) {
                                jscriptAction.getOutput(outputNames.get(i).toString()).setValue(convertWrappedJavaObject(value));
                            } else {
                                break;
                            }
                        }
                    } else {
                        jscriptAction.getOutput(outputNames.get(0).toString()).setValue(convertWrappedJavaObject(resultObject));
                    }
                } else {
                    if ((outputNames.size() == 1) && (resultObject != null)) {
                        jscriptAction.getOutput(outputNames.get(0).toString()).setValue(convertWrappedJavaObject(resultObject));
                    } else {
                        List<String> setOutputs = new ArrayList<String>(outputNames.size());
                        Object[] ids = ScriptableObject.getPropertyIds(scope);
                        for (Object element : ids) {
                            int idx = outputNames.indexOf(element.toString());
                            if (idx >= 0) {
                                jscriptAction.getOutput(outputNames.get(idx).toString()).setValue(convertWrappedJavaObject(ScriptableObject.getProperty(scope, (String) element)));
                                setOutputs.add(outputNames.get(idx));
                            }
                        }
                        // So, set it to null.
                        if (setOutputs.size() != outputNames.size()) {
                            for (int i = 0; i < outputNames.size(); i++) {
                                if (setOutputs.indexOf(outputNames.get(i)) < 0) {
                                    // An output that wasn't set in the
                                    // javascript component
                                    jscriptAction.getOutput(outputNames.get(i).toString()).setValue(null);
                                }
                            }
                        }
                    }
                }
                success = true;
            } catch (Exception e) {
                // $NON-NLS-1$
                error(Messages.getInstance().getErrorString("JSRULE.ERROR_0003_EXECUTION_FAILED"), e);
            }
        }
    } finally {
        Context.exit();
    }
    return success;
}
Also used : Context(org.mozilla.javascript.Context) NativeArray(org.mozilla.javascript.NativeArray) ScriptableObject(org.mozilla.javascript.ScriptableObject) IActionOutput(org.pentaho.actionsequence.dom.IActionOutput) ArrayList(java.util.ArrayList) Scriptable(org.mozilla.javascript.Scriptable) RhinoScriptable(org.pentaho.platform.plugin.condition.javascript.RhinoScriptable) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource) RhinoScriptable(org.pentaho.platform.plugin.condition.javascript.RhinoScriptable) ScriptableObject(org.mozilla.javascript.ScriptableObject) JavascriptAction(org.pentaho.actionsequence.dom.actions.JavascriptAction)

Aggregations

IActionOutput (org.pentaho.actionsequence.dom.IActionOutput)2 JavascriptAction (org.pentaho.actionsequence.dom.actions.JavascriptAction)2 ArrayList (java.util.ArrayList)1 Context (org.mozilla.javascript.Context)1 NativeArray (org.mozilla.javascript.NativeArray)1 Scriptable (org.mozilla.javascript.Scriptable)1 ScriptableObject (org.mozilla.javascript.ScriptableObject)1 IActionSequenceResource (org.pentaho.platform.api.engine.IActionSequenceResource)1 RhinoScriptable (org.pentaho.platform.plugin.condition.javascript.RhinoScriptable)1