Search in sources :

Example 6 with Value

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

use of org.pentaho.di.compatibility.Value 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 8 with Value

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

the class ScriptMeta method getValue.

public boolean getValue(Bindings scope, int i, Value res, StringBuilder message) {
    boolean error_found = false;
    if (fieldname[i] != null && fieldname[i].length() > 0) {
        res.setName(rename[i]);
        res.setType(type[i]);
        try {
            Object result = scope.get(fieldname[i]);
            if (result != null) {
                String classname = result.getClass().getName();
                switch(type[i]) {
                    case ValueMetaInterface.TYPE_NUMBER:
                        if (classname.equalsIgnoreCase("org.mozilla.javascript.Undefined")) {
                            res.setNull();
                        } else if (classname.equalsIgnoreCase("org.mozilla.javascript.NativeJavaObject")) {
                            // Is it a java Value class ?
                            Value v = (Value) result;
                            res.setValue(v.getNumber());
                        } else {
                            res.setValue(((Double) result).doubleValue());
                        }
                        break;
                    case ValueMetaInterface.TYPE_INTEGER:
                        if (classname.equalsIgnoreCase("java.lang.Byte")) {
                            res.setValue(((java.lang.Byte) result).longValue());
                        } else if (classname.equalsIgnoreCase("java.lang.Short")) {
                            res.setValue(((Short) result).longValue());
                        } else if (classname.equalsIgnoreCase("java.lang.Integer")) {
                            res.setValue(((Integer) result).longValue());
                        } else if (classname.equalsIgnoreCase("java.lang.Long")) {
                            res.setValue(((Long) result).longValue());
                        } else if (classname.equalsIgnoreCase("org.mozilla.javascript.Undefined")) {
                            res.setNull();
                        } else if (classname.equalsIgnoreCase("org.mozilla.javascript.NativeJavaObject")) {
                            // Is it a java Value class ?
                            Value v = (Value) result;
                            res.setValue(v.getInteger());
                        } else {
                            res.setValue(Math.round(((Double) result).doubleValue()));
                        }
                        break;
                    case ValueMetaInterface.TYPE_STRING:
                        if (classname.equalsIgnoreCase("org.mozilla.javascript.NativeJavaObject") || classname.equalsIgnoreCase("org.mozilla.javascript.Undefined")) {
                            // Is it a java Value class ?
                            try {
                                Value v = (Value) result;
                                res.setValue(v.getString());
                            } catch (Exception ev) {
                                // A String perhaps?
                                String s = (String) result;
                                res.setValue(s);
                            }
                        } else {
                            res.setValue(((String) result));
                        }
                        break;
                    case ValueMetaInterface.TYPE_DATE:
                        double dbl = 0;
                        if (classname.equalsIgnoreCase("org.mozilla.javascript.Undefined")) {
                            res.setNull();
                        } else {
                            if (classname.equalsIgnoreCase("org.mozilla.javascript.NativeDate")) {
                                // TODO AKRETION not sure!
                                dbl = (Double) result;
                            } else if (classname.equalsIgnoreCase("org.mozilla.javascript.NativeJavaObject")) {
                                // Is it a java Date() class ?
                                try {
                                    Date dat = (Date) result;
                                    dbl = dat.getTime();
                                } catch (Exception e) {
                                    // Nope, try a Value
                                    Value v = (Value) result;
                                    Date dat = v.getDate();
                                    if (dat != null) {
                                        dbl = dat.getTime();
                                    } else {
                                        res.setNull();
                                    }
                                }
                            } else {
                                // Finally, try a number conversion to time
                                dbl = ((Double) result).doubleValue();
                            }
                            long lng = Math.round(dbl);
                            Date dat = new Date(lng);
                            res.setValue(dat);
                        }
                        break;
                    case ValueMetaInterface.TYPE_BOOLEAN:
                        res.setValue(((Boolean) result).booleanValue());
                        break;
                    default:
                        res.setNull();
                }
            } else {
                res.setNull();
            }
        } catch (Exception e) {
            message.append(BaseMessages.getString(PKG, "ScriptMeta.CheckResult.ErrorRetrievingValue", fieldname[i]) + " : " + e.toString());
            error_found = true;
        }
        res.setLength(length[i], precision[i]);
        message.append(BaseMessages.getString(PKG, "ScriptMeta.CheckResult.RetrievedValue", fieldname[i], res.toStringMeta()));
    } else {
        message.append(BaseMessages.getString(PKG, "ScriptMeta.CheckResult.ValueIsEmpty", String.valueOf(i)));
        error_found = true;
    }
    return error_found;
}
Also used : Value(org.pentaho.di.compatibility.Value) KettleException(org.pentaho.di.core.exception.KettleException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) ScriptException(javax.script.ScriptException) KettlePluginException(org.pentaho.di.core.exception.KettlePluginException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) Date(java.util.Date)

Example 9 with Value

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

the class ScriptValuesMetaMod method getValue.

