Search in sources :

Example 1 with DefinitionGroup

use of eu.esdihumboldt.hale.common.schema.model.DefinitionGroup in project hale by halestudio.

the class CustomTypeContentHelper method applyConfiguration.

/**
 * Apply a custom type configuration to the given schema.
 *
 * @param index the XML schema
 * @param association the custom type configuration for an individual
 *            property
 */
public static void applyConfiguration(XmlIndex index, CustomTypeContentAssociation association) {
    CustomTypeContent config = association.getConfig();
    // property identified by a list of qualified names
    List<QName> property = association.getProperty();
    PropertyDefinition propDef = null;
    DefinitionGroup propParent = null;
    if (property != null && property.size() > 1) {
        QName typeName = property.get(0);
        TypeDefinition type = index.getType(typeName);
        if (type != null) {
            LinkedList<QName> nameQueue = new LinkedList<>(property.subList(1, property.size()));
            Definition<?> parent = null;
            Definition<?> child = type;
            while (!nameQueue.isEmpty() && child != null) {
                parent = child;
                QName name = nameQueue.pollFirst();
                child = DefinitionUtil.getChild(parent, name);
            }
            if (nameQueue.isEmpty() && child instanceof PropertyDefinition) {
                propDef = (PropertyDefinition) child;
                if (parent instanceof TypeDefinition) {
                    propParent = (DefinitionGroup) parent;
                } else if (parent instanceof ChildDefinition<?>) {
                    ChildDefinition<?> pc = (ChildDefinition<?>) parent;
                    if (pc.asProperty() != null) {
                        propParent = pc.asProperty().getPropertyType();
                    } else if (pc.asGroup() != null) {
                        propParent = pc.asGroup();
                    }
                } else {
                    log.error("Illegal parent for custom type content property");
                    return;
                }
            } else {
                log.warn("Cannot apply custom type content configuration due to invalid property path");
                return;
            }
        } else {
            log.warn("Cannot apply custom type content configuration due because the type {} starting the property path could not be found", typeName);
            return;
        }
    } else {
        log.warn("Cannot apply custom type content configuration due to missing property path");
        return;
    }
    switch(config.getMode()) {
        case simple:
            applySimpleMode(propDef, propParent, config);
            break;
        case elements:
            applyElementsMode(propDef, propParent, config, index);
            break;
        default:
            log.error("Unrecognized custom type content mode {}", config.getMode().name());
    }
}
Also used : QName(javax.xml.namespace.QName) ChildDefinition(eu.esdihumboldt.hale.common.schema.model.ChildDefinition) DefaultPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition) DefaultGroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultGroupPropertyDefinition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) DefinitionGroup(eu.esdihumboldt.hale.common.schema.model.DefinitionGroup) LinkedList(java.util.LinkedList) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)

Example 2 with DefinitionGroup

use of eu.esdihumboldt.hale.common.schema.model.DefinitionGroup in project hale by halestudio.

the class OGroup method configureDocument.

