use of eu.esdihumboldt.hale.common.align.model.ChildContext in project hale by halestudio.
the class AbstractPropertyTransformationHandler method getConditionalExpression.
/**
* Wraps the provided CQL expression in a conditional expression, based on
* the filter defined on the property.
*
* <p>
* TODO: current implementation is broken, don't use it (first argument of
* if_then_else must be an expression, cannot be a filter (i.e. cannot
* contain '=' sign))!
* </p>
*
* @param propertyEntityDef the property definition defining the condition
* @param cql the CQL expression to wrap
* @return a conditional expression wrapping the provided CQL expression
*/
protected static String getConditionalExpression(PropertyEntityDefinition propertyEntityDef, String cql) {
if (propertyEntityDef != null) {
String propertyName = propertyEntityDef.getDefinition().getName().getLocalPart();
List<ChildContext> propertyPath = propertyEntityDef.getPropertyPath();
// properties
if (propertyPath.size() == 1) {
Condition condition = propertyPath.get(0).getCondition();
if (condition != null) {
String fitlerText = AlignmentUtil.getFilterText(condition.getFilter());
// remove "parent" references
fitlerText = fitlerText.replace("parent.", "");
// replace "value" references with the local name of the
// property itself
fitlerText = fitlerText.replace("value", propertyName);
return String.format("if_then_else(%s, %s, Expression.NIL)", fitlerText, cql);
}
}
}
return cql;
}
use of eu.esdihumboldt.hale.common.align.model.ChildContext in project hale by halestudio.
the class AbstractPropertyTransformationHandler method handleAsFeatureGmlId.
/**
* This method is invoked when the target type is the feature type owning
* this attribute mapping, and the target property is <code>gml:id</code>,
* which needs special handling.
*
* <p>
* In practice, this means that <code><idExpression></code> is used in
* place of:
*
* <pre>
* <ClientProperty>
* <name>...</name>
* <value>...</value>
* </ClientProperty>
* </pre>
*
* and that the target attribute is set to the mapped feature type name.
*
* </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 handleAsFeatureGmlId(TypeDefinition featureType, String mappingName, AppSchemaMappingContext context) {
PropertyEntityDefinition targetPropertyEntityDef = targetProperty.getDefinition();
List<ChildContext> gmlIdPath = targetPropertyEntityDef.getPropertyPath();
attributeMapping = context.getOrCreateAttributeMapping(featureType, mappingName, gmlIdPath);
// set targetAttribute to feature type qualified name
attributeMapping.setTargetAttribute(featureTypeMapping.getTargetElement());
// set id expression
AttributeExpressionMappingType idExpression = new AttributeExpressionMappingType();
idExpression.setOCQL(getSourceExpressionAsCQL());
// TODO: not sure whether any CQL expression can be used here
attributeMapping.setIdExpression(idExpression);
}
use of eu.esdihumboldt.hale.common.align.model.ChildContext in project hale by halestudio.
the class JoinHandler method handleTypeTransformation.
/**
* @see eu.esdihumboldt.hale.io.appschema.writer.internal.TypeTransformationHandler#handleTypeTransformation(eu.esdihumboldt.hale.common.align.model.Cell,
* eu.esdihumboldt.hale.io.appschema.writer.internal.mapping.AppSchemaMappingContext)
*/
@Override
public FeatureTypeMapping handleTypeTransformation(Cell typeCell, AppSchemaMappingContext context) {
AppSchemaMappingWrapper mapping = context.getMappingWrapper();
Alignment alignment = context.getAlignment();
FeatureChaining featureChaining = context.getFeatureChaining();
final JoinParameter joinParameter = typeCell.getTransformationParameters().get(PARAMETER_JOIN).get(0).as(JoinParameter.class);
String validation = joinParameter.validate();
if (validation != null)
throw new IllegalArgumentException("Join parameter invalid: " + validation);
// check only single predicate conditions have been used
int[] conditionCount = new int[joinParameter.getTypes().size()];
List<JoinCondition> joinConditions = getSortedJoinConditions(joinParameter);
for (JoinCondition joinCondition : joinConditions) {
TypeEntityDefinition joinType = AlignmentUtil.getTypeEntity(joinCondition.joinProperty);
int typeIdx = joinParameter.getTypes().indexOf(joinType);
conditionCount[typeIdx]++;
if (conditionCount[typeIdx] > 1) {
throw new IllegalArgumentException("Only single condition joins are supported so far");
}
}
FeatureTypeMapping topMostMapping = null;
for (int chainIdx = 0; chainIdx < joinParameter.getTypes().size() - 1; chainIdx++) {
ChainConfiguration previousChainConf = null;
ChainConfiguration chainConf = null;
if (featureChaining != null) {
chainConf = featureChaining.getChain(typeCell.getId(), chainIdx);
if (chainConf != null && chainConf.getPrevChainIndex() >= 0) {
previousChainConf = featureChaining.getChain(typeCell.getId(), chainConf.getPrevChainIndex());
}
}
// join is done pair-wise: I assume the first type is the container
// type, whereas the second type is nested in the first
JoinCondition joinCondition = joinConditions.get(chainIdx);
baseProperty = joinCondition.baseProperty;
joinProperty = joinCondition.joinProperty;
TypeEntityDefinition containerType = AlignmentUtil.getTypeEntity(baseProperty);
TypeEntityDefinition nestedType = AlignmentUtil.getTypeEntity(joinProperty);
// build FeatureTypeMapping for container type
// Entity containerTypeTarget = typeCell.getTarget().values().iterator().next();
// TypeDefinition containerTypeTargetType = containerTypeTarget.getDefinition().getType();
EntityDefinition containerTypeTarget = null;
TypeDefinition containerTypeTargetType = null;
String containerTypeTargetMappingName = null;
if (previousChainConf == null) {
containerTypeTarget = getTargetType(typeCell).getDefinition();
containerTypeTargetType = containerTypeTarget.getType();
} else {
containerTypeTarget = previousChainConf.getNestedTypeTarget();
containerTypeTargetType = previousChainConf.getNestedTypeTarget().getDefinition().getPropertyType();
containerTypeTargetMappingName = previousChainConf.getMappingName();
}
String containerMappingName = null;
if (previousChainConf != null) {
containerMappingName = previousChainConf.getMappingName();
}
FeatureTypeMapping containerFTMapping = context.getOrCreateFeatureTypeMapping(containerTypeTargetType, containerMappingName);
containerFTMapping.setSourceType(containerType.getDefinition().getName().getLocalPart());
// build FeatureTypeMapping for nested type
TypeDefinition nestedFT = null;
List<ChildContext> nestedFTPath = null;
FeatureTypeMapping nestedFTMapping = null;
if (chainConf != null) {
nestedFT = chainConf.getNestedTypeTarget().getDefinition().getPropertyType();
nestedFTPath = chainConf.getNestedTypeTarget().getPropertyPath();
// remove last element
nestedFTPath = nestedFTPath.subList(0, nestedFTPath.size() - 1);
nestedFTMapping = context.getOrCreateFeatureTypeMapping(nestedFT, chainConf.getMappingName());
nestedFTMapping.setSourceType(nestedType.getDefinition().getName().getLocalPart());
} else {
if (joinParameter.getTypes().size() > 2) {
throw new IllegalArgumentException("If no feature chaining configuration is provided, only join between 2 types is supported");
}
// do your best to figure it out on your own... good luck!
Collection<? extends Cell> propertyCells = alignment.getPropertyCells(typeCell);
for (Cell propertyCell : propertyCells) {
Property sourceProperty = AppSchemaMappingUtils.getSourceProperty(propertyCell);
if (sourceProperty != null) {
TypeDefinition sourceType = sourceProperty.getDefinition().getDefinition().getParentType();
if (sourceType.getName().equals(nestedType.getDefinition().getName())) {
// source property belongs to nested type: determine
// target type
Property targetProperty = getTargetProperty(propertyCell);
// nestedFT =
// findOwningFeatureType(targetProperty.getDefinition());
nestedFT = findOwningType(targetProperty.getDefinition(), context.getRelevantTargetTypes());
if (nestedFT != null && !nestedFT.getName().equals(containerTypeTargetType.getName())) {
// target property belongs to a feature type
// different from the already mapped one: build
// a new mapping
nestedFTPath = findOwningTypePath(targetProperty.getDefinition(), context.getRelevantTargetTypes());
nestedFTMapping = context.getOrCreateFeatureTypeMapping(nestedFT);
nestedFTMapping.setSourceType(nestedType.getDefinition().getName().getLocalPart());
// in the join
break;
} else if (isHRefAttribute(targetProperty.getDefinition().getDefinition())) {
// check if target property is a href attribute
Property hrefProperty = targetProperty;
List<ChildContext> hrefPropertyPath = hrefProperty.getDefinition().getPropertyPath();
List<ChildContext> hrefContainerPath = hrefPropertyPath.subList(0, hrefPropertyPath.size() - 1);
TypeDefinition hrefParentType = hrefProperty.getDefinition().getDefinition().getParentType();
// TypeDefinition childFT =
// findChildFeatureType(hrefParentType);
TypeDefinition childFT = AppSchemaMappingUtils.findChildType(hrefParentType, context.getRelevantTargetTypes());
if (childFT != null) {
nestedFTPath = hrefContainerPath;
nestedFTMapping = context.getOrCreateFeatureTypeMapping(childFT);
nestedFTMapping.setSourceType(nestedType.getDefinition().getName().getLocalPart());
// involved in the join
break;
}
}
}
}
}
}
// build join mapping
if (nestedFTMapping != null && nestedFTPath != null) {
AttributeMappingType containerJoinMapping = context.getOrCreateAttributeMapping(containerTypeTargetType, containerTypeTargetMappingName, nestedFTPath);
containerJoinMapping.setTargetAttribute(mapping.buildAttributeXPath(containerTypeTargetType, nestedFTPath));
// set isMultiple attribute
PropertyDefinition targetPropertyDef = nestedFTPath.get(nestedFTPath.size() - 1).getChild().asProperty();
if (AppSchemaMappingUtils.isMultiple(targetPropertyDef)) {
containerJoinMapping.setIsMultiple(true);
}
AttributeExpressionMappingType containerSourceExpr = new AttributeExpressionMappingType();
// join column extracted from join condition
containerSourceExpr.setOCQL(baseProperty.getDefinition().getName().getLocalPart());
containerSourceExpr.setLinkElement(getLinkElementValue(nestedFTMapping));
String linkField = context.getUniqueFeatureLinkAttribute(nestedFT, nestedFTMapping.getMappingName());
containerSourceExpr.setLinkField(linkField);
containerJoinMapping.setSourceExpression(containerSourceExpr);
AttributeMappingType nestedJoinMapping = new AttributeMappingType();
AttributeExpressionMappingType nestedSourceExpr = new AttributeExpressionMappingType();
// join column extracted from join condition
nestedSourceExpr.setOCQL(joinProperty.getDefinition().getName().getLocalPart());
nestedJoinMapping.setSourceExpression(nestedSourceExpr);
nestedJoinMapping.setTargetAttribute(linkField);
nestedFTMapping.getAttributeMappings().getAttributeMapping().add(nestedJoinMapping);
}
if (chainIdx == 0) {
topMostMapping = containerFTMapping;
}
}
return topMostMapping;
}
use of eu.esdihumboldt.hale.common.align.model.ChildContext in project hale by halestudio.
the class CityGMLPropagateVisitor method findSourceTypes.
/**
* Find source types to use to propagate the given example cell. If
* possible, common super types will be returned.
*
* @param exampleCell the example cell
* @param targetType the target type
* @param index the index to store the replacement property paths in
* @return the source types to propagate the cell to
*/
private Collection<TypeDefinition> findSourceTypes(Cell exampleCell, TypeDefinition targetType, TypeEntityIndex<List<ChildContext>> index) {
Set<TypeDefinition> possibleSources = findAllPossibleSources(targetType);
/*
* Add all super types, because if possible, we want to do the mapping
* on super types.
*/
Set<TypeDefinition> superTypes = new HashSet<TypeDefinition>();
for (TypeDefinition type : possibleSources) {
TypeDefinition superType = type.getSuperType();
while (superType != null) {
if (superTypes.add(superType) || !possibleSources.contains(superType)) {
superType = superType.getSuperType();
} else {
superType = null;
}
}
}
possibleSources.addAll(superTypes);
/*
* Check source entities and filter all source types that don't match
* the entity.
*/
TypeDefinition originalSource = null;
for (Entity source : exampleCell.getSource().values()) {
EntityDefinition ed = source.getDefinition();
// check source type
if (originalSource == null) {
originalSource = ed.getType();
} else {
if (!originalSource.equals(ed.getType())) {
System.err.println("WARNING: ignoring cell with sources in different types");
return null;
}
}
if (ed.getPropertyPath().isEmpty()) {
// don't handle type cells
return null;
}
// remove all types w/o compatible property
Iterator<TypeDefinition> it = possibleSources.iterator();
while (it.hasNext()) {
TypeDefinition type = it.next();
List<ChildContext> newPath = hasCompatibleProperty(type, ed.getPropertyPath());
if (newPath == null) {
it.remove();
} else {
// remember child path per root type and entity
index.put(type, source, newPath);
}
}
}
/*
* Remove all types that have super types contained in the set.
*/
Set<TypeDefinition> toTest = new HashSet<TypeDefinition>(possibleSources);
for (TypeDefinition type : toTest) {
TypeDefinition superType = type.getSuperType();
while (superType != null) {
if (possibleSources.contains(superType)) {
possibleSources.remove(type);
// other super types are tested on their own
break;
}
superType = superType.getSuperType();
}
}
return possibleSources;
}
use of eu.esdihumboldt.hale.common.align.model.ChildContext in project hale by halestudio.
the class AppSchemaMappingWrapper method buildAttributeXPath.
/**
* Build an XPath expression to be used as <targetAttribute> for the
* provided target property definition.
*
* <p>
* The algorithm to build the path is as follows:
* <ol>
* <li>the property path is traversed backwards, from end to beginning</li>
* <li>on each step, a new path segment is added at the top of the list, but
* only if the child definition describes a property and not a group</li>
* <li>on each step, if a non-null context name is defined on the child
* context, <code>[<context name>]</code> string is appended to the
* path segment</li>
* <li>the traversal stops when the parent type of the last visited property
* equals to the provided owning type</li>
* </ol>
*
* @param owningType the type owning the target property
* @param propertyPath the target property path
* @return the XPath expression pointing to the target property
*/
public String buildAttributeXPath(TypeDefinition owningType, List<ChildContext> propertyPath) {
List<String> pathSegments = new ArrayList<String>();
for (int i = propertyPath.size() - 1; i >= 0; i--) {
ChildContext childContext = propertyPath.get(i);
// TODO: how to handle conditions?
Integer contextId = childContext.getContextName();
ChildDefinition<?> child = childContext.getChild();
// the xpath expression
if (child.asProperty() != null) {
String namespaceURI = child.getName().getNamespaceURI();
String prefix = child.getName().getPrefix();
String name = child.getName().getLocalPart();
Namespace ns = getOrCreateNamespace(namespaceURI, prefix);
String path = ns.getPrefix() + ":" + name;
if (contextId != null) {
// XPath indices start from 1, whereas contextId starts from
// 0 --> add 1
path = String.format("%s[%d]", path, contextId + 1);
}
// insert path segment at the first position
pathSegments.add(0, path);
}
if (child.getParentType() != null && child.getParentType().getName().equals(owningType.getName())) {
// I reached the owning type: stop walking the path
break;
}
}
String xPath = Joiner.on("/").join(pathSegments);
return xPath;
}
Aggregations