Search in sources :

Example 86 with KettleValueException

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

the class Value method readObj.

/**
 * Read the metadata and data for this Value object from the specified data input stream
 *
 * @param dis
 * @throws IOException
 */
public void readObj(DataInputStream dis) throws IOException {
    // type
    int theType = dis.readInt();
    newValue(theType);
    // name-length
    int nameLength = dis.readInt();
    // name
    StringBuilder nameBuffer = new StringBuilder();
    for (int i = 0; i < nameLength; i++) {
        nameBuffer.append(dis.readChar());
    }
    setName(new String(nameBuffer));
    // length & precision
    setLength(dis.readInt(), dis.readInt());
    // Null?
    setNull(dis.readBoolean());
    // Read the values
    if (!isNull()) {
        switch(getType()) {
            case VALUE_TYPE_STRING:
                // read the length
                int stringLength = dis.readInt();
                if (stringLength < 0) {
                    setValue((String) null);
                } else {
                    byte[] chars = new byte[stringLength];
                    dis.readFully(chars);
                    setValue(new String(chars, Const.XML_ENCODING));
                }
                break;
            case VALUE_TYPE_BIGNUMBER:
                // read the length
                int bnLength = dis.readInt();
                if (bnLength < 0) {
                    setValue((BigDecimal) null);
                } else {
                    StringBuilder buffer = new StringBuilder();
                    for (int i = 0; i < bnLength; i++) {
                        buffer.append(dis.readChar());
                    }
                    setValue(buffer.toString());
                    try {
                        convertString(VALUE_TYPE_BIGNUMBER);
                    } catch (KettleValueException e) {
                        throw new IOException("Unable to convert String to BigNumber while reading from data input stream [" + getString() + "]");
                    }
                }
                break;
            case VALUE_TYPE_DATE:
                if (dis.readBoolean()) {
                    setValue(new Date(dis.readLong()));
                }
                break;
            case VALUE_TYPE_NUMBER:
                setValue(dis.readDouble());
                break;
            case VALUE_TYPE_INTEGER:
                setValue(dis.readLong());
                break;
            case VALUE_TYPE_BOOLEAN:
                setValue(dis.readBoolean());
                break;
            default:
                break;
        }
    }
}
Also used : KettleValueException(org.pentaho.di.core.exception.KettleValueException) IOException(java.io.IOException) Date(java.util.Date)

Example 87 with KettleValueException

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

the class Value method trunc.

@SuppressWarnings("fallthrough")
public Value trunc(int level) throws KettleValueException {
    if (isNull()) {
        // don't do anything, leave it at NULL!
        return this;
    }
    if (isInteger()) {
        // nothing to do.
        return this;
    }
    if (isBigNumber()) {
        getBigNumber().setScale(level, BigDecimal.ROUND_FLOOR);
    } else if (isNumber()) {
        double pow = Math.pow(10, level);
        setValue(Math.floor(getNumber() * pow) / pow);
    } else if (isDate()) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDate());
        switch(level) {
            // MONTHS
            case 5:
                cal.set(Calendar.MONTH, 1);
            // DAYS
            case 4:
                cal.set(Calendar.DAY_OF_MONTH, 1);
            // HOURS
            case 3:
                cal.set(Calendar.HOUR_OF_DAY, 0);
            // MINUTES
            case 2:
                cal.set(Calendar.MINUTE, 0);
            // SECONDS
            case 1:
                cal.set(Calendar.SECOND, 0);
            // MILI-SECONDS
            case 0:
                cal.set(Calendar.MILLISECOND, 0);
                break;
            default:
                throw new KettleValueException("Argument of TRUNC of date has to be between 0 and 5");
        }
    } else {
        throw new KettleValueException("Function TRUNC only works with numbers and dates");
    }
    return this;
}
Also used : Calendar(java.util.Calendar) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Example 88 with KettleValueException

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

the class Value method multiply.