private void configureDocument(ORecordAbstract<?> document, ODatabaseRecord db, DefinitionGroup definition) {
    // document.setDatabase(db);
    if (document instanceof ODocument) {
        // reset class name
        ODocument doc = (ODocument) document;
        /*
			 * Attention: Two long class names cause problems as file names will
			 * be based on them.
			 */
        String className = null;
        if (definition != null) {
            className = ONamespaceMap.encode(determineName(definition));
        } else if (doc.containsField(OSerializationHelper.BINARY_WRAPPER_FIELD) || doc.containsField(OSerializationHelper.FIELD_SERIALIZATION_TYPE)) {
            className = OSerializationHelper.BINARY_WRAPPER_CLASSNAME;
        }
        if (className != null) {
            OSchema schema = db.getMetadata().getSchema();
            if (!schema.existsClass(className)) {
                // if the class doesn't exist yet, create a physical cluster
                // manually for it
                int cluster = db.addCluster(className, CLUSTER_TYPE.PHYSICAL);
                schema.createClass(className, cluster);
            }
            doc.setClassName(className);
        }
        // configure children
        for (Entry<String, Object> field : doc) {
            List<ODocument> docs = new ArrayList<ODocument>();
            List<ORecordAbstract<?>> recs = new ArrayList<ORecordAbstract<?>>();
            if (field.getValue() instanceof Collection<?>) {
                for (Object value : (Collection<?>) field.getValue()) {
                    if (value instanceof ODocument && !getSpecialFieldNames().contains(field.getKey())) {
                        docs.add((ODocument) value);
                    } else if (value instanceof ORecordAbstract<?>) {
                        recs.add((ORecordAbstract<?>) value);
                    }
                }
            } else if (field.getValue() instanceof ODocument && !getSpecialFieldNames().contains(field.getKey())) {
                docs.add((ODocument) field.getValue());
            } else if (field.getValue() instanceof ORecordAbstract<?>) {
                recs.add((ORecordAbstract<?>) field.getValue());
            }
            if (definition != null) {
                for (ODocument valueDoc : docs) {
                    ChildDefinition<?> child = definition.getChild(decodeProperty(field.getKey()));
                    DefinitionGroup childGroup;
                    if (child.asProperty() != null) {
                        childGroup = child.asProperty().getPropertyType();
                    } else if (child.asGroup() != null) {
                        childGroup = child.asGroup();
                    } else {
                        throw new IllegalStateException("Document is associated neither with a property nor a property group.");
                    }
                    configureDocument(valueDoc, db, childGroup);
                }
            }
            for (ORecordAbstract<?> fieldRec : recs) {
                configureDocument(fieldRec, db, null);
            }
        }
    }
}
Also used : ORecordAbstract(com.orientechnologies.orient.core.record.ORecordAbstract) OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ArrayList(java.util.ArrayList) DefinitionGroup(eu.esdihumboldt.hale.common.schema.model.DefinitionGroup) Collection(java.util.Collection) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 3 with DefinitionGroup

use of eu.esdihumboldt.hale.common.schema.model.DefinitionGroup in project hale by halestudio.

the class PropertyBean method createEntityDefinition.

/**
 * @see EntityBean#createEntityDefinition(TypeIndex, SchemaSpaceID)
 */
@Override
protected PropertyEntityDefinition createEntityDefinition(TypeIndex index, SchemaSpaceID schemaSpace) {
    TypeDefinition typeDef = index.getType(getTypeName());
    if (typeDef == null) {
        throw new IllegalStateException(MessageFormat.format("TypeDefinition for type {0} not found", getTypeName()));
    }
    List<ChildContext> path = new ArrayList<ChildContext>();
    DefinitionGroup parent = typeDef;
    for (ChildContextBean childContext : properties) {
        if (parent == null) {
            throw new IllegalStateException("Could not resolve property entity definition: child not present");
        }
        Pair<ChildDefinition<?>, List<ChildDefinition<?>>> childs = findChild(parent, childContext.getChildName());
        ChildDefinition<?> child = childs.getFirst();
        // if the child is still null throw an exception
        if (child == null) {
            throw new IllegalStateException("Could not resolve property entity definition: child not found");
        }
        if (childs.getSecond() != null) {
            for (ChildDefinition<?> pathElems : childs.getSecond()) {
                path.add(new ChildContext(childContext.getContextName(), childContext.getContextIndex(), createCondition(childContext.getConditionFilter()), pathElems));
            }
        }
        path.add(new ChildContext(childContext.getContextName(), childContext.getContextIndex(), createCondition(childContext.getConditionFilter()), child));
        if (child instanceof DefinitionGroup) {
            parent = (DefinitionGroup) child;
        } else if (child.asProperty() != null) {
            parent = child.asProperty().getPropertyType();
        } else {
            parent = null;
        }
    }
    return new PropertyEntityDefinition(typeDef, path, schemaSpace, FilterDefinitionManager.getInstance().parse(getFilter()));
}
Also used : PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) ArrayList(java.util.ArrayList) ChildDefinition(eu.esdihumboldt.hale.common.schema.model.ChildDefinition) ChildContext(eu.esdihumboldt.hale.common.align.model.ChildContext) ArrayList(java.util.ArrayList) List(java.util.List) DefinitionGroup(eu.esdihumboldt.hale.common.schema.model.DefinitionGroup) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 4 with DefinitionGroup

