Search in sources :

Example 1 with ClientProperty

use of eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType.ClientProperty in project hale by halestudio.

the class AppSchemaMappingTest method testXmlAttributeEncodingUnqualified.

@Test
public void testXmlAttributeEncodingUnqualified() {
    final String CODE_SPACE = "http://www.example.com/codespace";
    final String OCQL = "'" + CODE_SPACE + "'";
    DefaultCell cell = new DefaultCell();
    cell.setTransformationIdentifier(AssignFunction.ID);
    ListMultimap<String, ParameterValue> parameters = ArrayListMultimap.create();
    parameters.put(AssignFunction.PARAMETER_VALUE, new ParameterValue(CODE_SPACE));
    cell.setTransformationParameters(parameters);
    cell.setTarget(getCodeSpaceTargetProperty());
    AssignHandler handler = new AssignHandler();
    AttributeMappingType attrMapping = handler.handlePropertyTransformation(getDefaultTypeCell(unitDenormType, landCoverUnitType), cell, new AppSchemaMappingContext(mappingWrapper));
    assertNull(attrMapping.getSourceExpression());
    assertEquals("gml:name", attrMapping.getTargetAttribute());
    assertNotNull(attrMapping.getClientProperty());
    assertEquals(1, attrMapping.getClientProperty().size());
    ClientProperty attr = attrMapping.getClientProperty().get(0);
    assertEquals("codeSpace", attr.getName());
    assertEquals(OCQL, attr.getValue());
}
Also used : AssignHandler(eu.esdihumboldt.hale.io.appschema.writer.internal.AssignHandler) ClientProperty(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType.ClientProperty) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) AttributeMappingType(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) AppSchemaMappingContext(eu.esdihumboldt.hale.io.appschema.writer.internal.mapping.AppSchemaMappingContext) Test(org.junit.Test)

Example 2 with ClientProperty

use of eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType.ClientProperty in project hale by halestudio.

the class AbstractPropertyTransformationHandler method handleAsXmlAttribute.

/**
 * This method is invoked when the target property is an XML attribute (
 * {@link XmlAttributeFlag} constraint is set).
 *
 * <p>
 * The property transformation is translated to:
 *
 * <pre>
 *   <code>&lt;ClientProperty&gt;
 *     &lt;name&gt;[target property name]&lt;/name&gt;
 *     &lt;value&gt;[CQL expression]&lt;/value&gt;
 *   &lt;/ClientProperty&gt;</code>
 * </pre>
 *
 * and added to the attribute mapping generated for the XML element owning
 * the attribute.
 * </p>
 *
 * @param featureType the target feature type
 * @param mappingName the target feature type's mapping name (may be
 *            <code>null</code>)
 * @param context the app-schema mapping context
 */
protected void handleAsXmlAttribute(TypeDefinition featureType, String mappingName, AppSchemaMappingContext context) {
    PropertyEntityDefinition targetPropertyEntityDef = targetProperty.getDefinition();
    PropertyDefinition targetPropertyDef = targetPropertyEntityDef.getDefinition();
    // fetch attribute mapping for parent property
    EntityDefinition parentDef = AlignmentUtil.getParent(targetPropertyEntityDef);
    if (parentDef != null) {
        List<ChildContext> parentPropertyPath = parentDef.getPropertyPath();
        PropertyDefinition parentPropertyDef = parentPropertyPath.get(parentPropertyPath.size() - 1).getChild().asProperty();
        if (parentPropertyDef != null) {
            attributeMapping = context.getOrCreateAttributeMapping(featureType, mappingName, parentPropertyPath);
            // set targetAttribute if empty
            if (attributeMapping.getTargetAttribute() == null || attributeMapping.getTargetAttribute().isEmpty()) {
                attributeMapping.setTargetAttribute(mapping.buildAttributeXPath(featureType, parentPropertyPath));
            }
            Namespace targetPropNS = context.getOrCreateNamespace(targetPropertyDef.getName().getNamespaceURI(), targetPropertyDef.getName().getPrefix());
            String unqualifiedName = targetPropertyDef.getName().getLocalPart();
            boolean isQualified = targetPropNS != null && !Strings.isNullOrEmpty(targetPropNS.getPrefix());
            // encode attribute as <ClientProperty>
            ClientProperty clientProperty = new ClientProperty();
            @SuppressWarnings("null") String clientPropName = (isQualified) ? targetPropNS.getPrefix() + ":" + unqualifiedName : unqualifiedName;
            clientProperty.setName(clientPropName);
            clientProperty.setValue(getSourceExpressionAsCQL());
            setEncodeIfEmpty(clientProperty);
            // don't add client property if it already exists
            if (!hasClientProperty(clientProperty.getName())) {
                attributeMapping.getClientProperty().add(clientProperty);
                // when nilReason is null and viceversa)
                if (isNilReason(targetPropertyDef) && isNillable(parentPropertyDef) && attributeMapping.getSourceExpression() == null) {
                    addOrReplaceXsiNilAttribute(clientProperty.getValue(), true, context);
                }
            }
        }
    }
}
Also used : PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) ClientProperty(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType.ClientProperty) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) ChildContext(eu.esdihumboldt.hale.common.align.model.ChildContext) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) Namespace(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.NamespacesPropertyType.Namespace)

