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