Search in sources :

Example 6 with Namespace

use of eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.NamespacesPropertyType.Namespace 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 7 with Namespace

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

the class AppSchemaMappingWrapper method getOrCreateNamespace.

/**
 * Return a namespace object with the provided URI and prefix.
 *
 * <p>
 * If a namespace object for the same URI already exists, it is returned.
 * Otherwise, a new one is created.
 * </p>
 * <p>
 * If the prefix is empty, a non-empty prefix is automatically generated.
 * If, in a subsequent call to this method, a non-empty prefix is provided,
 * the user-provided prefix will replace the generated one.
 * </p>
 *
 * @param namespaceURI the namespace URI
 * @param prefix the namespace prefix
 * @return the created namespace object
 */
Namespace getOrCreateNamespace(String namespaceURI, String prefix) {
    if (namespaceURI != null && !namespaceURI.isEmpty()) {
        if (!namespaceUriMap.containsKey(namespaceURI)) {
            String basePrefix, uniquePrefix;
            if (prefix == null || prefix.trim().isEmpty()) {
                basePrefix = defaultPrefix;
                uniquePrefix = basePrefix + prefixCounter;
                prefixCounter++;
            } else {
                basePrefix = prefix;
                uniquePrefix = basePrefix;
            }
            // make sure prefix is unique
            while (namespacePrefixMap.containsKey(uniquePrefix)) {
                uniquePrefix = basePrefix + prefixCounter;
                prefixCounter++;
            }
            Namespace ns = new Namespace();
            ns.setPrefix(uniquePrefix);
            ns.setUri(namespaceURI);
            namespaceUriMap.put(namespaceURI, ns);
            namespacePrefixMap.put(uniquePrefix, ns);
            appSchemaMapping.getNamespaces().getNamespace().add(ns);
            return ns;
        } else {
            // update prefix if provided prefix is not empty and currently
            // assigned prefix was made up
            Namespace ns = namespaceUriMap.get(namespaceURI);
            if (prefix != null && !prefix.isEmpty() && ns.getPrefix().startsWith(defaultPrefix)) {
                // // check prefix is unique
                // if (!namespacePrefixMap.containsKey(prefix)) {
                // remove old prefix-NS mapping from namespacePrefixMap
                namespacePrefixMap.remove(ns.getPrefix());
                // add new prefix-NS mapping to namespacePrefixMap
                ns.setPrefix(prefix);
                namespacePrefixMap.put(prefix, ns);
            // }
            }
            return ns;
        }
    } else {
        return null;
    }
}
Also used : Namespace(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.NamespacesPropertyType.Namespace)

Example 8 with Namespace

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

the class AppSchemaMappingWrapper method cloneNamespace.

static Namespace cloneNamespace(Namespace ns) {
    if (ns == null) {
        return null;
    }
    Namespace clone = new Namespace();
    clone.setPrefix(ns.getPrefix());
    clone.setUri(ns.getUri());
    return clone;
}
Also used : Namespace(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.NamespacesPropertyType.Namespace)

Example 9 with Namespace

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

the class AppSchemaMappingWrapper method cloneMapping.

static AppSchemaDataAccessType cloneMapping(AppSchemaDataAccessType mapping) {
    AppSchemaDataAccessType clone = new AppSchemaDataAccessType();
    initMapping(clone);
    clone.setCatalog(mapping.getCatalog());
    clone.getIncludedTypes().getInclude().addAll(mapping.getIncludedTypes().getInclude());
    for (Namespace ns : mapping.getNamespaces().getNamespace()) {
        clone.getNamespaces().getNamespace().add(cloneNamespace(ns));
    }
    for (DataStore ds : mapping.getSourceDataStores().getDataStore()) {
        clone.getSourceDataStores().getDataStore().add(cloneDataStore(ds));
    }
    clone.getTargetTypes().getFeatureType().getSchemaUri().addAll(mapping.getTargetTypes().getFeatureType().getSchemaUri());
    for (FeatureTypeMapping ftMapping : mapping.getTypeMappings().getFeatureTypeMapping()) {
        clone.getTypeMappings().getFeatureTypeMapping().add(cloneFeatureTypeMapping(ftMapping));
    }
    return clone;
}
Also used : AppSchemaDataAccessType(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AppSchemaDataAccessType) DataStore(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.SourceDataStoresPropertyType.DataStore) FeatureTypeMapping(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.TypeMappingsPropertyType.FeatureTypeMapping) Namespace(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.NamespacesPropertyType.Namespace)

Example 10 with Namespace

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

the class AppSchemaMappingWrapper method buildAttributeXPath.

/**
 * Build an XPath expression to be used as &lt;targetAttribute&gt; 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>[&lt;context name&gt;]</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;
}
Also used : ArrayList(java.util.ArrayList) ChildContext(eu.esdihumboldt.hale.common.align.model.ChildContext) Namespace(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.NamespacesPropertyType.Namespace)

Aggregations

Namespace (eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.NamespacesPropertyType.Namespace)11 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)3 FeatureTypeMapping (eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.TypeMappingsPropertyType.FeatureTypeMapping)3 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)2 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)2 AppSchemaDataAccessType (eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AppSchemaDataAccessType)2 ArrayList (java.util.ArrayList)2 DefaultIOReporter (eu.esdihumboldt.hale.common.core.io.report.impl.DefaultIOReporter)1 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)1 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)1 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)1 ClientProperty (eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType.ClientProperty)1 DataStore (eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.SourceDataStoresPropertyType.DataStore)1 AttributeMappings (eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.TypeMappingsPropertyType.FeatureTypeMapping.AttributeMappings)1 WorkspaceMetadata (eu.esdihumboldt.hale.io.appschema.model.WorkspaceMetadata)1 AbstractAppSchemaConfigurator (eu.esdihumboldt.hale.io.appschema.writer.AbstractAppSchemaConfigurator)1 AppSchemaMappingGenerator (eu.esdihumboldt.hale.io.appschema.writer.AppSchemaMappingGenerator)1 Workspace (eu.esdihumboldt.hale.io.geoserver.Workspace)1 QName (javax.xml.namespace.QName)1