use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.
the class AbstractScriptedPropertyTransformation method evaluate.
@Override
protected final ListMultimap<String, Object> evaluate(String transformationIdentifier, E engine, ListMultimap<String, PropertyValue> variables, ListMultimap<String, PropertyEntityDefinition> resultNames, Map<String, String> executionParameters, TransformationLog log) throws TransformationException {
ListMultimap<String, ParameterValue> originalParameters = getParameters();
ListMultimap<String, Value> transformedParameters = ArrayListMultimap.create();
if (originalParameters != null) {
for (Map.Entry<String, ParameterValue> entry : originalParameters.entries()) {
if (!entry.getValue().needsProcessing()) {
Value value = entry.getValue().intern();
if (!value.isRepresentedAsDOM()) {
value = Value.simple(getExecutionContext().getVariables().replaceVariables(value.getStringRepresentation()));
}
transformedParameters.put(entry.getKey(), value);
} else {
// type is a script
ScriptFactory factory = ScriptExtension.getInstance().getFactory(entry.getValue().getType());
if (factory == null)
throw new TransformationException("Couldn't find factory for script id " + entry.getValue().getType());
Script script;
try {
script = factory.createExtensionObject();
} catch (Exception e) {
throw new TransformationException("Couldn't create script from factory", e);
}
Object result;
try {
String scriptStr = entry.getValue().as(String.class);
if (script.requiresReplacedTransformationVariables()) {
// replace transformation variables
scriptStr = getExecutionContext().getVariables().replaceVariables(scriptStr);
}
result = script.evaluate(scriptStr, variables.values(), getExecutionContext());
} catch (ScriptException e) {
throw new TransformationException("Couldn't evaluate a transformation parameter", e);
}
// XXX use conversion service instead of valueOf?
transformedParameters.put(entry.getKey(), Value.simple(result));
}
}
}
this.transformedParameters = Multimaps.unmodifiableListMultimap(transformedParameters);
return evaluateImpl(transformationIdentifier, engine, variables, resultNames, executionParameters, log);
}
use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.
the class ClassificationMapping method evaluate.
@Override
protected Object evaluate(String transformationIdentifier, TransformationEngine engine, ListMultimap<String, PropertyValue> variables, String resultName, PropertyEntityDefinition resultProperty, Map<String, String> executionParameters, TransformationLog log) throws TransformationException, NoResultException {
String source = variables.values().iterator().next().getValueAs(String.class);
LookupTable lookup = ClassificationMappingUtil.getClassificationLookup(getParameters(), getExecutionContext());
if (lookup == null) {
// throw new TransformationException("No classification specified");
log.warn(log.createMessage("No classification specified", null));
} else {
Value target = lookup.lookup(Value.of(source));
if (target != null) {
// return value w/ transformation variables replaced
return getExecutionContext().getVariables().replaceVariables(target);
}
}
String notClassifiedAction = getOptionalParameter(PARAMETER_NOT_CLASSIFIED_ACTION, Value.of(USE_NULL_ACTION)).as(String.class);
if (USE_SOURCE_ACTION.equals(notClassifiedAction)) {
return source;
} else if (notClassifiedAction.startsWith(USE_FIXED_VALUE_ACTION_PREFIX)) {
String notClassified = notClassifiedAction.substring(notClassifiedAction.indexOf(':') + 1);
// return w/ transformation variables replaced
return getExecutionContext().getVariables().replaceVariables(notClassified);
} else {
// return null;
throw new NoResultException();
}
}
use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.
the class CSVLookupReader method execute.
/**
* @see eu.esdihumboldt.hale.common.core.io.impl.AbstractIOProvider#execute(eu.esdihumboldt.hale.common.core.io.ProgressIndicator,
* eu.esdihumboldt.hale.common.core.io.report.IOReporter)
*/
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
int keyColumn = getParameter(LookupTableExportConstants.LOOKUP_KEY_COLUMN).as(Integer.class);
int valueColumn = getParameter(LookupTableExportConstants.LOOKUP_VALUE_COLUMN).as(Integer.class);
boolean skipFirst = getParameter(LookupTableExportConstants.PARAM_SKIP_FIRST_LINE).as(Boolean.class);
DefaultCSVLookupReader reader = new DefaultCSVLookupReader();
Map<Value, Value> values = reader.read(getSource().getInput(), getCharset(), CSVUtil.getSep(this), CSVUtil.getQuote(this), CSVUtil.getEscape(this), skipFirst, keyColumn, valueColumn);
lookupTable = new LookupTableInfoImpl((new LookupTableImpl(values)), getName(), getDescription());
reporter.setSuccess(true);
return reporter;
}
use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.
the class CSVLookupWriter method execute.
/**
* @see eu.esdihumboldt.hale.common.core.io.impl.AbstractIOProvider#execute(eu.esdihumboldt.hale.common.core.io.ProgressIndicator,
* eu.esdihumboldt.hale.common.core.io.report.IOReporter)
*/
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
String sourceColumn = getParameter(LookupTableExportConstants.PARAM_SOURCE_COLUMN).as(String.class);
String targetColumn = getParameter(LookupTableExportConstants.PARAM_TARGET_COLUMN).as(String.class);
CSVWriter writer = new CSVWriter(new OutputStreamWriter(getTarget().getOutput()), CSVUtil.getSep(this), CSVUtil.getQuote(this), CSVUtil.getEscape(this));
Map<Value, Value> table = getLookupTable().getTable().asMap();
// write header first
String[] values = new String[] { sourceColumn, targetColumn };
writer.writeNext(values);
for (Value key : table.keySet()) {
values = new String[] { key.as(String.class), table.get(key).as(String.class) };
writer.writeNext(values);
}
writer.close();
reporter.setSuccess(true);
return reporter;
}
use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.
the class ProjectToJaxb method convert.
/**
* Convert the given project to the corresponding JAXB type.
*
* @param project the project to convert
* @return the converted project
*/
public static ProjectType convert(Project project) {
ProjectType result = new ProjectType();
result.setAuthor(project.getAuthor());
result.setCreated(toXMLCalendar(project.getCreated()));
result.setDescription(project.getDescription());
result.setModified(toXMLCalendar(project.getModified()));
result.setName(project.getName());
result.setSaveConfig(toIOConfigurationType(project.getSaveConfiguration()));
if (project.getHaleVersion() != null) {
result.setVersion(project.getHaleVersion().toString());
}
// resources
for (IOConfiguration resource : project.getResources()) {
result.getResource().add(toIOConfigurationType(resource));
}
// export configs
for (Entry<String, IOConfiguration> confEntry : project.getExportConfigurations().entrySet()) {
IOConfigurationType conf = toIOConfigurationType(confEntry.getValue());
ExportConfigurationType exportConf = new ExportConfigurationType();
exportConf.setConfiguration(conf);
exportConf.setName(confEntry.getKey());
result.getExportConfig().add(exportConf);
}
// project files
for (ProjectFileInfo file : project.getProjectFiles()) {
result.getFile().add(toProjectFileType(file));
}
// properties
for (Entry<String, Value> property : project.getProperties().entrySet()) {
Object p = createProperty(property.getKey(), property.getValue());
if (p instanceof PropertyType) {
result.getAbstractProperty().add(of.createProperty((PropertyType) p));
} else if (p instanceof ComplexPropertyType) {
result.getAbstractProperty().add(of.createComplexProperty((ComplexPropertyType) p));
}
}
return result;
}
Aggregations