Search in sources :

Example 1 with ChainConfiguration

use of eu.esdihumboldt.hale.io.appschema.model.ChainConfiguration in project hale by halestudio.

the class AppSchemaMappingUtils method resolvePropertyTypes.

/**
 * Goes through all chain configurations in the provided feature chaining
 * configuration and attempts to resolve all unresolved property entity
 * definitions.
 *
 * <p>
 * More specifically, resolution for a particular chain configuration is
 * attempted if {@link ChainConfiguration#getJaxbNestedTypeTarget()} returns
 * a value, while {@link ChainConfiguration#getNestedTypeTarget()} returns
 * <code>null</code>.
 * </p>
 *
 * <p>
 * Upon successful resolution,
 * {@link ChainConfiguration#setJaxbNestedTypeTarget(PropertyType)} is
 * invoked with a <code>null</code> argument, to avoid further entity
 * resolution attempts.
 * </p>
 *
 * <p>
 * A {@link DefaultEntityResolver} instance is used to resolve entities.
 * </p>
 *
 * @param featureChaining the global feature chaining configuration
 * @param types the schema to use for entity lookup
 * @param ssid the schema space identifier
 */
public static void resolvePropertyTypes(FeatureChaining featureChaining, TypeIndex types, SchemaSpaceID ssid) {
    if (featureChaining != null) {
        EntityResolver resolver = new DefaultEntityResolver();
        for (String joinCellId : featureChaining.getJoins().keySet()) {
            List<ChainConfiguration> chains = featureChaining.getChains(joinCellId);
            for (ChainConfiguration chain : chains) {
                if (chain.getNestedTypeTarget() == null && chain.getJaxbNestedTypeTarget() != null) {
                    Property resolved = resolver.resolveProperty(chain.getJaxbNestedTypeTarget(), types, ssid);
                    if (resolved != null) {
                        chain.setNestedTypeTarget(resolved.getDefinition());
                        chain.setJaxbNestedTypeTarget(null);
                    }
                }
            }
        }
    }
}
Also used : DefaultEntityResolver(eu.esdihumboldt.hale.common.align.io.impl.DefaultEntityResolver) ChainConfiguration(eu.esdihumboldt.hale.io.appschema.model.ChainConfiguration) EntityResolver(eu.esdihumboldt.hale.common.align.io.EntityResolver) DefaultEntityResolver(eu.esdihumboldt.hale.common.align.io.impl.DefaultEntityResolver) Property(eu.esdihumboldt.hale.common.align.model.Property)

Example 2 with ChainConfiguration

use of eu.esdihumboldt.hale.io.appschema.model.ChainConfiguration in project hale by halestudio.

the class AbstractPropertyTransformationHandler method handlePropertyTransformation.

/**
 * @see eu.esdihumboldt.hale.io.appschema.writer.internal.PropertyTransformationHandler#handlePropertyTransformation(eu.esdihumboldt.hale.common.align.model.Cell,
 *      eu.esdihumboldt.hale.common.align.model.Cell,
 *      eu.esdihumboldt.hale.io.appschema.writer.internal.mapping.AppSchemaMappingContext)
 */
@Override
public AttributeMappingType handlePropertyTransformation(Cell typeCell, Cell propertyCell, AppSchemaMappingContext context) {
    this.mapping = context.getMappingWrapper();
    this.typeCell = typeCell;
    this.propertyCell = propertyCell;
    // TODO: does this hold for any transformation function?
    this.targetProperty = getTargetProperty(propertyCell);
    PropertyEntityDefinition targetPropertyEntityDef = targetProperty.getDefinition();
    PropertyDefinition targetPropertyDef = targetPropertyEntityDef.getDefinition();
    TypeDefinition featureType = null;
    String mappingName = null;
    if (AppSchemaMappingUtils.isJoin(typeCell)) {
        if (context.getFeatureChaining() != null) {
            ChainConfiguration chainConf = findChainConfiguration(context);
            if (chainConf != null) {
                featureType = chainConf.getNestedTypeTargetType();
                mappingName = chainConf.getMappingName();
            }
        } else {
            // this is just a best effort attempt to determine the target
            // feature type, may result in incorrect mappings
            featureType = findOwningType(targetPropertyEntityDef, context.getRelevantTargetTypes());
        }
    }
    if (featureType == null) {
        featureType = getTargetType(typeCell).getDefinition().getType();
    }
    // chaining configuration other than the current one
    if (context.getFeatureChaining() != null) {
        for (String joinId : context.getFeatureChaining().getJoins().keySet()) {
            List<ChainConfiguration> chains = context.getFeatureChaining().getChains(joinId);
            ChainConfiguration chainConf = findLongestNestedPath(targetPropertyEntityDef.getPropertyPath(), chains);
            if (chainConf != null && !chainConf.getNestedTypeTargetType().equals(featureType)) {
                // don't translate mapping, will do it (or have done it)
                // elsewhere!
                featureType = null;
                break;
            }
        }
    }
    if (featureType != null) {
        // fetch FeatureTypeMapping from mapping configuration
        this.featureTypeMapping = context.getOrCreateFeatureTypeMapping(featureType, mappingName);
        // fetch AttributeMappingType from mapping
        if (isXmlAttribute(targetPropertyDef)) {
            // gml:id attribute requires special handling, i.e. an
            // <idExpression> tag must be added to the attribute mapping for
            // target feature types and geometry types
            TypeDefinition parentType = targetPropertyDef.getParentType();
            if (isGmlId(targetPropertyDef)) {
                // TODO: handle gml:id for geometry types
                if (featureType.equals(parentType)) {
                    handleAsFeatureGmlId(featureType, mappingName, context);
                } else if (isGeometryType(parentType)) {
                    handleAsGeometryGmlId(featureType, mappingName, context);
                } else {
                    handleAsXmlAttribute(featureType, mappingName, context);
                }
            } else {
                handleAsXmlAttribute(featureType, mappingName, context);
            }
        } else {
            handleAsXmlElement(featureType, mappingName, context);
        }
    }
    return attributeMapping;
}
Also used : PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) ChainConfiguration(eu.esdihumboldt.hale.io.appschema.model.ChainConfiguration) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 3 with ChainConfiguration

