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);
}
}
}
}
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()));
}
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;
}
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;
}
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);
}
}
Aggregations