Search in sources :

Example 1 with HopValueException

use of org.apache.hop.core.exception.HopValueException in project hop by apache.

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 HopValueException {
    int idx = rowMeta.indexOfValue(valueName);
    if (idx < 0) {
        throw new HopValueException("Unknown column '" + valueName + "'");
    }
    IValueMeta metaType = rowMeta.getValueMeta(idx);
    // find by source value type
    switch(metaType.getType()) {
        case IValueMeta.TYPE_STRING:
            String vs = rowMeta.getString(data, idx);
            return getStringAsJavaType(vs, destinationType, converter);
        case IValueMeta.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 IValueMeta.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 IValueMeta.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 HopValueException("Unknown conversion from " + metaType.getTypeDesc() + " into " + destinationType);
}
Also used : IValueMeta(org.apache.hop.core.row.IValueMeta) HopValueException(org.apache.hop.core.exception.HopValueException)

Example 2 with HopValueException

use of org.apache.hop.core.exception.HopValueException in project hop by apache.

the class RowMetaAndData method isEmptyValue.

public boolean isEmptyValue(String valueName) throws HopValueException {
    int idx = rowMeta.indexOfValue(valueName);
    if (idx < 0) {
        throw new HopValueException("Unknown column '" + valueName + "'");
    }
    IValueMeta metaType = rowMeta.getValueMeta(idx);
    // find by source value type
    switch(metaType.getType()) {
        case IValueMeta.TYPE_STRING:
            return rowMeta.getString(data, idx) == null;
        case IValueMeta.TYPE_BOOLEAN:
            return rowMeta.getBoolean(data, idx) == null;
        case IValueMeta.TYPE_INTEGER:
            return rowMeta.getInteger(data, idx) == null;
        case IValueMeta.TYPE_NUMBER:
            return rowMeta.getNumber(data, idx) == null;
        case IValueMeta.TYPE_BIGNUMBER:
            return rowMeta.getBigNumber(data, idx) == null;
        case IValueMeta.TYPE_BINARY:
            return rowMeta.getBinary(data, idx) == null;
        case IValueMeta.TYPE_DATE:
        case IValueMeta.TYPE_TIMESTAMP:
            return rowMeta.getDate(data, idx) == null;
        case IValueMeta.TYPE_INET:
            return rowMeta.getString(data, idx) == null;
    }
    throw new HopValueException("Unknown source type: " + metaType.getTypeDesc());
}
Also used : IValueMeta(org.apache.hop.core.row.IValueMeta) HopValueException(org.apache.hop.core.exception.HopValueException)

Example 3 with HopValueException

use of org.apache.hop.core.exception.HopValueException in project hop by apache.

the class BaseTransform method handlePutRow.

private void handlePutRow(IRowMeta rowMeta, Object[] row) throws HopTransformException {
    // 
    while (paused.get() && !stopped.get()) {
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            throw new HopTransformException(e);
        }
    }
    // 
    if (stopped.get() && !safeStopped.get()) {
        if (log.isDebug()) {
            logDebug(BaseMessages.getString(PKG, "BaseTransform.Log.StopPuttingARow"));
        }
        stopAll();
        return;
    }
    // 
    if (this.checkPipelineRunning == false) {
        int counter = 0;
        while (!pipeline.isRunning() && !stopped.get()) {
            try {
                Thread.sleep(1000);
                counter++;
            } catch (InterruptedException e) {
            // Ignore
            }
            // wait 3s max
            if (counter >= 3) {
                break;
            }
        }
        this.checkPipelineRunning = true;
    }
    // 
    for (IRowListener listener : rowListeners) {
        listener.rowWrittenEvent(rowMeta, row);
    }
    // 
    if (terminator && terminatorRows != null) {
        try {
            terminatorRows.add(rowMeta.cloneRow(row));
        } catch (HopValueException e) {
            throw new HopTransformException("Unable to clone row while adding rows to the terminator rows.", e);
        }
    }
    outputRowSetsLock.readLock().lock();
    try {
        if (outputRowSets.isEmpty()) {
            // No more output rowsets!
            // Still update the nr of lines written.
            // 
            incrementLinesWritten();
            // we're done here!
            return;
        }
        // 
        switch(repartitioning) {
            case TransformPartitioningMeta.PARTITIONING_METHOD_NONE:
                noPartitioning(rowMeta, row);
                break;
            case TransformPartitioningMeta.PARTITIONING_METHOD_SPECIAL:
                specialPartitioning(rowMeta, row);
                break;
            case TransformPartitioningMeta.PARTITIONING_METHOD_MIRROR:
                mirrorPartitioning(rowMeta, row);
                break;
            default:
                throw new HopTransformException("Internal error: invalid repartitioning type: " + repartitioning);
        }
    } finally {
        outputRowSetsLock.readLock().unlock();
    }
}
Also used : HopValueException(org.apache.hop.core.exception.HopValueException) HopTransformException(org.apache.hop.core.exception.HopTransformException)

