Search in sources :

Example 41 with CodeList

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

the class CodeListItemDao method updateSortOrder.

protected void updateSortOrder(PersistedCodeListItem item, int newSortOrder) {
    CodeList list = item.getCodeList();
    CollectSurvey survey = (CollectSurvey) list.getSurvey();
    JooqDSLContext dsl = dsl(list);
    Integer oldSortOrder = item.getSortOrder();
    if (newSortOrder == oldSortOrder) {
        return;
    } else {
        // give top sort order to the item
        dsl.update(OFC_CODE_LIST).set(OFC_CODE_LIST.SORT_ORDER, 0).where(OFC_CODE_LIST.ID.equal(item.getSystemId())).execute();
        Condition parentIdCondition = item.getParentId() == null ? OFC_CODE_LIST.PARENT_ID.isNull() : OFC_CODE_LIST.PARENT_ID.equal(item.getParentId());
        if (newSortOrder > oldSortOrder) {
            // move backwards previous items
            dsl.update(OFC_CODE_LIST).set(OFC_CODE_LIST.SORT_ORDER, OFC_CODE_LIST.SORT_ORDER.sub(1)).where(OFC_CODE_LIST.SURVEY_ID.equal(survey.getId()), OFC_CODE_LIST.CODE_LIST_ID.equal(list.getId()), parentIdCondition, OFC_CODE_LIST.SORT_ORDER.greaterThan(oldSortOrder), OFC_CODE_LIST.SORT_ORDER.lessOrEqual(newSortOrder)).execute();
        } else {
            // move forward next items
            dsl.update(OFC_CODE_LIST).set(OFC_CODE_LIST.SORT_ORDER, OFC_CODE_LIST.SORT_ORDER.add(1)).where(OFC_CODE_LIST.SURVEY_ID.equal(survey.getId()), OFC_CODE_LIST.CODE_LIST_ID.equal(list.getId()), parentIdCondition, OFC_CODE_LIST.SORT_ORDER.greaterOrEqual(newSortOrder), OFC_CODE_LIST.SORT_ORDER.lessThan(oldSortOrder)).execute();
        }
        // set item sort order to final value
        dsl.update(OFC_CODE_LIST).set(OFC_CODE_LIST.SORT_ORDER, newSortOrder).where(OFC_CODE_LIST.ID.equal(item.getSystemId())).execute();
        if (isCacheInUse(list)) {
            cache.clearItemsByCodeList(list);
        }
    }
}
Also used : Condition(org.jooq.Condition) CodeList(org.openforis.idm.metamodel.CodeList) CollectSurvey(org.openforis.collect.model.CollectSurvey)

Example 42 with CodeList

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

the class CodeListItemDao method removeLabels.

public void removeLabels(CollectSurvey survey, int fromLanguagePosition) {
    JooqDSLContext jf = dsl(null);
    List<CodeList> codeLists = survey.getCodeLists();
    for (CodeList codeList : codeLists) {
        int codeListId = codeList.getId();
        Map<TableField<OfcCodeListRecord, String>, String> updateFields = new HashMap<TableField<OfcCodeListRecord, String>, String>();
        for (int i = fromLanguagePosition - 1; i < LABEL_FIELDS.length; i++) {
            @SuppressWarnings("unchecked") TableField<OfcCodeListRecord, String> labelField = LABEL_FIELDS[i];
            updateFields.put(labelField, (String) null);
        }
        jf.update(OFC_CODE_LIST).set(updateFields).where(OFC_CODE_LIST.SURVEY_ID.eq(survey.getId()).and(OFC_CODE_LIST.CODE_LIST_ID.eq(codeListId))).execute();
        if (isCacheInUse(codeList)) {
            cache.clearItemsByCodeList(codeList);
        }
    }
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) HashMap(java.util.HashMap) OfcCodeListRecord(org.openforis.collect.persistence.jooq.tables.records.OfcCodeListRecord) TableField(org.jooq.TableField)

Example 43 with CodeList

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

the class DatabaseExternalCodeListProvider method createChildItemsFilters.

