Search in sources :

Example 1 with ScriptFactory

use of eu.esdihumboldt.hale.common.scripting.ScriptFactory 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);
}
Also used : Script(eu.esdihumboldt.hale.common.scripting.Script) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) ScriptException(javax.script.ScriptException) ScriptException(javax.script.ScriptException) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) Value(eu.esdihumboldt.hale.common.core.io.Value) Map(java.util.Map) ScriptFactory(eu.esdihumboldt.hale.common.scripting.ScriptFactory)

Aggregations

ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)1 PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)1 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)1 Value (eu.esdihumboldt.hale.common.core.io.Value)1 Script (eu.esdihumboldt.hale.common.scripting.Script)1 ScriptFactory (eu.esdihumboldt.hale.common.scripting.ScriptFactory)1 Map (java.util.Map)1 ScriptException (javax.script.ScriptException)1