Search in sources :

Example 1 with RecordSummary

use of org.openforis.collect.model.RecordSummary in project collect by openforis.

the class RecordSummaryDao method mapRecordsToSummaries.

private List<RecordSummary> mapRecordsToSummaries(List<Record> records, List<AttributeDefinition> keyDefs, List<EntityDefinition> countable) {
    List<RecordSummary> result = new ArrayList<RecordSummary>();
    for (Record record : records) {
        Integer id = record.getValueAsInteger(RECORD.ID);
        // String createdBy = r.getValueAsString(USER_MODIFIED_BY_ALIAS);
        String createdBy = null;
        Date dateCreated = record.getValueAsDate(DATE_CREATED_ALIAS);
        // String modifiedBy = r.getValueAsString(USER_CREATED_BY_ALIAS);
        String modifiedBy = null;
        Date modifiedDate = record.getValueAsDate(DATE_MODIFIED_ALIAS);
        Integer step = record.getValueAsInteger(RECORD.STEP);
        Integer warnings = record.getValueAsInteger(RECORD.WARNINGS);
        Integer errors = record.getValueAsInteger(RECORD.ERRORS);
        Integer skipped = record.getValueAsInteger(RECORD.SKIPPED);
        Integer missing = record.getValueAsInteger(RECORD.MISSING);
        // create count map
        Map<String, Integer> entityCounts = new HashMap<String, Integer>();
        for (EntityDefinition def : countable) {
            String alias = COUNT_ALIAS_PREFIX + def.getName();
            String key = def.getName();
            Integer value = record.getValueAsInteger(alias);
            entityCounts.put(key, value);
        }
        // create key attributes map
        Map<String, String> keyAttributes = new HashMap<String, String>();
        for (AttributeDefinition attributeDefinition : keyDefs) {
            String projectionAlias = KEY_ALIAS_PREFIX + attributeDefinition.getName();
            String key = attributeDefinition.getName();
            Object value = record.getValue(projectionAlias);
            String valueStr = value != null ? value.toString() : "";
            keyAttributes.put(key, valueStr);
        }
        RecordSummary recordSummary = new RecordSummary(id, keyAttributes, entityCounts, createdBy, dateCreated, modifiedBy, modifiedDate, step, skipped, missing, errors, warnings);
        result.add(recordSummary);
    }
    return result;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) Date(java.sql.Date) RecordSummary(org.openforis.collect.model.RecordSummary) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Record(org.jooq.Record)

Example 2 with RecordSummary

use of org.openforis.collect.model.RecordSummary in project collect by openforis.

the class RecordSummaryDao method load.

@Transactional
public List<RecordSummary> load(EntityDefinition rootEntityDefinition, List<EntityDefinition> countable, int offset, int maxRecords, String orderByField, String filter) {
    List<AttributeDefinition> keyDefs = rootEntityDefinition.getKeyAttributeDefinitions();
    Factory jf = getJooqFactory();
    org.openforis.collect.persistence.jooq.tables.Record r = RECORD.as("r");
    SelectQuery q = jf.selectQuery();
    q.addFrom(r);
    q.addSelect(r.DATE_CREATED.as(DATE_CREATED_ALIAS), r.DATE_MODIFIED.as(DATE_MODIFIED_ALIAS), r.ERRORS, r.ID, r.LOCKED_BY_ID, r.MISSING, r.MODEL_VERSION, r.MODIFIED_BY_ID, r.ROOT_ENTITY_ID, r.SKIPPED, r.STATE, r.STEP, r.WARNINGS);
    {
        // add keys to select with an alias like KEY_ALIAS_PREFIX + ATTRIBUTE_NAME
        int position = 1;
        for (AttributeDefinition def : keyDefs) {
            String alias = KEY_ALIAS_PREFIX + def.getName();
            Field<?> field = RecordDaoUtil.getKeyField(r, def, position).as(alias);
            q.addSelect(field);
            position++;
        }
    }
    {
        // add count columns to select with an alias like COUNT_ALIAS_PREFIX + ENTITY_NAME
        int position = 1;
        for (EntityDefinition def : countable) {
            String alias = COUNT_ALIAS_PREFIX + def.getName();
            Field<?> field = RecordDaoUtil.getCountField(r, position).as(alias);
            q.addSelect(field);
            position++;
        }
    }
    // add order by condition
    Field<?> orderBy = null;
    if (orderByField != null) {
        List<Field<?>> selectFields = q.getSelect();
        for (Field<?> field : selectFields) {
            if (orderByField.equals(field.getName())) {
                orderBy = field;
                break;
            }
        }
    }
    if (orderBy != null) {
        q.addOrderBy(orderBy);
    }
    // always order by ID to avoid pagination issues
    q.addOrderBy(r.ID);
    // add limit
    q.addLimit(offset, maxRecords);
    // fetch results
    Result<Record> records = q.fetch();
    List<RecordSummary> result = mapRecordsToSummaries(records, keyDefs, countable);
    return result;
}
Also used : AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) Factory(org.jooq.impl.Factory) SelectQuery(org.jooq.SelectQuery) RecordSummary(org.openforis.collect.model.RecordSummary) Field(org.jooq.Field) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Record(org.jooq.Record) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Record (org.jooq.Record)2 RecordSummary (org.openforis.collect.model.RecordSummary)2 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)2 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)2 Date (java.sql.Date)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Field (org.jooq.Field)1 SelectQuery (org.jooq.SelectQuery)1 Factory (org.jooq.impl.Factory)1 Transactional (org.springframework.transaction.annotation.Transactional)1