public Value multiply(Value v) throws KettleValueException {
    // a number and a string!
    if (isNull() || v.isNull()) {
        setNull();
        return this;
    }
    if ((v.isString() && isNumeric()) || (v.isNumeric() && isString())) {
        StringBuilder s;
        String append = "";
        int n;
        if (v.isString()) {
            s = new StringBuilder(v.getString());
            append = v.getString();
            n = (int) getInteger();
        } else {
            s = new StringBuilder(getString());
            append = getString();
            n = (int) v.getInteger();
        }
        if (n == 0) {
            s.setLength(0);
        } else {
            for (int i = 1; i < n; i++) {
                s.append(append);
            }
        }
        setValue(s);
    } else if (isBigNumber() || v.isBigNumber()) {
        // big numbers
        setValue(ValueDataUtil.multiplyBigDecimals(getBigNumber(), v.getBigNumber(), null));
    } else if (isNumber() || v.isNumber()) {
        // numbers
        setValue(getNumber() * v.getNumber());
    } else if (isInteger() || v.isInteger()) {
        // integers
        setValue(getInteger() * v.getInteger());
    } else {
        throw new KettleValueException("Multiplication can only be done with numbers or a number and a string!");
    }
    return this;
}
Also used : KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Example 89 with KettleValueException

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

the class KettleDatabaseRepositoryConnectionDelegate method getDatabaseAttributes.

public Collection<RowMetaAndData> getDatabaseAttributes(ObjectId id_database) throws KettleDatabaseException, KettleValueException {
    String sql = "SELECT * FROM " + quoteTable(KettleDatabaseRepository.TABLE_R_DATABASE_ATTRIBUTE) + " WHERE " + quote(KettleDatabaseRepository.FIELD_DATABASE_ID_DATABASE) + " = ?";
    // Get the prepared statement
    // 
    PreparedStatement ps = getPreparedStatement(sql);
    // Assemble the parameter (if any)
    // 
    RowMetaInterface parameterMeta = new RowMeta();
    parameterMeta.addValueMeta(new ValueMetaInteger("id"));
    Object[] parameterData = new Object[] { ((LongObjectId) id_database).longValue() };
    List<RowMetaAndData> attrs = new ArrayList<RowMetaAndData>();
    return callRead(new Callable<Collection<RowMetaAndData>>() {

        @Override
        public Collection<RowMetaAndData> call() throws Exception {
            ResultSet resultSet = null;
            try {
                resultSet = database.openQuery(ps, parameterMeta, parameterData);
                List<Object[]> rows = database.getRows(resultSet, 0, null);
                for (Object[] row : rows) {
                    RowMetaAndData rowWithMeta = new RowMetaAndData(database.getReturnRowMeta(), row);
                    long id = rowWithMeta.getInteger(quote(KettleDatabaseRepository.FIELD_DATABASE_ATTRIBUTE_ID_DATABASE_ATTRIBUTE), 0);
                    if (id > 0) {
                        attrs.add(rowWithMeta);
                    }
                }
                return attrs;
            } catch (KettleDatabaseException e) {
                throw e;
            } finally {
                database.closeQuery(resultSet);
            }
        }
    });
}
Also used : RowMeta(org.pentaho.di.core.row.RowMeta) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) LongObjectId(org.pentaho.di.repository.LongObjectId) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) SQLException(java.sql.SQLException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) RowMetaAndData(org.pentaho.di.core.RowMetaAndData) ResultSet(java.sql.ResultSet) Collection(java.util.Collection) ValueMetaInteger(org.pentaho.di.core.row.value.ValueMetaInteger) SimpleLoggingObject(org.pentaho.di.core.logging.SimpleLoggingObject) RepositoryObject(org.pentaho.di.repository.RepositoryObject) ArrayList(java.util.ArrayList) List(java.util.List)

Example 90 with KettleValueException

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

the class ValueMetaBaseLenientStringConversionTest method testStrToBigNumberStrict.

@Test
public void testStrToBigNumberStrict() throws Exception {
    System.setProperty(Const.KETTLE_LENIENT_STRING_TO_NUMBER_CONVERSION, "N");
    String[] values = new String[] { "1b", "1,5", "10,000,000.25" };
    ValueMetaBigNumber meta = new ValueMetaBigNumber();
    Throwable exc = null;
    BigDecimal converted = null;
    for (String value : values) {
        try {
            converted = meta.convertStringToBigNumber(value);
        } catch (Exception e) {
            exc = e;
        } finally {
            Assert.assertTrue("Conversion of '" + value + "' didn't fail. Value is " + converted, exc instanceof KettleValueException);
            exc = null;
        }
    }
}
Also used : KettleValueException(org.pentaho.di.core.exception.KettleValueException) BigDecimal(java.math.BigDecimal) KettleValueException(org.pentaho.di.core.exception.KettleValueException) Test(org.junit.Test)

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