Search in sources :

Example 71 with KettleValueException

use of org.pentaho.di.core.exception.KettleValueException 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)

Example 72 with KettleValueException

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

the class ValueMetaInternetAddress method getInteger.

@Override
public Long getInteger(Object object) throws KettleValueException {
    InetAddress address = getInternetAddress(object);
    if (address == null) {
        return null;
    }
    long total = 0L;
    byte[] addr = address.getAddress();
    if (addr.length > 8) {
        throw new KettleValueException("Unable to convert Internet Address v6 to an Integer: " + getString(object) + " (The precision is too high to be contained in a long integer value)");
    }
    for (int i = 0; i < addr.length; i++) {
        total += (addr[i] & 0xFF) * ((long) Math.pow(256, (addr.length - 1 - i)));
    }
    return total;
}
Also used : KettleValueException(org.pentaho.di.core.exception.KettleValueException) InetAddress(java.net.InetAddress)

Example 73 with KettleValueException

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

the class ValueMetaTimestamp method writeData.

@Override
public void writeData(DataOutputStream outputStream, Object object) throws KettleFileException {
    try {
        // Is the value NULL?
        outputStream.writeBoolean(object == null);
        if (object != null) {
            switch(storageType) {
                case STORAGE_TYPE_NORMAL:
                    // Handle Content -- only when not NULL
                    Timestamp timestamp = convertDateToTimestamp((Date) object);
                    outputStream.writeLong(timestamp.getTime());
                    outputStream.writeInt(timestamp.getNanos());
                    break;
                case STORAGE_TYPE_BINARY_STRING:
                    // Handle binary string content -- only when not NULL
                    // In this case, we opt not to convert anything at all for speed.
                    // That way, we can save on CPU power.
                    // Since the streams can be compressed, volume shouldn't be an issue
                    // at all.
                    // 
                    writeBinaryString(outputStream, (byte[]) object);
                    break;
                case STORAGE_TYPE_INDEXED:
                    // just an index
                    writeInteger(outputStream, (Integer) object);
                    break;
                default:
                    throw new KettleFileException(toString() + " : Unknown storage type " + getStorageType());
            }
        }
    } catch (ClassCastException e) {
        throw new RuntimeException(toString() + " : There was a data type error: the data type of " + object.getClass().getName() + " object [" + object + "] does not correspond to value meta [" + toStringMeta() + "]");
    } catch (IOException e) {
        throw new KettleFileException(toString() + " : Unable to write value timestamp data to output stream", e);
    } catch (KettleValueException e) {
        throw new RuntimeException(toString() + " : There was a data type error: the data type of " + object.getClass().getName() + " object [" + object + "] does not correspond to value meta [" + toStringMeta() + "]");
    }
}
Also used : KettleFileException(org.pentaho.di.core.exception.KettleFileException) IOException(java.io.IOException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) Timestamp(java.sql.Timestamp)

Example 74 with KettleValueException

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

the class ValueMetaTimestamp method convertStringToTimestamp.

protected synchronized Timestamp convertStringToTimestamp(String string) throws KettleValueException {
    // See if trimming needs to be performed before conversion
    // 
    string = Const.trimToType(string, getTrimType());
    if (Utils.isEmpty(string)) {
        return null;
    }
    Timestamp returnValue;
    try {
        returnValue = Timestamp.valueOf(string);
    } catch (IllegalArgumentException e) {
        try {
            returnValue = (Timestamp) getDateFormat().parse(string);
        } catch (ParseException ex) {
            throw new KettleValueException(toString() + " : couldn't convert string [" + string + "] to a timestamp, expecting format [yyyy-mm-dd hh:mm:ss.ffffff]", e);
        }
    }
    return returnValue;
}
Also used : ParseException(java.text.ParseException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) Timestamp(java.sql.Timestamp)

Example 75 with KettleValueException

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

the class StringEvaluator method challengeConversions.

