Search in sources :

Example 71 with CodeList

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

the class CodeListManager method loadItemByAttribute.

@SuppressWarnings("unchecked")
public <T extends CodeListItem> T loadItemByAttribute(CodeAttribute attribute) {
    CodeAttributeDefinition defn = attribute.getDefinition();
    CodeList list = defn.getList();
    boolean persistedSurvey = list.getSurvey().getId() != null;
    if (persistedSurvey && list.isExternal()) {
        return (T) provider.getItem(attribute);
    } else if (persistedSurvey && list.isEmpty()) {
        return (T) loadPersistedItem(attribute);
    } else {
        return (T) getInternalCodeListItem(attribute);
    }
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition)

Example 72 with CodeList

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

the class CodeListImagesImportTask method execute.

@Override
protected void execute() throws Throwable {
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        String entryName = entry.getName();
        if (CodeListImageEntry.isValidEntry(entryName)) {
            CodeListImageEntry codeListImageEntry = CodeListImageEntry.parseEntryName(entryName);
            CodeList codeList = survey.getCodeListById(codeListImageEntry.getListId());
            if (codeList != null) {
                CodeListItem item = codeListManager.loadItem(codeList, codeListImageEntry.getItemId());
                if (item != null && item instanceof PersistedCodeListItem) {
                    byte[] content = IOUtils.toByteArray(zipFile.getInputStream(entry));
                    codeListManager.saveImageContent((PersistedCodeListItem) item, new FileWrapper(content, codeListImageEntry.getFileName()));
                } else {
                    log().warn("Error restoring code list image file : " + entry.getName());
                }
            }
        }
    }
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) ZipEntry(java.util.zip.ZipEntry) FileWrapper(org.openforis.collect.model.FileWrapper) PersistedCodeListItem(org.openforis.idm.metamodel.PersistedCodeListItem) CodeListItem(org.openforis.idm.metamodel.CodeListItem) PersistedCodeListItem(org.openforis.idm.metamodel.PersistedCodeListItem)

Example 73 with CodeList

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

the class CodeListItemPR method createItem.

protected void createItem(int id, int level) {
    CodeList list = getCodeList();
    this.item = list.createItem(id, level);
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList)

Example 74 with CodeList

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

the class CodeListImportProcessIntegrationTest method testDuplicateValues.

@Test
public void testDuplicateValues() throws Exception {
    CodeList codeList = survey.createCodeList();
    codeList.setName(TEST_CODE_LIST_NAME);
    survey.addCodeList(codeList);
    CodeListImportProcess process = importCSVFile(INVALID_TEST_CSV, codeList);
    CodeListImportStatus status = process.getStatus();
    assertTrue(status.isError());
    List<ParsingError> errors = status.getErrors();
    // different label
    assertTrue(containsError(errors, 4, "region_label_en"));
    assertTrue(containsError(errors, 4, "district_code"));
    assertTrue(containsError(errors, 7, "district_code"));
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) ParsingError(org.openforis.collect.io.metadata.parsing.ParsingError) CodeListImportStatus(org.openforis.collect.manager.codelistimport.CodeListImportStatus) CodeListImportProcess(org.openforis.collect.manager.codelistimport.CodeListImportProcess) CollectIntegrationTest(org.openforis.collect.CollectIntegrationTest) Test(org.junit.Test)

Example 75 with CodeList

use of org.openforis.idm.metamodel.CodeList 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)

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