Search in sources :

Example 76 with KettleValueException

use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.

the class StringUtil method str2dat.

public static Date str2dat(String arg0, String arg1, String val) throws KettleValueException {
    SimpleDateFormat df = new SimpleDateFormat();
    DateFormatSymbols dfs = new DateFormatSymbols();
    if (arg1 != null) {
        dfs.setLocalPatternChars(arg1);
    }
    if (arg0 != null) {
        df.applyPattern(arg0);
    }
    try {
        return df.parse(val);
    } catch (Exception e) {
        throw new KettleValueException("TO_DATE Couldn't convert String to Date " + e.toString());
    }
}
Also used : DateFormatSymbols(java.text.DateFormatSymbols) KettleValueException(org.pentaho.di.core.exception.KettleValueException) SimpleDateFormat(java.text.SimpleDateFormat) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Example 77 with KettleValueException

use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.

the class StringUtil method str2num.

public static double str2num(String pattern, String decimal, String grouping, String currency, String value) throws KettleValueException {
    // 0 : pattern
    // 1 : Decimal separator
    // 2 : Grouping separator
    // 3 : Currency symbol
    NumberFormat nf = NumberFormat.getInstance();
    DecimalFormat df = (DecimalFormat) nf;
    DecimalFormatSymbols dfs = new DecimalFormatSymbols();
    if (!Utils.isEmpty(pattern)) {
        df.applyPattern(pattern);
    }
    if (!Utils.isEmpty(decimal)) {
        dfs.setDecimalSeparator(decimal.charAt(0));
    }
    if (!Utils.isEmpty(grouping)) {
        dfs.setGroupingSeparator(grouping.charAt(0));
    }
    if (!Utils.isEmpty(currency)) {
        dfs.setCurrencySymbol(currency);
    }
    try {
        df.setDecimalFormatSymbols(dfs);
        return df.parse(value).doubleValue();
    } catch (Exception e) {
        String message = "Couldn't convert string to number " + e.toString();
        if (!isEmpty(pattern)) {
            message += " pattern=" + pattern;
        }
        if (!isEmpty(decimal)) {
            message += " decimal=" + decimal;
        }
        if (!isEmpty(grouping)) {
            message += " grouping=" + grouping.charAt(0);
        }
        if (!isEmpty(currency)) {
            message += " currency=" + currency;
        }
        throw new KettleValueException(message);
    }
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) KettleValueException(org.pentaho.di.core.exception.KettleValueException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) NumberFormat(java.text.NumberFormat)

Example 78 with KettleValueException

use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.

the class ValueDataUtil method DateDiff.

/**
 * Returns the number of days that have elapsed between dataA and dataB.
 *
 * @param metaA
 * @param dataA
 *          The "end date"
 * @param metaB
 * @param dataB
 *          The "start date"
 * @param resultType
 *          The "result type" (ms, s, mn, h, d)
 * @return Number of days
 * @throws KettleValueException
 */
public static Object DateDiff(ValueMetaInterface metaA, Object dataA, ValueMetaInterface metaB, Object dataB, String resultType) throws KettleValueException {
    if (dataA != null && dataB != null) {
        Date startDate = metaB.getDate(dataB);
        Date endDate = metaA.getDate(dataA);
        Calendar stDateCal = Calendar.getInstance();
        Calendar endDateCal = Calendar.getInstance();
        stDateCal.setTime(startDate);
        endDateCal.setTime(endDate);
        long endL = endDateCal.getTimeInMillis() + endDateCal.getTimeZone().getOffset(endDateCal.getTimeInMillis());
        long startL = stDateCal.getTimeInMillis() + stDateCal.getTimeZone().getOffset(stDateCal.getTimeInMillis());
        long diff = endL - startL;
        if (Utils.isEmpty(resultType)) {
            return new Long(diff / 86400000);
        } else if (resultType.equals("ms")) {
            return new Long(diff);
        } else if (resultType.equals("s")) {
            // second
            return new Long(diff / 1000);
        } else if (resultType.equals("mn")) {
            // minute
            return new Long(diff / 60000);
        } else if (resultType.equals("h")) {
            // hour
            return new Long(diff / 3600000);
        } else if (resultType.equals("d")) {
            return new Long(diff / 86400000);
        } else {
            throw new KettleValueException("Unknown result type option '" + resultType + "'");
        }
    } else {
        return null;
    }
}
Also used : Calendar(java.util.Calendar) KettleValueException(org.pentaho.di.core.exception.KettleValueException) Date(java.util.Date)

Example 79 with KettleValueException

use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.

the class RowMetaAndData method getAsJavaType.

/**
 * Returns value as specified java type using converter. Used for metadata injection.
 */