use of eu.esdihumboldt.hale.common.schema.model.DefinitionGroup in project hale by halestudio.

the class EntityFinder method find.

/**
 * Find entities matching the condition in (and including) the given parent.
 *
 * @param parent the entity to search
 * @param checked the set of already checked type definitions (to avoid
 *            endless recursion)
 * @return the entities found matching the condition
 */
protected List<EntityDefinition> find(EntityDefinition parent, Set<DefinitionGroup> checked) {
    final int level = parent.getPropertyPath().size();
    if (maxLevel != UNBOUNDED_LEVELS && maxLevel < level) {
        return Collections.emptyList();
    }
    // check if type has already been found
    DefinitionGroup typeDef = AlignmentUtil.getDefinitionGroup(parent);
    if (checked.contains(typeDef)) {
        return Collections.emptyList();
    } else {
        checked = new HashSet<>(checked);
        checked.add(typeDef);
    }
    List<EntityDefinition> found = new ArrayList<>();
    // add parent if it matches the condition
    if (condition.test(parent)) {
        found.add(parent);
    }
    if (maxLevel != UNBOUNDED_LEVELS && maxLevel < level + 1) {
        return found;
    }
    // check children
    for (EntityDefinition child : AlignmentUtil.getChildrenWithoutContexts(parent)) {
        found.addAll(find(child, checked));
    }
    return found;
}
Also used : EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) ArrayList(java.util.ArrayList) DefinitionGroup(eu.esdihumboldt.hale.common.schema.model.DefinitionGroup)

Example 5 with DefinitionGroup

use of eu.esdihumboldt.hale.common.schema.model.DefinitionGroup in project hale by halestudio.

the class GroupPath method allowAdd.

/**
 * Determines if the adding a property value for the given property to the
 * last element in the path is allowed.
 *
 * @param propertyName the property name
 * @param strict states if additional checks are applied apart from whether
 *            the property exists
 * @param ignoreNamespaces if a property with a differing namespace may be
 *            accepted
 * @return if adding the property value to the last element in the path is
 *         allowed
 */
public boolean allowAdd(QName propertyName, boolean strict, boolean ignoreNamespaces) {
    if (children == null || children.isEmpty()) {
        // check last parent
        MutableGroup parent = parents.get(parents.size() - 1);
        ChildDefinition<?> child = GroupUtil.findChild(parent.getDefinition(), propertyName, ignoreNamespaces);
        if (child.asProperty() != null) {
            return !strict || GroupUtil.allowAdd(parent, null, child.asProperty().getName());
        } else {
            return false;
        }
    } else {
        // check last child
        DefinitionGroup child = children.get(children.size() - 1);
        ChildDefinition<?> property = GroupUtil.findChild(child, propertyName, ignoreNamespaces);
        if (property == null) {
            return false;
        }
        if (child instanceof GroupPropertyDefinition && ((GroupPropertyDefinition) child).getConstraint(ChoiceFlag.class).isEnabled()) {
            // group is a choice
            return true;
        }
        return !strict || GroupUtil.allowAdd(null, child, property.getName());
    }
}
Also used : GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) MutableGroup(eu.esdihumboldt.hale.common.instance.model.MutableGroup) DefinitionGroup(eu.esdihumboldt.hale.common.schema.model.DefinitionGroup)

Aggregations

DefinitionGroup (eu.esdihumboldt.hale.common.schema.model.DefinitionGroup)15 ArrayList (java.util.ArrayList)8 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)6 QName (javax.xml.namespace.QName)6 MutableGroup (eu.esdihumboldt.hale.common.instance.model.MutableGroup)5 ChildDefinition (eu.esdihumboldt.hale.common.schema.model.ChildDefinition)5 GroupPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition)4 List (java.util.List)4 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)3 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)2 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)2 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)2 DefaultGroup (eu.esdihumboldt.hale.common.instance.model.impl.DefaultGroup)2 Definition (eu.esdihumboldt.hale.common.schema.model.Definition)2 LinkedList (java.util.LinkedList)2 Point (org.locationtech.jts.geom.Point)2 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)1 ORecordAbstract (com.orientechnologies.orient.core.record.ORecordAbstract)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 ChildContextType (eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ChildContextType)1