Search in sources :

Example 1 with CodeList

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

the class CodeListItemDao method insert.

/**
 * Inserts the items in batch.
 *
 * @param items
 */
public void insert(List<PersistedCodeListItem> items) {
    if (items != null && items.size() > 0) {
        PersistedCodeListItem firstItem = items.get(0);
        CodeList list = firstItem.getCodeList();
        JooqDSLContext jf = dsl(list);
        int nextId = jf.nextId();
        int maxId = nextId;
        Insert<OfcCodeListRecord> query = jf.createInsertStatement();
        BatchBindStep batch = jf.batch(query);
        for (PersistedCodeListItem item : items) {
            Integer id = item.getSystemId();
            if (id == null) {
                id = nextId++;
                item.setSystemId(id);
            }
            List<Object> values = jf.extractValues(item);
            batch.bind(values.toArray(new Object[values.size()]));
            maxId = Math.max(maxId, id);
        }
        batch.execute();
        jf.restartSequence(maxId + 1);
    }
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) BatchBindStep(org.jooq.BatchBindStep) SurveyObject(org.openforis.idm.metamodel.SurveyObject) OfcCodeListRecord(org.openforis.collect.persistence.jooq.tables.records.OfcCodeListRecord) PersistedCodeListItem(org.openforis.idm.metamodel.PersistedCodeListItem)

Example 2 with CodeList

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

the class CodeListItemDao method shiftItem.

public void shiftItem(PersistedCodeListItem item, int toIndex) {
    CodeList list = item.getCodeList();
    List<PersistedCodeListItem> siblings = loadChildItems(list, item.getParentId());
    int newSortOrder;
    int prevItemIdx;
    if (toIndex >= siblings.size()) {
        prevItemIdx = siblings.size() - 1;
    } else {
        prevItemIdx = toIndex;
    }
    PersistedCodeListItem previousItem = siblings.get(prevItemIdx);
    newSortOrder = previousItem.getSortOrder();
    updateSortOrder(item, newSortOrder);
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList) PersistedCodeListItem(org.openforis.idm.metamodel.PersistedCodeListItem)

Example 3 with CodeList

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

the class CodeListItemDao method delete.

public void delete(PersistedCodeListItem item) {
    CodeList codeList = item.getCodeList();
    JooqDSLContext dsl = dsl(null);
    dsl.deleteQuery(item.getSystemId()).execute();
    if (dsl.getDialect() == SQLDialect.SQLITE) {
        // SQLite foreign keys support disabled in order to have better performances
        // delete referencing items programmatically
        deleteInvalidParentReferenceItems(codeList);
    }
    if (isCacheInUse(codeList)) {
        cache.clearItemsByCodeList(codeList);
    }
}
Also used : CodeList(org.openforis.idm.metamodel.CodeList)

Example 4 with CodeList

use of org.openforis.idm.metamodel.CodeList 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 5 with CodeList

use of org.openforis.idm.metamodel.CodeList 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)

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