Example 3 with ClientProperty

use of eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType.ClientProperty in project hale by halestudio.

the class AppSchemaMappingTest method testXmlAttributeEncodingQualified.

@Test
public void testXmlAttributeEncodingQualified() {
    final String GML_ID = "ti.ds.landcover";
    final String OCQL = "'" + GML_ID + "'";
    DefaultCell cell = new DefaultCell();
    cell.setTransformationIdentifier(AssignFunction.ID);
    ListMultimap<String, ParameterValue> parameters = ArrayListMultimap.create();
    parameters.put(AssignFunction.PARAMETER_VALUE, new ParameterValue(GML_ID));
    cell.setTransformationParameters(parameters);
    cell.setTarget(getTimeInstantGmlIdTargetProperty());
    AssignHandler handler = new AssignHandler();
    AttributeMappingType attrMapping = handler.handlePropertyTransformation(getDefaultTypeCell(datasetType, landCoverDatasetType), cell, new AppSchemaMappingContext(mappingWrapper));
    assertNull(attrMapping.getSourceExpression());
    assertEquals("lcv:extent/gmd:EX_Extent/gmd:temporalElement/gmd:EX_TemporalExtent/gmd:extent/gml:TimeInstant", attrMapping.getTargetAttribute());
    assertNotNull(attrMapping.getClientProperty());
    assertEquals(1, attrMapping.getClientProperty().size());
    ClientProperty attr = attrMapping.getClientProperty().get(0);
    assertEquals("gml:id", attr.getName());
    assertEquals(OCQL, attr.getValue());
}
Also used : AssignHandler(eu.esdihumboldt.hale.io.appschema.writer.internal.AssignHandler) ClientProperty(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType.ClientProperty) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) AttributeMappingType(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) AppSchemaMappingContext(eu.esdihumboldt.hale.io.appschema.writer.internal.mapping.AppSchemaMappingContext) Test(org.junit.Test)

Example 4 with ClientProperty

use of eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType.ClientProperty in project hale by halestudio.

the class AppSchemaMappingTest method testNillable.

@Test
public void testNillable() {
    final String nilReason = "missing";
    final String nilReasonCQL = "'" + nilReason + "'";
    final String expectedXsiNilWithSource = "if_then_else(isNull(" + SOURCE_UNIT_ID + "), 'true', Expression.NIL)";
    DefaultCell renameBeginLifespan = new DefaultCell();
    renameBeginLifespan.setTransformationIdentifier(RenameFunction.ID);
    renameBeginLifespan.setSource(getUnitIdSourceProperty(unitType));
    renameBeginLifespan.setTarget(getBeginLifespanTargetProperty());
    DefaultCell assignNilReason = new DefaultCell();
    assignNilReason.setTransformationIdentifier(AssignFunction.ID);
    ListMultimap<String, ParameterValue> parameters = ArrayListMultimap.create();
    parameters.put(AssignFunction.PARAMETER_VALUE, new ParameterValue(nilReason));
    assignNilReason.setTarget(getBeginLifespanNilReasonTargetProperty());
    assignNilReason.setTransformationParameters(parameters);
    Cell typeCell = getDefaultTypeCell(unitType, landCoverUnitType);
    AttributeMappingType attrMapping = null;
    AppSchemaMappingContext context = new AppSchemaMappingContext(mappingWrapper);
    AssignHandler assignHandler = new AssignHandler();
    attrMapping = assignHandler.handlePropertyTransformation(typeCell, assignNilReason, context);
    assertNotNull(attrMapping);
    assertEquals("lcv:beginLifespanVersion", attrMapping.getTargetAttribute());
    assertNull(attrMapping.getSourceExpression());
    assertEquals(2, attrMapping.getClientProperty().size());
    assertEquals(GML_NIL_REASON, attrMapping.getClientProperty().get(0).getName());
    assertEquals(nilReasonCQL, attrMapping.getClientProperty().get(0).getValue());
    assertEquals("xsi:nil", attrMapping.getClientProperty().get(1).getName());
    assertEquals("if_then_else(isNull(" + nilReasonCQL + "), Expression.NIL, 'true')", attrMapping.getClientProperty().get(1).getValue());
    assertTrue(attrMapping.isEncodeIfEmpty());
    // element mapping should cause xsi:nil expression being overridden
    RenameHandler renameHandler = new RenameHandler();
    attrMapping = renameHandler.handlePropertyTransformation(typeCell, renameBeginLifespan, context);
    assertNotNull(attrMapping);
    assertNotNull(attrMapping.getSourceExpression());
    assertEquals(SOURCE_UNIT_ID, attrMapping.getSourceExpression().getOCQL());
    assertEquals("lcv:beginLifespanVersion", attrMapping.getTargetAttribute());
    assertEquals(2, attrMapping.getClientProperty().size());
    boolean xsiNilFound = false;
    for (ClientProperty property : attrMapping.getClientProperty()) {
        if ("xsi:nil".equals(property.getName())) {
            assertEquals(expectedXsiNilWithSource, property.getValue());
            xsiNilFound = true;
        }
    }
    assertTrue(xsiNilFound);
    assertTrue(attrMapping.isEncodeIfEmpty());
    // remove nilReason client property
    attrMapping.getClientProperty().remove(0);
    // run again assing handler, nothing should change
    attrMapping = assignHandler.handlePropertyTransformation(typeCell, assignNilReason, context);
    assertEquals(2, attrMapping.getClientProperty().size());
    assertEquals("xsi:nil", attrMapping.getClientProperty().get(0).getName());
    assertEquals(expectedXsiNilWithSource, attrMapping.getClientProperty().get(0).getValue());
}
Also used : AssignHandler(eu.esdihumboldt.hale.io.appschema.writer.internal.AssignHandler) ClientProperty(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType.ClientProperty) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) AttributeMappingType(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) RenameHandler(eu.esdihumboldt.hale.io.appschema.writer.internal.RenameHandler) AppSchemaMappingContext(eu.esdihumboldt.hale.io.appschema.writer.internal.mapping.AppSchemaMappingContext) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) Cell(eu.esdihumboldt.hale.common.align.model.Cell) Test(org.junit.Test)

