Search in sources :

Example 1 with PathElement

use of eu.esdihumboldt.hale.common.align.groovy.accessor.PathElement in project hale by halestudio.

the class EntityAccessorUtil method createEntity.

/**
 * Create an entity definition from a path.
 *
 * @param path the path, the topmost element has to represent an
 *            {@link EntityDefinition}, all other elements must represent
 *            {@link ChildContext}s
 * @return the created entity definition or <code>null</code> if the path
 *         was <code>null</code>
 */
public static EntityDefinition createEntity(Path<PathElement> path) {
    if (path == null) {
        return null;
    }
    List<PathElement> elements = new ArrayList<>(path.getElements());
    // create entity definition
    PathElement top = elements.remove(0);
    if (top.getRoot() == null) {
        throw new IllegalArgumentException("Topmost path element must be an entity definition");
    }
    EntityDefinition entity = top.getRoot();
    // collect type information
    TypeDefinition type = entity.getType();
    SchemaSpaceID schemaSpace = entity.getSchemaSpace();
    Filter filter = entity.getFilter();
    List<ChildContext> contextPath = new ArrayList<>(entity.getPropertyPath());
    for (PathElement element : elements) {
        ChildContext cc = element.getChild();
        if (cc == null) {
            throw new IllegalArgumentException("All child elements must be defined by a child context");
        }
        contextPath.add(cc);
    }
    return AlignmentUtil.createEntity(type, contextPath, schemaSpace, filter);
}
Also used : EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) PathElement(eu.esdihumboldt.hale.common.align.groovy.accessor.PathElement) Filter(eu.esdihumboldt.hale.common.instance.model.Filter) ArrayList(java.util.ArrayList) ChildContext(eu.esdihumboldt.hale.common.align.model.ChildContext) SchemaSpaceID(eu.esdihumboldt.hale.common.schema.SchemaSpaceID) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 2 with PathElement

use of eu.esdihumboldt.hale.common.align.groovy.accessor.PathElement in project hale by halestudio.

the class AbstractGeotoolsFilter method resolveProperty.

/**
 * Resolve a property name based on the given context.
 *
 * @param name the property name
 * @param context the entity context
 * @param log the operation log
 * @return the resolved entity definition if it could be resolved uniquely
 */
private Optional<EntityDefinition> resolveProperty(PropertyName name, EntityDefinition context, SimpleLog log) {
    List<QName> path = PropertyResolver.getQNamesFromPath(name.getPropertyName());
    EntityAccessor acc = new EntityAccessor(context);
    for (QName element : path) {
        acc = acc.findChildren(element);
    }
    try {
        return Optional.ofNullable(acc.toEntityDefinition());
    } catch (IllegalStateException e) {
        List<? extends Path<PathElement>> candidates = acc.all();
        if (candidates.isEmpty()) {
            log.error("Unable to find reference to {0}", name);
            return Optional.empty();
        } else {
            log.warn("Could not find unique reference to {0}, trying first match", name);
            Path<PathElement> selected = candidates.get(0);
            /*
				 * XXX dirty hack to work around conditions in AdV
				 * GeographicalNames alignment not being defined properly
				 */
            if (candidates.size() > 1) {
                // prefer next candidate if this one is name in gml
                // namespace
                List<PathElement> elements = selected.getElements();
                PathElement last = elements.get(elements.size() - 1);
                QName lastName = last.getDefinition().getName();
                if ("name".equals(lastName.getLocalPart()) && "http://www.opengis.net/gml/3.2".equals(lastName.getNamespaceURI())) {
                    selected = candidates.get(1);
                }
            }
            return Optional.ofNullable(EntityAccessorUtil.createEntity(selected));
        }
    }
}
Also used : EntityAccessor(eu.esdihumboldt.hale.common.align.groovy.accessor.EntityAccessor) Path(eu.esdihumboldt.util.groovy.paths.Path) PathElement(eu.esdihumboldt.hale.common.align.groovy.accessor.PathElement) QName(javax.xml.namespace.QName) List(java.util.List)

Aggregations

PathElement (eu.esdihumboldt.hale.common.align.groovy.accessor.PathElement)2 EntityAccessor (eu.esdihumboldt.hale.common.align.groovy.accessor.EntityAccessor)1 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)1 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)1 Filter (eu.esdihumboldt.hale.common.instance.model.Filter)1 SchemaSpaceID (eu.esdihumboldt.hale.common.schema.SchemaSpaceID)1 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)1 Path (eu.esdihumboldt.util.groovy.paths.Path)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 QName (javax.xml.namespace.QName)1