use of org.knime.base.node.jsnippet.type.flowvar.TypeConverter in project knime-core by knime.
the class OutFieldsTableModel method validateJavaTypeColumn.
/**
* Validate java type in the given row.
* @param row the row to check
* @return error message if error occurs
*/
@SuppressWarnings("rawtypes")
private String validateJavaTypeColumn(final int row) {
Object value = this.getValueAt(row, Column.JAVA_TYPE);
if (null == value) {
return "Please select a value";
}
if (value instanceof JavaToDataCellConverterFactory) {
if (getValueAt(row, Column.FIELD_TYPE) == FieldType.FlowVariable) {
return "Cannot use DataCell converters for flow variables.";
}
} else if (value instanceof String) {
final String id = (String) value;
final Optional<JavaToDataCellConverterFactory<?>> factory = ConverterUtil.getJavaToDataCellConverterFactory(id);
return factory.isPresent() ? null : "Converter factory " + id + " was not found.";
} else if (value instanceof Class) {
Class javaType = (Class) value;
Object outType = getValueAt(row, Column.DATA_TYPE);
if (outType instanceof DataType) {
DataType elemType = (DataType) outType;
boolean isCollection = (Boolean) getValueAt(row, Column.IS_COLLECTION);
DataType dataType = isCollection ? ListCell.getCollectionType(elemType) : elemType;
final Collection<JavaToDataCellConverterFactory<?>> factories = ConverterUtil.getFactoriesForDestinationType(dataType);
if (factories.isEmpty()) {
return "The java type \"" + javaType.getSimpleName() + "\" is not supported for output columns.";
}
if (dataType.isCollectionType() && !javaType.isArray()) {
return "Please choose an java array for collection types.";
}
if (!dataType.isCollectionType() && javaType.isArray()) {
return "An array cannot be written to a non collection column";
}
} else if (outType instanceof Type) {
Type type = (Type) outType;
TypeConverter typeConversion = TypeProvider.getDefault().getTypeConverter(type);
if (!typeConversion.canProvideJavaType(javaType)) {
return "The java type \"" + javaType.getSimpleName() + "\" is not supported.";
}
}
} else {
return "Cannot find class " + value.toString();
}
// no error found
return null;
}
use of org.knime.base.node.jsnippet.type.flowvar.TypeConverter in project knime-core by knime.
the class OutFieldsTableModel method getAllowedJavaTypes.
/**
* {@inheritDoc}
*/
@Override
public Object[] getAllowedJavaTypes(final int row) {
Object input = getValueAt(row, Column.DATA_TYPE);
if (input instanceof DataType) {
DataType elemType = (DataType) input;
boolean isCollection = (Boolean) getValueAt(row, Column.IS_COLLECTION);
final DataType dataType = isCollection ? ListCell.getCollectionType(elemType) : elemType;
final Collection<JavaToDataCellConverterFactory<?>> factories = ConverterUtil.getFactoriesForDestinationType(dataType).stream().filter(factory -> JavaSnippet.getBuildPathFromCache(factory.getIdentifier()) != null).collect(Collectors.toList());
return factories.toArray(new Object[factories.size()]);
} else if (input instanceof Type) {
Type type = (Type) input;
TypeConverter typeConversion = TypeProvider.getDefault().getTypeConverter(type);
return typeConversion.canCreatedFromJavaTypes();
} else {
return new Class[] { String.class };
}
}
use of org.knime.base.node.jsnippet.type.flowvar.TypeConverter in project knime-core by knime.
the class FlowVariableRepository method getValueOfType.
/**
* Get value of a flow variable. The type of the returned object is equal
* to the given className.
* @param name the name of the flow variable
* @param className the type of the returned object
* @return the value of the flow variable
*/
@SuppressWarnings("rawtypes")
public Object getValueOfType(final String name, final Class className) {
FlowVariable flowVar = getFlowVariable(name);
if (null == flowVar) {
throw new FlowVariableException("The flow variable with name \"" + name + "\" does not exist.");
}
TypeConverter converter = TypeProvider.getDefault().getTypeConverter(flowVar.getType());
return converter.getValue(flowVar, className);
}
use of org.knime.base.node.jsnippet.type.flowvar.TypeConverter in project knime-core by knime.
the class FlowVariableRepository method isOfType.
/**
* Returns true when getValueOfType(String, Class) does not throw
* an TypeException when called with the given flow variable and the given
* class name.
* @param name the name of the flow variable
* @param className the type
* @return true when flow variable is of type.
*/
@SuppressWarnings("rawtypes")
public boolean isOfType(final String name, final Class className) {
FlowVariable flowVar = getFlowVariable(name);
if (null == flowVar) {
throw new FlowVariableException("The flow variable with name \"" + name + "\" does not exist.");
}
TypeConverter converter = TypeProvider.getDefault().getTypeConverter(flowVar.getType());
return converter.canProvideJavaType(className);
}
use of org.knime.base.node.jsnippet.type.flowvar.TypeConverter in project knime-core by knime.
the class InFieldsTableModel method getAllowedJavaTypes.
/**
* {@inheritDoc}
*/
@Override
public Object[] getAllowedJavaTypes(final int row) {
Object input = getValueAt(row, Column.COLUMN);
if (input instanceof DataColumnSpec) {
DataColumnSpec colSpec = (DataColumnSpec) input;
final Collection<DataCellToJavaConverterFactory<?, ?>> factories = ConverterUtil.getFactoriesForSourceType(colSpec.getType()).stream().filter(factory -> JavaSnippet.getBuildPathFromCache(factory.getIdentifier()) != null).sorted((a, b) -> a.getName().compareToIgnoreCase(b.getName())).collect(Collectors.toList());
final Object[] array = factories.toArray(new Object[factories.size()]);
return array;
} else if (input instanceof FlowVariable) {
FlowVariable flowVar = (FlowVariable) input;
TypeConverter typeConversion = TypeProvider.getDefault().getTypeConverter(flowVar.getType());
return typeConversion.canProvideJavaTypes();
} else {
return new Class[0];
}
}
Aggregations