Search in sources :

Example 1 with ScriptValuesModDummy

use of org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesModDummy in project pentaho-kettle by pentaho.

the class ScriptValuesModDialog method test.

private boolean test(boolean getvars, boolean popup) {
    boolean retval = true;
    StyledTextComp wScript = getStyledTextComp();
    String scr = wScript.getText();
    KettleException testException = null;
    Context jscx;
    Scriptable jsscope;
    // Script jsscript;
    // Making Refresh to get Active Script State
    refreshScripts();
    jscx = ContextFactory.getGlobal().enterContext();
    jscx.setOptimizationLevel(-1);
    jsscope = jscx.initStandardObjects(null, false);
    // Adding the existing Scripts to the Context
    for (int i = 0; i < folder.getItemCount(); i++) {
        StyledTextComp sItem = getStyledTextComp(folder.getItem(i));
        Scriptable jsR = Context.toObject(sItem.getText(), jsscope);
        jsscope.put(folder.getItem(i).getText(), jsscope, jsR);
    }
    // Adding the Name of the Transformation to the Context
    jsscope.put("_TransformationName_", jsscope, this.stepname);
    try {
        RowMetaInterface rowMeta = transMeta.getPrevStepFields(stepname);
        if (rowMeta != null) {
            ScriptValuesModDummy dummyStep = new ScriptValuesModDummy(rowMeta, transMeta.getStepFields(stepname));
            Scriptable jsvalue = Context.toObject(dummyStep, jsscope);
            jsscope.put("_step_", jsscope, jsvalue);
            // Modification for Additional Script parsing
            try {
                if (input.getAddClasses() != null) {
                    for (int i = 0; i < input.getAddClasses().length; i++) {
                        Object jsOut = Context.javaToJS(input.getAddClasses()[i].getAddObject(), jsscope);
                        ScriptableObject.putProperty(jsscope, input.getAddClasses()[i].getJSName(), jsOut);
                    }
                }
            } catch (Exception e) {
                testException = new KettleException(BaseMessages.getString(PKG, "ScriptValuesDialogMod.CouldNotAddToContext", e.toString()));
                retval = false;
            }
            // Adding some default JavaScriptFunctions to the System
            try {
                Context.javaToJS(ScriptValuesAddedFunctions.class, jsscope);
                ((ScriptableObject) jsscope).defineFunctionProperties(jsFunctionList, ScriptValuesAddedFunctions.class, ScriptableObject.DONTENUM);
            } catch (Exception ex) {
                testException = new KettleException(BaseMessages.getString(PKG, "ScriptValuesDialogMod.CouldNotAddDefaultFunctions", ex.toString()));
                retval = false;
            }
            // Adding some Constants to the JavaScript
            try {
                jsscope.put("SKIP_TRANSFORMATION", jsscope, Integer.valueOf(SKIP_TRANSFORMATION));
                jsscope.put("ABORT_TRANSFORMATION", jsscope, Integer.valueOf(ABORT_TRANSFORMATION));
                jsscope.put("ERROR_TRANSFORMATION", jsscope, Integer.valueOf(ERROR_TRANSFORMATION));
                jsscope.put("CONTINUE_TRANSFORMATION", jsscope, Integer.valueOf(CONTINUE_TRANSFORMATION));
            } catch (Exception ex) {
                testException = new KettleException(BaseMessages.getString(PKG, "ScriptValuesDialogMod.CouldNotAddTransformationConstants", ex.toString()));
                retval = false;
            }
            try {
                Object[] row = new Object[rowMeta.size()];
                Scriptable jsRowMeta = Context.toObject(rowMeta, jsscope);
                jsscope.put("rowMeta", jsscope, jsRowMeta);
                for (int i = 0; i < rowMeta.size(); i++) {
                    ValueMetaInterface valueMeta = rowMeta.getValueMeta(i);
                    Object valueData = null;
                    // 
                    if (valueMeta.isDate()) {
                        valueData = new Date();
                    }
                    if (valueMeta.isString()) {
                        valueData = "test value test value test value test value test value " + "test value test value test value test value test value";
                    }
                    if (valueMeta.isInteger()) {
                        valueData = Long.valueOf(0L);
                    }
                    if (valueMeta.isNumber()) {
                        valueData = new Double(0.0);
                    }
                    if (valueMeta.isBigNumber()) {
                        valueData = BigDecimal.ZERO;
                    }
                    if (valueMeta.isBoolean()) {
                        valueData = Boolean.TRUE;
                    }
                    if (valueMeta.isBinary()) {
                        valueData = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                    }
                    if (valueMeta.isStorageBinaryString()) {
                        valueMeta.setStorageType(ValueMetaInterface.STORAGE_TYPE_NORMAL);
                    }
                    row[i] = valueData;
                    if (wCompatible.getSelection()) {
                        Value value = valueMeta.createOriginalValue(valueData);
                        Scriptable jsarg = Context.toObject(value, jsscope);
                        jsscope.put(valueMeta.getName(), jsscope, jsarg);
                    } else {
                        Scriptable jsarg = Context.toObject(valueData, jsscope);
                        jsscope.put(valueMeta.getName(), jsscope, jsarg);
                    }
                }
                // OK, for these input values, we're going to allow the user to edit the default values...
                // We are displaying a
                // 2)
                // Add support for Value class (new Value())
                Scriptable jsval = Context.toObject(Value.class, jsscope);
                jsscope.put("Value", jsscope, jsval);
                // 
                if (wCompatible.getSelection()) {
                    Row v2Row = RowMeta.createOriginalRow(rowMeta, row);
                    Scriptable jsV2Row = Context.toObject(v2Row, jsscope);
                    jsscope.put("row", jsscope, jsV2Row);
                } else {
                    Scriptable jsRow = Context.toObject(row, jsscope);
                    jsscope.put("row", jsscope, jsRow);
                }
            } catch (Exception ev) {
                testException = new KettleException(BaseMessages.getString(PKG, "ScriptValuesDialogMod.CouldNotAddInputFields", ev.toString()));
                retval = false;
            }
            try {
                // Checking for StartScript
                if (strActiveStartScript != null && !folder.getSelection().getText().equals(strActiveStartScript) && strActiveStartScript.length() > 0) {
                    String strStartScript = getStyledTextComp(folder.getItem(getCTabPosition(strActiveStartScript))).getText();
                    /* Object startScript = */
                    jscx.evaluateString(jsscope, strStartScript, "trans_Start", 1, null);
                }
            } catch (Exception e) {
                testException = new KettleException(BaseMessages.getString(PKG, "ScriptValuesDialogMod.CouldProcessStartScript", e.toString()));
                retval = false;
            }
            try {
                Script evalScript = jscx.compileString(scr, "script", 1, null);
                evalScript.exec(jscx, jsscope);
                if (getvars) {
                    ScriptNode tree = parseVariables(jscx, jsscope, scr, "script", 1, null);
                    for (int i = 0; i < tree.getParamAndVarCount(); i++) {
                        String varname = tree.getParamOrVarName(i);
                        if (!varname.equalsIgnoreCase("row") && !varname.equalsIgnoreCase("trans_Status")) {
                            int type = ValueMetaInterface.TYPE_STRING;
                            int length = -1, precision = -1;
                            Object result = jsscope.get(varname, jsscope);
                            if (result != null) {
                                String classname = result.getClass().getName();
                                if (classname.equalsIgnoreCase("java.lang.Byte")) {
                                    // MAX = 127
                                    type = ValueMetaInterface.TYPE_INTEGER;
                                    length = 3;
                                    precision = 0;
                                } else if (classname.equalsIgnoreCase("java.lang.Integer")) {
                                    // MAX = 2147483647
                                    type = ValueMetaInterface.TYPE_INTEGER;
                                    length = 9;
                                    precision = 0;
                                } else if (classname.equalsIgnoreCase("java.lang.Long")) {
                                    // MAX = 9223372036854775807
                                    type = ValueMetaInterface.TYPE_INTEGER;
                                    length = 18;
                                    precision = 0;
                                } else if (classname.equalsIgnoreCase("java.lang.Double")) {
                                    type = ValueMetaInterface.TYPE_NUMBER;
                                    length = 16;
                                    precision = 2;
                                } else if (classname.equalsIgnoreCase("org.mozilla.javascript.NativeDate") || classname.equalsIgnoreCase("java.util.Date")) {
                                    type = ValueMetaInterface.TYPE_DATE;
                                } else if (classname.equalsIgnoreCase("java.lang.Boolean")) {
                                    type = ValueMetaInterface.TYPE_BOOLEAN;
                                }
                            }
                            TableItem ti = new TableItem(wFields.table, SWT.NONE);
                            ti.setText(1, varname);
                            ti.setText(2, "");
                            ti.setText(3, ValueMetaFactory.getValueMetaName(type));
                            ti.setText(4, length >= 0 ? ("" + length) : "");
                            ti.setText(5, precision >= 0 ? ("" + precision) : "");
                            // If the variable name exists in the input, suggest to replace the value
                            // 
                            ti.setText(6, (rowMeta.indexOfValue(varname) >= 0) ? YES_NO_COMBO[1] : YES_NO_COMBO[0]);
                        }
                    }
                    wFields.removeEmptyRows();
                    wFields.setRowNums();
                    wFields.optWidth(true);
                }
            // End Script!
            } catch (EvaluatorException e) {
                String position = "(" + e.lineNumber() + ":" + e.columnNumber() + ")";
                String message = BaseMessages.getString(PKG, "ScriptValuesDialogMod.Exception.CouldNotExecuteScript", position);
                testException = new KettleException(message, e);
                retval = false;
            } catch (JavaScriptException e) {
                String position = "(" + e.lineNumber() + ":" + e.columnNumber() + ")";
                String message = BaseMessages.getString(PKG, "ScriptValuesDialogMod.Exception.CouldNotExecuteScript", position);
                testException = new KettleException(message, e);
                retval = false;
            } catch (Exception e) {
                testException = new KettleException(BaseMessages.getString(PKG, "ScriptValuesDialogMod.Exception.CouldNotExecuteScript2"), e);
                retval = false;
            }
        } else {
            testException = new KettleException(BaseMessages.getString(PKG, "ScriptValuesDialogMod.Exception.CouldNotGetFields"));
            retval = false;
        }
        if (popup) {
            if (retval) {
                if (!getvars) {
                    MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_INFORMATION);
                    mb.setMessage(BaseMessages.getString(PKG, "ScriptValuesDialogMod.ScriptCompilationOK") + Const.CR);
                    mb.setText("OK");
                    mb.open();
                }
            } else {
                new ErrorDialog(shell, BaseMessages.getString(PKG, "ScriptValuesDialogMod.TestFailed.DialogTitle"), BaseMessages.getString(PKG, "ScriptValuesDialogMod.TestFailed.DialogMessage"), testException);
            }
        }
    } catch (KettleException ke) {
        retval = false;
        new ErrorDialog(shell, BaseMessages.getString(PKG, "ScriptValuesDialogMod.TestFailed.DialogTitle"), BaseMessages.getString(PKG, "ScriptValuesDialogMod.TestFailed.DialogMessage"), ke);
    } finally {
        if (jscx != null) {
            Context.exit();
        }
    }
    return retval;
}
Also used : StyledTextComp(org.pentaho.di.ui.core.widget.StyledTextComp) KettleException(org.pentaho.di.core.exception.KettleException) TableItem(org.eclipse.swt.widgets.TableItem) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) EvaluatorException(org.mozilla.javascript.EvaluatorException) Context(org.mozilla.javascript.Context) ScriptValuesScript(org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesScript) Script(org.mozilla.javascript.Script) ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptValuesModDummy(org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesModDummy) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) Scriptable(org.mozilla.javascript.Scriptable) ScriptNode(org.mozilla.javascript.ast.ScriptNode) Point(org.eclipse.swt.graphics.Point) EvaluatorException(org.mozilla.javascript.EvaluatorException) JavaScriptException(org.mozilla.javascript.JavaScriptException) KettleException(org.pentaho.di.core.exception.KettleException) Date(java.util.Date) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) JavaScriptException(org.mozilla.javascript.JavaScriptException) MessageBox(org.eclipse.swt.widgets.MessageBox) Value(org.pentaho.di.compatibility.Value) ScriptableObject(org.mozilla.javascript.ScriptableObject) Row(org.pentaho.di.compatibility.Row)

Aggregations

Date (java.util.Date)1 Point (org.eclipse.swt.graphics.Point)1 MessageBox (org.eclipse.swt.widgets.MessageBox)1 TableItem (org.eclipse.swt.widgets.TableItem)1 Context (org.mozilla.javascript.Context)1 EvaluatorException (org.mozilla.javascript.EvaluatorException)1 JavaScriptException (org.mozilla.javascript.JavaScriptException)1 Script (org.mozilla.javascript.Script)1 Scriptable (org.mozilla.javascript.Scriptable)1 ScriptableObject (org.mozilla.javascript.ScriptableObject)1 ScriptNode (org.mozilla.javascript.ast.ScriptNode)1 Row (org.pentaho.di.compatibility.Row)1 Value (org.pentaho.di.compatibility.Value)1 KettleException (org.pentaho.di.core.exception.KettleException)1 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)1 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)1 ScriptValuesModDummy (org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesModDummy)1 ScriptValuesScript (org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesScript)1 ErrorDialog (org.pentaho.di.ui.core.dialog.ErrorDialog)1 StyledTextComp (org.pentaho.di.ui.core.widget.StyledTextComp)1