Search in sources :

Example 16 with DateAndTimeCell

use of org.knime.core.data.date.DateAndTimeCell in project knime-core by knime.

the class TimePresetNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    BufferedDataTable input = inData[0];
    DataTableSpec spec = input.getDataTableSpec();
    String selectedColName = m_selectedCol.getStringValue();
    final int colIdx = spec.findColumnIndex(selectedColName);
    if (colIdx < 0) {
        throw new IllegalArgumentException("Column " + selectedColName + " not found in input table!");
    }
    DataColumnSpecCreator colSpecCreator = new DataColumnSpecCreator(spec.getColumnSpec(selectedColName));
    /*
         * Reset the domain here. Had problems when time was there and a date is
         * added, then the time without the date will stay the lower bound of 
         * the domain.
         */
    colSpecCreator.setDomain(null);
    ColumnRearranger rearranger = new ColumnRearranger(spec);
    final Calendar preset = m_presetCalendar.getCalendar();
    rearranger.replace(new SingleCellFactory(colSpecCreator.createSpec()) {

        @Override
        public DataCell getCell(final DataRow row) {
            DataCell dc = row.getCell(colIdx);
            if (dc.isMissing()) {
                if (m_replaceMissingValues.getBooleanValue()) {
                    return new DateAndTimeCell(preset.getTimeInMillis(), m_presetCalendar.useDate(), m_presetCalendar.useTime(), m_presetCalendar.useMilliseconds());
                }
                return DataType.getMissingCell();
            }
            if (dc.getType().isCompatible(DateAndTimeValue.class)) {
                DateAndTimeValue v = (DateAndTimeValue) dc;
                Calendar existing = v.getUTCCalendarClone();
                // look at the date
                if (m_presetCalendar.useDate() && !v.hasDate()) {
                    // set it
                    existing.set(Calendar.YEAR, preset.get(Calendar.YEAR));
                    existing.set(Calendar.MONTH, preset.get(Calendar.MONTH));
                    existing.set(Calendar.DAY_OF_MONTH, preset.get(Calendar.DAY_OF_MONTH));
                }
                if (m_presetCalendar.useTime() && !v.hasTime()) {
                    // set it
                    existing.set(Calendar.HOUR_OF_DAY, preset.get(Calendar.HOUR_OF_DAY));
                    existing.set(Calendar.MINUTE, preset.get(Calendar.MINUTE));
                    existing.set(Calendar.SECOND, preset.get(Calendar.SECOND));
                    if (m_presetCalendar.useMilliseconds()) {
                        existing.set(Calendar.MILLISECOND, preset.get(Calendar.MILLISECOND));
                    }
                }
                return new DateAndTimeCell(existing.getTimeInMillis(), m_presetCalendar.useDate() || v.hasDate(), m_presetCalendar.useTime() || v.hasTime(), m_presetCalendar.useMilliseconds() || v.hasMillis());
            }
            LOGGER.error("Unsupported type " + dc.getType() + " found in row " + row.getKey() + "!");
            return DataType.getMissingCell();
        }
    }, colIdx);
    BufferedDataTable out = exec.createColumnRearrangeTable(input, rearranger, exec);
    return new BufferedDataTable[] { out };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) Calendar(java.util.Calendar) SettingsModelCalendar(org.knime.timeseries.util.SettingsModelCalendar) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataRow(org.knime.core.data.DataRow) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) SingleCellFactory(org.knime.core.data.container.SingleCellFactory)

Aggregations

DateAndTimeCell (org.knime.core.data.date.DateAndTimeCell)16 DataCell (org.knime.core.data.DataCell)12 DateAndTimeValue (org.knime.core.data.date.DateAndTimeValue)11 Calendar (java.util.Calendar)9 DataRow (org.knime.core.data.DataRow)8 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)8 DoubleValue (org.knime.core.data.DoubleValue)6 IntValue (org.knime.core.data.IntValue)6 LongValue (org.knime.core.data.LongValue)6 DoubleCell (org.knime.core.data.def.DoubleCell)6 IntCell (org.knime.core.data.def.IntCell)6 LongCell (org.knime.core.data.def.LongCell)6 SettingsModelCalendar (org.knime.timeseries.util.SettingsModelCalendar)6 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)4 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)2 StringValue (org.knime.core.data.StringValue)2 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)2 BufferedDataTable (org.knime.core.node.BufferedDataTable)2