Search in sources :

Example 6 with LookupTableImpl

use of eu.esdihumboldt.hale.common.lookup.impl.LookupTableImpl in project hale by halestudio.

the class ClassificationMappingParameterPage method getConfiguration.

/**
 * @see eu.esdihumboldt.hale.ui.function.generic.pages.ParameterPage#getConfiguration()
 */
@Override
public ListMultimap<String, ParameterValue> getConfiguration() {
    ListMultimap<String, ParameterValue> configuration = ArrayListMultimap.create();
    for (TabItem tabItem : tabs.getSelection()) {
        if (tabItem.equals(fromFileItem)) {
            // Set the selected lookupTable
            IStructuredSelection selection = (IStructuredSelection) lookupTableComboViewer.getSelection();
            configuration.put(PARAMETER_LOOKUPTABLE_ID, new ParameterValue(selection.getFirstElement().toString()));
        } else {
            if (tabItem.equals(manualItem)) {
                LookupTable realLookupTable = new LookupTableImpl(lookupTable);
                configuration.put(PARAMETER_LOOKUPTABLE, new ParameterValue(Value.complex(realLookupTable)));
            }
        }
    }
    switch(notClassifiedActionOptions.indexOf(((IStructuredSelection) notClassifiedActionViewer.getSelection()).getFirstElement())) {
        case 1:
            notClassifiedAction = "source";
            break;
        case 2:
            notClassifiedAction = "fixed:" + fixedValueText.getText();
            break;
        case 0:
        case -1:
        default:
            notClassifiedAction = "null";
    }
    configuration.put(PARAMETER_NOT_CLASSIFIED_ACTION, new ParameterValue(notClassifiedAction));
    return configuration;
}
Also used : TabItem(org.eclipse.swt.widgets.TabItem) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) LookupTable(eu.esdihumboldt.hale.common.lookup.LookupTable) StyledString(org.eclipse.jface.viewers.StyledString) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) LookupTableImpl(eu.esdihumboldt.hale.common.lookup.impl.LookupTableImpl)

Example 7 with LookupTableImpl

use of eu.esdihumboldt.hale.common.lookup.impl.LookupTableImpl in project hale by halestudio.

the class AppSchemaMappingTest method testClassificationHandler.

