Search in sources :

Example 16 with ChildDefinition

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

the class InstanceValidator method validateChildren.

/**
 * Validates the given property values (their values - as instances - and/or
 * group children).
 *
 * @param properties the array of existing properties, may be null
 * @param childDef their definition
 * @param reporter the reporter to report to
 * @param type the top level type
 * @param path the current property path
 * @param onlyCheckExistingChildren whether to only validate existing
 *            children (in case of a choice) or not
 * @param reference the instance reference
 * @param context the instance validation context
 * @param entity the entity definition related to the property values or
 *            <code>null</code>
 */
private void validateChildren(Object[] properties, ChildDefinition<?> childDef, InstanceValidationReporter reporter, QName type, List<QName> path, boolean onlyCheckExistingChildren, InstanceReference reference, InstanceValidationContext context, @Nullable EntityDefinition entity) {
    if (properties != null && properties.length > 0) {
        for (Object property : properties) {
            if (property instanceof Instance) {
                validateInstance((Instance) property, reporter, type, path, onlyCheckExistingChildren, reference, context, childDef, entity);
            } else if (property instanceof Group) {
                validateGroupChildren((Group) property, reporter, type, path, onlyCheckExistingChildren, reference, context, childDef, entity);
            } else {
                if (childDef.asGroup() != null)
                    reporter.warn(new DefaultInstanceValidationMessage(reference, type, new ArrayList<QName>(path), "Wrong group", "A property is no group"));
                else if (childDef.asProperty() != null) {
                    if (!skipValidation(childDef.asProperty().getPropertyType(), property)) {
                        // don't skip property
                        // wrap value in dummy instance for type validation
                        MutableInstance instance = new DefaultInstance(childDef.asProperty().getPropertyType(), null);
                        instance.setValue(property);
                        validateInstance(instance, reporter, type, path, onlyCheckExistingChildren, reference, context, childDef, entity);
                    }
                }
            }
        }
    } else {
        /*
			 * Special case: No property value, but a combination of minimum
			 * cardinality greater than zero and NillableFlag is set. Then there
			 * can be sub-properties that are required.
			 * 
			 * Applicable for XML (simple) types with mandatory attributes.
			 */
        if (childDef.asProperty() != null && childDef.asProperty().getConstraint(Cardinality.class).getMinOccurs() > 0 && childDef.asProperty().getConstraint(NillableFlag.class).isEnabled() && childDef.asProperty().getPropertyType().getConstraint(HasValueFlag.class).isEnabled() && !childDef.asProperty().getPropertyType().getChildren().isEmpty()) {
            // collect XML attribute children
            List<ChildDefinition<?>> attributes = new ArrayList<ChildDefinition<?>>();
            for (ChildDefinition<?> child : childDef.asProperty().getPropertyType().getChildren()) {
                if (child.asProperty() != null && child.asProperty().getConstraint(XmlAttributeFlag.class).isEnabled()) {
                    attributes.add(child);
                }
            }
            if (!attributes.isEmpty()) {
                // create an empty dummy instance
                Instance instance = new DefaultInstance(childDef.asProperty().getPropertyType(), null);
                validateGroupChildren(instance, attributes, reporter, type, path, onlyCheckExistingChildren, reference, context, entity);
            }
        }
    }
}
Also used : Group(eu.esdihumboldt.hale.common.instance.model.Group) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) DefaultInstanceValidationMessage(eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationMessage) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) HasValueFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag) ArrayList(java.util.ArrayList) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) ChildDefinition(eu.esdihumboldt.hale.common.schema.model.ChildDefinition)

Example 17 with ChildDefinition

use of eu.esdihumboldt.hale.common.schema.model.ChildDefinition 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 18 with ChildDefinition

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

the class AbstractGeometrySchemaService method generalizeGeometryProperty.

/**
 * Generalize the path to the geometry property for the given type. This
 * serves to prevent focusing on a single geometry property in a choice.
 *
 * @param type the type definition
 * @param geometryPath the geometry path
 * @return the generalized geometry path
 */
private List<QName> generalizeGeometryProperty(TypeDefinition type, List<QName> geometryPath) {
    // collect child definitions associated to path names
    List<ChildDefinition<?>> pathChildren = new ArrayList<ChildDefinition<?>>();
    DefinitionGroup parent = type;
    for (QName name : geometryPath) {
        ChildDefinition<?> child = parent.getChild(name);
        if (child == null) {
            // invalid path
            break;
        }
        pathChildren.add(child);
        if (child.asProperty() != null) {
            parent = child.asProperty().getPropertyType();
        } else if (child.asGroup() != null) {
            parent = child.asGroup();
        } else {
            throw new IllegalStateException("Invalid child definition");
        }
    }
    // remove geometry properties parented by a choice
    for (int i = pathChildren.size() - 1; i > 0; i--) {
        // peek at the previous item
        ChildDefinition<?> previous = pathChildren.get(i - 1);
        if (previous.asGroup() != null && previous.asGroup().getConstraint(ChoiceFlag.class).isEnabled()) {
            // previous item is a choice:
            // delete the current item
            pathChildren.remove(i);
        // and continue
        } else {
            // everything after it
            break;
        }
    }
    // create a name list from the child list
    List<QName> names = new ArrayList<QName>(pathChildren.size());
    for (ChildDefinition<?> child : pathChildren) {
        names.add(child.getName());
    }
    return names;
}
Also used : QName(javax.xml.namespace.QName) ChildDefinition(eu.esdihumboldt.hale.common.schema.model.ChildDefinition) ArrayList(java.util.ArrayList) DefinitionGroup(eu.esdihumboldt.hale.common.schema.model.DefinitionGroup)

