Search in sources :

Example 1 with Factory

use of org.jooq.impl.Factory 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

Field (org.jooq.Field)1 Record (org.jooq.Record)1 SelectQuery (org.jooq.SelectQuery)1 Factory (org.jooq.impl.Factory)1 RecordSummary (org.openforis.collect.model.RecordSummary)1 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)1 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)1 Transactional (org.springframework.transaction.annotation.Transactional)1