public boolean getValue(Scriptable scope, int i, Value res, StringBuilder message) {
    boolean error_found = false;
    if (fieldname[i] != null && fieldname[i].length() > 0) {
        res.setName(rename[i]);
        res.setType(type[i]);
        try {
            Object result = scope.get(fieldname[i], scope);
            if (result != null) {
                String classname = result.getClass().getName();
                switch(type[i]) {
                    case ValueMetaInterface.TYPE_NUMBER:
                        if (classname.equalsIgnoreCase("org.mozilla.javascript.Undefined")) {
                            res.setNull();
                        } else if (classname.equalsIgnoreCase("org.mozilla.javascript.NativeJavaObject")) {
                            // Is it a java Value class ?
                            Value v = (Value) Context.jsToJava(result, Value.class);
                            res.setValue(v.getNumber());
                        } else {
                            res.setValue(((Double) result).doubleValue());
                        }
                        break;
                    case ValueMetaInterface.TYPE_INTEGER:
                        if (classname.equalsIgnoreCase("java.lang.Byte")) {
                            res.setValue(((java.lang.Byte) result).longValue());
                        } else if (classname.equalsIgnoreCase("java.lang.Short")) {
                            res.setValue(((Short) result).longValue());
                        } else if (classname.equalsIgnoreCase("java.lang.Integer")) {
                            res.setValue(((Integer) result).longValue());
                        } else if (classname.equalsIgnoreCase("java.lang.Long")) {
                            res.setValue(((Long) result).longValue());
                        } else if (classname.equalsIgnoreCase("org.mozilla.javascript.Undefined")) {
                            res.setNull();
                        } else if (classname.equalsIgnoreCase("org.mozilla.javascript.NativeJavaObject")) {
                            // Is it a java Value class ?
                            Value v = (Value) Context.jsToJava(result, Value.class);
                            res.setValue(v.getInteger());
                        } else {
                            res.setValue(Math.round(((Double) result).doubleValue()));
                        }
                        break;
                    case ValueMetaInterface.TYPE_STRING:
                        if (classname.equalsIgnoreCase("org.mozilla.javascript.NativeJavaObject") || classname.equalsIgnoreCase("org.mozilla.javascript.Undefined")) {
                            // Is it a java Value class ?
                            try {
                                Value v = (Value) Context.jsToJava(result, Value.class);
                                res.setValue(v.getString());
                            } catch (Exception ev) {
                                // A String perhaps?
                                String s = (String) Context.jsToJava(result, String.class);
                                res.setValue(s);
                            }
                        } else {
                            res.setValue(((String) result));
                        }
                        break;
                    case ValueMetaInterface.TYPE_DATE:
                        double dbl = 0;
                        if (classname.equalsIgnoreCase("org.mozilla.javascript.Undefined")) {
                            res.setNull();
                        } else {
                            if (classname.equalsIgnoreCase("org.mozilla.javascript.NativeDate")) {
                                dbl = Context.toNumber(result);
                            } else if (classname.equalsIgnoreCase("org.mozilla.javascript.NativeJavaObject")) {
                                // Is it a java Date() class ?
                                try {
                                    Date dat = (Date) Context.jsToJava(result, java.util.Date.class);
                                    dbl = dat.getTime();
                                } catch (Exception e) {
                                    // Nope, try a Value
                                    Value v = (Value) Context.jsToJava(result, Value.class);
                                    Date dat = v.getDate();
                                    if (dat != null) {
                                        dbl = dat.getTime();
                                    } else {
                                        res.setNull();
                                    }
                                }
                            } else {
                                // Finally, try a number conversion to time
                                dbl = ((Double) result).doubleValue();
                            }
                            long lng = Math.round(dbl);
                            Date dat = new Date(lng);
                            res.setValue(dat);
                        }
                        break;
                    case ValueMetaInterface.TYPE_BOOLEAN:
                        res.setValue(((Boolean) result).booleanValue());
                        break;
                    default:
                        res.setNull();
                }
            } else {
                res.setNull();
            }
        } catch (Exception e) {
            message.append(BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.ErrorRetrievingValue", fieldname[i]) + " : " + e.toString());
            error_found = true;
        }
        res.setLength(length[i], precision[i]);
        message.append(BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.RetrievedValue", fieldname[i], res.toStringMeta()));
    } else {
        message.append(BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.ValueIsEmpty", String.valueOf(i)));
        error_found = true;
    }
    return error_found;
}
Also used : 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) Value(org.pentaho.di.compatibility.Value) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 10 with Value

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

the class ValueMetaBase method createOriginalValue.

/**
 * Create an old-style value for backward compatibility reasons
 *
 * @param data
 *          the data to store in the value
 * @return a newly created Value object
 * @throws KettleValueException
 *           case there is a data conversion problem
 */
@Override
public Value createOriginalValue(Object data) throws KettleValueException {
    Value value = new Value(name, type);
    value.setLength(length, precision);
    if (isNull(data)) {
        value.setNull();
    } else {
        switch(value.getType()) {
            case TYPE_STRING:
                value.setValue(getString(data));
                break;
            case TYPE_NUMBER:
                value.setValue(getNumber(data).doubleValue());
                break;
            case TYPE_INTEGER:
                value.setValue(getInteger(data).longValue());
                break;
            case TYPE_DATE:
                value.setValue(getDate(data));
                break;
            case TYPE_BOOLEAN:
                value.setValue(getBoolean(data).booleanValue());
                break;
            case TYPE_BIGNUMBER:
                value.setValue(getBigNumber(data));
                break;
            case TYPE_BINARY:
                value.setValue(getBinary(data));
                break;
            default:
                throw new KettleValueException(toString() + " : We can't convert data type " + getTypeDesc() + " to an original (V2) Value");
        }
    }
    return value;
}
Also used : Value(org.pentaho.di.compatibility.Value) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Aggregations

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