Example 19 with ChildDefinition

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

the class PropertyDefinitionDialog method getObjectFromSelection.

@Override
protected EntityDefinition getObjectFromSelection(ISelection selection) {
    if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
        Object element = ((IStructuredSelection) selection).getFirstElement();
        if (element instanceof EntityDefinition) {
            return (EntityDefinition) element;
        }
    }
    if (!selection.isEmpty() && selection instanceof ITreeSelection) {
        // create property definition w/ default contexts
        TreePath path = ((ITreeSelection) selection).getPaths()[0];
        // get parent type
        TypeDefinition type = ((ChildDefinition<?>) path.getFirstSegment()).getParentType();
        // determine definition path
        List<ChildContext> defPath = new ArrayList<ChildContext>();
        for (int i = 0; i < path.getSegmentCount(); i++) {
            defPath.add(new ChildContext((ChildDefinition<?>) path.getSegment(i)));
        }
        // TODO check if property entity definition is applicable?
        return new PropertyEntityDefinition(type, defPath, ssid, null);
    }
    return null;
}
Also used : PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) ITreeSelection(org.eclipse.jface.viewers.ITreeSelection) TreePath(org.eclipse.jface.viewers.TreePath) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) ChildDefinition(eu.esdihumboldt.hale.common.schema.model.ChildDefinition) ArrayList(java.util.ArrayList) ChildContext(eu.esdihumboldt.hale.common.align.model.ChildContext) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 20 with ChildDefinition

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

the class FilterEditor method setValue.

/**
 * @see Editor#setValue(Object)
 */
@Override
public void setValue(final Filter filter) {
    filterEnabled.setSelection(false);
    setControlsEnabled(false);
    if (filter != null) {
        filter.accept(new FilterVisitor() {

            @Override
            protected void visitFilter(FilterIdentifier id, String propertyName, String value) {
                Boolean invalidProperty = false;
                List<ChildContext> path = new ArrayList<ChildContext>();
                // set the correct selected name for the property selector
                List<QName> qNames = PropertyResolver.getQNamesFromPath(propertyName);
                ChildDefinition<?> child = typeDefinition.getChild(qNames.get(0));
                if (child != null) {
                    path.add(new ChildContext(child));
                    for (int i = 1; i < qNames.size(); i++) {
                        child = DefinitionUtil.getChild(child, qNames.get(i));
                        if (child != null) {
                            path.add(new ChildContext(child));
                        } else {
                            invalidProperty = true;
                            break;
                        }
                    }
                } else {
                    invalidProperty = true;
                }
                if (!invalidProperty && !path.isEmpty()) {
                    PropertyEntityDefinition entity = new PropertyEntityDefinition(typeDefinition, path, null, null);
                    propertySelect.setSelection(new StructuredSelection(entity));
                } else {
                    propertySelect.setSelection(new StructuredSelection());
                }
                // set filter
                filterSelect.setSelection(new StructuredSelection(id));
                // set value
                literal.getDocument().set(value);
                filterEnabled.setSelection(true);
                setControlsEnabled(true);
            }
        }, null);
    }
}
Also used : PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) DefaultFilterVisitor(org.geotools.filter.visitor.DefaultFilterVisitor) ChildDefinition(eu.esdihumboldt.hale.common.schema.model.ChildDefinition) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ChildContext(eu.esdihumboldt.hale.common.align.model.ChildContext) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ChildDefinition (eu.esdihumboldt.hale.common.schema.model.ChildDefinition)32 ArrayList (java.util.ArrayList)20 QName (javax.xml.namespace.QName)19 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)11 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)6 Definition (eu.esdihumboldt.hale.common.schema.model.Definition)6 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)5 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)5 DefinitionGroup (eu.esdihumboldt.hale.common.schema.model.DefinitionGroup)5 List (java.util.List)5 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)4 Pair (eu.esdihumboldt.util.Pair)3 LinkedList (java.util.LinkedList)3 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)3 TreePath (org.eclipse.jface.viewers.TreePath)3 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)2 InstanceValidationMessage (eu.esdihumboldt.hale.common.instance.extension.validation.report.InstanceValidationMessage)2 InstanceValidationReport (eu.esdihumboldt.hale.common.instance.extension.validation.report.InstanceValidationReport)2 Group (eu.esdihumboldt.hale.common.instance.model.Group)2 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)2