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