Search in sources :

Example 6 with TypeConverter

use of org.knime.base.node.jsnippet.type.flowvar.TypeConverter in project knime-core by knime.

the class InFieldsTableModel method validateJavaTypeColumn.

/**
 * Validate java type in the given row.
 * @param row the row to check
 * @return error message if error occurs
 */
private String validateJavaTypeColumn(final int row) {
    Object value = this.getValueAt(row, Column.JAVA_TYPE);
    if (null == value) {
        return "Please select a value";
    }
    Object input = this.getValueAt(row, Column.COLUMN);
    if (value instanceof DataCellToJavaConverterFactory) {
        if (input instanceof FlowVariable) {
            return "DataCell converters do not work for flow variables.";
        } else if (input instanceof DataColumnSpec) {
            final DataType dataType = ((DataColumnSpec) input).getType();
            if (!dataType.getValueClasses().contains(((DataCellToJavaConverterFactory<?, ?>) value).getSourceType())) {
                return "Input DataCell is incompatible with this java type.";
            }
        }
    // otherwise user has to fix column/flowvar first.
    } else if (value instanceof String) {
        // converter factory id
        final String id = (String) value;
        if (input instanceof DataColumnSpec) {
            final DataColumnSpec colSpec = (DataColumnSpec) input;
            final Optional<DataCellToJavaConverterFactory<?, ?>> factory = ConverterUtil.getDataCellToJavaConverterFactory(id);
            if (!factory.isPresent()) {
                return "Converter with id " + id + " could not be found.";
            }
            if (!colSpec.getType().getValueClasses().contains(factory.get().getSourceType())) {
                // But just in case it does, this will make sure we know.
                return "Selected factory does not support the input type.";
            }
            return null;
        } else if (input instanceof FlowVariable) {
            return "DataCell converters do not work for flow variables.";
        }
    } else if (value instanceof Class) {
        Class<?> javaType = (Class<?>) value;
        if (input instanceof DataColumnSpec) {
            DataColumnSpec colSpec = (DataColumnSpec) input;
            final Collection<?> factories = ConverterUtil.getFactoriesForSourceType(colSpec.getType());
            return factories.isEmpty() ? "The java type \"" + javaType.getSimpleName() + "\" is not supported." : null;
        } else if (input instanceof FlowVariable) {
            FlowVariable flowVar = (FlowVariable) input;
            TypeConverter typeConversion = TypeProvider.getDefault().getTypeConverter(flowVar.getType());
            return typeConversion.canProvideJavaType(javaType) ? null : "The java type \"" + javaType.getSimpleName() + "\" is not supported.";
        }
    } else {
        return "Cannot find type or converter " + value.toString();
    }
    return null;
}
Also used : TypeConverter(org.knime.base.node.jsnippet.type.flowvar.TypeConverter) DataColumnSpec(org.knime.core.data.DataColumnSpec) Optional(java.util.Optional) DataType(org.knime.core.data.DataType) DataCellToJavaConverterFactory(org.knime.core.data.convert.java.DataCellToJavaConverterFactory) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Aggregations

TypeConverter (org.knime.base.node.jsnippet.type.flowvar.TypeConverter)6 FlowVariable (org.knime.core.node.workflow.FlowVariable)5 Optional (java.util.Optional)4 DataType (org.knime.core.data.DataType)4 DataColumnSpec (org.knime.core.data.DataColumnSpec)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 JavaSnippet (org.knime.base.node.jsnippet.JavaSnippet)2 FlowVariableException (org.knime.base.node.jsnippet.expression.FlowVariableException)2 ConverterUtil (org.knime.base.node.jsnippet.type.ConverterUtil)2 TypeProvider (org.knime.base.node.jsnippet.type.TypeProvider)2 FieldType (org.knime.base.node.jsnippet.util.field.JavaField.FieldType)2 JavaToDataCellConverterFactory (org.knime.core.data.convert.datacell.JavaToDataCellConverterFactory)2 DataCellToJavaConverterFactory (org.knime.core.data.convert.java.DataCellToJavaConverterFactory)2 Type (org.knime.core.node.workflow.FlowVariable.Type)2 Array (java.lang.reflect.Array)1 ListCell (org.knime.core.data.collection.ListCell)1 ArrayToCollectionConverterFactory (org.knime.core.data.convert.datacell.ArrayToCollectionConverterFactory)1