Search in sources :

Example 16 with ModelVersion

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);
        }
    }
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) ExternalCodeListItem(org.openforis.idm.metamodel.ExternalCodeListItem) CodeAttribute(org.openforis.idm.model.CodeAttribute) Record(org.openforis.idm.model.Record) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) CodeList(org.openforis.idm.metamodel.CodeList) List(java.util.List) ModelVersion(org.openforis.idm.metamodel.ModelVersion) Code(org.openforis.idm.model.Code) PersistedCodeListItem(org.openforis.idm.metamodel.PersistedCodeListItem)

Example 17 with ModelVersion

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;
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Entity(org.openforis.idm.model.Entity) ArrayList(java.util.ArrayList) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) ModelVersion(org.openforis.idm.metamodel.ModelVersion) NodePointer(org.openforis.idm.model.NodePointer)

Example 18 with ModelVersion

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);
                    }
                }
            });
        }
    }
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Entity(org.openforis.idm.model.Entity) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) BooleanAttributeDefinition(org.openforis.idm.metamodel.BooleanAttributeDefinition) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) Record(org.openforis.idm.model.Record) ModelVersion(org.openforis.idm.metamodel.ModelVersion) NodeVisitor(org.openforis.idm.model.NodeVisitor)

Example 19 with ModelVersion

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++;
            }
        }
    }
}
Also used : Entity(org.openforis.idm.model.Entity) CodeListService(org.openforis.idm.metamodel.CodeListService) Code(org.openforis.idm.model.Code) CodeList(org.openforis.idm.metamodel.CodeList) Survey(org.openforis.idm.metamodel.Survey) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) CodeAttribute(org.openforis.idm.model.CodeAttribute) Record(org.openforis.idm.model.Record) ModelVersion(org.openforis.idm.metamodel.ModelVersion) CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Example 20 with ModelVersion

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;
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) ArrayList(java.util.ArrayList) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) ModelVersion(org.openforis.idm.metamodel.ModelVersion) NodePointer(org.openforis.idm.model.NodePointer)

Aggregations

ModelVersion (org.openforis.idm.metamodel.ModelVersion)40 ArrayList (java.util.ArrayList)9 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)9 Record (org.openforis.idm.model.Record)9 CollectSurvey (org.openforis.collect.model.CollectSurvey)7 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)7 CodeListItem (org.openforis.idm.metamodel.CodeListItem)7 CodeList (org.openforis.idm.metamodel.CodeList)5 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)5 ExternalCodeListItem (org.openforis.idm.metamodel.ExternalCodeListItem)5 PersistedCodeListItem (org.openforis.idm.metamodel.PersistedCodeListItem)5 CodeAttribute (org.openforis.idm.model.CodeAttribute)5 Code (org.openforis.idm.model.Code)4 Entity (org.openforis.idm.model.Entity)4 SessionStatus (org.openforis.collect.designer.session.SessionStatus)2 UIOptions (org.openforis.collect.metamodel.ui.UIOptions)2 Survey (org.openforis.idm.metamodel.Survey)2 NodePointer (org.openforis.idm.model.NodePointer)2 Command (org.zkoss.bind.annotation.Command)2 GlobalCommand (org.zkoss.bind.annotation.GlobalCommand)2