use of eu.esdihumboldt.hale.common.align.model.ChildContext in project hale by halestudio.
the class EntityDefinitionServiceImpl method getChildren.
/**
* @see EntityDefinitionService#getChildren(EntityDefinition)
*/
@Override
public Collection<? extends EntityDefinition> getChildren(EntityDefinition entity) {
List<ChildContext> path = entity.getPropertyPath();
Collection<? extends ChildDefinition<?>> children;
if (path == null || path.isEmpty()) {
// entity is a type, children are the type children
children = entity.getType().getChildren();
} else {
// get parent context
ChildContext parentContext = path.get(path.size() - 1);
if (parentContext.getChild().asGroup() != null) {
children = parentContext.getChild().asGroup().getDeclaredChildren();
} else if (parentContext.getChild().asProperty() != null) {
children = parentContext.getChild().asProperty().getPropertyType().getChildren();
} else {
throw new IllegalStateException("Illegal child definition type encountered");
}
}
if (children == null || children.isEmpty()) {
return Collections.emptyList();
}
Collection<EntityDefinition> result = new ArrayList<EntityDefinition>(children.size());
for (ChildDefinition<?> child : children) {
// add default child entity definition to result
ChildContext context = new ChildContext(child);
EntityDefinition defaultEntity = createEntity(entity.getType(), createPath(entity.getPropertyPath(), context), entity.getSchemaSpace(), entity.getFilter());
result.add(defaultEntity);
// look up additional instance contexts and add them
synchronized (namedContexts) {
for (Integer contextName : namedContexts.get(defaultEntity)) {
ChildContext namedContext = new ChildContext(contextName, null, null, child);
EntityDefinition namedChild = createEntity(entity.getType(), createPath(entity.getPropertyPath(), namedContext), entity.getSchemaSpace(), entity.getFilter());
result.add(namedChild);
}
}
synchronized (indexContexts) {
for (Integer index : indexContexts.get(defaultEntity)) {
ChildContext indexContext = new ChildContext(null, index, null, child);
EntityDefinition indexChild = createEntity(entity.getType(), createPath(entity.getPropertyPath(), indexContext), entity.getSchemaSpace(), entity.getFilter());
result.add(indexChild);
}
}
synchronized (conditionContexts) {
for (Condition condition : conditionContexts.get(defaultEntity)) {
ChildContext conditionContext = new ChildContext(null, null, condition, child);
EntityDefinition conditionChild = createEntity(entity.getType(), createPath(entity.getPropertyPath(), conditionContext), entity.getSchemaSpace(), entity.getFilter());
result.add(conditionChild);
}
}
}
return result;
}
use of eu.esdihumboldt.hale.common.align.model.ChildContext in project hale by halestudio.
the class EntityDefinitionServiceImpl method createWithCondition.
/**
* Creates a new entity definition.
*
* @param sibling the entity definition to use as base
* @param condition the new condition, not <code>null</code>
* @return a new entity definition
*/
private EntityDefinition createWithCondition(EntityDefinition sibling, Condition condition) {
EntityDefinition result;
List<ChildContext> path = sibling.getPropertyPath();
if (path.isEmpty()) {
// create type entity definition with filter
result = createEntity(sibling.getType(), path, sibling.getSchemaSpace(), condition.getFilter());
} else {
List<ChildContext> newPath = new ArrayList<ChildContext>(path);
ChildContext last = newPath.remove(path.size() - 1);
// new condition context, w/o name or index context
newPath.add(new ChildContext(null, null, condition, last.getChild()));
result = createEntity(sibling.getType(), newPath, sibling.getSchemaSpace(), sibling.getFilter());
}
return result;
}
use of eu.esdihumboldt.hale.common.align.model.ChildContext in project hale by halestudio.
the class EntityDefinitionServiceImpl method addNamedContext.
/**
* @see EntityDefinitionService#addNamedContext(EntityDefinition)
*/
@Override
public EntityDefinition addNamedContext(EntityDefinition sibling) {
List<ChildContext> path = sibling.getPropertyPath();
if (sibling.getSchemaSpace() == SchemaSpaceID.SOURCE || path.isEmpty()) {
// XXX throw exception instead?
return null;
}
// XXX any checks? see InstanceContextTester
EntityDefinition def = AlignmentUtil.getDefaultEntity(sibling);
Integer newName;
synchronized (namedContexts) {
// get registered context names
Collection<Integer> names = namedContexts.get(def);
if (names == null || names.isEmpty()) {
newName = Integer.valueOf(0);
} else {
// get the maximum value available as a name
SortedSet<Integer> sortedNames = new TreeSet<Integer>(names);
int max = sortedNames.last();
// and use its value increased by one
newName = Integer.valueOf(max + 1);
}
namedContexts.put(def, newName);
}
List<ChildContext> newPath = new ArrayList<ChildContext>(path);
ChildDefinition<?> lastChild = newPath.get(newPath.size() - 1).getChild();
newPath.remove(path.size() - 1);
// new named context, w/o index or condition context
newPath.add(new ChildContext(newName, null, null, lastChild));
EntityDefinition result = createEntity(def.getType(), newPath, sibling.getSchemaSpace(), sibling.getFilter());
notifyContextAdded(result);
return result;
}
use of eu.esdihumboldt.hale.common.align.model.ChildContext in project hale by halestudio.
the class EntityDefinitionServiceImpl method addIndexContext.
/**
* @see EntityDefinitionService#addIndexContext(EntityDefinition, Integer)
*/
@Override
public EntityDefinition addIndexContext(EntityDefinition sibling, Integer index) {
List<ChildContext> path = sibling.getPropertyPath();
if (sibling.getSchemaSpace() == SchemaSpaceID.TARGET || path.isEmpty()) {
// XXX throw exception instead?
return null;
}
// XXX any checks? see InstanceContextTester
EntityDefinition def = AlignmentUtil.getDefaultEntity(sibling);
boolean doAdd = true;
synchronized (indexContexts) {
// get registered context indexes
Set<Integer> existingIndexes = indexContexts.get(def);
if (index == null) {
// determine index automatically
if (existingIndexes == null || existingIndexes.isEmpty()) {
index = Integer.valueOf(0);
} else {
// get the sorted existing indexes
SortedSet<Integer> sortedIndexes = new TreeSet<Integer>(existingIndexes);
// find the smallest value not present
int expected = 0;
Iterator<Integer> it = sortedIndexes.iterator();
while (index == null && it.hasNext()) {
int existingIndex = it.next();
if (existingIndex != expected) {
index = expected;
}
expected++;
}
if (index == null) {
index = expected;
}
}
} else if (existingIndexes.contains(index)) {
// this index context is not new, but already there
doAdd = false;
}
if (doAdd) {
indexContexts.put(def, index);
}
}
List<ChildContext> newPath = new ArrayList<ChildContext>(path);
ChildDefinition<?> lastChild = newPath.get(newPath.size() - 1).getChild();
newPath.remove(path.size() - 1);
// new index context, w/o name or condition context
newPath.add(new ChildContext(null, index, null, lastChild));
EntityDefinition result = createEntity(def.getType(), newPath, sibling.getSchemaSpace(), sibling.getFilter());
if (doAdd) {
notifyContextAdded(result);
}
return result;
}
use of eu.esdihumboldt.hale.common.align.model.ChildContext in project hale by halestudio.
the class EntityDefinitionServiceImpl method addConditionContext.
/**
* @see EntityDefinitionService#addConditionContext(EntityDefinition,
* Filter)
*/
@Override
public EntityDefinition addConditionContext(EntityDefinition sibling, Filter filter) {
if (filter == null) {
throw new NullPointerException("Filter must not be null");
}
List<ChildContext> path = sibling.getPropertyPath();
if (sibling.getSchemaSpace() == SchemaSpaceID.TARGET && path.isEmpty()) {
// XXX throw exception instead?
return null;
}
Condition condition = new Condition(filter);
EntityDefinition def = AlignmentUtil.getDefaultEntity(sibling);
boolean doAdd = true;
synchronized (conditionContexts) {
// get registered context indexes
Set<Condition> existingConditions = conditionContexts.get(def);
if (existingConditions.contains(condition)) {
// this condition context is not new, but already there
doAdd = false;
}
if (doAdd) {
conditionContexts.put(def, condition);
}
}
EntityDefinition result = createWithCondition(sibling, condition);
if (doAdd) {
notifyContextAdded(result);
}
return result;
}
Aggregations