private void challengeConversions(String value) {
    List<StringEvaluationResult> all = new ArrayList<StringEvaluationResult>(evaluationResults);
    ValueMetaInterface stringMetaClone = null;
    for (StringEvaluationResult cmm : all) {
        if (cmm.getConversionMeta().isBoolean()) {
            // Boolean conversion never fails.
            // If it's a Y, N, true, false it's a boolean otherwise it ain't.
            // 
            String string;
            if (tryTrimming) {
                string = Const.trim(value);
            } else {
                string = value;
            }
            if (StringUtils.isEmpty(value)) {
                cmm.incrementNrNull();
            } else if (!("Y".equalsIgnoreCase(string) || "N".equalsIgnoreCase(string) || "TRUE".equalsIgnoreCase(string) || "FALSE".equalsIgnoreCase(string))) {
                evaluationResults.remove(cmm);
            } else {
                cmm.incrementSuccesses();
            }
        } else if (cmm.getConversionMeta().isDate()) {
            String dateFormat = cmm.getConversionMeta().getConversionMask();
            if (!DateDetector.isValidDateFormatToStringDate(dateFormat, value, "en_US")) {
                evaluationResults.remove(cmm);
            } else {
                try {
                    Object object = DateDetector.getDateFromStringByFormat(value, dateFormat);
                    cmm.incrementSuccesses();
                    if (cmm.getMin() == null || cmm.getConversionMeta().compare(cmm.getMin(), object) > 0) {
                        cmm.setMin(object);
                    }
                    if (cmm.getMax() == null || cmm.getConversionMeta().compare(cmm.getMax(), object) < 0) {
                        cmm.setMax(object);
                    }
                } catch (ParseException e) {
                    evaluationResults.remove(cmm);
                } catch (KettleValueException e) {
                    evaluationResults.remove(cmm);
                }
            }
        } else {
            try {
                if (cmm.getConversionMeta().isNumeric()) {
                    boolean stop = false;
                    int nrDots = 0;
                    int nrCommas = 0;
                    int pos = 0;
                    for (char c : value.toCharArray()) {
                        boolean currencySymbolMatch = !String.valueOf(c).equals(cmm.getConversionMeta().getCurrencySymbol()) && c != '(' && c != ')';
                        if (!Character.isDigit(c) && c != '.' && c != ',' && !Character.isSpaceChar(c) && currencySymbolMatch && // allow + & - at the 1st position
                        (pos > 0 && (c == '+' || c == '-'))) {
                            evaluationResults.remove(cmm);
                            stop = true;
                            break;
                        }
                        // 
                        if ((c == '.' && cmm.getConversionMeta().isInteger()) || (c == ',' && cmm.getConversionMeta().isInteger())) {
                            evaluationResults.remove(cmm);
                            stop = true;
                            break;
                        }
                        if (c == '.') {
                            nrDots++;
                        }
                        if (c == ',') {
                            nrCommas++;
                        }
                        pos++;
                    }
                    if (nrDots > 1 && nrCommas > 1) {
                        evaluationResults.remove(cmm);
                        stop = true;
                    }
                    if (stop) {
                        continue;
                    }
                }
                if (stringMetaClone == null) {
                    // avoid cloning each time
                    stringMetaClone = stringMeta.clone();
                }
                stringMetaClone.setConversionMetadata(cmm.getConversionMeta());
                stringMetaClone.setTrimType(cmm.getConversionMeta().getTrimType());
                Object object = stringMetaClone.convertDataUsingConversionMetaData(value);
                // 
                if (cmm.getConversionMeta().isNull(object)) {
                    cmm.incrementNrNull();
                } else {
                    cmm.incrementSuccesses();
                }
                if (cmm.getMin() == null || cmm.getConversionMeta().compare(cmm.getMin(), object) > 0) {
                    cmm.setMin(object);
                }
                if (cmm.getMax() == null || cmm.getConversionMeta().compare(cmm.getMax(), object) < 0) {
                    cmm.setMax(object);
                }
            } catch (KettleValueException e) {
                // This one doesn't work, remove it from the list!
                // 
                evaluationResults.remove(cmm);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) ParseException(java.text.ParseException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

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