Search in sources :

Example 46 with CodeList

use of org.openforis.idm.metamodel.CodeList in project collect by openforis.

the class DatabaseExternalCodeListProvider method getChildItem.

public ExternalCodeListItem getChildItem(ExternalCodeListItem item, String code) {
    CodeList list = item.getCodeList();
    List<NameValueEntry> filters = createChildItemsFilters(item);
    int itemLevel = item.getLevel();
    int childrenLevel = itemLevel + 1;
    String childrenLevelColName = getLevelKeyColumnName(list, childrenLevel);
    NameValueEntry codeFilter = new NameValueEntry(childrenLevelColName, code);
    filters.add(codeFilter);
    Map<String, String> row = dynamicTableDao.loadRow(list.getLookupTable(), filters.toArray(new NameValueEntry[0]));
    return parseRow(row, list, childrenLevel);
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) NameValueEntry(org.openforis.collect.model.NameValueEntry)

Example 47 with CodeList

use of org.openforis.idm.metamodel.CodeList in project collect by openforis.

the class DatabaseExternalCodeListProvider method getLevelKeyColumnName.

protected String getLevelKeyColumnName(CodeAttribute codeAttribute) {
    CodeAttributeDefinition defn = codeAttribute.getDefinition();
    CodeList list = defn.getList();
    if (list.getHierarchy().isEmpty()) {
        return defn.getName();
    } else {
        int level = defn.getLevelPosition();
        return getLevelKeyColumnName(list, level);
    }
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition)

Example 48 with CodeList

use of org.openforis.idm.metamodel.CodeList in project collect by openforis.

the class DatabaseExternalCodeListProvider method hasChildItems.

public boolean hasChildItems(ExternalCodeListItem item) {
    CodeList list = item.getCodeList();
    List<NameValueEntry> filters = createChildItemsFilters(item);
    int itemLevel = item.getLevel();
    int childrenLevel = itemLevel + 1;
    String childrenKeyColName = getLevelKeyColumnName(list, childrenLevel);
    String[] notNullColumns = new String[] { childrenKeyColName };
    return dynamicTableDao.exists(list.getLookupTable(), filters.toArray(new NameValueEntry[0]), notNullColumns);
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) NameValueEntry(org.openforis.collect.model.NameValueEntry)

Example 49 with CodeList

use of org.openforis.idm.metamodel.CodeList in project collect by openforis.

the class SurveyViewGenerator method generateView.

public SurveyView generateView(final CollectSurvey survey) {
    final SurveyView surveyView = new SurveyView(survey);
    List<CodeList> codeLists = survey.getCodeLists();
    for (CodeList codeList : codeLists) {
        CodeListView codeListView = new CodeListView();
        codeListView.setId(codeList.getId());
        codeListView.setName(codeList.getName());
        codeListView.setLabel(codeList.getLabel(CodeListLabel.Type.ITEM, languageCode));
        if (includeCodeListValues && !codeList.isExternal()) {
            CodeListService service = survey.getContext().getCodeListService();
            List<CodeListItem> items = service.loadRootItems(codeList);
            for (CodeListItem item : items) {
                codeListView.addItem(createCodeListItemView(item));
            }
        }
        surveyView.addCodeList(codeListView);
    }
    final Map<Integer, NodeDefView> viewById = new HashMap<Integer, NodeDefView>();
    survey.getSchema().traverse(new NodeDefinitionVisitor() {

        public void visit(NodeDefinition def) {
            int id = def.getId();
            String name = def.getName();
            String label = getLabel(def);
            NodeDefView view;
            if (def instanceof EntityDefinition) {
                view = new EntityDefView(((EntityDefinition) def).isRoot(), id, name, label, def.isMultiple());
            } else if (def instanceof CodeAttributeDefinition) {
                CodeAttributeDefinition attrDef = (CodeAttributeDefinition) def;
                int codeListId = attrDef.getList() == null ? -1 : attrDef.getList().getId();
                view = new CodeAttributeDefView(id, name, label, AttributeType.valueOf(attrDef), attrDef.getFieldNames(), attrDef.isKey(), attrDef.isMultiple(), survey.getAnnotations().isShowInSummary(attrDef), survey.getAnnotations().isQualifier(attrDef), codeListId);
            } else {
                AttributeDefinition attrDef = (AttributeDefinition) def;
                view = new AttributeDefView(id, name, label, AttributeType.valueOf(attrDef), attrDef.getFieldNames(), attrDef.isKey(), attrDef.isMultiple(), survey.getAnnotations().isShowInSummary(attrDef), survey.getAnnotations().isQualifier(attrDef));
            }
            NodeDefinition parentDef = def.getParentDefinition();
            if (parentDef == null) {
                surveyView.getSchema().addRootEntity((EntityDefView) view);
            } else {
                EntityDefView parentView = (EntityDefView) viewById.get(parentDef.getId());
                parentView.addChild(view);
            }
            viewById.put(id, view);
        }
    });
    return surveyView;
}
Also used : HashMap(java.util.HashMap) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) CodeListService(org.openforis.idm.metamodel.CodeListService) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) NodeDefinitionVisitor(org.openforis.idm.metamodel.NodeDefinitionVisitor) CodeList(org.openforis.idm.metamodel.CodeList) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Example 50 with CodeList

