use of org.openforis.idm.metamodel.ModelVersion in project collect by openforis.
the class CodeListManager method loadParentItem.
protected CodeListItem loadParentItem(CodeAttribute attribute) {
CodeList list = attribute.getDefinition().getList();
boolean persistedSurvey = list.getSurvey().getId() != null;
Record record = attribute.getRecord();
ModelVersion version = record.getVersion();
if (persistedSurvey && list.isExternal()) {
ExternalCodeListItem item = (ExternalCodeListItem) loadItemByAttribute(attribute);
return provider.getParentItem(item);
} else if (persistedSurvey && list.isEmpty()) {
PersistedCodeListItem lastParentItem = null;
List<CodeAttribute> codeAncestors = attribute.getCodeAncestors();
for (int i = 0; i < codeAncestors.size(); i++) {
CodeAttribute ancestor = codeAncestors.get(i);
Integer lastParentItemId = lastParentItem == null ? null : lastParentItem.getSystemId();
Code code = ancestor.getValue();
lastParentItem = codeListItemDao.loadItem(list, lastParentItemId, code.getCode(), version);
if (lastParentItem == null) {
break;
}
}
return lastParentItem;
} else {
CodeAttribute codeParent = attribute.getCodeParent();
if (codeParent == null) {
return null;
} else {
return loadItemByAttribute(codeParent);
}
}
}
use of org.openforis.idm.metamodel.ModelVersion in project collect by openforis.
the class RecordUpdater method getDescendantNodePointers.
private List<NodePointer> getDescendantNodePointers(Entity entity) {
ModelVersion version = entity.getRecord().getVersion();
List<NodePointer> pointers = new ArrayList<NodePointer>();
EntityDefinition definition = entity.getDefinition();
for (NodeDefinition childDef : definition.getChildDefinitionsInVersion(version)) {
pointers.add(new NodePointer(entity, childDef));
if (childDef instanceof EntityDefinition) {
for (Node<?> childEntity : entity.getChildren(childDef)) {
pointers.addAll(getDescendantNodePointers((Entity) childEntity));
}
}
}
return pointers;
}
use of org.openforis.idm.metamodel.ModelVersion in project collect by openforis.
the class RecordUpdater method addEmptyNodes.
private void addEmptyNodes(Entity entity) {
Record record = entity.getRecord();
ModelVersion version = record.getVersion();
addEmptyEnumeratedEntities(entity);
EntityDefinition entityDefn = entity.getDefinition();
List<NodeDefinition> childDefinitions = entityDefn.getChildDefinitionsInVersion(version);
for (NodeDefinition childDefn : childDefinitions) {
if (entity.getCount(childDefn) == 0) {
if (addEmptyMultipleEntitiesWhenAddingNewEntities || !(childDefn instanceof EntityDefinition && childDefn.isMultiple())) {
int toBeInserted = entity.getMinCount(childDefn);
if (toBeInserted <= 0 && childDefn instanceof AttributeDefinition || !childDefn.isMultiple()) {
// insert at least one node
toBeInserted = 1;
}
addEmptyChildren(entity, childDefn, toBeInserted);
}
} else {
entity.visitChildren(childDefn, new NodeVisitor() {
public void visit(Node<? extends NodeDefinition> child, int idx) {
if (child instanceof Entity) {
addEmptyNodes((Entity) child);
}
}
});
}
}
}
use of org.openforis.idm.metamodel.ModelVersion in project collect by openforis.
the class RecordUpdater method addEmptyEnumeratedEntities.
private void addEmptyEnumeratedEntities(Entity parentEntity, EntityDefinition enumerableEntityDefn) {
Record record = parentEntity.getRecord();
ModelVersion version = record.getVersion();
CodeAttributeDefinition enumeratingCodeDefn = enumerableEntityDefn.getEnumeratingKeyCodeAttribute(version);
if (enumeratingCodeDefn != null) {
CodeList list = enumeratingCodeDefn.getList();
Survey survey = record.getSurvey();
CodeListService codeListService = survey.getContext().getCodeListService();
List<CodeListItem> items = codeListService.loadRootItems(list);
int i = 0;
for (CodeListItem item : items) {
if (version == null || version.isApplicable(item)) {
String code = item.getCode();
Entity enumeratedEntity = parentEntity.getEnumeratedEntity(enumerableEntityDefn, enumeratingCodeDefn, code);
if (enumeratedEntity == null) {
Entity addedEntity = performEntityAdd(parentEntity, enumerableEntityDefn, i);
addEmptyNodes(addedEntity);
// set the value of the key CodeAttribute
CodeAttribute addedCode = (CodeAttribute) addedEntity.getChild(enumeratingCodeDefn, 0);
addedCode.setValue(new Code(code));
addedCode.updateSummaryInfo();
} else if (enumeratedEntity.getIndex() != i) {
parentEntity.move(enumerableEntityDefn, enumeratedEntity.getIndex(), i);
}
i++;
}
}
}
}
use of org.openforis.idm.metamodel.ModelVersion in project collect by openforis.
the class RecordUpdater method getChildNodePointers.
private List<NodePointer> getChildNodePointers(Entity entity) {
ModelVersion version = entity.getRecord().getVersion();
List<NodePointer> pointers = new ArrayList<NodePointer>();
EntityDefinition definition = entity.getDefinition();
for (NodeDefinition childDef : definition.getChildDefinitionsInVersion(version)) {
pointers.add(new NodePointer(entity, childDef));
}
return pointers;
}
Aggregations