Example 4 with HopValueException

use of org.apache.hop.core.exception.HopValueException in project hop by apache.

the class Janino method calcFields.

private Object[] calcFields(IRowMeta rowMeta, Object[] r) throws HopValueException {
    try {
        Object[] outputRowData = RowDataUtil.createResizedCopy(r, data.outputRowMeta.size());
        int tempIndex = rowMeta.size();
        // 
        if (data.expressionEvaluators == null) {
            data.expressionEvaluators = new ExpressionEvaluator[meta.getFormula().length];
            data.argumentIndexes = new ArrayList<>();
            for (int i = 0; i < meta.getFormula().length; i++) {
                List<Integer> argIndexes = new ArrayList<>();
                data.argumentIndexes.add(argIndexes);
            }
            for (int m = 0; m < meta.getFormula().length; m++) {
                List<Integer> argIndexes = data.argumentIndexes.get(m);
                List<String> parameterNames = new ArrayList<>();
                List<Class<?>> parameterTypes = new ArrayList<>();
                for (int i = 0; i < data.outputRowMeta.size(); i++) {
                    IValueMeta valueMeta = data.outputRowMeta.getValueMeta(i);
                    // 
                    if (meta.getFormula()[m].getFormula().contains(valueMeta.getName())) {
                        // If so, add it to the indexes...
                        argIndexes.add(i);
                        parameterTypes.add(valueMeta.getNativeDataTypeClass());
                        parameterNames.add(valueMeta.getName());
                    }
                }
                JaninoMetaFunction fn = meta.getFormula()[m];
                if (!Utils.isEmpty(fn.getFieldName())) {
                    // Create the expression evaluator: is relatively slow so we do it only for the first
                    // row...
                    // 
                    data.expressionEvaluators[m] = new ExpressionEvaluator();
                    data.expressionEvaluators[m].setParameters(parameterNames.toArray(new String[parameterNames.size()]), parameterTypes.toArray(new Class<?>[parameterTypes.size()]));
                    data.expressionEvaluators[m].setReturnType(Object.class);
                    data.expressionEvaluators[m].setThrownExceptions(new Class<?>[] { Exception.class });
                    data.expressionEvaluators[m].cook(fn.getFormula());
                } else {
                    throw new HopException("Unable to find field name for formula [" + (!StringUtil.isEmpty(fn.getFormula()) ? fn.getFormula() : "") + "]");
                }
            }
        }
        for (int i = 0; i < meta.getFormula().length; i++) {
            List<Integer> argumentIndexes = data.argumentIndexes.get(i);
            // This method can only accept the specified number of values...
            // 
            Object[] argumentData = new Object[argumentIndexes.size()];
            for (int x = 0; x < argumentIndexes.size(); x++) {
                int index = argumentIndexes.get(x);
                IValueMeta outputValueMeta = data.outputRowMeta.getValueMeta(index);
                argumentData[x] = outputValueMeta.convertToNormalStorageType(outputRowData[index]);
            }
            Object formulaResult = data.expressionEvaluators[i].evaluate(argumentData);
            Object value = null;
            if (formulaResult == null) {
                value = null;
            } else {
                IValueMeta valueMeta = data.returnType[i];
                if (valueMeta.getNativeDataTypeClass().isAssignableFrom(formulaResult.getClass())) {
                    value = formulaResult;
                } else if (formulaResult instanceof Integer && valueMeta.getType() == IValueMeta.TYPE_INTEGER) {
                    value = ((Integer) formulaResult).longValue();
                } else {
                    throw new HopValueException(BaseMessages.getString(PKG, "Janino.Error.ValueTypeMismatch", valueMeta.getTypeDesc(), meta.getFormula()[i].getFieldName(), formulaResult.getClass(), meta.getFormula()[i].getFormula()));
                }
            }
            // 
            if (data.replaceIndex[i] < 0) {
                outputRowData[tempIndex++] = value;
            } else {
                outputRowData[data.replaceIndex[i]] = value;
            }
        }
        return outputRowData;
    } catch (Exception e) {
        throw new HopValueException(e);
    }
}
Also used : HopException(org.apache.hop.core.exception.HopException) ArrayList(java.util.ArrayList) ExpressionEvaluator(org.codehaus.janino.ExpressionEvaluator) HopException(org.apache.hop.core.exception.HopException) HopValueException(org.apache.hop.core.exception.HopValueException) IValueMeta(org.apache.hop.core.row.IValueMeta) HopValueException(org.apache.hop.core.exception.HopValueException)

