Search in sources :

Example 1 with JulianDayConverter

use of org.talend.dataquality.converters.JulianDayConverter in project data-prep by Talend.

the class DateCalendarConverter method applyOnColumn.

/**
 * @see ColumnAction#applyOnColumn(DataSetRow, ActionContext)
 */
@Override
public void applyOnColumn(DataSetRow row, ActionContext context) {
    final String columnId = context.getColumnId();
    // Change the date calendar
    final String originalValue = row.get(columnId);
    if (StringUtils.isBlank(originalValue)) {
        row.set(ActionsUtils.getTargetColumnId(context), originalValue);
        return;
    }
    try {
        String newValue = null;
        if (context.get(IS_FROM_CHRONOLOGY_INTERNAL_KEY)) {
            // it is From Chronology
            AbstractChronology fromCalendarTypeKey = context.get(FROM_CALENDAR_TYPE_KEY);
            Locale fromLocaleKey = context.get(FROM_LOCALE_KEY);
            String fromPattern = parseDateFromPatterns(originalValue, context.get(FROM_DATE_PATTERNS_KEY), fromCalendarTypeKey, fromLocaleKey);
            org.talend.dataquality.converters.DateCalendarConverter dateConverter = getDateCalendarConverterInstance(fromPattern, context);
            if (dateConverter != null) {
                newValue = dateConverter.convert(originalValue);
            }
        } else {
            // it is From JulianDay.JulianDay->Chronology OR JulianDay->JulianDay
            JulianDayConverter julianDayConvert = context.get(JULIAN_DAY_CONVERT_KEY);
            newValue = julianDayConvert.convert(originalValue);
        }
        if (StringUtils.isNotEmpty(newValue) && StringUtils.isNotBlank(newValue)) {
            row.set(ActionsUtils.getTargetColumnId(context), newValue);
        }
    } catch (DateTimeException e) {
        // cannot parse the date, let's leave it as is
        LOGGER.debug("Unable to parse date {}.", originalValue);
    }
}
Also used : DateTimeException(java.time.DateTimeException) JulianDayConverter(org.talend.dataquality.converters.JulianDayConverter)

Example 2 with JulianDayConverter

use of org.talend.dataquality.converters.JulianDayConverter in project data-prep by Talend.

the class DateCalendarConverter method getDateCalendarConverterInstance.

/**
 * Create instance DateCalendarConverter only once for each pattern. It is used to covert from Chronology.
 */
private org.talend.dataquality.converters.DateCalendarConverter getDateCalendarConverterInstance(String fromPattern, ActionContext context) {
    if (!(boolean) context.get(IS_FROM_CHRONOLOGY_INTERNAL_KEY) || StringUtils.isEmpty(fromPattern)) {
        return null;
    }
    org.talend.dataquality.converters.DateCalendarConverter dateConvert = dateCalendarConverterMap.get(fromPattern);
    if (dateConvert != null) {
        return dateConvert;
    }
    AbstractChronology fromCalendarTypeKey = context.get(FROM_CALENDAR_TYPE_KEY);
    Locale fromLocaleKey = context.get(FROM_LOCALE_KEY);
    if (context.get(IS_TO_CHRONOLOGY_INTERNAL_KEY)) {
        // Chronology->Chronology
        dateConvert = new org.talend.dataquality.converters.DateCalendarConverter(fromPattern, fromPattern, fromCalendarTypeKey, context.get(TO_CALENDAR_TYPE_KEY), fromLocaleKey, context.get(TO_LOCALE_KEY));
        dateCalendarConverterMap.put(fromPattern, dateConvert);
        return dateConvert;
    } else {
        // Chronology->TemporalField
        JulianDayConverter julianDayConvert = new JulianDayConverter(fromCalendarTypeKey, fromPattern, fromLocaleKey, context.get(TO_CALENDAR_TYPE_KEY));
        dateCalendarConverterMap.put(fromPattern, julianDayConvert);
        return julianDayConvert;
    }
}
Also used : JulianDayConverter(org.talend.dataquality.converters.JulianDayConverter)

Example 3 with JulianDayConverter

use of org.talend.dataquality.converters.JulianDayConverter in project data-prep by Talend.

the class DateCalendarConverter method compile.

