use of org.talend.dataprep.api.dataset.ColumnMetadata in project data-prep by Talend.
the class StandardizeInvalid method applyOnColumn.
@Override
public void applyOnColumn(DataSetRow row, ActionContext context) {
if (isApplicable(row, context)) {
final String columnId = context.getColumnId();
final String value = row.get(columnId);
final RowMetadata rowMetadata = context.getRowMetadata();
final ColumnMetadata column = rowMetadata.getById(columnId);
final Double threshold = context.get(MATCH_THRESHOLD_KEY);
String closestValue = CategoryRegistryManager.getInstance().findMostSimilarValue(value, column.getDomain(), threshold);
// If not found the similar value, display original value.
if (!StringUtils.isEmpty(closestValue)) {
row.set(columnId, closestValue);
}
}
}
use of org.talend.dataprep.api.dataset.ColumnMetadata in project data-prep by Talend.
the class ComputeTimeSince method applyOnColumn.
@Override
public void applyOnColumn(DataSetRow row, ActionContext context) {
RowMetadata rowMetadata = context.getRowMetadata();
Map<String, String> parameters = context.getParameters();
String columnId = context.getColumnId();
TemporalUnit unit = ChronoUnit.valueOf(parameters.get(TIME_UNIT_PARAMETER).toUpperCase());
String newValue;
try {
String mode = context.get(SINCE_WHEN_PARAMETER);
LocalDateTime since;
switch(mode) {
case OTHER_COLUMN_MODE:
ColumnMetadata selectedColumn = rowMetadata.getById(parameters.get(SELECTED_COLUMN_PARAMETER));
String dateToCompare = row.get(selectedColumn.getId());
since = Providers.get().parse(dateToCompare, selectedColumn);
break;
case SPECIFIC_DATE_MODE:
case NOW_SERVER_SIDE_MODE:
default:
since = context.get(SINCE_DATE_PARAMETER);
break;
}
// parse the date
if (since == null) {
newValue = StringUtils.EMPTY;
} else {
String value = row.get(columnId);
LocalDateTime temporalAccessor = Providers.get().parse(value, context.getRowMetadata().getById(columnId));
Temporal valueAsDate = LocalDateTime.from(temporalAccessor);
newValue = String.valueOf(unit.between(valueAsDate, since));
}
} catch (DateTimeException e) {
LOGGER.trace("Error on dateTime parsing", e);
// Nothing to do: in this case, temporalAccessor is left null
newValue = StringUtils.EMPTY;
}
row.set(ActionsUtils.getTargetColumnId(context), newValue);
}
use of org.talend.dataprep.api.dataset.ColumnMetadata in project data-prep by Talend.
the class DomainChange method applyOnColumn.
/**
* @see ColumnAction#applyOnColumn(DataSetRow, ActionContext)
*/
@Override
public void applyOnColumn(DataSetRow row, ActionContext context) {
final String columnId = context.getColumnId();
final Map<String, String> parameters = context.getParameters();
LOGGER.debug("DomainChange for columnId {} with parameters {} ", columnId, parameters);
final RowMetadata rowMetadata = context.getRowMetadata();
final ColumnMetadata columnMetadata = rowMetadata.getById(columnId);
final String newDomainId = parameters.get(NEW_DOMAIN_ID_PARAMETER_KEY);
if (StringUtils.isNotEmpty(newDomainId)) {
columnMetadata.setDomain(newDomainId);
columnMetadata.setDomainLabel(parameters.get(NEW_DOMAIN_LABEL_PARAMETER_KEY));
columnMetadata.setDomainFrequency(0);
columnMetadata.setDomainForced(true);
}
rowMetadata.update(columnId, columnMetadata);
context.setActionStatus(ActionContext.ActionStatus.DONE);
}
use of org.talend.dataprep.api.dataset.ColumnMetadata in project data-prep by Talend.
the class ReorderColumn method swapColumnMetadata.
protected void swapColumnMetadata(ColumnMetadata originColumn, ColumnMetadata targetColumn) throws Exception {
ColumnMetadata targetColumnCopy = ColumnMetadata.Builder.column().copy(targetColumn).build();
ColumnMetadata originColumnCopy = ColumnMetadata.Builder.column().copy(originColumn).build();
BeanUtils.copyProperties(targetColumn, originColumn);
BeanUtils.copyProperties(originColumn, targetColumnCopy);
Statistics originalStatistics = originColumnCopy.getStatistics();
Statistics targetStatistics = targetColumnCopy.getStatistics();
BeanUtils.copyProperties(targetColumn.getStatistics(), originalStatistics);
BeanUtils.copyProperties(originColumn.getStatistics(), targetStatistics);
Quality originalQuality = originColumnCopy.getQuality();
Quality targetQualityCopty = targetColumnCopy.getQuality();
BeanUtils.copyProperties(targetColumn.getQuality(), originalQuality);
BeanUtils.copyProperties(originColumn.getQuality(), targetQualityCopty);
}
use of org.talend.dataprep.api.dataset.ColumnMetadata in project data-prep by Talend.
the class Swap method applyOnColumn.
@Override
public void applyOnColumn(DataSetRow row, ActionContext context) {
RowMetadata rowMetadata = context.getRowMetadata();
Map<String, String> parameters = context.getParameters();
ColumnMetadata selectedColumn = rowMetadata.getById(parameters.get(SELECTED_COLUMN_PARAMETER));
if (selectedColumn == null) {
return;
}
final String columnId = context.getColumnId();
LOGGER.debug("swapping columns {} <-> {}", columnId, selectedColumn.getId());
String columnValue = row.get(columnId);
String selectedColumnValue = row.get(selectedColumn.getId());
row.set(columnId, selectedColumnValue == null ? StringUtils.EMPTY : selectedColumnValue);
row.set(selectedColumn.getId(), columnValue == null ? StringUtils.EMPTY : columnValue);
}
Aggregations