Search in sources :

Example 1 with ReplaceOnValueHelper

use of org.talend.dataprep.transformation.actions.common.ReplaceOnValueHelper in project data-prep by Talend.

the class ClearMatching method toClear.

@Override
public boolean toClear(DataSetRow dataSetRow, String columnId, ActionContext actionContext) {
    final Map<String, String> parameters = actionContext.getParameters();
    final RowMetadata rowMetadata = actionContext.getRowMetadata();
    final ColumnMetadata columnMetadata = rowMetadata.getById(columnId);
    final String value = dataSetRow.get(columnId);
    final String equalsValue = parameters.get(VALUE_PARAMETER);
    if (Type.get(columnMetadata.getType()) == Type.BOOLEAN) {
        // for boolean we can accept True equalsIgnoreCase true
        return StringUtils.equalsIgnoreCase(value, equalsValue);
    } else {
        ReplaceOnValueHelper replaceOnValueHelper = new ReplaceOnValueHelper().build(equalsValue, true);
        return replaceOnValueHelper.matches(value);
    }
}
Also used : ColumnMetadata(org.talend.dataprep.api.dataset.ColumnMetadata) ReplaceOnValueHelper(org.talend.dataprep.transformation.actions.common.ReplaceOnValueHelper) RowMetadata(org.talend.dataprep.api.dataset.RowMetadata)

Example 2 with ReplaceOnValueHelper

use of org.talend.dataprep.transformation.actions.common.ReplaceOnValueHelper in project data-prep by Talend.

the class ReplaceOnValue method computeNewValue.

/**
 * Compute the new action based on the current action context.
 *
 * @param context the action context.
 * @param originalValue the original value.
 * @return the new value to set based on the parameters within the action context.
 */
protected String computeNewValue(ActionContext context, String originalValue) {
    if (originalValue == null) {
        return null;
    }
    // There are direct calls to this method from unit tests, normally such checks are done during transformation.
    if (context.getActionStatus() != ActionContext.ActionStatus.OK) {
        return originalValue;
    }
    final Map<String, String> parameters = context.getParameters();
    String replacement = parameters.get(REPLACE_VALUE_PARAMETER);
    boolean replaceEntireCell = Boolean.valueOf(parameters.get(REPLACE_ENTIRE_CELL_PARAMETER));
    try {
        final ReplaceOnValueHelper replaceOnValueParameter = context.get(REGEX_HELPER_KEY);
        boolean matches = replaceOnValueParameter.matches(originalValue);
        if (matches) {
            if (replaceOnValueParameter.getOperator().equals(ReplaceOnValueHelper.REGEX_MODE)) {
                final Pattern pattern = replaceEntireCell ? replaceOnValueParameter.getPattern() : Pattern.compile(replaceOnValueParameter.getToken());
                try {
                    return pattern.matcher(originalValue).replaceAll(replacement);
                } catch (IndexOutOfBoundsException e) {
                    // catch to fix TDP_1227 PB#1
                    return originalValue;
                }
            } else if (replaceEntireCell) {
                return replacement;
            } else {
                return originalValue.replace(replaceOnValueParameter.getToken(), replacement);
            }
        } else {
            return originalValue;
        }
    } catch (InvalidParameterException e) {
        return originalValue;
    }
}
Also used : Pattern(java.util.regex.Pattern) InvalidParameterException(java.security.InvalidParameterException) ReplaceOnValueHelper(org.talend.dataprep.transformation.actions.common.ReplaceOnValueHelper)

Example 3 with ReplaceOnValueHelper

use of org.talend.dataprep.transformation.actions.common.ReplaceOnValueHelper in project data-prep by Talend.

the class ReplaceOnValue method compile.

@Override
public void compile(ActionContext actionContext) {
    super.compile(actionContext);
    if (ActionsUtils.doesCreateNewColumn(actionContext.getParameters(), CREATE_NEW_COLUMN_DEFAULT)) {
        ActionsUtils.createNewColumn(actionContext, getAdditionalColumns(actionContext));
    }
    if (actionContext.getActionStatus() == OK) {
        final Map<String, String> parameters = actionContext.getParameters();
        String rawParam = parameters.get(CELL_VALUE_PARAMETER);
        try {
            final ReplaceOnValueHelper regexParametersHelper = new ReplaceOnValueHelper();
            actionContext.get(REGEX_HELPER_KEY, p -> regexParametersHelper.build(rawParam, false));
        } catch (IllegalArgumentException e) {
            actionContext.setActionStatus(CANCELED);
        }
    }
}
Also used : ReplaceOnValueHelper(org.talend.dataprep.transformation.actions.common.ReplaceOnValueHelper)

Example 4 with ReplaceOnValueHelper

use of org.talend.dataprep.transformation.actions.common.ReplaceOnValueHelper in project data-prep by Talend.

the class Cut method applyOnColumn.

@Override
public void applyOnColumn(DataSetRow row, ActionContext context) {
    final String columnId = context.getColumnId();
    final String toCut = row.get(columnId);
    if (toCut != null) {
        final ReplaceOnValueHelper replaceOnValueParameter = context.get(REGEX_HELPER_KEY);
        String value;
        if (replaceOnValueParameter.matches(toCut)) {
            if (replaceOnValueParameter.getOperator().equals(ReplaceOnValueHelper.REGEX_MODE)) {
                // $NON-NLS-1$
                value = toCut.replaceAll(replaceOnValueParameter.getToken(), "");
            } else {
                // $NON-NLS-1$
                value = toCut.replace(replaceOnValueParameter.getToken(), "");
            }
        } else {
            value = toCut;
        }
        row.set(ActionsUtils.getTargetColumnId(context), value);
    }
}
Also used : ReplaceOnValueHelper(org.talend.dataprep.transformation.actions.common.ReplaceOnValueHelper)

Example 5 with ReplaceOnValueHelper

use of org.talend.dataprep.transformation.actions.common.ReplaceOnValueHelper in project data-prep by Talend.

the class Cut method compile.

@Override
public void compile(ActionContext actionContext) {
    super.compile(actionContext);
    if (ActionsUtils.doesCreateNewColumn(actionContext.getParameters(), CREATE_NEW_COLUMN_DEFAULT)) {
        ActionsUtils.createNewColumn(actionContext, getAdditionalColumns(actionContext));
    }
    if (actionContext.getActionStatus() == OK) {
        final Map<String, String> parameters = actionContext.getParameters();
        String rawParam = parameters.get(PATTERN_PARAMETER);
        ReplaceOnValueHelper regexParametersHelper = new ReplaceOnValueHelper();
        try {
            actionContext.get(REGEX_HELPER_KEY, p -> regexParametersHelper.build(rawParam, false));
        } catch (IllegalArgumentException e) {
            actionContext.setActionStatus(CANCELED);
        }
    }
}
Also used : ReplaceOnValueHelper(org.talend.dataprep.transformation.actions.common.ReplaceOnValueHelper)

Aggregations

ReplaceOnValueHelper (org.talend.dataprep.transformation.actions.common.ReplaceOnValueHelper)7 InvalidParameterException (java.security.InvalidParameterException)1 Pattern (java.util.regex.Pattern)1 ColumnMetadata (org.talend.dataprep.api.dataset.ColumnMetadata)1 RowMetadata (org.talend.dataprep.api.dataset.RowMetadata)1