Search in sources :

Example 11 with HopTransformException

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

the class UserDefinedJavaClassMeta method getFields.

@Override
public void getFields(IRowMeta row, String originTransformName, IRowMeta[] info, TransformMeta nextTransform, IVariables variables, IHopMetadataProvider metadataProvider) throws HopTransformException {
    if (!checkClassCookings(getLog())) {
        if (cookErrors.size() > 0) {
            throw new HopTransformException("Error initializing UserDefinedJavaClass to get fields: ", cookErrors.get(0));
        } else {
            return;
        }
    }
    try {
        Method getFieldsMethod = cookedTransformClass.getMethod("getFields", boolean.class, IRowMeta.class, String.class, IRowMeta[].class, TransformMeta.class, IVariables.class, List.class);
        getFieldsMethod.invoke(null, isClearingResultFields(), row, originTransformName, info, nextTransform, variables, getFieldInfo());
    } catch (Exception e) {
        throw new HopTransformException("Error executing UserDefinedJavaClass.getFields(): ", e);
    }
}
Also used : IRowMeta(org.apache.hop.core.row.IRowMeta) HopTransformException(org.apache.hop.core.exception.HopTransformException) Method(java.lang.reflect.Method) HopException(org.apache.hop.core.exception.HopException) CompileException(org.codehaus.commons.compiler.CompileException) HopXmlException(org.apache.hop.core.exception.HopXmlException) IOException(java.io.IOException) HopTransformException(org.apache.hop.core.exception.HopTransformException)

Example 12 with HopTransformException

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

the class TransformClassBase method get.

public FieldHelper get(Fields type, String name) throws HopTransformException {
    FieldHelper fh;
    switch(type) {
        case In:
            fh = inFieldHelpers.get(name);
            if (fh == null) {
                try {
                    fh = new FieldHelper(data.inputRowMeta, name);
                } catch (IllegalArgumentException e) {
                    throw new HopTransformException(BaseMessages.getString(PKG, "TransformClassBase.Exception.UnableToFindFieldHelper", type.name(), name));
                }
                inFieldHelpers.put(name, fh);
            }
            break;
        case Out:
            fh = outFieldHelpers.get(name);
            if (fh == null) {
                try {
                    fh = new FieldHelper(data.outputRowMeta, name);
                } catch (IllegalArgumentException e) {
                    throw new HopTransformException(BaseMessages.getString(PKG, "TransformClassBase.Exception.UnableToFindFieldHelper", type.name(), name));
                }
                outFieldHelpers.put(name, fh);
            }
            break;
        case Info:
            fh = infoFieldHelpers.get(name);
            if (fh == null) {
                IRowMeta rmi = getPipelineMeta().getPrevInfoFields(parent, getTransformName());
                try {
                    fh = new FieldHelper(rmi, name);
                } catch (IllegalArgumentException e) {
                    throw new HopTransformException(BaseMessages.getString(PKG, "TransformClassBase.Exception.UnableToFindFieldHelper", type.name(), name));
                }
                infoFieldHelpers.put(name, fh);
            }
            break;
        default:
            throw new HopTransformException(BaseMessages.getString(PKG, "TransformClassBase.Exception.InvalidFieldsType", type.name(), name));
    }
    return fh;
}
Also used : IRowMeta(org.apache.hop.core.row.IRowMeta) HopTransformException(org.apache.hop.core.exception.HopTransformException)

Example 13 with HopTransformException

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

the class PipelineExecutorMeta method addFieldToRow.

protected void addFieldToRow(IRowMeta row, String fieldName, int type, int length, int precision) throws HopTransformException {
    if (!Utils.isEmpty(fieldName)) {
        try {
            IValueMeta value = ValueMetaFactory.createValueMeta(fieldName, type, length, precision);
            value.setOrigin(getParentTransformMeta().getName());
            row.addValueMeta(value);
        } catch (HopPluginException e) {
            throw new HopTransformException(BaseMessages.getString(PKG, "PipelineExecutorMeta.ValueMetaInterfaceCreation", fieldName), e);
        }
    }
}
Also used : IValueMeta(org.apache.hop.core.row.IValueMeta) HopPluginException(org.apache.hop.core.exception.HopPluginException) HopTransformException(org.apache.hop.core.exception.HopTransformException)

Example 14 with HopTransformException

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

the class RegexEval method processRow.