use of eu.esdihumboldt.hale.io.appschema.model.ChainConfiguration in project hale by halestudio.

the class AbstractPropertyTransformationHandler method findLongestNestedPath.

private ChainConfiguration findLongestNestedPath(List<ChildContext> targetPropertyPath, List<ChainConfiguration> chains) {
    ChainConfiguration chainConf = null;
    if (chains != null && chains.size() > 0 && targetPropertyPath != null && targetPropertyPath.size() > 0) {
        int maxPathLength = 0;
        for (ChainConfiguration chain : chains) {
            List<ChildContext> nestedTargetPath = chain.getNestedTypeTarget().getPropertyPath();
            boolean isNested = isNested(nestedTargetPath, targetPropertyPath);
            if (isNested && maxPathLength < nestedTargetPath.size()) {
                maxPathLength = nestedTargetPath.size();
                chainConf = chain;
            }
        }
    }
    return chainConf;
}
Also used : ChainConfiguration(eu.esdihumboldt.hale.io.appschema.model.ChainConfiguration) ChildContext(eu.esdihumboldt.hale.common.align.model.ChildContext)

Example 4 with ChainConfiguration

use of eu.esdihumboldt.hale.io.appschema.model.ChainConfiguration in project hale by halestudio.

the class AbstractPropertyTransformationHandler method findChainConfiguration.

/**
 * @param context the mapping context
 * @return the chain configuration that applies to the current property
 *         mapping
 */
private ChainConfiguration findChainConfiguration(AppSchemaMappingContext context) {
    ChainConfiguration chainConf = null;
    PropertyEntityDefinition targetPropertyEntityDef = targetProperty.getDefinition();
    FeatureChaining featureChaining = context.getFeatureChaining();
    if (featureChaining != null) {
        List<ChildContext> targetPropertyPath = targetPropertyEntityDef.getPropertyPath();
        List<ChainConfiguration> chains = featureChaining.getChains(typeCell.getId());
        chainConf = findLongestNestedPath(targetPropertyPath, chains);
    }
    return chainConf;
}
Also used : FeatureChaining(eu.esdihumboldt.hale.io.appschema.model.FeatureChaining) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) ChainConfiguration(eu.esdihumboldt.hale.io.appschema.model.ChainConfiguration) ChildContext(eu.esdihumboldt.hale.common.align.model.ChildContext)

Example 5 with ChainConfiguration

use of eu.esdihumboldt.hale.io.appschema.model.ChainConfiguration 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;
}
Also used : FeatureChaining(eu.esdihumboldt.hale.io.appschema.model.FeatureChaining) ChainConfiguration(eu.esdihumboldt.hale.io.appschema.model.ChainConfiguration) JoinParameter(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter) FeatureTypeMapping(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.TypeMappingsPropertyType.FeatureTypeMapping) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) JoinCondition(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) Alignment(eu.esdihumboldt.hale.common.align.model.Alignment) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) AttributeMappingType(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType) ChildContext(eu.esdihumboldt.hale.common.align.model.ChildContext) List(java.util.List) AppSchemaMappingWrapper(eu.esdihumboldt.hale.io.appschema.writer.internal.mapping.AppSchemaMappingWrapper) AttributeExpressionMappingType(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeExpressionMappingType) Cell(eu.esdihumboldt.hale.common.align.model.Cell) AppSchemaMappingUtils.getTargetProperty(eu.esdihumboldt.hale.io.appschema.writer.AppSchemaMappingUtils.getTargetProperty) Property(eu.esdihumboldt.hale.common.align.model.Property)

Aggregations

ChainConfiguration (eu.esdihumboldt.hale.io.appschema.model.ChainConfiguration)5 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)3 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)3 Property (eu.esdihumboldt.hale.common.align.model.Property)2 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)2 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)2 FeatureChaining (eu.esdihumboldt.hale.io.appschema.model.FeatureChaining)2 EntityResolver (eu.esdihumboldt.hale.common.align.io.EntityResolver)1 DefaultEntityResolver (eu.esdihumboldt.hale.common.align.io.impl.DefaultEntityResolver)1 Alignment (eu.esdihumboldt.hale.common.align.model.Alignment)1 Cell (eu.esdihumboldt.hale.common.align.model.Cell)1 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)1 JoinParameter (eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter)1 JoinCondition (eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition)1 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)1 AttributeExpressionMappingType (eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeExpressionMappingType)1 AttributeMappingType (eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType)1 FeatureTypeMapping (eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.TypeMappingsPropertyType.FeatureTypeMapping)1 AppSchemaMappingUtils.getTargetProperty (eu.esdihumboldt.hale.io.appschema.writer.AppSchemaMappingUtils.getTargetProperty)1 AppSchemaMappingWrapper (eu.esdihumboldt.hale.io.appschema.writer.internal.mapping.AppSchemaMappingWrapper)1