use of org.talend.dataprep.transformation.actions.date.DatePattern in project data-prep by Talend.
the class AbstractFillWith method applyOnColumn.
// TODO : utility Overriden methdo WTF
public void applyOnColumn(DataSetRow row, ActionContext context) {
final Map<String, String> parameters = context.getParameters();
final String columnId = context.getColumnId();
final ColumnMetadata columnMetadata = context.getRowMetadata().getById(columnId);
if (shouldBeProcessed(row, columnId)) {
String newValue;
// First, get raw new value regarding mode (constant or other column):
if (parameters.get(MODE_PARAMETER).equals(CONSTANT_MODE)) {
newValue = parameters.get(DEFAULT_VALUE_PARAMETER);
} else {
final RowMetadata rowMetadata = context.getRowMetadata();
final ColumnMetadata selectedColumn = rowMetadata.getById(parameters.get(SELECTED_COLUMN_PARAMETER));
newValue = row.get(selectedColumn.getId());
}
// Second: if we're on a date column, format new value with the most frequent pattern of the column:
Type type = columnMetadata == null ? Type.ANY : Type.get(columnMetadata.getType());
if (type.equals(Type.DATE)) {
try {
final LocalDateTime date = Providers.get().parse(newValue, columnMetadata);
final String mostUsedDatePattern = RowMetadataUtils.getMostUsedDatePattern(columnMetadata);
DateTimeFormatter ourNiceFormatter = mostUsedDatePattern == null ? DEFAULT_FORMATTER : new DatePattern(mostUsedDatePattern).getFormatter();
newValue = ourNiceFormatter.format(date);
} catch (DateTimeException e) {
// Nothing to do, if we can't get a valid pattern, keep the raw value
LOGGER.debug("Unable to parse date {}.", row.get(columnId), e);
}
}
// At the end, set the new value:
row.set(ActionsUtils.getTargetColumnId(context), newValue);
}
}
Aggregations