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);
}
}
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;
}
}
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);
}
}
}
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);
}
}
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);
}
}
}
Aggregations