Example 5 with ClientProperty

use of eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType.ClientProperty in project hale by halestudio.

the class AbstractPropertyTransformationHandler method addOrReplaceXsiNilAttribute.

/**
 * Adds an {@code xsi:nil} client property to the attribute mapping, or
 * updates the existing one.
 *
 * <p>
 * The CQL expression for the property's value is derived from the provided
 * source expression in this way:
 *
 * <ul>
 * <li>{@code sameLogic=true} -->
 * {@code if_then_else(isNull([sourceExpression]), Expression.NIL, 'true')}</li>
 * <li>{@code sameLogic=false} -->
 * {@code if_then_else(isNull([sourceExpression]), 'true', Expression.NIL)}</li>
 * </ul>
 * </p>
 *
 * @param sourceExpression the source expression
 * @param sameLogic if {@code true}, {@code xsi:nil} will be {@code null}
 *            when {@code sourceExpression} is and {@code 'true'} when it
 *            isn't, if {@code false} the opposite applies
 * @param context the app-schema mapping context
 */
private void addOrReplaceXsiNilAttribute(String sourceExpression, boolean sameLogic, AppSchemaMappingContext context) {
    final String sameLogicPattern = "if_then_else(isNull(%s), Expression.NIL, 'true')";
    final String invertedLogicPattern = "if_then_else(isNull(%s), 'true', Expression.NIL)";
    final String pattern = sameLogic ? sameLogicPattern : invertedLogicPattern;
    // make sure xsi namespace is included in the mapping
    context.getOrCreateNamespace(XSI_URI, XSI_PREFIX);
    String xsiNilQName = QNAME_XSI_NIL.getPrefix() + ":" + QNAME_XSI_NIL.getLocalPart();
    ClientProperty xsiNil = getClientProperty(xsiNilQName);
    if (xsiNil == null) {
        xsiNil = new ClientProperty();
        // add xsi:nil attribute
        attributeMapping.getClientProperty().add(xsiNil);
    }
    xsiNil.setName(xsiNilQName);
    xsiNil.setValue(String.format(pattern, sourceExpression));
}
Also used : ClientProperty(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType.ClientProperty)

Aggregations

ClientProperty (eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType.ClientProperty)6 AttributeMappingType (eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType)4 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)3 DefaultCell (eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)3 AssignHandler (eu.esdihumboldt.hale.io.appschema.writer.internal.AssignHandler)3 AppSchemaMappingContext (eu.esdihumboldt.hale.io.appschema.writer.internal.mapping.AppSchemaMappingContext)3 Test (org.junit.Test)3 Cell (eu.esdihumboldt.hale.common.align.model.Cell)1 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)1 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)1 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)1 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)1 Namespace (eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.NamespacesPropertyType.Namespace)1 RenameHandler (eu.esdihumboldt.hale.io.appschema.writer.internal.RenameHandler)1