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());
}
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><ClientProperty>
* <name>[target property name]</name>
* <value>[CQL expression]</value>
* </ClientProperty></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);
}
}
}
}
}
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());
}
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());
}
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));
}
Aggregations