Search in sources :

Example 6 with PersistedCodeListItem

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

the class CodeListItemDaoIntegrationTest method moveItemInPersistedListTest.

@Test
public void moveItemInPersistedListTest() {
    CodeList list = survey.getCodeList("admin_unit");
    List<PersistedCodeListItem> items = codeListItemDao.loadRootItems(list);
    List<String> codes = getCodes(items);
    assertEquals(Arrays.asList("001", "002", "003", "004", "005", "006", "007", "008"), codes);
    PersistedCodeListItem item = items.get(2);
    assertEquals(Integer.valueOf(3), item.getSortOrder());
    codeListItemDao.shiftItem(item, 0);
    List<PersistedCodeListItem> items2 = codeListItemDao.loadRootItems(list);
    List<String> codes2 = getCodes(items2);
    assertEquals(Arrays.asList("003", "001", "002", "004", "005", "006", "007", "008"), codes2);
    PersistedCodeListItem item2 = items2.get(0);
    assertEquals(Integer.valueOf(1), item2.getSortOrder());
    codeListItemDao.shiftItem(item2, 4);
    List<PersistedCodeListItem> items3 = codeListItemDao.loadRootItems(list);
    List<String> codes3 = getCodes(items3);
    assertEquals(Arrays.asList("001", "002", "004", "005", "003", "006", "007", "008"), codes3);
    PersistedCodeListItem item3 = items3.get(4);
    assertEquals(Integer.valueOf(5), item3.getSortOrder());
    codeListItemDao.shiftItem(item3, 8);
    List<PersistedCodeListItem> items4 = codeListItemDao.loadRootItems(list);
    List<String> codes4 = getCodes(items4);
    assertEquals(Arrays.asList("001", "002", "004", "005", "006", "007", "008", "003"), codes4);
    PersistedCodeListItem item4 = items4.get(7);
    assertEquals(Integer.valueOf(8), item4.getSortOrder());
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) PersistedCodeListItem(org.openforis.idm.metamodel.PersistedCodeListItem) CollectIntegrationTest(org.openforis.collect.CollectIntegrationTest) Test(org.junit.Test)

Example 7 with PersistedCodeListItem

use of org.openforis.idm.metamodel.PersistedCodeListItem 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 8 with PersistedCodeListItem

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

the class CodeListImagesExportTask method execute.

@Override
protected void execute() throws Throwable {
    List<CodeList> codeLists = survey.getCodeLists();
    for (CodeList list : codeLists) {
        if (!list.isExternal()) {
            Deque<CodeListItem> stack = new LinkedList<CodeListItem>();
            List<CodeListItem> rootItems = codeListManager.loadRootItems(list);
            stack.addAll(rootItems);
            while (!stack.isEmpty()) {
                if (!isRunning()) {
                    break;
                }
                CodeListItem item = stack.pop();
                if (item instanceof PersistedCodeListItem && item.hasUploadedImage()) {
                    FileWrapper imageFileWrapper = codeListManager.loadImageContent((PersistedCodeListItem) item);
                    ZipEntry entry = new ZipEntry(getEntryName(item));
                    zipOutputStream.putNextEntry(entry);
                    IOUtils.write(imageFileWrapper.getContent(), zipOutputStream);
                    zipOutputStream.closeEntry();
                }
                List<CodeListItem> childItems = codeListManager.loadChildItems(item);
                for (CodeListItem childItem : childItems) {
                    stack.push(childItem);
                }
            }
        }
    }
}
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) LinkedList(java.util.LinkedList) PersistedCodeListItem(org.openforis.idm.metamodel.PersistedCodeListItem)

Example 9 with PersistedCodeListItem

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

the class CodeListItemDao method loadItem.

public PersistedCodeListItem loadItem(CodeList codeList, Integer parentItemId, String code, ModelVersion version) {
    boolean usingCache = isCacheInUse(codeList);
    if (usingCache) {
        PersistedCodeListItem item = cache.getItem(codeList, parentItemId, code, version);
        if (item == null) {
            // update cache
            loadChildItems(codeList, parentItemId, version);
            item = cache.getItem(codeList, parentItemId, code, version);
        }
        if (item != null) {
            return item;
        }
    }
    JooqDSLContext jf = dsl(codeList);
    SelectQuery<Record> q = createSelectChildItemsQuery(jf, codeList, parentItemId, false);
    q.addConditions(OFC_CODE_LIST.CODE.equal(code));
    Result<Record> result = q.fetch();
    List<PersistedCodeListItem> list = jf.fromResult(result);
    List<PersistedCodeListItem> filteredByVersion = filterApplicableItems(list, version);
    PersistedCodeListItem item = filteredByVersion.isEmpty() ? null : filteredByVersion.get(0);
    return item;
}
Also used : OfcCodeListRecord(org.openforis.collect.persistence.jooq.tables.records.OfcCodeListRecord) Record(org.jooq.Record) PersistedCodeListItem(org.openforis.idm.metamodel.PersistedCodeListItem)

Example 10 with PersistedCodeListItem

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

PersistedCodeListItem (org.openforis.idm.metamodel.PersistedCodeListItem)18 CodeList (org.openforis.idm.metamodel.CodeList)12 CodeListItem (org.openforis.idm.metamodel.CodeListItem)8 Test (org.junit.Test)5 CollectIntegrationTest (org.openforis.collect.CollectIntegrationTest)5 OfcCodeListRecord (org.openforis.collect.persistence.jooq.tables.records.OfcCodeListRecord)3 ExternalCodeListItem (org.openforis.idm.metamodel.ExternalCodeListItem)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 ZipEntry (java.util.zip.ZipEntry)2 Record (org.jooq.Record)2 FileWrapper (org.openforis.collect.model.FileWrapper)2 ModelVersion (org.openforis.idm.metamodel.ModelVersion)2 Code (org.openforis.idm.model.Code)2 Record (org.openforis.idm.model.Record)2 List (java.util.List)1 BatchBindStep (org.jooq.BatchBindStep)1 CollectSurvey (org.openforis.collect.model.CollectSurvey)1 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)1 SurveyObject (org.openforis.idm.metamodel.SurveyObject)1