@Override
public boolean processRow() throws HopException {
    Object[] row = getRow();
    if (row == null) {
        // no more input to be expected...
        setOutputDone();
        return false;
    }
    if (first) {
        // we just got started
        first = false;
        // get the RowMeta
        data.outputRowMeta = getInputRowMeta().clone();
        int captureIndex = getInputRowMeta().size();
        meta.getFields(data.outputRowMeta, getTransformName(), null, null, this, metadataProvider);
        // Let's check that Result Field is given
        if (Utils.isEmpty(resolve(meta.getResultFieldName()))) {
            if (!meta.isAllowCaptureGroupsFlagSet()) {
                // Result field is missing !
                logError(BaseMessages.getString(PKG, "RegexEval.Log.ErrorResultFieldMissing"));
                throw new HopTransformException(BaseMessages.getString(PKG, "RegexEval.Exception.ErrorResultFieldMissing"));
            }
            data.indexOfResultField = -1;
        } else {
            if (meta.isReplacefields()) {
                data.indexOfResultField = getInputRowMeta().indexOfValue(meta.getResultFieldName());
            }
            if (data.indexOfResultField < 0) {
                data.indexOfResultField = getInputRowMeta().size();
                captureIndex++;
            }
        }
        // Check if a Field (matcher) is given
        if (meta.getMatcher() == null) {
            // Matcher is missing !
            logError(BaseMessages.getString(PKG, "RegexEval.Log.ErrorMatcherMissing"));
            throw new HopTransformException(BaseMessages.getString(PKG, "RegexEval.Exception.ErrorMatcherMissing"));
        }
        // ICache the position of the Field
        data.indexOfFieldToEvaluate = getInputRowMeta().indexOfValue(meta.getMatcher());
        if (data.indexOfFieldToEvaluate < 0) {
            // The field is unreachable !
            logError(BaseMessages.getString(PKG, "RegexEval.Log.ErrorFindingField") + "[" + meta.getMatcher() + "]");
            throw new HopTransformException(BaseMessages.getString(PKG, "RegexEval.Exception.CouldnotFindField", meta.getMatcher()));
        }
        // ICache the position of the CaptureGroups
        if (meta.isAllowCaptureGroupsFlagSet()) {
            data.positions = new int[meta.getFieldName().length];
            String[] fieldName = meta.getFieldName();
            for (int i = 0; i < fieldName.length; i++) {
                if (fieldName[i] == null || fieldName[i].length() == 0) {
                    continue;
                }
                if (meta.isReplacefields()) {
                    data.positions[i] = data.outputRowMeta.indexOfValue(fieldName[i]);
                } else {
                    data.positions[i] = captureIndex;
                    captureIndex++;
                }
            }
        } else {
            data.positions = new int[0];
        }
        // Now create objects to do string to data type conversion...
        // 
        data.conversionRowMeta = data.outputRowMeta.cloneToType(IValueMeta.TYPE_STRING);
    }
    // reserve room
    Object[] outputRow = RowDataUtil.allocateRowData(data.outputRowMeta.size());
    System.arraycopy(row, 0, outputRow, 0, getInputRowMeta().size());
    try {
        // Get the Field value
        String fieldValue;
        boolean isMatch;
        if (getInputRowMeta().isNull(row, data.indexOfFieldToEvaluate)) {
            fieldValue = "";
            isMatch = false;
        } else {
            fieldValue = getInputRowMeta().getString(row, data.indexOfFieldToEvaluate);
            // Start search engine
            Matcher m = data.pattern.matcher(fieldValue);
            isMatch = m.matches();
            if (meta.isAllowCaptureGroupsFlagSet() && data.positions.length != m.groupCount()) {
                // Runtime exception case. The number of capture groups in the
                // regex doesn't match the number of fields.
                logError(BaseMessages.getString(PKG, "RegexEval.Log.ErrorCaptureGroupFieldsMismatch", String.valueOf(m.groupCount()), String.valueOf(data.positions.length)));
                throw new HopTransformException(BaseMessages.getString(PKG, "RegexEval.Exception.ErrorCaptureGroupFieldsMismatch", String.valueOf(m.groupCount()), String.valueOf(data.positions.length)));
            }
            for (int i = 0; i < data.positions.length; i++) {
                int index = data.positions[i];
                String value;
                if (isMatch) {
                    value = m.group(i + 1);
                } else {
                    value = null;
                }
                // preserve the incoming data, but allow for "trim type", etc.
                if (value == null) {
                    try {
                        value = data.outputRowMeta.getString(outputRow, index);
                    } catch (ArrayIndexOutOfBoundsException err) {
                    // Ignore errors
                    }
                }
                IValueMeta valueMeta = data.outputRowMeta.getValueMeta(index);
                IValueMeta conversionValueMeta = data.conversionRowMeta.getValueMeta(index);
                Object convertedValue = valueMeta.convertDataFromString(value, conversionValueMeta, meta.getFieldNullIf()[i], meta.getFieldIfNull()[i], meta.getFieldTrimType()[i]);
                outputRow[index] = convertedValue;
            }
        }
        if (data.indexOfResultField >= 0) {
            outputRow[data.indexOfResultField] = isMatch;
        }
        if (log.isRowLevel()) {
            logRowlevel(BaseMessages.getString(PKG, "RegexEval.Log.ReadRow") + " " + getInputRowMeta().getString(row));
        }
        // copy row to output rowset(s)
        // 
        putRow(data.outputRowMeta, outputRow);
    } catch (HopException e) {
        boolean sendToErrorRow = false;
        String errorMessage = null;
        if (getTransformMeta().isDoingErrorHandling()) {
            sendToErrorRow = true;
            errorMessage = e.toString();
        } else {
            throw new HopTransformException(BaseMessages.getString(PKG, "RegexEval.Log.ErrorInTransform"), e);
        }
        if (sendToErrorRow) {
            // Simply add this row to the error row
            putError(getInputRowMeta(), outputRow, 1, errorMessage, null, "REGEX001");
        }
    }
    return true;
}
Also used : IValueMeta(org.apache.hop.core.row.IValueMeta) Matcher(java.util.regex.Matcher) HopException(org.apache.hop.core.exception.HopException) HopTransformException(org.apache.hop.core.exception.HopTransformException)