@Test
public void testClassificationHandler() {
    final int FIRST_SOURCE = 1000;
    final String FIRST_TARGET = "http://www.example.com/first";
    final int SECOND_SOURCE = 2000;
    final String SECOND_TARGET = "http://www.example.com/second";
    final int THIRD_SOURCE = 3000;
    final String THIRD_TARGET = "http://www.example.com/third";
    final String FIXED_VALUE = "http://www.example.com/unknown";
    final String OCQL_PATTERN = "if_then_else(in(unit_id,%s), Recode(unit_id,%s), %s)";
    Cell typeCell = getDefaultTypeCell(unitDenormType, landCoverUnitType);
    DefaultCell cell = new DefaultCell();
    cell.setTransformationIdentifier(ClassificationMappingFunction.ID);
    cell.setSource(getUnitIdSourceProperty(unitDenormType));
    cell.setTarget(getMetaDataHrefTargetProperty());
    Map<Value, Value> tableValues = new HashMap<Value, Value>();
    tableValues.put(new StringValue(FIRST_SOURCE), new StringValue(FIRST_TARGET));
    tableValues.put(new StringValue(SECOND_SOURCE), new StringValue(SECOND_TARGET));
    tableValues.put(new StringValue(THIRD_SOURCE), new StringValue(THIRD_TARGET));
    LookupTable lookupTable = new LookupTableImpl(tableValues);
    ListMultimap<String, ParameterValue> parameters = ArrayListMultimap.create();
    parameters.put(ClassificationMappingFunction.PARAMETER_LOOKUPTABLE, new ParameterValue(new ComplexValue(lookupTable)));
    parameters.put(ClassificationMapping.PARAMETER_NOT_CLASSIFIED_ACTION, new ParameterValue(ClassificationMapping.USE_SOURCE_ACTION));
    cell.setTransformationParameters(parameters);
    StringBuilder inArgs = new StringBuilder();
    StringBuilder recodeArgs = new StringBuilder();
    int count = 0;
    for (Value sourceValue : tableValues.keySet()) {
        inArgs.append(sourceValue.as(String.class));
        recodeArgs.append(sourceValue).append(",'").append(tableValues.get(sourceValue)).append("'");
        if (count < tableValues.size() - 1) {
            inArgs.append(",");
            recodeArgs.append(",");
        }
        count++;
    }
    final String OCQL_USE_SOURCE = String.format(OCQL_PATTERN, inArgs.toString(), recodeArgs.toString(), "unit_id");
    final String OCQL_USE_NULL = String.format(OCQL_PATTERN, inArgs.toString(), recodeArgs.toString(), "Expression.NIL");
    final String OCQL_USE_FIXED = String.format(OCQL_PATTERN, inArgs.toString(), recodeArgs.toString(), "'" + FIXED_VALUE + "'");
    ClassificationHandler classificationHandler = new ClassificationHandler();
    AttributeMappingType attrMapping = classificationHandler.handlePropertyTransformation(typeCell, cell, new AppSchemaMappingContext(mappingWrapper));
    assertNotNull(attrMapping.getClientProperty());
    assertEquals(1, attrMapping.getClientProperty().size());
    assertEquals("xlink:href", attrMapping.getClientProperty().get(0).getName());
    assertEquals(OCQL_USE_SOURCE, attrMapping.getClientProperty().get(0).getValue());
    assertEquals(TARGET_METADATA, attrMapping.getTargetAttribute());
    // reset mapping
    initMapping();
    parameters.removeAll(ClassificationMapping.PARAMETER_NOT_CLASSIFIED_ACTION);
    parameters.put(ClassificationMapping.PARAMETER_NOT_CLASSIFIED_ACTION, new ParameterValue(ClassificationMapping.USE_NULL_ACTION));
    cell.setTransformationParameters(parameters);
    attrMapping = classificationHandler.handlePropertyTransformation(typeCell, cell, new AppSchemaMappingContext(mappingWrapper));
    assertNotNull(attrMapping.getClientProperty());
    assertEquals(1, attrMapping.getClientProperty().size());
    assertEquals("xlink:href", attrMapping.getClientProperty().get(0).getName());
    assertEquals(OCQL_USE_NULL, attrMapping.getClientProperty().get(0).getValue());
    assertEquals(TARGET_METADATA, attrMapping.getTargetAttribute());
    // reset mapping
    initMapping();
    parameters.removeAll(ClassificationMapping.PARAMETER_NOT_CLASSIFIED_ACTION);
    parameters.put(ClassificationMapping.PARAMETER_NOT_CLASSIFIED_ACTION, new ParameterValue(ClassificationMapping.USE_FIXED_VALUE_ACTION_PREFIX + FIXED_VALUE));
    cell.setTransformationParameters(parameters);
    attrMapping = classificationHandler.handlePropertyTransformation(typeCell, cell, new AppSchemaMappingContext(mappingWrapper));
    assertNotNull(attrMapping.getClientProperty());
    assertEquals(1, attrMapping.getClientProperty().size());
    assertEquals("xlink:href", attrMapping.getClientProperty().get(0).getName());
    assertEquals(OCQL_USE_FIXED, attrMapping.getClientProperty().get(0).getValue());
    assertEquals(TARGET_METADATA, attrMapping.getTargetAttribute());
}
Also used : ComplexValue(eu.esdihumboldt.hale.common.core.io.impl.ComplexValue) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) HashMap(java.util.HashMap) AttributeMappingType(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) ComplexValue(eu.esdihumboldt.hale.common.core.io.impl.ComplexValue) StringValue(eu.esdihumboldt.hale.common.core.io.impl.StringValue) Value(eu.esdihumboldt.hale.common.core.io.Value) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) LookupTable(eu.esdihumboldt.hale.common.lookup.LookupTable) AppSchemaMappingContext(eu.esdihumboldt.hale.io.appschema.writer.internal.mapping.AppSchemaMappingContext) StringValue(eu.esdihumboldt.hale.common.core.io.impl.StringValue) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) Cell(eu.esdihumboldt.hale.common.align.model.Cell) ClassificationHandler(eu.esdihumboldt.hale.io.appschema.writer.internal.ClassificationHandler) LookupTableImpl(eu.esdihumboldt.hale.common.lookup.impl.LookupTableImpl) Test(org.junit.Test)

Example 8 with LookupTableImpl

use of eu.esdihumboldt.hale.common.lookup.impl.LookupTableImpl in project hale by halestudio.

the class ClassificationMappingUtil method getClassificationLookup.

/**
 * Get the classification lookup table from the transformation parameters.
 *
 * @param parameters the transformation parameters
 * @param serviceProvider service provider in case a lookup table has to be
 *            retrieved through a service
 * @return the classification lookup table
 */
