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());
}
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);
}
}
}
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);
}
}
}
}
}
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;
}
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());
}
Aggregations