Search in sources :

Example 1 with Row

use of org.pentaho.di.compatibility.Row in project pentaho-kettle by pentaho.

the class RowMeta method createOriginalRow.

public static Row createOriginalRow(RowMetaInterface rowMeta, Object[] rowData) throws KettleValueException {
    Row row = new Row();
    for (int i = 0; i < rowMeta.size(); i++) {
        ValueMetaInterface valueMeta = rowMeta.getValueMeta(i);
        Object valueData = rowData[i];
        Value value = valueMeta.createOriginalValue(valueData);
        row.addValue(value);
    }
    return row;
}
Also used : Value(org.pentaho.di.compatibility.Value) Row(org.pentaho.di.compatibility.Row)

Example 2 with Row

use of org.pentaho.di.compatibility.Row in project pentaho-kettle by pentaho.

the class ScriptValuesMetaMod method check.

public void check(List<CheckResultInterface> remarks, TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev, String[] input, String[] output, RowMetaInterface info, VariableSpace space, Repository repository, IMetaStore metaStore) {
    boolean error_found = false;
    String error_message = "";
    CheckResult cr;
    Context jscx;
    Scriptable jsscope;
    Script jsscript;
    jscx = ContextFactory.getGlobal().enterContext();
    jsscope = jscx.initStandardObjects(null, false);
    try {
        jscx.setOptimizationLevel(Integer.valueOf(transMeta.environmentSubstitute(optimizationLevel)));
    } catch (NumberFormatException nfe) {
        error_message = "Error with optimization level.  Could not convert the value of " + transMeta.environmentSubstitute(optimizationLevel) + " to an integer.";
        cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
        remarks.add(cr);
    } catch (IllegalArgumentException iae) {
        cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, iae.getMessage(), stepMeta);
        remarks.add(cr);
    }
    String strActiveStartScriptName = "";
    String strActiveEndScriptName = "";
    String strActiveScript = "";
    String strActiveStartScript = "";
    String strActiveEndScript = "";
    // Building the Scripts
    if (jsScripts.length > 0) {
        for (int i = 0; i < jsScripts.length; i++) {
            if (jsScripts[i].isTransformScript()) {
                // strActiveScriptName =jsScripts[i].getScriptName();
                strActiveScript = jsScripts[i].getScript();
            } else if (jsScripts[i].isStartScript()) {
                strActiveStartScriptName = jsScripts[i].getScriptName();
                strActiveStartScript = jsScripts[i].getScript();
            } else if (jsScripts[i].isEndScript()) {
                strActiveEndScriptName = jsScripts[i].getScriptName();
                strActiveEndScript = jsScripts[i].getScript();
            }
        }
    }
    if (prev != null && strActiveScript.length() > 0) {
        cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.ConnectedStepOK", String.valueOf(prev.size())), stepMeta);
        remarks.add(cr);
        // Adding the existing Scripts to the Context
        for (int i = 0; i < getNumberOfJSScripts(); i++) {
            Scriptable jsR = Context.toObject(jsScripts[i].getScript(), jsscope);
            jsscope.put(jsScripts[i].getScriptName(), jsscope, jsR);
        }
        // Modification for Additional Script parsing
        try {
            if (getAddClasses() != null) {
                for (int i = 0; i < getAddClasses().length; i++) {
                    Object jsOut = Context.javaToJS(getAddClasses()[i].getAddObject(), jsscope);
                    ScriptableObject.putProperty(jsscope, getAddClasses()[i].getJSName(), jsOut);
                }
            }
        } catch (Exception e) {
            error_message = ("Couldn't add JavaClasses to Context! Error:");
            cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
            remarks.add(cr);
        }
        // Adding some default JavaScriptFunctions to the System
        try {
            Context.javaToJS(ScriptValuesAddedFunctions.class, jsscope);
            ((ScriptableObject) jsscope).defineFunctionProperties(ScriptValuesAddedFunctions.jsFunctionList, ScriptValuesAddedFunctions.class, ScriptableObject.DONTENUM);
        } catch (Exception ex) {
            error_message = "Couldn't add Default Functions! Error:" + Const.CR + ex.toString();
            cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
            remarks.add(cr);
        }
        // Adding some Constants to the JavaScript
        try {
            jsscope.put("SKIP_TRANSFORMATION", jsscope, Integer.valueOf(ScriptValuesMod.SKIP_TRANSFORMATION));
            jsscope.put("ABORT_TRANSFORMATION", jsscope, Integer.valueOf(ScriptValuesMod.ABORT_TRANSFORMATION));
            jsscope.put("ERROR_TRANSFORMATION", jsscope, Integer.valueOf(ScriptValuesMod.ERROR_TRANSFORMATION));
            jsscope.put("CONTINUE_TRANSFORMATION", jsscope, Integer.valueOf(ScriptValuesMod.CONTINUE_TRANSFORMATION));
        } catch (Exception ex) {
            error_message = "Couldn't add Transformation Constants! Error:" + Const.CR + ex.toString();
            cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
            remarks.add(cr);
        }
        try {
            ScriptValuesModDummy dummyStep = new ScriptValuesModDummy(prev, transMeta.getStepFields(stepMeta));
            Scriptable jsvalue = Context.toObject(dummyStep, jsscope);
            jsscope.put("_step_", jsscope, jsvalue);
            Object[] row = new Object[prev.size()];
            Scriptable jsRowMeta = Context.toObject(prev, jsscope);
            jsscope.put("rowMeta", jsscope, jsRowMeta);
            for (int i = 0; i < prev.size(); i++) {
                ValueMetaInterface valueMeta = prev.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 };
                }
                row[i] = valueData;
                if (isCompatible()) {
                    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);
                }
            }
            // Add support for Value class (new Value())
            Scriptable jsval = Context.toObject(Value.class, jsscope);
            jsscope.put("Value", jsscope, jsval);
            // 
            if (isCompatible()) {
                Row v2Row = RowMeta.createOriginalRow(prev, 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) {
            error_message = "Couldn't add Input fields to Script! Error:" + Const.CR + ev.toString();
            cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
            remarks.add(cr);
        }
        try {
            // Checking for StartScript
            if (strActiveStartScript != null && strActiveStartScript.length() > 0) {
                /* Object startScript = */
                jscx.evaluateString(jsscope, strActiveStartScript, "trans_Start", 1, null);
                error_message = "Found Start Script. " + strActiveStartScriptName + " Processing OK";
                cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, error_message, stepMeta);
                remarks.add(cr);
            }
        } catch (Exception e) {
            error_message = "Couldn't process Start Script! Error:" + Const.CR + e.toString();
            cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
            remarks.add(cr);
        }
        try {
            jsscript = jscx.compileString(strActiveScript, "script", 1, null);
            cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.ScriptCompiledOK"), stepMeta);
            remarks.add(cr);
            try {
                jsscript.exec(jscx, jsscope);
                cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.ScriptCompiledOK2"), stepMeta);
                remarks.add(cr);
                if (fieldname.length > 0) {
                    StringBuilder message = new StringBuilder(BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.FailedToGetValues", String.valueOf(fieldname.length)) + Const.CR + Const.CR);
                    if (error_found) {
                        cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, message.toString(), stepMeta);
                    } else {
                        cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, message.toString(), stepMeta);
                    }
                    remarks.add(cr);
                }
            } catch (JavaScriptException jse) {
                Context.exit();
                error_message = BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.CouldNotExecuteScript") + Const.CR + jse.toString();
                cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
                remarks.add(cr);
            } catch (Exception e) {
                Context.exit();
                error_message = BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.CouldNotExecuteScript2") + Const.CR + e.toString();
                cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
                remarks.add(cr);
            }
            // Checking End Script
            try {
                if (strActiveEndScript != null && strActiveEndScript.length() > 0) {
                    /* Object endScript = */
                    jscx.evaluateString(jsscope, strActiveEndScript, "trans_End", 1, null);
                    error_message = "Found End Script. " + strActiveEndScriptName + " Processing OK";
                    cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, error_message, stepMeta);
                    remarks.add(cr);
                }
            } catch (Exception e) {
                error_message = "Couldn't process End Script! Error:" + Const.CR + e.toString();
                cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
                remarks.add(cr);
            }
        } catch (Exception e) {
            Context.exit();
            error_message = BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.CouldNotCompileScript") + Const.CR + e.toString();
            cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
            remarks.add(cr);
        }
    } else {
        Context.exit();
        error_message = BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.CouldNotGetFieldsFromPreviousStep");
        cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
        remarks.add(cr);
    }
    // See if we have input streams leading to this step!
    if (input.length > 0) {
        cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.ConnectedStepOK2"), stepMeta);
        remarks.add(cr);
    } else {
        cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.NoInputReceived"), stepMeta);
        remarks.add(cr);
    }
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable) KettleException(org.pentaho.di.core.exception.KettleException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) JavaScriptException(org.mozilla.javascript.JavaScriptException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) Date(java.util.Date) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) JavaScriptException(org.mozilla.javascript.JavaScriptException) CheckResult(org.pentaho.di.core.CheckResult) Value(org.pentaho.di.compatibility.Value) ScriptableObject(org.mozilla.javascript.ScriptableObject) Row(org.pentaho.di.compatibility.Row)