Example 15 with HopTransformException

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

the class ModPartitioner method getPartition.

@Override
public int getPartition(IVariables variables, IRowMeta rowMeta, Object[] row) throws HopException {
    if (rowMeta == null) {
        throw new HopException("No row metadata was provided and so a partition can't be calculated on field '" + fieldName + "' using a mod partitioner");
    }
    init(variables, rowMeta);
    if (partitionColumnIndex < 0) {
        partitionColumnIndex = rowMeta.indexOfValue(fieldName);
        if (partitionColumnIndex < 0) {
            throw new HopTransformException("Unable to find partitioning field name [" + fieldName + "] in the output row..." + rowMeta);
        }
    }
    long value;
    IValueMeta valueMeta = rowMeta.getValueMeta(partitionColumnIndex);
    Object valueData = row[partitionColumnIndex];
    switch(valueMeta.getType()) {
        case IValueMeta.TYPE_INTEGER:
            Long longValue = rowMeta.getInteger(row, partitionColumnIndex);
            if (longValue == null) {
                value = valueMeta.hashCode(valueData);
            } else {
                value = longValue.longValue();
            }
            break;
        default:
            value = valueMeta.hashCode(valueData);
    }
    int targetLocation = (int) (Math.abs(value) % nrPartitions);
    return targetLocation;
}
Also used : IValueMeta(org.apache.hop.core.row.IValueMeta) HopException(org.apache.hop.core.exception.HopException) HopTransformException(org.apache.hop.core.exception.HopTransformException)

Aggregations

HopTransformException (org.apache.hop.core.exception.HopTransformException)189 HopException (org.apache.hop.core.exception.HopException)109 IValueMeta (org.apache.hop.core.row.IValueMeta)88 IRowMeta (org.apache.hop.core.row.IRowMeta)76 RowMeta (org.apache.hop.core.row.RowMeta)37 PipelineMeta (org.apache.hop.pipeline.PipelineMeta)22 HopXmlException (org.apache.hop.core.exception.HopXmlException)21 DatabaseMeta (org.apache.hop.core.database.DatabaseMeta)20 ValueMetaString (org.apache.hop.core.row.value.ValueMetaString)20 ArrayList (java.util.ArrayList)19 HopValueException (org.apache.hop.core.exception.HopValueException)19 RowAdapter (org.apache.hop.pipeline.transform.RowAdapter)17 FileObject (org.apache.commons.vfs2.FileObject)15 HopPluginException (org.apache.hop.core.exception.HopPluginException)14 BaseTransformMeta (org.apache.hop.pipeline.transform.BaseTransformMeta)12 IVariables (org.apache.hop.core.variables.IVariables)11 FormAttachment (org.eclipse.swt.layout.FormAttachment)11 FormData (org.eclipse.swt.layout.FormData)11 Const (org.apache.hop.core.Const)10 HopDatabaseException (org.apache.hop.core.exception.HopDatabaseException)10