use of org.activityinfo.server.command.QueryFilter in project activityinfo by bedatadriven.
the class PivotAdapter method executeTargetValuesQuery.
private void executeTargetValuesQuery(Integer databaseId) {
if (filter.isRestricted(DimensionType.AttributeGroup) || filter.isRestricted(DimensionType.AdminLevel) || filter.isRestricted(DimensionType.Site) || filter.isRestricted(DimensionType.Location)) {
// No results, exit now
return;
}
ResourceId targetFormClassId = CuidAdapter.cuid(CuidAdapter.TARGET_FORM_CLASS_DOMAIN, databaseId);
QueryModel queryModel = new QueryModel(targetFormClassId);
QueryFilter queryFilter = new QueryFilter(filter, attributeFilters, LOGGER);
queryModel.setFilter(queryFilter.composeTargetFilter());
final Collection<Activity> activities = databases.get(databaseId);
// Add all indicators we're querying for
for (Activity activity : activities) {
for (ActivityField indicator : selectedIndicators(activity)) {
queryModel.selectField(targetFieldExpr(indicator)).as(alias(indicator));
}
}
for (DimBinding dimension : groupBy) {
for (ColumnModel columnModel : dimension.getTargetColumnQuery(targetFormClassId)) {
queryModel.addColumn(columnModel);
}
}
enqueueQuery(queryModel, new Function<ColumnSet, Void>() {
@Nullable
@Override
public Void apply(@Nullable ColumnSet columnSet) {
for (Activity activity : activities) {
for (ActivityField indicator : selectedIndicators(activity)) {
ColumnView measureView = columnSet.getColumnView(alias(indicator));
DimensionCategory indicatorCategory = null;
if (indicatorDimension.isPresent()) {
indicatorCategory = indicatorDimension.get().category(indicator);
}
for (int i = 0; i < columnSet.getNumRows(); i++) {
double value = measureView.getDouble(i);
if (!Double.isNaN(value) && !Double.isInfinite(value)) {
Map<Dimension, DimensionCategory> key = new HashMap<>();
for (DimBinding dim : groupBy) {
DimensionCategory category = dim.extractTargetCategory(activity, columnSet, i);
if (category != null) {
key.put(dim.getModel(), category);
}
}
if (indicatorCategory != null) {
key.put(indicatorDimension.get().getModel(), indicatorCategory);
}
Accumulator bucket = bucketForKey(key, indicator.getAggregation());
bucket.addValue(value);
}
}
}
}
return null;
}
});
}
Aggregations