Search in sources :

Example 31 with Value

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);
}
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)

Example 32 with Value

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();
    }
}
Also used : LookupTable(eu.esdihumboldt.hale.common.lookup.LookupTable) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) Value(eu.esdihumboldt.hale.common.core.io.Value) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException)

Example 33 with Value

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;
}
Also used : DefaultCSVLookupReader(eu.esdihumboldt.hale.io.csv.reader.DefaultCSVLookupReader) Value(eu.esdihumboldt.hale.common.core.io.Value) LookupTableInfoImpl(eu.esdihumboldt.hale.common.lookup.impl.LookupTableInfoImpl) LookupTableImpl(eu.esdihumboldt.hale.common.lookup.impl.LookupTableImpl)

Example 34 with Value

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;
}
Also used : Value(eu.esdihumboldt.hale.common.core.io.Value) CSVWriter(au.com.bytecode.opencsv.CSVWriter) OutputStreamWriter(java.io.OutputStreamWriter)

Example 35 with Value

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;
}
Also used : IOConfigurationType(eu.esdihumboldt.hale.common.core.io.project.model.internal.generated.IOConfigurationType) ComplexPropertyType(eu.esdihumboldt.hale.common.core.io.project.model.internal.generated.ComplexPropertyType) ExportConfigurationType(eu.esdihumboldt.hale.common.core.io.project.model.internal.generated.ExportConfigurationType) IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) ProjectType(eu.esdihumboldt.hale.common.core.io.project.model.internal.generated.ProjectType) ProjectFileInfo(eu.esdihumboldt.hale.common.core.io.project.model.ProjectFileInfo) Value(eu.esdihumboldt.hale.common.core.io.Value) PropertyType(eu.esdihumboldt.hale.common.core.io.project.model.internal.generated.PropertyType) ComplexPropertyType(eu.esdihumboldt.hale.common.core.io.project.model.internal.generated.ComplexPropertyType)

Aggregations

Value (eu.esdihumboldt.hale.common.core.io.Value)81 ValueList (eu.esdihumboldt.hale.common.core.io.ValueList)12 LookupTable (eu.esdihumboldt.hale.common.lookup.LookupTable)12 HashMap (java.util.HashMap)12 ValueProperties (eu.esdihumboldt.hale.common.core.io.ValueProperties)11 LookupTableImpl (eu.esdihumboldt.hale.common.lookup.impl.LookupTableImpl)11 IOConfiguration (eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration)10 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)9 ArrayList (java.util.ArrayList)9 URI (java.net.URI)8 Test (org.junit.Test)6 StyledString (org.eclipse.jface.viewers.StyledString)5 Composite (org.eclipse.swt.widgets.Composite)5 DefaultCustomPropertyFunction (eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunction)4 PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)4 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)4 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)4 ProjectService (eu.esdihumboldt.hale.ui.service.project.ProjectService)4 IOException (java.io.IOException)4 Locale (java.util.Locale)4