public Object getAsJavaType(String valueName, Class<?> destinationType, InjectionTypeConverter converter) throws KettleValueException {
    int idx = rowMeta.indexOfValue(valueName);
    if (idx < 0) {
        throw new KettleValueException("Unknown column '" + valueName + "'");
    }
    ValueMetaInterface metaType = rowMeta.getValueMeta(idx);
    // find by source value type
    switch(metaType.getType()) {
        case ValueMetaInterface.TYPE_STRING:
            String vs = rowMeta.getString(data, idx);
            return getStringAsJavaType(vs, destinationType, converter);
        case ValueMetaInterface.TYPE_BOOLEAN:
            Boolean vb = rowMeta.getBoolean(data, idx);
            if (String.class.isAssignableFrom(destinationType)) {
                return converter.boolean2string(vb);
            } else if (int.class.isAssignableFrom(destinationType)) {
                return converter.boolean2intPrimitive(vb);
            } else if (Integer.class.isAssignableFrom(destinationType)) {
                return converter.boolean2integer(vb);
            } else if (long.class.isAssignableFrom(destinationType)) {
                return converter.boolean2longPrimitive(vb);
            } else if (Long.class.isAssignableFrom(destinationType)) {
                return converter.boolean2long(vb);
            } else if (boolean.class.isAssignableFrom(destinationType)) {
                return converter.boolean2booleanPrimitive(vb);
            } else if (Boolean.class.isAssignableFrom(destinationType)) {
                return converter.boolean2boolean(vb);
            } else if (destinationType.isEnum()) {
                return converter.boolean2enum(destinationType, vb);
            } else {
                throw new RuntimeException("Wrong value conversion to " + destinationType);
            }
        case ValueMetaInterface.TYPE_INTEGER:
            Long vi = rowMeta.getInteger(data, idx);
            if (String.class.isAssignableFrom(destinationType)) {
                return converter.integer2string(vi);
            } else if (int.class.isAssignableFrom(destinationType)) {
                return converter.integer2intPrimitive(vi);
            } else if (Integer.class.isAssignableFrom(destinationType)) {
                return converter.integer2integer(vi);
            } else if (long.class.isAssignableFrom(destinationType)) {
                return converter.integer2longPrimitive(vi);
            } else if (Long.class.isAssignableFrom(destinationType)) {
                return converter.integer2long(vi);
            } else if (boolean.class.isAssignableFrom(destinationType)) {
                return converter.integer2booleanPrimitive(vi);
            } else if (Boolean.class.isAssignableFrom(destinationType)) {
                return converter.integer2boolean(vi);
            } else if (destinationType.isEnum()) {
                return converter.integer2enum(destinationType, vi);
            } else {
                throw new RuntimeException("Wrong value conversion to " + destinationType);
            }
        case ValueMetaInterface.TYPE_NUMBER:
            Double vn = rowMeta.getNumber(data, idx);
            if (String.class.isAssignableFrom(destinationType)) {
                return converter.number2string(vn);
            } else if (int.class.isAssignableFrom(destinationType)) {
                return converter.number2intPrimitive(vn);
            } else if (Integer.class.isAssignableFrom(destinationType)) {
                return converter.number2integer(vn);
            } else if (long.class.isAssignableFrom(destinationType)) {
                return converter.number2longPrimitive(vn);
            } else if (Long.class.isAssignableFrom(destinationType)) {
                return converter.number2long(vn);
            } else if (boolean.class.isAssignableFrom(destinationType)) {
                return converter.number2booleanPrimitive(vn);
            } else if (Boolean.class.isAssignableFrom(destinationType)) {
                return converter.number2boolean(vn);
            } else if (destinationType.isEnum()) {
                return converter.number2enum(destinationType, vn);
            } else {
                throw new RuntimeException("Wrong value conversion to " + destinationType);
            }
    }
    throw new KettleValueException("Unknown conversion from " + metaType.getTypeDesc() + " into " + destinationType);
}
Also used : KettleValueException(org.pentaho.di.core.exception.KettleValueException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Example 80 with KettleValueException

use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.

the class Value method dat2str.

public Value dat2str(String arg0, String arg1) throws KettleValueException {
    if (isNull()) {
        setType(VALUE_TYPE_STRING);
    } else {
        if (getType() == VALUE_TYPE_DATE) {
            SimpleDateFormat df = new SimpleDateFormat();
            DateFormatSymbols dfs = new DateFormatSymbols();
            if (arg1 != null) {
                dfs.setLocalPatternChars(arg1);
            }
            if (arg0 != null) {
                df.applyPattern(arg0);
            }
            try {
                setValue(df.format(getDate()));
            } catch (Exception e) {
                setType(VALUE_TYPE_STRING);
                setNull();
                throw new KettleValueException("TO_CHAR Couldn't convert Date to String " + e.toString());
            }
        } else {
            throw new KettleValueException("Function DAT2STR only works on a date");
        }
    }
    return this;
}
Also used : DateFormatSymbols(java.text.DateFormatSymbols) KettleValueException(org.pentaho.di.core.exception.KettleValueException) SimpleDateFormat(java.text.SimpleDateFormat) KettleFileException(org.pentaho.di.core.exception.KettleFileException) KettleEOFException(org.pentaho.di.core.exception.KettleEOFException) IOException(java.io.IOException) EOFException(java.io.EOFException) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Aggregations

KettleValueException (org.pentaho.di.core.exception.KettleValueException)127 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)35 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)35 KettleException (org.pentaho.di.core.exception.KettleException)25 KettleStepException (org.pentaho.di.core.exception.KettleStepException)17 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)16 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)13 IOException (java.io.IOException)12 Test (org.junit.Test)11 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)11 KettleFileException (org.pentaho.di.core.exception.KettleFileException)11 ParseException (java.text.ParseException)10 Date (java.util.Date)9 EOFException (java.io.EOFException)8 SQLException (java.sql.SQLException)8 ArrayList (java.util.ArrayList)8 Calendar (java.util.Calendar)8 KettleEOFException (org.pentaho.di.core.exception.KettleEOFException)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 BigDecimal (java.math.BigDecimal)6