use of org.activityinfo.legacy.shared.reports.content.EntityCategory in project activityinfo by bedatadriven.
the class PolygonLayerGenerator method queryBuckets.
private void queryBuckets(DispatcherSync dispatcher, Filter layerFilter) {
PivotSites query = new PivotSites();
query.setFilter(layerFilter);
AdminDimension adminDimension = new AdminDimension(layer.getAdminLevelId());
query.setDimensions(adminDimension);
MagnitudeScaleBuilder scaleBuilder = new MagnitudeScaleBuilder(layer);
this.pivotResult = query.isTooBroad() ? new PivotResult() : dispatcher.execute(query);
for (Bucket bucket : pivotResult.getBuckets()) {
EntityCategory category = (EntityCategory) bucket.getCategory(adminDimension);
if (category != null) {
int adminEntityId = category.getId();
AdminMarker polygon = overlay.getPolygon(adminEntityId);
if (polygon != null) {
polygon.setValue(bucket.doubleValue());
scaleBuilder.addValue(bucket.doubleValue());
}
}
}
colorScale = scaleBuilder.build();
}
use of org.activityinfo.legacy.shared.reports.content.EntityCategory in project activityinfo by bedatadriven.
the class CalculatedIndicatorsQuery method queryAttributeGroups.
private void queryAttributeGroups() {
Set<Integer> groupIds = Sets.newHashSet();
for (Dimension dim : query.getDimensions()) {
if (dim instanceof AttributeGroupDimension) {
AttributeGroupDimension groupDim = (AttributeGroupDimension) dim;
groupIds.add(groupDim.getAttributeGroupId());
}
}
SqlQuery.select().appendColumn("g.attributeGroupId", "groupId").appendColumn("g.name", "groupName").appendColumn("a.attributeId").appendColumn("a.name").appendColumn("a.sortOrder").from(Tables.ATTRIBUTE, "a").leftJoin(Tables.ATTRIBUTE_GROUP, "g").on("a.attributeGroupId=g.attributeGroupId").where("a.attributeGroupId").in(groupIds).execute(queryContext.getExecutionContext().getTransaction(), new SqlResultCallback() {
@Override
public void onSuccess(SqlTransaction tx, SqlResultSet results) {
for (SqlResultSetRow row : results.getRows()) {
int groupId = row.getInt("groupId");
int attributeId = row.getInt("attributeId");
int sortOrder = row.getInt("sortOrder");
String attributeName = row.getString("name");
attributes.put(groupId, new EntityCategory(attributeId, attributeName, sortOrder));
}
querySites();
}
});
}
use of org.activityinfo.legacy.shared.reports.content.EntityCategory in project activityinfo by bedatadriven.
the class CalculatedIndicatorsQuery method aggregateSites.
private void aggregateSites(SiteResult result) {
for (int i = 0; i != result.getTotalLength(); ++i) {
SiteDTO site = result.getData().get(i);
// Now loop over each value
for (EntityCategory indicator : indicatorMap.values()) {
Double value = site.getIndicatorDoubleValue(indicator.getId());
if (value != null && !Double.isNaN(value) && !Double.isInfinite(value)) {
Bucket bucket = new Bucket(value);
bucket.setAggregationMethod(indicatorAggregationMap.get(indicator.getId()));
bucket.setCategory(new Dimension(DimensionType.Target), TargetCategory.REALIZED);
if (query.isPivotedBy(DimensionType.Indicator)) {
bucket.setCategory(new Dimension(DimensionType.Indicator), indicator);
}
for (int j = 0; j != dimAccessors.size(); ++j) {
bucket.setCategory(dimAccessors.get(j).getDimension(), dimAccessors.get(j).getCategory(site));
}
queryContext.addBucket(bucket);
}
}
}
}
use of org.activityinfo.legacy.shared.reports.content.EntityCategory in project activityinfo by bedatadriven.
the class CalculatedIndicatorsQuery method execute.
@Override
public void execute(final AsyncCallback<Void> callback) {
this.callback = callback;
final SqlQuery query = SqlQuery.selectDistinct().appendColumn("i.indicatorId", "indicatorId").appendColumn("i.name", "indicatorName").appendColumn("i.activityId", "activityId").appendColumn("i.sortOrder", "indicatorOrder").appendColumn("i.aggregation", "aggregation").appendColumn("a.name", "activityName").appendColumn("a.category", "activityCategory").appendColumn("a.sortOrder", "activityOrder").appendColumn("db.DatabaseId", "databaseId").appendColumn("db.name", "databaseName").from(Tables.INDICATOR, "i").leftJoin(Tables.ACTIVITY, "a").on("a.activityId=i.activityId").leftJoin(Tables.USER_DATABASE, "db").on("a.databaseId=db.databaseId").whereTrue("i.calculatedAutomatically=1 and i.Expression is not null");
Filter filter = this.query.getFilter();
if (filter.isRestricted(DimensionType.Indicator)) {
query.where("i.indicatorId").in(filter.getRestrictions(DimensionType.Indicator));
} else if (filter.isRestricted(DimensionType.Activity)) {
query.where("i.activityId").in(filter.getRestrictions(DimensionType.Activity));
} else if (filter.isRestricted(DimensionType.Database)) {
query.where("a.databaseId").in(filter.getRestrictions(DimensionType.Database));
} else {
// too broad
callback.onSuccess(null);
return;
}
// enforce visibility rules
query.whereTrue(visibilityRules());
query.execute(queryContext.getExecutionContext().getTransaction(), new SqlResultCallback() {
@Override
public void onSuccess(SqlTransaction tx, SqlResultSet results) {
if (results.getRows().isEmpty()) {
callback.onSuccess(null);
} else {
boolean hasSumAggregation = false;
for (SqlResultSetRow row : results.getRows()) {
LOGGER.finer("row = " + row);
int activityId = row.getInt("activityId");
int indicatorId = row.getInt("indicatorId");
activityIds.add(activityId);
indicatorIds.add(indicatorId);
activityMap.put(activityId, new EntityCategory(activityId, row.getString("activityName"), row.getInt("activityOrder")));
activityCategoryMap.put(activityId, new SimpleCategory(row.getString("activityCategory")));
activityToDatabaseMap.put(activityId, new EntityCategory(row.getInt("databaseId"), row.getString("databaseName")));
indicatorMap.put(indicatorId, new EntityCategory(indicatorId, row.getString("indicatorName"), row.getInt("indicatorOrder")));
indicatorAggregationMap.put(indicatorId, row.getInt("aggregation"));
if (!hasSumAggregation) {
hasSumAggregation = row.getInt("aggregation") == 0;
}
}
// set aggregation to Sum for all indicators if at least one indicator has different aggregation
if (hasSumAggregation) {
for (Map.Entry<Integer, Integer> entry : indicatorAggregationMap.entrySet()) {
// set to Sum
entry.setValue(0);
}
}
if (queryContext.getCommand().isPivotedBy(DimensionType.AttributeGroup)) {
queryAttributeGroups();
} else {
querySites();
}
}
}
});
}
use of org.activityinfo.legacy.shared.reports.content.EntityCategory in project activityinfo by bedatadriven.
the class DrillDownProxy method toRows.
private List<DrillDownRow> toRows(PivotSites.PivotResult result) {
List<DrillDownRow> rows = new ArrayList<>();
for (Bucket bucket : result.getBuckets()) {
DrillDownRow row = new DrillDownRow(getEntity(bucket, siteDimension).getId());
row.set("partner", getEntity(bucket, partnerDimension).getLabel());
EntityCategory location = getEntity(bucket, locationDimension);
if (location != null) {
row.set("location", location.getLabel());
}
row.set("date", getDate(bucket));
row.set("database", getEntity(bucket, databaseDimension).getLabel());
row.set("activity", getEntity(bucket, actvitiyDimension).getLabel());
row.set("indicator", getEntity(bucket, indicatorDimension).getLabel());
row.set("value", bucket.doubleValue());
rows.add(row);
}
return rows;
}
Aggregations