diff --git a/openbis_knime/source/java/ch/systemsx/cisd/openbis/knime/query/ColumnType.java b/openbis_knime/source/java/ch/systemsx/cisd/openbis/knime/query/ColumnType.java new file mode 100644 index 0000000000000000000000000000000000000000..e04e18ec1cd91178b1892b6e701f4a4891725e92 --- /dev/null +++ b/openbis_knime/source/java/ch/systemsx/cisd/openbis/knime/query/ColumnType.java @@ -0,0 +1,76 @@ +/* + * Copyright 2010 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.openbis.knime.query; + +import java.io.Serializable; + +import org.knime.core.data.DataCell; +import org.knime.core.data.DataType; +import org.knime.core.data.def.DoubleCell; +import org.knime.core.data.def.LongCell; +import org.knime.core.data.def.StringCell; + +enum ColumnType +{ + DOUBLE(DoubleCell.TYPE) + { + @Override + public DataCell createCell(Serializable valueOrNull) + { + if (valueOrNull instanceof Double) + { + return new DoubleCell((Double) valueOrNull); + } + return null; + } + }, + LONG(LongCell.TYPE) + { + @Override + public DataCell createCell(Serializable valueOrNull) + { + if (valueOrNull instanceof Long) + { + return new LongCell((Long) valueOrNull); + } + return null; + } + }, + STRING(StringCell.TYPE) + { + + @Override + public DataCell createCell(Serializable valueOrNull) + { + return new StringCell(valueOrNull == null ? "" : valueOrNull.toString()); + } + }; + + private final DataType dataType; + + private ColumnType(DataType dataType) + { + this.dataType = dataType; + } + + public DataType getDataType() + { + return dataType; + } + + public abstract DataCell createCell(Serializable valueOrNull); +} \ No newline at end of file diff --git a/openbis_knime/source/java/ch/systemsx/cisd/openbis/knime/query/QueryNodeModel.java b/openbis_knime/source/java/ch/systemsx/cisd/openbis/knime/query/QueryNodeModel.java index bdb3ae5bf7d3a709d09f403d2d2b1244b0f89c4c..1b28e45f7c04d101e662f3e38f4cb8c1122a60e8 100644 --- a/openbis_knime/source/java/ch/systemsx/cisd/openbis/knime/query/QueryNodeModel.java +++ b/openbis_knime/source/java/ch/systemsx/cisd/openbis/knime/query/QueryNodeModel.java @@ -25,9 +25,6 @@ import org.knime.core.data.DataCell; import org.knime.core.data.DataTableSpec; import org.knime.core.data.DataType; import org.knime.core.data.def.DefaultRow; -import org.knime.core.data.def.DoubleCell; -import org.knime.core.data.def.LongCell; -import org.knime.core.data.def.StringCell; import org.knime.core.node.BufferedDataContainer; import org.knime.core.node.BufferedDataTable; import org.knime.core.node.CanceledExecutionException; @@ -125,14 +122,16 @@ public class QueryNodeModel extends NodeModel QueryTableModel result = facade.executeQuery(queryDescription.getId(), parameterBindings.getBindings()); List<QueryTableColumn> columns = result.getColumns(); String[] columnTitles = new String[columns.size()]; - DataType[] columnTypes = new DataType[columns.size()]; + DataType[] dataTypes = new DataType[columns.size()]; + ColumnType[] columnTypes = new ColumnType[columns.size()]; for (int i = 0, n = columns.size(); i < n; i++) { QueryTableColumn column = columns.get(i); columnTitles[i] = column.getTitle(); - columnTypes[i] = Util.translateDataType(column.getDataType()); + columnTypes[i] = Util.getColumnType(column.getDataType()); + dataTypes[i] = columnTypes[i].getDataType(); } - DataTableSpec dataTableSpec = new DataTableSpec(columnTitles, columnTypes); + DataTableSpec dataTableSpec = new DataTableSpec(columnTitles, dataTypes); BufferedDataContainer container = exec.createDataContainer(dataTableSpec); List<Serializable[]> rows = result.getRows(); for (int i = 0, n = rows.size(); i < n; i++) @@ -141,19 +140,7 @@ public class QueryNodeModel extends NodeModel DataCell[] cells = new DataCell[row.length]; for (int c = 0; c < row.length; c++) { - Serializable value = row[c]; - DataCell cell = null; - if (value instanceof Double) - { - cell = new DoubleCell((Double) value); - } else if (value instanceof Long) - { - cell = new LongCell((Long) value); - } else - { - cell = new StringCell(value.toString()); - } - cells[c] = cell; + cells[c] = columnTypes[c].createCell(row[c]); } container.addRowToTable(new DefaultRow(Integer.toString(i), cells)); } diff --git a/openbis_knime/source/java/ch/systemsx/cisd/openbis/knime/query/Util.java b/openbis_knime/source/java/ch/systemsx/cisd/openbis/knime/query/Util.java index 02023d45fca50f3b3a3df2dd70a19b0cb1c0b09e..5159c51cd2c870c8e53acf378d2dee6ad6e1653c 100644 --- a/openbis_knime/source/java/ch/systemsx/cisd/openbis/knime/query/Util.java +++ b/openbis_knime/source/java/ch/systemsx/cisd/openbis/knime/query/Util.java @@ -22,11 +22,6 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; -import org.knime.core.data.DataType; -import org.knime.core.data.def.DoubleCell; -import org.knime.core.data.def.LongCell; -import org.knime.core.data.def.StringCell; - import ch.systemsx.cisd.openbis.plugin.query.shared.api.v1.dto.QueryDescription; import ch.systemsx.cisd.openbis.plugin.query.shared.api.v1.dto.QueryTableColumnDataType; @@ -70,13 +65,13 @@ class Util } } - static DataType translateDataType(QueryTableColumnDataType dataType) + static ColumnType getColumnType(QueryTableColumnDataType dataType) { switch (dataType) { - case DOUBLE: return DoubleCell.TYPE; - case LONG: return LongCell.TYPE; - default: return StringCell.TYPE; + case DOUBLE: return ColumnType.DOUBLE; + case LONG: return ColumnType.LONG; + default: return ColumnType.STRING; } } }