use of org.openforis.idm.metamodel.CodeList in project collect by openforis.

the class CodeListManagerIntegrationTest method cascadeDeleteTest.

@Test
public void cascadeDeleteTest() {
    CodeList list = survey.getCodeList("admin_unit");
    List<CodeListItem> rootItems = codeListManager.loadItems(list, 1);
    assertEquals(8, rootItems.size());
    PersistedCodeListItem parentItem = (PersistedCodeListItem) rootItems.get(1);
    List<CodeListItem> childItems = codeListManager.loadChildItems(parentItem);
    assertEquals(3, childItems.size());
    codeListManager.delete(parentItem);
    rootItems = codeListManager.loadItems(list, 1);
    assertEquals(7, rootItems.size());
    PersistedCodeListItem fakeItem = new PersistedCodeListItem(list, parentItem.getId());
    fakeItem.setSystemId(parentItem.getSystemId());
    List<CodeListItem> reloadedItems = codeListManager.loadChildItems(fakeItem);
    assertTrue(reloadedItems.isEmpty());
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) PersistedCodeListItem(org.openforis.idm.metamodel.PersistedCodeListItem) CodeListItem(org.openforis.idm.metamodel.CodeListItem) PersistedCodeListItem(org.openforis.idm.metamodel.PersistedCodeListItem) CollectIntegrationTest(org.openforis.collect.CollectIntegrationTest) Test(org.junit.Test)

Aggregations

CodeList (org.openforis.idm.metamodel.CodeList)88 CodeListItem (org.openforis.idm.metamodel.CodeListItem)24 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)20 ArrayList (java.util.ArrayList)16 CollectSurvey (org.openforis.collect.model.CollectSurvey)15 PersistedCodeListItem (org.openforis.idm.metamodel.PersistedCodeListItem)14 Test (org.junit.Test)10 CollectIntegrationTest (org.openforis.collect.CollectIntegrationTest)10 HashMap (java.util.HashMap)7 NameValueEntry (org.openforis.collect.model.NameValueEntry)7 CodeListService (org.openforis.idm.metamodel.CodeListService)7 ExternalCodeListItem (org.openforis.idm.metamodel.ExternalCodeListItem)6 List (java.util.List)5 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)5 ModelVersion (org.openforis.idm.metamodel.ModelVersion)5 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)5 Code (org.openforis.idm.model.Code)5 Record (org.openforis.idm.model.Record)5 CodeAttributeDefinitionFormObject (org.openforis.collect.designer.form.CodeAttributeDefinitionFormObject)4 CodeListImportProcess (org.openforis.collect.manager.codelistimport.CodeListImportProcess)4