use of org.openforis.idm.metamodel.ExternalCodeListItem in project collect by openforis.
the class DatabaseExternalCodeListProvider method visitChildItems.
@Override
public void visitChildItems(final ExternalCodeListItem item, final Visitor<CodeListItem> visitor) {
final CodeList list = item.getCodeList();
int itemLevel = item.getLevel();
final int childrenLevel = itemLevel + 1;
if (childrenLevel > list.getHierarchy().size()) {
return;
}
List<NameValueEntry> filters = createChildItemsFilters(item);
String childrenKeyColName = getLevelKeyColumnName(list, childrenLevel);
String[] notNullColumns = new String[] { childrenKeyColName };
dynamicTableDao.visitRows(list.getLookupTable(), filters.toArray(new NameValueEntry[filters.size()]), notNullColumns, new Visitor<Map<String, String>>() {
public void visit(Map<String, String> row) {
ExternalCodeListItem item = parseRow(row, list, childrenLevel);
visitor.visit(item);
}
});
}
use of org.openforis.idm.metamodel.ExternalCodeListItem in project collect by openforis.
the class DatabaseExternalCodeListProvider method getItem.
@Override
public ExternalCodeListItem getItem(CodeAttribute attribute) {
CodeAttributeDefinition defn = attribute.getDefinition();
CodeList list = defn.getList();
List<NameValueEntry> filters = new ArrayList<NameValueEntry>();
addSurveyFilter(list, filters);
CodeAttribute codeParent = attribute.getCodeParent();
while (codeParent != null) {
String colName = getLevelKeyColumnName(codeParent);
String codeValue = getCodeValue(codeParent);
filters.add(new NameValueEntry(colName, codeValue));
codeParent = codeParent.getCodeParent();
}
String colName = getLevelKeyColumnName(attribute);
String codeValue = getCodeValue(attribute);
filters.add(new NameValueEntry(colName, codeValue));
int level = defn.getLevelPosition();
List<NameValueEntry> emptyNextLevelsFilters = createEmptyNextLevelFilters(list, level);
filters.addAll(emptyNextLevelsFilters);
Map<String, String> row = dynamicTableDao.loadRow(list.getLookupTable(), filters.toArray(new NameValueEntry[filters.size()]));
if (row == null) {
return null;
} else {
ExternalCodeListItem result = parseRow(row, list, level);
return result;
}
}
use of org.openforis.idm.metamodel.ExternalCodeListItem in project collect by openforis.
the class ExternalCodeListIntegrationTest method getChildItemsTest.
@Test
public void getChildItemsTest() throws SQLException {
List<ExternalCodeListItem> firstLevelItems = codeListManager.loadRootItems(hierarchicalList);
ExternalCodeListItem parent = firstLevelItems.get(0);
List<ExternalCodeListItem> childItems = codeListManager.loadChildItems(parent);
assertEquals(2, childItems.size());
{
ExternalCodeListItem item = childItems.get(0);
assertEquals("011", item.getCode());
assertEquals("Code 1-1", item.getLabel(EN_LANG_CODE));
}
{
ExternalCodeListItem item = childItems.get(1);
assertEquals("012", item.getCode());
assertEquals("Code 1-2", item.getLabel(EN_LANG_CODE));
}
}
use of org.openforis.idm.metamodel.ExternalCodeListItem in project collect by openforis.
the class ExternalCodeValidator method evaluate.
@Override
public ValidationResultFlag evaluate(CodeAttribute codeAttribute) {
if (codeAttribute.getSurvey().getId() == null) {
return ValidationResultFlag.OK;
}
ExternalCodeListProvider externalCodeListProvider = getExternalCodeListProvider(codeAttribute);
ExternalCodeListItem item = externalCodeListProvider.getItem(codeAttribute);
if (item == null || item.getCode() == null || !item.getCode().equals(codeAttribute.getValue().getCode())) {
if (isUnlistedAllowed(codeAttribute)) {
return ValidationResultFlag.WARNING;
} else {
return ValidationResultFlag.ERROR;
}
} else {
return ValidationResultFlag.OK;
}
}
use of org.openforis.idm.metamodel.ExternalCodeListItem 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);
}
}
}
Aggregations