Example 5 with HopValueException

use of org.apache.hop.core.exception.HopValueException in project hop by apache.

the class FieldHelperTest method getNativeDataTypeSimpleName_Unknown.

@Test
public void getNativeDataTypeSimpleName_Unknown() throws Exception {
    HopValueException e = new HopValueException();
    IValueMeta v = mock(IValueMeta.class);
    doThrow(e).when(v).getNativeDataTypeClass();
    LogChannel log = mock(LogChannel.class);
    whenNew(LogChannel.class).withAnyArguments().thenReturn(log);
    assertEquals("Object", FieldHelper.getNativeDataTypeSimpleName(v));
    verify(log, times(1)).logDebug("Unable to get name from data type");
}
Also used : IValueMeta(org.apache.hop.core.row.IValueMeta) HopValueException(org.apache.hop.core.exception.HopValueException) LogChannel(org.apache.hop.core.logging.LogChannel) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

HopValueException (org.apache.hop.core.exception.HopValueException)67 IValueMeta (org.apache.hop.core.row.IValueMeta)33 HopException (org.apache.hop.core.exception.HopException)23 IRowMeta (org.apache.hop.core.row.IRowMeta)13 HopTransformException (org.apache.hop.core.exception.HopTransformException)12 ValueMetaString (org.apache.hop.core.row.value.ValueMetaString)10 FileObject (org.apache.commons.vfs2.FileObject)9 RowMetaAndData (org.apache.hop.core.RowMetaAndData)8 IOException (java.io.IOException)5 HopFileException (org.apache.hop.core.exception.HopFileException)5 Test (org.junit.Test)5 BigDecimal (java.math.BigDecimal)4 ArrayList (java.util.ArrayList)4 HopDatabaseException (org.apache.hop.core.exception.HopDatabaseException)4 SocketTimeoutException (java.net.SocketTimeoutException)3 Date (java.util.Date)3 FileSystemException (org.apache.commons.vfs2.FileSystemException)3 HopFileNotFoundException (org.apache.hop.core.exception.HopFileNotFoundException)3 EOFException (java.io.EOFException)2 ParseException (java.text.ParseException)2