use of eu.esdihumboldt.hale.common.instance.model.Group in project hale by halestudio.
the class OInstance method setMetaData.
/**
* {@inheritDoc} The parameter values may not contain an ODocument
*/
@Override
public void setMetaData(String key, Object... values) {
for (Object value : values) {
Preconditions.checkArgument(!(value instanceof ODocument || value instanceof Instance || value instanceof Group));
}
ODocument metaData;
if (document.field(FIELD_METADATA) == null) {
metaData = new ODocument();
document.field(FIELD_METADATA, metaData);
} else
metaData = ((ODocument) document.field(FIELD_METADATA));
setPropertyInternal(metaData, new QName(key), values);
}
use of eu.esdihumboldt.hale.common.instance.model.Group in project hale by halestudio.
the class GeometryUtil method getGeometries.
/**
* Get the default geometry of an instance.
*
* @param instance the instance
* @param path the property path to start the search at, a <code>null</code>
* will yield no geometries
* @return the default geometries or an empty collection if there is none
*/
public static Collection<GeometryProperty<?>> getGeometries(Instance instance, List<QName> path) {
Collection<GeometryProperty<?>> geometries = new ArrayList<GeometryProperty<?>>();
if (path == null) {
return geometries;
}
// descend path and return the geometries found
Queue<Group> parents = new LinkedList<Group>();
parents.add(instance);
for (int i = 0; i < path.size(); i++) {
QName name = path.get(i);
Queue<Group> children = new LinkedList<Group>();
for (Group parent : parents) {
Object[] values = parent.getProperty(name);
if (values != null) {
for (Object value : values) {
if (value instanceof Group) {
children.add((Group) value);
}
if (value instanceof Instance) {
value = ((Instance) value).getValue();
}
if (value != null && !(value instanceof Group) && i == path.size() - 1) {
// detect geometry values at end of path
// as they are not searched later on
Collection<GeometryProperty<?>> geoms = getGeometryProperties(value);
geometries.addAll(geoms);
}
}
}
}
// prepare for next step
parents = children;
}
// early exit #1
if (!geometries.isEmpty()) {
// XXX is this OK in all cases?
return geometries;
}
// search in those groups/instances for additional geometries
while (!parents.isEmpty()) {
Group parent = parents.poll();
// add values contained in the instance
Collection<GeometryProperty<?>> geoms = getGeometryProperties(parent);
if (!geoms.isEmpty()) {
geometries.addAll(geoms);
// early exit #2
// don't check the children as they usually are only parts of
// the geometry found here
} else {
// check children for geometries
for (QName name : parent.getPropertyNames()) {
Object[] values = parent.getProperty(name);
if (values != null) {
for (Object value : values) {
if (value instanceof Group) {
// check group later on
parents.add((Group) value);
} else {
// add geometries for value
geometries.addAll(getGeometryProperties(value));
}
}
}
}
}
}
return geometries;
}
use of eu.esdihumboldt.hale.common.instance.model.Group in project hale by halestudio.
the class ChoiceFlagValidator method validateGroupPropertyConstraint.
@Override
public void validateGroupPropertyConstraint(Object[] values, GroupPropertyConstraint constraint, GroupPropertyDefinition property, InstanceValidationContext context) throws ValidationException {
if (((ChoiceFlag) constraint).isEnabled() && values != null) {
for (Object value : values) {
if (value instanceof Group) {
Iterator<QName> properties = ((Group) value).getPropertyNames().iterator();
if (!properties.hasNext())
throw new ValidationException("A choice with no child.");
properties.next();
if (properties.hasNext())
throw new ValidationException("A choice with different children.");
}
// XXX what if value is null or not a Group?
// XXX what if it is an Instance? May it have a value?
}
}
}
use of eu.esdihumboldt.hale.common.instance.model.Group in project hale by halestudio.
the class EntityPopulationCount method evaluateContext.
/**
* count the population for the given entity with all contexts
*
* @param group A {@link Group}
* @param groupDef An {@link EntityDefinition}
*/
private void evaluateContext(Group group, EntityDefinition groupDef) {
List<ChildContext> path = groupDef.getPropertyPath();
if (path != null && !path.isEmpty()) {
ChildContext context = path.get(path.size() - 1);
Object[] values = group.getProperty(context.getChild().getName());
if (values != null) {
// apply the possible source contexts
if (context.getIndex() != null) {
// select only the item at the index
int index = context.getIndex();
if (index < values.length) {
values = new Object[] { values[index] };
} else {
values = new Object[] {};
}
}
if (context.getCondition() != null) {
// select only values that match the condition
List<Object> matchedValues = new ArrayList<Object>();
for (Object value : values) {
if (AlignmentUtil.matchCondition(context.getCondition(), value, group)) {
matchedValues.add(value);
}
}
values = matchedValues.toArray();
}
if (context.getChild().getName().equals(groupDef.getDefinition().getName())) {
increase(groupDef, values.length);
}
for (Object value : values) {
if (value instanceof Group) {
addToPopulation((Group) value, groupDef);
}
}
} else {
increase(groupDef, 0);
}
}
}
use of eu.esdihumboldt.hale.common.instance.model.Group in project hale by halestudio.
the class Rename method structuralRename.
/**
* Performs a structural rename on the given source object to the given
* target definition.
*
* @param source the source value (or group/instance)
* @param targetDefinition the target definition
* @param allowIgnoreNamespaces if for the structure comparison, namespaces
* may be ignored
* @param instanceFactory the instance factory
* @param copyGeometries specifies if geometry objects should be copied
* @return the transformed value (or group/instance) or NO_MATCH
*/
public static Object structuralRename(Object source, ChildDefinition<?> targetDefinition, boolean allowIgnoreNamespaces, InstanceFactory instanceFactory, boolean copyGeometries) {
if (!(source instanceof Group)) {
// source simple value
if (targetDefinition.asProperty() != null) {
// target can have value
TypeDefinition propertyType = targetDefinition.asProperty().getPropertyType();
if (copyGeometries || !isGeometry(source)) {
if (propertyType.getChildren().isEmpty()) {
// simple value
return convertValue(source, targetDefinition.asProperty().getPropertyType());
} else {
// instance with value
MutableInstance instance = instanceFactory.createInstance(propertyType);
instance.setDataSet(DataSet.TRANSFORMED);
instance.setValue(convertValue(source, propertyType));
return instance;
}
} else {
return Result.NO_MATCH;
}
}
}
// source is group or instance
if (targetDefinition.asProperty() != null) {
// target can have value
TypeDefinition propertyType = targetDefinition.asProperty().getPropertyType();
if (source instanceof Instance) {
// source has value
if (propertyType.getChildren().isEmpty()) {
// simple value
return convertValue(((Instance) source).getValue(), targetDefinition.asProperty().getPropertyType());
} else {
// instance with value
MutableInstance instance = instanceFactory.createInstance(targetDefinition.asProperty().getPropertyType());
instance.setDataSet(DataSet.TRANSFORMED);
if (copyGeometries || !isGeometry(((Instance) source).getValue())) {
instance.setValue(convertValue(((Instance) source).getValue(), targetDefinition.asProperty().getPropertyType()));
}
renameChildren((Group) source, instance, targetDefinition, allowIgnoreNamespaces, instanceFactory, copyGeometries);
return instance;
}
} else {
// source has no value
if (targetDefinition.asProperty().getPropertyType().getChildren().isEmpty())
// no match possible
return Result.NO_MATCH;
else {
// instance with no value set
MutableInstance instance = instanceFactory.createInstance(targetDefinition.asProperty().getPropertyType());
instance.setDataSet(DataSet.TRANSFORMED);
if (renameChildren((Group) source, instance, targetDefinition, allowIgnoreNamespaces, instanceFactory, copyGeometries))
return instance;
else
// no child matched and no value
return Result.NO_MATCH;
}
}
} else if (targetDefinition.asGroup() != null) {
// target can not have a value
if (targetDefinition.asGroup().getDeclaredChildren().isEmpty())
// target neither has a value nor
return Result.NO_MATCH;
else // children?
{
// group
MutableGroup group = new DefaultGroup(targetDefinition.asGroup());
if (renameChildren((Group) source, group, targetDefinition, allowIgnoreNamespaces, instanceFactory, copyGeometries))
return group;
else
// no child matched and no value
return Result.NO_MATCH;
}
} else {
// neither asProperty nor asGroup -> illegal ChildDefinition
throw new IllegalStateException("Illegal child type.");
}
}
Aggregations