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