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