Search in sources :

Example 51 with CodeListItem

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

the class CodeListImportProcessIntegrationTest method testImport.

@Test
public void testImport() throws Exception {
    CodeList codeList = survey.createCodeList();
    codeList.setName(TEST_CODE_LIST_NAME);
    survey.addCodeList(codeList);
    CodeListImportProcess process = importCSVFile(VALID_TEST_CSV, codeList);
    CodeListImportStatus status = process.getStatus();
    assertTrue(status.isComplete());
    assertTrue(status.getSkippedRows().isEmpty());
    assertEquals(6, status.getProcessed());
    List<CodeListItem> items = codeListManager.loadRootItems(codeList);
    assertEquals(3, items.size());
    {
        CodeListItem item = codeListManager.loadRootItem(codeList, "001", null);
        assertNotNull(item);
        assertEquals("Dodoma", item.getLabel(LANG));
        List<CodeListItem> childItems = codeListManager.loadChildItems(item);
        assertEquals(2, childItems.size());
        CodeListItem childItem = childItems.get(0);
        assertEquals("001", childItem.getCode());
        assertEquals("Kondoa", childItem.getLabel(LANG));
        childItem = childItems.get(1);
        assertEquals("002", childItem.getCode());
        assertEquals("Mpwapwa", childItem.getLabel(LANG));
    }
    {
        CodeListItem item = codeListManager.loadRootItem(codeList, "002", null);
        assertNotNull(item);
        assertEquals("Arusha", item.getLabel(LANG));
        List<CodeListItem> childItems = codeListManager.loadChildItems(item);
        assertEquals(2, childItems.size());
        CodeListItem childItem = childItems.get(0);
        assertEquals("001", childItem.getCode());
        assertEquals("Monduli", childItem.getLabel(LANG));
        childItem = childItems.get(1);
        assertEquals("002", childItem.getCode());
        assertEquals("Arumeru", childItem.getLabel(LANG));
    }
    {
        CodeListItem item = codeListManager.loadRootItem(codeList, "003", null);
        assertNotNull(item);
    }
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) CodeListImportStatus(org.openforis.collect.manager.codelistimport.CodeListImportStatus) CodeList(org.openforis.idm.metamodel.CodeList) List(java.util.List) CodeListItem(org.openforis.idm.metamodel.CodeListItem) CodeListImportProcess(org.openforis.collect.manager.codelistimport.CodeListImportProcess) CollectIntegrationTest(org.openforis.collect.CollectIntegrationTest) Test(org.junit.Test)

Example 52 with CodeListItem

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

the class DataService method findAssignableCodeListItems.

/**
 * Finds a list of code list items assignable to the specified attribute and matching the passed codes
 *
 * @param parentEntityId
 * @param attributeName
 * @param codes
 * @return
 */
@Secured(USER)
public List<CodeListItemProxy> findAssignableCodeListItems(int parentEntityId, String attributeName, String[] codes) {
    CollectRecord record = getActiveRecord();
    Entity parent = (Entity) record.getNodeByInternalId(parentEntityId);
    CodeAttributeDefinition def = (CodeAttributeDefinition) parent.getDefinition().getChildDefinition(attributeName);
    List<CodeListItem> items = codeListManager.findValidItems(parent, def, codes);
    List<CodeListItemProxy> result = new ArrayList<CodeListItemProxy>();
    for (CodeListItem item : items) {
        result.add(new CodeListItemProxy(item));
    }
    return result;
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) Entity(org.openforis.idm.model.Entity) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) ArrayList(java.util.ArrayList) CodeListItem(org.openforis.idm.metamodel.CodeListItem) CodeListItemProxy(org.openforis.collect.metamodel.proxy.CodeListItemProxy) Secured(org.springframework.security.access.annotation.Secured)

Example 53 with CodeListItem

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

the class CodeListController method loadAvailableItems.

@RequestMapping(value = "survey/{surveyId}/codelist/{codeListId}", method = GET)
@ResponseBody
public List<CodeListItemView> loadAvailableItems(@PathVariable Integer surveyId, @PathVariable Integer codeListId, @RequestParam Integer recordId, @RequestParam Integer parentEntityId, @RequestParam Integer codeAttrDefId) {
    CollectSurvey survey = surveyManager.getOrLoadSurveyById(surveyId);
    CollectRecord record = recordManager.load(survey, recordId, false);
    Entity parentEntity = (Entity) record.getNodeByInternalId(parentEntityId);
    CodeAttributeDefinition codeAttrDef = (CodeAttributeDefinition) survey.getSchema().getDefinitionById(codeAttrDefId);
    List<CodeListItem> items = codeListManager.loadValidItems(parentEntity, codeAttrDef);
    return toViews(items);
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) Entity(org.openforis.idm.model.Entity) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) CollectSurvey(org.openforis.collect.model.CollectSurvey) CodeListItem(org.openforis.idm.metamodel.CodeListItem) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 54 with CodeListItem

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

the class CodeListImportTask method getCodeListItemInDescendants.

protected CodeListItem getCodeListItemInDescendants(String code) {
    Deque<CodeListItem> stack = new LinkedList<CodeListItem>();
    stack.addAll(codeToRootItem.values());
    while (!stack.isEmpty()) {
        CodeListItem item = stack.pop();
        if (item.matchCode(code)) {
            return item;
        } else {
            stack.addAll(item.getChildItems());
        }
    }
    return null;
}
Also used : CodeListItem(org.openforis.idm.metamodel.CodeListItem) LinkedList(java.util.LinkedList)

Example 55 with CodeListItem

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

the class CodeListImportTask method isDuplicate.

/**
 * Returns when:
 * not is leaf but has different label than existing node with same code
 * or is leaf and:
 * - LOCAL scope and exist item with same code at the same level
 * - SCHEME scope and exist item with same code in some level
 * @param code
 * @param parentItem
 * @param lastLevel
 * @return
 */
protected boolean isDuplicate(String code, CodeListItem parentItem) {
    CodeListItem duplicateItem;
    duplicateItem = getChildItem(parentItem, code);
    return duplicateItem != null;
}
Also used : CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Aggregations

CodeListItem (org.openforis.idm.metamodel.CodeListItem)69 CodeList (org.openforis.idm.metamodel.CodeList)26 ArrayList (java.util.ArrayList)19 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)19 PersistedCodeListItem (org.openforis.idm.metamodel.PersistedCodeListItem)18 CodeListService (org.openforis.idm.metamodel.CodeListService)13 List (java.util.List)7 CollectSurvey (org.openforis.collect.model.CollectSurvey)7 ModelVersion (org.openforis.idm.metamodel.ModelVersion)7 Test (org.junit.Test)5 CollectIntegrationTest (org.openforis.collect.CollectIntegrationTest)5 ExternalCodeListItem (org.openforis.idm.metamodel.ExternalCodeListItem)5 Entity (org.openforis.idm.model.Entity)5 Record (org.openforis.idm.model.Record)5 CodeListItemProxy (org.openforis.collect.metamodel.proxy.CodeListItemProxy)4 CollectRecord (org.openforis.collect.model.CollectRecord)4 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)4 CodeAttribute (org.openforis.idm.model.CodeAttribute)4 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)3