protected List<NameValueEntry> createChildItemsFilters(ExternalCodeListItem item) {
    List<NameValueEntry> filters = new ArrayList<NameValueEntry>();
    CodeList list = item.getCodeList();
    Collection<NameValueEntry> parentKeys = getParentKeys(item);
    filters.addAll(parentKeys);
    addSurveyFilter(list, filters);
    int itemLevel = item.getLevel();
    int childrenLevel = itemLevel + 1;
    String itemKeyColName = getLevelKeyColumnName(list, itemLevel);
    filters.add(new NameValueEntry(itemKeyColName, item.getCode()));
    List<NameValueEntry> emptyNextLevelsFilters = createEmptyNextLevelFilters(list, childrenLevel);
    filters.addAll(emptyNextLevelsFilters);
    return filters;
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) ArrayList(java.util.ArrayList) NameValueEntry(org.openforis.collect.model.NameValueEntry)

Example 44 with CodeList

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

the class DatabaseExternalCodeListProvider method getParentItem.

public ExternalCodeListItem getParentItem(ExternalCodeListItem item) {
    List<NameValueEntry> filters = new ArrayList<NameValueEntry>();
    CodeList list = item.getCodeList();
    Collection<NameValueEntry> parentKeys = getParentKeys(item);
    filters.addAll(parentKeys);
    addSurveyFilter(list, filters);
    int level = parentKeys.size() + 1;
    int parentLevel = level - 1;
    List<NameValueEntry> emptyNextLevelsFilters = createEmptyNextLevelFilters(list, parentLevel);
    filters.addAll(emptyNextLevelsFilters);
    Map<String, String> row = dynamicTableDao.loadRow(list.getLookupTable(), filters.toArray(new NameValueEntry[0]));
    return parseRow(row, list, parentLevel);
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) ArrayList(java.util.ArrayList) NameValueEntry(org.openforis.collect.model.NameValueEntry)

Example 45 with CodeList

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

the class DatabaseExternalCodeListProvider method getChildItems.

@Override
public List<ExternalCodeListItem> getChildItems(ExternalCodeListItem item) {
    CodeList list = item.getCodeList();
    int itemLevel = item.getLevel();
    int childrenLevel = itemLevel + 1;
    if (childrenLevel > list.getHierarchy().size()) {
        return Collections.emptyList();
    }
    List<NameValueEntry> filters = createChildItemsFilters(item);
    String childrenKeyColName = getLevelKeyColumnName(list, childrenLevel);
    String[] notNullColumns = new String[] { childrenKeyColName };
    List<Map<String, String>> rows = dynamicTableDao.loadRows(list.getLookupTable(), filters.toArray(new NameValueEntry[filters.size()]), notNullColumns);
    List<ExternalCodeListItem> result = new ArrayList<ExternalCodeListItem>();
    for (Map<String, String> row : rows) {
        ExternalCodeListItem child = parseRow(row, list, childrenLevel);
        result.add(child);
    }
    return result;
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) ExternalCodeListItem(org.openforis.idm.metamodel.ExternalCodeListItem) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) NameValueEntry(org.openforis.collect.model.NameValueEntry)

Aggregations

CodeList (org.openforis.idm.metamodel.CodeList)88 CodeListItem (org.openforis.idm.metamodel.CodeListItem)24 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)20 ArrayList (java.util.ArrayList)16 CollectSurvey (org.openforis.collect.model.CollectSurvey)15 PersistedCodeListItem (org.openforis.idm.metamodel.PersistedCodeListItem)14 Test (org.junit.Test)10 CollectIntegrationTest (org.openforis.collect.CollectIntegrationTest)10 HashMap (java.util.HashMap)7 NameValueEntry (org.openforis.collect.model.NameValueEntry)7 CodeListService (org.openforis.idm.metamodel.CodeListService)7 ExternalCodeListItem (org.openforis.idm.metamodel.ExternalCodeListItem)6 List (java.util.List)5 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)5 ModelVersion (org.openforis.idm.metamodel.ModelVersion)5 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)5 Code (org.openforis.idm.model.Code)5 Record (org.openforis.idm.model.Record)5 CodeAttributeDefinitionFormObject (org.openforis.collect.designer.form.CodeAttributeDefinitionFormObject)4 CodeListImportProcess (org.openforis.collect.manager.codelistimport.CodeListImportProcess)4