@Override
public void compile(ActionContext actionContext) {
    super.compile(actionContext);
    if (ActionsUtils.doesCreateNewColumn(actionContext.getParameters(), CREATE_NEW_COLUMN_DEFAULT)) {
        ActionsUtils.createNewColumn(actionContext, singletonList(ActionsUtils.additionalColumn().withName(actionContext.getColumnName() + NEW_COLUMN_SUFFIX)));
        ColumnMetadata targetColumn = actionContext.getRowMetadata().getById(ActionsUtils.getTargetColumnId(actionContext));
        ColumnMetadata originalColumn = actionContext.getRowMetadata().getById(actionContext.getColumnId());
        targetColumn.setStatistics(new Statistics(originalColumn.getStatistics()));
    }
    if (actionContext.getActionStatus() == OK) {
        dateCalendarConverterMap = new HashMap<>();
        String fromCalendarParameter = actionContext.getParameters().get(FROM_CALENDAR_TYPE_PARAMETER);
        String toCalendarParameter = actionContext.getParameters().get(TO_CALENDAR_TYPE_PARAMETER);
        final boolean isFromChronology = actionContext.get(IS_FROM_CHRONOLOGY_INTERNAL_KEY, p -> valueOf(fromCalendarParameter).isChronology());
        final boolean isToChronology = actionContext.get(IS_TO_CHRONOLOGY_INTERNAL_KEY, p -> valueOf(toCalendarParameter).isChronology());
        if (isFromChronology) {
            AbstractChronology fromCalendarType = valueOf(fromCalendarParameter).getCalendarType();
            Locale fromLocale = valueOf(fromCalendarParameter).getDefaultLocale();
            actionContext.get(FROM_CALENDAR_TYPE_KEY, p -> fromCalendarType);
            actionContext.get(FROM_LOCALE_KEY, p -> fromLocale);
            actionContext.get(FROM_DATE_PATTERNS_KEY, p -> compileFromDatePattern(actionContext));
        } else {
            // from JulianDay,no need to input pattern and Locale
            TemporalField fromTemporalField = valueOf(fromCalendarParameter).getTemporalField();
            actionContext.get(FROM_CALENDAR_TYPE_KEY, p -> fromTemporalField);
        }
        if (isToChronology) {
            AbstractChronology toCalendarType = valueOf(toCalendarParameter).getCalendarType();
            Locale toLocale = valueOf(toCalendarParameter).getDefaultLocale();
            actionContext.get(TO_CALENDAR_TYPE_KEY, p -> toCalendarType);
            actionContext.get(TO_LOCALE_KEY, p -> toLocale);
        } else {
            // to JulianDay,no need to output pattern and Locale
            TemporalField toTemporalField = valueOf(toCalendarParameter).getTemporalField();
            actionContext.get(TO_CALENDAR_TYPE_KEY, p -> toTemporalField);
        }
        // init an instance 'JulianDayConverter' when the converter is from JulianDay
        if (!isFromChronology) {
            JulianDayConverter julianDayConvert;
            if (isToChronology) {
                // convert JulianDay to ISO Calendar and use default output pattern.
                julianDayConvert = new JulianDayConverter(actionContext.get(FROM_CALENDAR_TYPE_KEY), actionContext.get(TO_CALENDAR_TYPE_KEY), DEFAULT_OUTPUT_PATTERN, ISO.getDefaultLocale());
            } else {
                julianDayConvert = new JulianDayConverter((TemporalField) actionContext.get(FROM_CALENDAR_TYPE_KEY), (TemporalField) actionContext.get(TO_CALENDAR_TYPE_KEY));
            }
            actionContext.get(JULIAN_DAY_CONVERT_KEY, p -> julianDayConvert);
        }
    }
}
Also used : ColumnMetadata(org.talend.dataprep.api.dataset.ColumnMetadata) TemporalField(java.time.temporal.TemporalField) JulianDayConverter(org.talend.dataquality.converters.JulianDayConverter) Statistics(org.talend.dataprep.api.dataset.statistics.Statistics)

Aggregations

JulianDayConverter (org.talend.dataquality.converters.JulianDayConverter)3 DateTimeException (java.time.DateTimeException)1 TemporalField (java.time.temporal.TemporalField)1 ColumnMetadata (org.talend.dataprep.api.dataset.ColumnMetadata)1 Statistics (org.talend.dataprep.api.dataset.statistics.Statistics)1