Search in sources :

Example 6 with EntityCategory

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();
}
Also used : PivotSites(org.activityinfo.legacy.shared.command.PivotSites) PivotResult(org.activityinfo.legacy.shared.command.PivotSites.PivotResult) Bucket(org.activityinfo.legacy.shared.command.result.Bucket) AdminMarker(org.activityinfo.legacy.shared.reports.content.AdminMarker) AdminDimension(org.activityinfo.legacy.shared.reports.model.AdminDimension) EntityCategory(org.activityinfo.legacy.shared.reports.content.EntityCategory)

Example 7 with EntityCategory

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();
        }
    });
}
Also used : SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) Dimension(org.activityinfo.legacy.shared.reports.model.Dimension) AdminDimension(org.activityinfo.legacy.shared.reports.model.AdminDimension) DateDimension(org.activityinfo.legacy.shared.reports.model.DateDimension) AttributeGroupDimension(org.activityinfo.legacy.shared.reports.model.AttributeGroupDimension) AttributeGroupDimension(org.activityinfo.legacy.shared.reports.model.AttributeGroupDimension) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow) EntityCategory(org.activityinfo.legacy.shared.reports.content.EntityCategory)

Example 8 with EntityCategory

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);
            }
        }
    }
}
Also used : Bucket(org.activityinfo.legacy.shared.command.result.Bucket) SiteDTO(org.activityinfo.legacy.shared.model.SiteDTO) Dimension(org.activityinfo.legacy.shared.reports.model.Dimension) AdminDimension(org.activityinfo.legacy.shared.reports.model.AdminDimension) DateDimension(org.activityinfo.legacy.shared.reports.model.DateDimension) AttributeGroupDimension(org.activityinfo.legacy.shared.reports.model.AttributeGroupDimension) EntityCategory(org.activityinfo.legacy.shared.reports.content.EntityCategory)

Example 9 with EntityCategory

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();
                }
            }
        }
    });
}
Also used : SimpleCategory(org.activityinfo.legacy.shared.reports.content.SimpleCategory) SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) SqlQuery(com.bedatadriven.rebar.sql.client.query.SqlQuery) Filter(org.activityinfo.legacy.shared.command.Filter) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow) EntityCategory(org.activityinfo.legacy.shared.reports.content.EntityCategory)

Example 10 with EntityCategory

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;
}
Also used : Bucket(org.activityinfo.legacy.shared.command.result.Bucket) ArrayList(java.util.ArrayList) EntityCategory(org.activityinfo.legacy.shared.reports.content.EntityCategory)

Aggregations

EntityCategory (org.activityinfo.legacy.shared.reports.content.EntityCategory)10 Bucket (org.activityinfo.legacy.shared.command.result.Bucket)4 AdminDimension (org.activityinfo.legacy.shared.reports.model.AdminDimension)3 Dimension (org.activityinfo.legacy.shared.reports.model.Dimension)3 SqlResultCallback (com.bedatadriven.rebar.sql.client.SqlResultCallback)2 SqlResultSet (com.bedatadriven.rebar.sql.client.SqlResultSet)2 SqlResultSetRow (com.bedatadriven.rebar.sql.client.SqlResultSetRow)2 SqlTransaction (com.bedatadriven.rebar.sql.client.SqlTransaction)2 PivotSites (org.activityinfo.legacy.shared.command.PivotSites)2 DimensionCategory (org.activityinfo.legacy.shared.reports.content.DimensionCategory)2 AttributeGroupDimension (org.activityinfo.legacy.shared.reports.model.AttributeGroupDimension)2 DateDimension (org.activityinfo.legacy.shared.reports.model.DateDimension)2 ColumnView (org.activityinfo.model.query.ColumnView)2 SqlQuery (com.bedatadriven.rebar.sql.client.query.SqlQuery)1 ArrayList (java.util.ArrayList)1 Filter (org.activityinfo.legacy.shared.command.Filter)1 GetPartnersDimension (org.activityinfo.legacy.shared.command.GetPartnersDimension)1 PivotResult (org.activityinfo.legacy.shared.command.PivotSites.PivotResult)1 PartnerResult (org.activityinfo.legacy.shared.command.result.PartnerResult)1 PartnerDTO (org.activityinfo.legacy.shared.model.PartnerDTO)1