Search in sources :

Example 1 with Value

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

the class JavaScriptUtils method jsToDate.

public static Date jsToDate(Object value, String classType) throws KettleValueException {
    double dbl;
    if (classType.equalsIgnoreCase("org.mozilla.javascript.Undefined")) {
        return null;
    } else {
        if (classType.equalsIgnoreCase("org.mozilla.javascript.NativeDate")) {
            dbl = Context.toNumber(value);
        } else if (classType.equalsIgnoreCase("org.mozilla.javascript.NativeJavaObject") || classType.equalsIgnoreCase("java.util.Date")) {
            // Is it a java Date() class ?
            try {
                Date dat = (Date) Context.jsToJava(value, java.util.Date.class);
                dbl = dat.getTime();
            } catch (Exception e) {
                // 
                try {
                    Value v = (Value) Context.jsToJava(value, Value.class);
                    return v.getDate();
                } catch (Exception e2) {
                    try {
                        String string = Context.toString(value);
                        return XMLHandler.stringToDate(string);
                    } catch (Exception e3) {
                        throw new KettleValueException("Can't convert a string to a date");
                    }
                }
            }
        } else if (classType.equalsIgnoreCase("java.lang.Double")) {
            dbl = (Double) value;
        } else {
            String string = (String) Context.jsToJava(value, String.class);
            dbl = Double.parseDouble(string);
        }
        long lng = Math.round(dbl);
        return new Date(lng);
    }
}
Also used : Value(org.pentaho.di.compatibility.Value) KettleValueException(org.pentaho.di.core.exception.KettleValueException) Date(java.util.Date) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Example 2 with Value

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

the class JavaScriptUtilsTest method jsToBigNumber_NativeJavaObject_BigDecimal.

@Test
public void jsToBigNumber_NativeJavaObject_BigDecimal() throws Exception {
    Value value = new Value();
    value.setValue(BigDecimal.ONE);
    Scriptable object = Context.toObject(value, scope);
    assertEquals(1.0, JavaScriptUtils.jsToBigNumber(object, JAVA_OBJECT).doubleValue(), 1e-6);
}
Also used : Value(org.pentaho.di.compatibility.Value) Scriptable(org.mozilla.javascript.Scriptable) Test(org.junit.Test)

Example 3 with Value

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

the class JavaScriptUtilsTest method getIntValue.

private static Scriptable getIntValue() {
    Value value = new Value();
    value.setValue(1);
    return Context.toObject(value, scope);
}
Also used : Value(org.pentaho.di.compatibility.Value)

Example 4 with Value

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

the class JavaScriptUtilsTest method getDoubleValue.

private static Scriptable getDoubleValue() {
    Value value = new Value();
    value.setValue(1.0);
    return Context.toObject(value, scope);
}
Also used : Value(org.pentaho.di.compatibility.Value)

Example 5 with Value

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

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