Search in sources :

Example 1 with ExternalCodeListItem

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);
        }
    });
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) ExternalCodeListItem(org.openforis.idm.metamodel.ExternalCodeListItem) HashMap(java.util.HashMap) Map(java.util.Map) NameValueEntry(org.openforis.collect.model.NameValueEntry)

Example 2 with ExternalCodeListItem

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;
    }
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) ExternalCodeListItem(org.openforis.idm.metamodel.ExternalCodeListItem) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) CodeAttribute(org.openforis.idm.model.CodeAttribute) ArrayList(java.util.ArrayList) NameValueEntry(org.openforis.collect.model.NameValueEntry)

Example 3 with ExternalCodeListItem

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));
    }
}
Also used : ExternalCodeListItem(org.openforis.idm.metamodel.ExternalCodeListItem) CollectIntegrationTest(org.openforis.collect.CollectIntegrationTest) Test(org.junit.Test)

Example 4 with ExternalCodeListItem

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;
    }
}
Also used : ExternalCodeListItem(org.openforis.idm.metamodel.ExternalCodeListItem) ExternalCodeListProvider(org.openforis.idm.metamodel.ExternalCodeListProvider)

Example 5 with ExternalCodeListItem

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

Aggregations

ExternalCodeListItem (org.openforis.idm.metamodel.ExternalCodeListItem)9 ArrayList (java.util.ArrayList)4 NameValueEntry (org.openforis.collect.model.NameValueEntry)4 CodeList (org.openforis.idm.metamodel.CodeList)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Test (org.junit.Test)2 CollectIntegrationTest (org.openforis.collect.CollectIntegrationTest)2 CodeAttribute (org.openforis.idm.model.CodeAttribute)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1 CollectSurvey (org.openforis.collect.model.CollectSurvey)1 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)1 ExternalCodeListProvider (org.openforis.idm.metamodel.ExternalCodeListProvider)1 ModelVersion (org.openforis.idm.metamodel.ModelVersion)1 PersistedCodeListItem (org.openforis.idm.metamodel.PersistedCodeListItem)1 Survey (org.openforis.idm.metamodel.Survey)1 Code (org.openforis.idm.model.Code)1 Record (org.openforis.idm.model.Record)1