public static LookupTable getClassificationLookup(Multimap<String, ? extends Value> parameters, ServiceProvider serviceProvider) {
    try {
        if (!(parameters.get(PARAMETER_LOOKUPTABLE).isEmpty())) {
            Collection<? extends Value> tmpMap = parameters.get(PARAMETER_LOOKUPTABLE);
            return tmpMap.iterator().next().as(LookupTable.class);
        }
        if (!(parameters.get(PARAMETER_LOOKUPTABLE_ID).isEmpty())) {
            LookupService lookupServiceImpl = serviceProvider.getService(LookupService.class);
            Collection<? extends Value> tmpMap = parameters.get(PARAMETER_LOOKUPTABLE_ID);
            LookupTableInfo lookupTableInfo = lookupServiceImpl.getTable(tmpMap.iterator().next().as(String.class));
            return lookupTableInfo.getTable();
        }
    } catch (NullPointerException e) {
        log.error("Service provider not accessible for retrieving lookup table", e);
    }
    // For reason of compatibility we need the following code
    // lookup table in strangely encoded string parameter
    Collection<? extends Value> mappings = parameters.get(PARAMETER_CLASSIFICATIONS);
    try {
        Map<Value, Value> lookupMap = new HashMap<Value, Value>();
        for (Value mapping : mappings) {
            String[] parts = mapping.as(String.class).split(" ");
            if (parts.length > 0) {
                Value target = Value.of(URLDecoder.decode(parts[0], "UTF-8"));
                for (int i = 1; i < parts.length; i++) {
                    lookupMap.put(Value.of(URLDecoder.decode(parts[i], "UTF-8")), target);
                }
            }
        }
        return new LookupTableImpl(lookupMap);
    } catch (UnsupportedEncodingException e) {
        throw new IllegalStateException("Failed to decode classification mapping.");
    }
}
Also used : HashMap(java.util.HashMap) LookupTableInfo(eu.esdihumboldt.hale.common.lookup.LookupTableInfo) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LookupService(eu.esdihumboldt.hale.common.lookup.LookupService) Value(eu.esdihumboldt.hale.common.core.io.Value) LookupTableImpl(eu.esdihumboldt.hale.common.lookup.impl.LookupTableImpl)

Example 9 with LookupTableImpl

use of eu.esdihumboldt.hale.common.lookup.impl.LookupTableImpl in project hale by halestudio.

the class LookupTableTypeTest method testComplexLookup.

/**
 * Test if a lookup table containing only complex values is the same when
 * converted to DOM and back again.
 */
@Test
public void testComplexLookup() {
    Map<Value, Value> values2 = createComplexLookup();
    LookupTable org2 = new LookupTableImpl(values2);
    // convert to DOM
    Element fragment = HaleIO.getComplexElement(org2);
    // convert back
    LookupTable conv = HaleIO.getComplexValue(fragment, LookupTable.class, null);
    checkTable(conv, values2);
}
Also used : Element(org.w3c.dom.Element) Value(eu.esdihumboldt.hale.common.core.io.Value) LookupTable(eu.esdihumboldt.hale.common.lookup.LookupTable) LookupTableImpl(eu.esdihumboldt.hale.common.lookup.impl.LookupTableImpl) Test(org.junit.Test)

Example 10 with LookupTableImpl

use of eu.esdihumboldt.hale.common.lookup.impl.LookupTableImpl in project hale by halestudio.

the class LookupTableTypeTest method testStringLookup.

/**
 * Test if a simple lookup table containing only string values is the same
 * when converted to DOM and back again.
 */
@Test
public void testStringLookup() {
    Map<Value, Value> values = createStringLookup();
    LookupTable org = new LookupTableImpl(values);
    // convert to DOM
    Element fragment = HaleIO.getComplexElement(org);
    // convert back
    LookupTable conv = HaleIO.getComplexValue(fragment, LookupTable.class, null);
    checkTable(conv, values);
}
Also used : Element(org.w3c.dom.Element) Value(eu.esdihumboldt.hale.common.core.io.Value) LookupTable(eu.esdihumboldt.hale.common.lookup.LookupTable) LookupTableImpl(eu.esdihumboldt.hale.common.lookup.impl.LookupTableImpl) Test(org.junit.Test)

Aggregations

LookupTableImpl (eu.esdihumboldt.hale.common.lookup.impl.LookupTableImpl)12 Value (eu.esdihumboldt.hale.common.core.io.Value)11 LookupTable (eu.esdihumboldt.hale.common.lookup.LookupTable)8 Test (org.junit.Test)5 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)3 LookupTableInfoImpl (eu.esdihumboldt.hale.common.lookup.impl.LookupTableInfoImpl)3 HashMap (java.util.HashMap)3 StringReader (java.io.StringReader)2 StringWriter (java.io.StringWriter)2 StyledString (org.eclipse.jface.viewers.StyledString)2 Element (org.w3c.dom.Element)2 Cell (eu.esdihumboldt.hale.common.align.model.Cell)1 DefaultCell (eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)1 ComplexValue (eu.esdihumboldt.hale.common.core.io.impl.ComplexValue)1 StringValue (eu.esdihumboldt.hale.common.core.io.impl.StringValue)1 LookupService (eu.esdihumboldt.hale.common.lookup.LookupService)1 LookupTableInfo (eu.esdihumboldt.hale.common.lookup.LookupTableInfo)1 LookupExportAdvisor (eu.esdihumboldt.hale.common.lookup.internal.LookupExportAdvisor)1 LookupLoadAdvisor (eu.esdihumboldt.hale.common.lookup.internal.LookupLoadAdvisor)1 Enumeration (eu.esdihumboldt.hale.common.schema.model.constraint.type.Enumeration)1