Search in sources :

Example 26 with KettleValueException

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

the class ValueDataUtil method loadFileContentInBinary.

public static Object loadFileContentInBinary(ValueMetaInterface metaA, Object dataA) throws KettleValueException {
    if (dataA == null) {
        return null;
    }
    FileObject file = null;
    FileInputStream fis = null;
    try {
        file = KettleVFS.getFileObject(dataA.toString());
        fis = (FileInputStream) ((LocalFile) file).getInputStream();
        int fileSize = (int) file.getContent().getSize();
        byte[] content = Const.createByteArray(fileSize);
        fis.read(content, 0, fileSize);
        return content;
    } catch (Exception e) {
        throw new KettleValueException(e);
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            fis = null;
            if (file != null) {
                file.close();
            }
            file = null;
        } catch (Exception e) {
        // Ignore
        }
    }
}
Also used : LocalFile(org.apache.commons.vfs2.provider.local.LocalFile) FileObject(org.apache.commons.vfs2.FileObject) KettleValueException(org.pentaho.di.core.exception.KettleValueException) FileInputStream(java.io.FileInputStream) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Example 27 with KettleValueException

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

the class ValueDataUtil method addMonths.

public static Object addMonths(ValueMetaInterface metaA, Object dataA, ValueMetaInterface metaB, Object dataB) throws KettleValueException {
    if (dataA != null && dataB != null) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(metaA.getDate(dataA));
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        month += metaB.getInteger(dataB).intValue();
        int newyear = year + (int) Math.floor(month / 12);
        int newmonth = month % 12;
        cal.set(newyear, newmonth, 1);
        int newday = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        if (newday < day) {
            cal.set(Calendar.DAY_OF_MONTH, newday);
        } else {
            cal.set(Calendar.DAY_OF_MONTH, day);
        }
        return (cal.getTime());
    } else {
        throw new KettleValueException("Unable to add months with a null value");
    }
}
Also used : Calendar(java.util.Calendar) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Example 28 with KettleValueException

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

the class Props method convertArguments.

public static final String[] convertArguments(RowMetaAndData row) {
    String[] args = new String[10];
    for (int i = 0; i < row.size(); i++) {
        ValueMetaInterface valueMeta = row.getValueMeta(i);
        int argNr = getArgumentNumber(valueMeta);
        if (argNr >= 0 && argNr < 10) {
            try {
                args[argNr] = row.getString(i, "");
            } catch (KettleValueException e) {
                // Should never happen
                args[argNr] = "";
            }
        }
    }
    return args;
}
Also used : KettleValueException(org.pentaho.di.core.exception.KettleValueException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Example 29 with KettleValueException

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

the class RowMetaAndData method isEmptyValue.

public boolean isEmptyValue(String valueName) 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:
            return rowMeta.getString(data, idx) == null;
        case ValueMetaInterface.TYPE_BOOLEAN:
            return rowMeta.getBoolean(data, idx) == null;
        case ValueMetaInterface.TYPE_INTEGER:
            return rowMeta.getInteger(data, idx) == null;
        case ValueMetaInterface.TYPE_NUMBER:
            return rowMeta.getNumber(data, idx) == null;
        case ValueMetaInterface.TYPE_BIGNUMBER:
            return rowMeta.getBigNumber(data, idx) == null;
        case ValueMetaInterface.TYPE_BINARY:
            return rowMeta.getBinary(data, idx) == null;
        case ValueMetaInterface.TYPE_DATE:
        case ValueMetaInterface.TYPE_TIMESTAMP:
            return rowMeta.getDate(data, idx) == null;
        case ValueMetaInterface.TYPE_INET:
            return rowMeta.getString(data, idx) == null;
    }
    throw new KettleValueException("Unknown source type: " + metaType.getTypeDesc());
}
Also used : KettleValueException(org.pentaho.di.core.exception.KettleValueException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Example 30 with KettleValueException

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

the class BaseDatabaseMeta method getSQLValue.

@Override
public String getSQLValue(ValueMetaInterface valueMeta, Object valueData, String dateFormat) throws KettleValueException {
    StringBuilder ins = new StringBuilder();
    if (valueMeta.isNull(valueData)) {
        ins.append("null");
    } else {
        // 
        switch(valueMeta.getType()) {
            case ValueMetaInterface.TYPE_BOOLEAN:
            case ValueMetaInterface.TYPE_STRING:
                String string = valueMeta.getString(valueData);
                // Have the database dialect do the quoting.
                // This also adds the single quotes around the string (thanks to PostgreSQL)
                // 
                string = quoteSQLString(string);
                ins.append(string);
                break;
            case ValueMetaInterface.TYPE_DATE:
                Date date = valueMeta.getDate(valueData);
                if (Utils.isEmpty(dateFormat)) {
                    ins.append("'" + valueMeta.getString(valueData) + "'");
                } else {
                    try {
                        java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(dateFormat);
                        ins.append("'" + formatter.format(date) + "'");
                    } catch (Exception e) {
                        throw new KettleValueException("Error : ", e);
                    }
                }
                break;
            default:
                ins.append(valueMeta.getString(valueData));
                break;
        }
    }
    return ins.toString();
}
Also used : KettleValueException(org.pentaho.di.core.exception.KettleValueException) Date(java.util.Date) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) SQLException(java.sql.SQLException) 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