Example 3 with Row

use of org.pentaho.di.compatibility.Row in project pentaho-kettle by pentaho.

the class ScriptValuesMod method addValues.

private boolean addValues(RowMetaInterface rowMeta, Object[] row) throws KettleException {
    if (first) {
        first = false;
        // What is the output row looking like?
        // 
        data.outputRowMeta = getInputRowMeta().clone();
        meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);
        // Determine the indexes of the fields used!
        // 
        determineUsedFields(rowMeta);
        // Get the indexes of the replaced fields...
        // 
        data.replaceIndex = new int[meta.getFieldname().length];
        for (int i = 0; i < meta.getFieldname().length; i++) {
            if (meta.getReplace()[i]) {
                data.replaceIndex[i] = rowMeta.indexOfValue(meta.getFieldname()[i]);
                if (data.replaceIndex[i] < 0) {
                    if (Utils.isEmpty(meta.getFieldname()[i])) {
                        throw new KettleStepException(BaseMessages.getString(PKG, "ScriptValuesMetaMod.Exception.FieldToReplaceNotFound", meta.getFieldname()[i]));
                    }
                    data.replaceIndex[i] = rowMeta.indexOfValue(meta.getRename()[i]);
                    if (data.replaceIndex[i] < 0) {
                        throw new KettleStepException(BaseMessages.getString(PKG, "ScriptValuesMetaMod.Exception.FieldToReplaceNotFound", meta.getRename()[i]));
                    }
                }
            } else {
                data.replaceIndex[i] = -1;
            }
        }
        // set the optimization level
        data.cx = ContextFactory.getGlobal().enterContext();
        try {
            String optimizationLevelAsString = environmentSubstitute(meta.getOptimizationLevel());
            if (!Utils.isEmpty(Const.trim(optimizationLevelAsString))) {
                data.cx.setOptimizationLevel(Integer.parseInt(optimizationLevelAsString.trim()));
                logBasic(BaseMessages.getString(PKG, "ScriptValuesMod.Optimization.Level", environmentSubstitute(meta.getOptimizationLevel())));
            } else {
                data.cx.setOptimizationLevel(Integer.parseInt(ScriptValuesMetaMod.OPTIMIZATION_LEVEL_DEFAULT));
                logBasic(BaseMessages.getString(PKG, "ScriptValuesMod.Optimization.UsingDefault", ScriptValuesMetaMod.OPTIMIZATION_LEVEL_DEFAULT));
            }
        } catch (NumberFormatException nfe) {
            throw new KettleStepException(BaseMessages.getString(PKG, "ScriptValuesMetaMod.Exception.NumberFormatException", environmentSubstitute(meta.getOptimizationLevel())));
        } catch (IllegalArgumentException iae) {
            throw new KettleException(iae.getMessage());
        }
        data.scope = data.cx.initStandardObjects(null, false);
        bFirstRun = true;
        Scriptable jsvalue = Context.toObject(this, data.scope);
        data.scope.put("_step_", data.scope, jsvalue);
        // Adding the existing Scripts to the Context
        for (int i = 0; i < meta.getNumberOfJSScripts(); i++) {
            Scriptable jsR = Context.toObject(jsScripts[i].getScript(), data.scope);
            data.scope.put(jsScripts[i].getScriptName(), data.scope, jsR);
        }
        // Adding the Name of the Transformation to the Context
        data.scope.put("_TransformationName_", data.scope, getTransMeta().getName());
        try {
            // 
            if (meta.isCompatible()) {
                Row v2Row = RowMeta.createOriginalRow(rowMeta, row);
                Scriptable jsV2Row = Context.toObject(v2Row, data.scope);
                data.scope.put("row", data.scope, jsV2Row);
            } else {
                Scriptable jsrow = Context.toObject(row, data.scope);
                data.scope.put("row", data.scope, jsrow);
            }
            // 
            for (int i = 0; i < data.fields_used.length; i++) {
                ValueMetaInterface valueMeta = rowMeta.getValueMeta(data.fields_used[i]);
                Object valueData = row[data.fields_used[i]];
                if (meta.isCompatible()) {
                    data.values_used[i] = valueMeta.createOriginalValue(valueData);
                    Scriptable jsarg = Context.toObject(data.values_used[i], data.scope);
                    data.scope.put(valueMeta.getName(), data.scope, jsarg);
                } else {
                    Object normalStorageValueData = valueMeta.convertToNormalStorageType(valueData);
                    Scriptable jsarg;
                    if (normalStorageValueData != null) {
                        jsarg = Context.toObject(normalStorageValueData, data.scope);
                    } else {
                        jsarg = null;
                    }
                    data.scope.put(valueMeta.getName(), data.scope, jsarg);
                }
            }
            // also add the meta information for the whole row
            // 
            Scriptable jsrowMeta = Context.toObject(rowMeta, data.scope);
            data.scope.put("rowMeta", data.scope, jsrowMeta);
            // 
            try {
                if (meta.getAddClasses() != null) {
                    for (int i = 0; i < meta.getAddClasses().length; i++) {
                        Object jsOut = Context.javaToJS(meta.getAddClasses()[i].getAddObject(), data.scope);
                        ScriptableObject.putProperty(data.scope, meta.getAddClasses()[i].getJSName(), jsOut);
                    }
                }
            } catch (Exception e) {
                throw new KettleValueException(BaseMessages.getString(PKG, "ScriptValuesMod.Log.CouldNotAttachAdditionalScripts"), e);
            }
            // Adding some default JavaScriptFunctions to the System
            try {
                Context.javaToJS(ScriptValuesAddedFunctions.class, data.scope);
                ((ScriptableObject) data.scope).defineFunctionProperties(ScriptValuesAddedFunctions.jsFunctionList, ScriptValuesAddedFunctions.class, ScriptableObject.DONTENUM);
            } catch (Exception ex) {
                // System.out.println(ex.toString());
                throw new KettleValueException(BaseMessages.getString(PKG, "ScriptValuesMod.Log.CouldNotAddDefaultFunctions"), ex);
            }
            // Adding some Constants to the JavaScript
            try {
                data.scope.put("SKIP_TRANSFORMATION", data.scope, Integer.valueOf(SKIP_TRANSFORMATION));
                data.scope.put("ABORT_TRANSFORMATION", data.scope, Integer.valueOf(ABORT_TRANSFORMATION));
                data.scope.put("ERROR_TRANSFORMATION", data.scope, Integer.valueOf(ERROR_TRANSFORMATION));
                data.scope.put("CONTINUE_TRANSFORMATION", data.scope, Integer.valueOf(CONTINUE_TRANSFORMATION));
            } catch (Exception ex) {
                // System.out.println("Exception Adding the Constants " + ex.toString());
                throw new KettleValueException(BaseMessages.getString(PKG, "ScriptValuesMod.Log.CouldNotAddDefaultConstants"), ex);
            }
            try {
                // Checking for StartScript
                if (strStartScript != null && strStartScript.length() > 0) {
                    Script startScript = data.cx.compileString(strStartScript, "trans_Start", 1, null);
                    startScript.exec(data.cx, data.scope);
                    if (log.isDetailed()) {
                        logDetailed(("Start Script found!"));
                    }
                } else {
                    if (log.isDetailed()) {
                        logDetailed(("No starting Script found!"));
                    }
                }
            } catch (Exception es) {
                // System.out.println("Exception processing StartScript " + es.toString());
                throw new KettleValueException(BaseMessages.getString(PKG, "ScriptValuesMod.Log.ErrorProcessingStartScript"), es);
            }
            // Now Compile our Script
            data.script = data.cx.compileString(strTransformScript, "script", 1, null);
        } catch (Exception e) {
            throw new KettleValueException(BaseMessages.getString(PKG, "ScriptValuesMod.Log.CouldNotCompileJavascript"), e);
        }
    }
    // Filling the defined TranVars with the Values from the Row
    // 
    Object[] outputRow = RowDataUtil.resizeArray(row, data.outputRowMeta.size());
    // Keep an index...
    int outputIndex = rowMeta.size();
    // Keep track of the changed values...
    // 
    final Map<Integer, Value> usedRowValues;
    if (meta.isCompatible()) {
        usedRowValues = new Hashtable<Integer, Value>();
    } else {
        usedRowValues = null;
    }
    try {
        try {
            if (meta.isCompatible()) {
                Row v2Row = RowMeta.createOriginalRow(rowMeta, row);
                Scriptable jsV2Row = Context.toObject(v2Row, data.scope);
                data.scope.put("row", data.scope, jsV2Row);
                v2Row.getUsedValueListeners().add(new ValueUsedListener() {

                    public void valueIsUsed(int index, Value value) {
                        usedRowValues.put(index, value);
                    }
                });
            } else {
                Scriptable jsrow = Context.toObject(row, data.scope);
                data.scope.put("row", data.scope, jsrow);
            }
            for (int i = 0; i < data.fields_used.length; i++) {
                ValueMetaInterface valueMeta = rowMeta.getValueMeta(data.fields_used[i]);
                Object valueData = row[data.fields_used[i]];
                if (meta.isCompatible()) {
                    data.values_used[i] = valueMeta.createOriginalValue(valueData);
                    Scriptable jsarg = Context.toObject(data.values_used[i], data.scope);
                    data.scope.put(valueMeta.getName(), data.scope, jsarg);
                } else {
                    Object normalStorageValueData = valueMeta.convertToNormalStorageType(valueData);
                    Scriptable jsarg;
                    if (normalStorageValueData != null) {
                        jsarg = Context.toObject(normalStorageValueData, data.scope);
                    } else {
                        jsarg = null;
                    }
                    data.scope.put(valueMeta.getName(), data.scope, jsarg);
                }
            }
            // also add the meta information for the hole row
            Scriptable jsrowMeta = Context.toObject(rowMeta, data.scope);
            data.scope.put("rowMeta", data.scope, jsrowMeta);
        } catch (Exception e) {
            throw new KettleValueException(BaseMessages.getString(PKG, "ScriptValuesMod.Log.UnexpectedeError"), e);
        }
        // Executing our Script
        data.script.exec(data.cx, data.scope);
        if (bFirstRun) {
            bFirstRun = false;
            // Check if we had a Transformation Status
            Object tran_stat = data.scope.get("trans_Status", data.scope);
            if (tran_stat != ScriptableObject.NOT_FOUND) {
                bWithTransStat = true;
                if (log.isDetailed()) {
                    logDetailed(("tran_Status found. Checking transformation status while script execution."));
                }
            } else {
                if (log.isDetailed()) {
                    logDetailed(("No tran_Status found. Transformation status checking not available."));
                }
                bWithTransStat = false;
            }
        }
        if (bWithTransStat) {
            iTranStat = (int) Context.toNumber(data.scope.get("trans_Status", data.scope));
        } else {
            iTranStat = CONTINUE_TRANSFORMATION;
        }
        if (iTranStat == CONTINUE_TRANSFORMATION) {
            bRC = true;
            for (int i = 0; i < meta.getFieldname().length; i++) {
                Object result = data.scope.get(meta.getFieldname()[i], data.scope);
                Object valueData = getValueFromJScript(result, i);
                if (data.replaceIndex[i] < 0) {
                    outputRow[outputIndex++] = valueData;
                } else {
                    outputRow[data.replaceIndex[i]] = valueData;
                }
            }
            // 
            if (meta.isCompatible()) {
                for (int i = 0; i < data.values_used.length; i++) {
                    ValueMetaInterface valueMeta = rowMeta.getValueMeta(data.fields_used[i]);
                    outputRow[data.fields_used[i]] = valueMeta.getValueData(data.values_used[i]);
                }
                // 
                for (Integer index : usedRowValues.keySet()) {
                    Value value = usedRowValues.get(index);
                    ValueMetaInterface valueMeta = rowMeta.getValueMeta(index);
                    outputRow[index] = valueMeta.getValueData(value);
                }
            }
            putRow(data.outputRowMeta, outputRow);
        } else {
            switch(iTranStat) {
                case SKIP_TRANSFORMATION:
                    // eat this row.
                    bRC = true;
                    break;
                case ABORT_TRANSFORMATION:
                    if (data.cx != null) {
                        Context.exit();
                    }
                    stopAll();
                    setOutputDone();
                    bRC = false;
                    break;
                case ERROR_TRANSFORMATION:
                    if (data.cx != null) {
                        Context.exit();
                    }
                    setErrors(1);
                    stopAll();
                    bRC = false;
                    break;
                default:
                    break;
            }
        // TODO: kick this "ERROR handling" junk out now that we have solid error handling in place.
        // 
        }
    } catch (Exception e) {
        throw new KettleValueException(BaseMessages.getString(PKG, "ScriptValuesMod.Log.JavascriptError"), e);
    }
    return bRC;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) Script(org.mozilla.javascript.Script) KettleStepException(org.pentaho.di.core.exception.KettleStepException) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable) KettleException(org.pentaho.di.core.exception.KettleException) EvaluatorException(org.mozilla.javascript.EvaluatorException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) Value(org.pentaho.di.compatibility.Value) ScriptableObject(org.mozilla.javascript.ScriptableObject) Row(org.pentaho.di.compatibility.Row) KettleValueException(org.pentaho.di.core.exception.KettleValueException) ValueUsedListener(org.pentaho.di.compatibility.ValueUsedListener)

Example 4 with Row

use of org.pentaho.di.compatibility.Row 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

Row (org.pentaho.di.compatibility.Row)4 Value (org.pentaho.di.compatibility.Value)4 Script (org.mozilla.javascript.Script)3 Scriptable (org.mozilla.javascript.Scriptable)3 ScriptableObject (org.mozilla.javascript.ScriptableObject)3 KettleException (org.pentaho.di.core.exception.KettleException)3 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)3 Date (java.util.Date)2 Context (org.mozilla.javascript.Context)2 EvaluatorException (org.mozilla.javascript.EvaluatorException)2 JavaScriptException (org.mozilla.javascript.JavaScriptException)2 KettleStepException (org.pentaho.di.core.exception.KettleStepException)2 Point (org.eclipse.swt.graphics.Point)1 MessageBox (org.eclipse.swt.widgets.MessageBox)1 TableItem (org.eclipse.swt.widgets.TableItem)1 ScriptNode (org.mozilla.javascript.ast.ScriptNode)1 ValueUsedListener (org.pentaho.di.compatibility.ValueUsedListener)1 CheckResult (org.pentaho.di.core.CheckResult)1 KettleValueException (org.pentaho.di.core.exception.KettleValueException)1 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)1