Search in sources :

Example 1 with Bucket

use of org.activityinfo.legacy.shared.command.result.Bucket in project activityinfo by bedatadriven.

the class GetPartnersDimensionHandler method execute.

@Override
public void execute(GetPartnersDimension cmd, ExecutionContext context, final AsyncCallback<PartnerResult> callback) {
    final Dimension dimension = new Dimension(DimensionType.Partner);
    PivotSites query = new PivotSites();
    query.setFilter(cmd.getFilter());
    query.setDimensions(dimension);
    query.setValueType(ValueType.TOTAL_SITES);
    if (query.isTooBroad()) {
        callback.onSuccess(new PartnerResult());
        return;
    }
    context.execute(query, new AsyncCallback<PivotSites.PivotResult>() {

        @Override
        public void onSuccess(PivotSites.PivotResult result) {
            Set<PartnerDTO> partners = new HashSet<PartnerDTO>();
            for (Bucket bucket : result.getBuckets()) {
                EntityCategory category = (EntityCategory) bucket.getCategory(dimension);
                if (category == null) {
                    Log.debug("Partner is null: " + bucket.toString());
                } else {
                    PartnerDTO partner = new PartnerDTO();
                    partner.setId(category.getId());
                    partner.setName(category.getLabel());
                    partners.add(partner);
                }
            }
            // sort partners by name (fallback on id)
            List<PartnerDTO> list = new ArrayList<PartnerDTO>(partners);
            Collections.sort(list, new Comparator<PartnerDTO>() {

                @Override
                public int compare(PartnerDTO p1, PartnerDTO p2) {
                    int result = p1.getName().compareToIgnoreCase(p2.getName());
                    if (result != 0) {
                        return result;
                    } else {
                        return ((Integer) p1.getId()).compareTo(p2.getId());
                    }
                }
            });
            callback.onSuccess(new PartnerResult(list));
        }

        @Override
        public void onFailure(Throwable caught) {
            callback.onFailure(caught);
        }
    });
}
Also used : PartnerDTO(org.activityinfo.legacy.shared.model.PartnerDTO) GetPartnersDimension(org.activityinfo.legacy.shared.command.GetPartnersDimension) Dimension(org.activityinfo.legacy.shared.reports.model.Dimension) PivotSites(org.activityinfo.legacy.shared.command.PivotSites) Bucket(org.activityinfo.legacy.shared.command.result.Bucket) EntityCategory(org.activityinfo.legacy.shared.reports.content.EntityCategory) PartnerResult(org.activityinfo.legacy.shared.command.result.PartnerResult)

Example 2 with Bucket

use of org.activityinfo.legacy.shared.command.result.Bucket in project activityinfo by bedatadriven.

the class PivotQuery method execute.

@Override
public void execute(final AsyncCallback<Void> callback) {
    baseTable.setupQuery(command, query);
    if (command.isPivotedBy(DimensionType.Location) || command.isPivotedBy(DimensionType.Site)) {
        query.leftJoin(Tables.LOCATION, "Location").on("Location.LocationId=" + baseTable.getDimensionIdColumn(DimensionType.Location));
    }
    if (command.isPivotedBy(DimensionType.Partner)) {
        query.leftJoin(Tables.PARTNER, "Partner").on("Partner.PartnerId=" + baseTable.getDimensionIdColumn(DimensionType.Partner));
    }
    if (command.isPivotedBy(DimensionType.Project)) {
        SqlQuery activeProjects = SqlQuery.selectAll().from(Tables.PROJECT, "AllProjects").where("AllProjects.dateDeleted").isNull();
        query.leftJoin(activeProjects, "Project").on("Project.ProjectId=" + baseTable.getDimensionIdColumn(DimensionType.Project));
    }
    addDimensionBundlers();
    // otherwise permissions have already been taken into account during synchronization
    if (isRemote()) {
        appendVisibilityFilter();
    }
    if (filter.getEndDateRange().getMinDate() != null) {
        query.where(baseTable.getDateCompleteColumn()).greaterThanOrEqualTo(filter.getEndDateRange().getMinDate());
    }
    if (filter.getEndDateRange().getMaxDate() != null) {
        query.where(baseTable.getDateCompleteColumn()).lessThanOrEqualTo(filter.getEndDateRange().getMaxDate());
    }
    appendDimensionRestrictions();
    Log.debug("PivotQuery (" + baseTable.getClass() + ") executing query: " + query.sql());
    query.execute(tx, new SqlResultCallback() {

        @Override
        public void onSuccess(SqlTransaction tx, SqlResultSet results) {
            for (SqlResultSetRow row : results.getRows()) {
                Bucket bucket = new Bucket();
                bucket.setAggregationMethod(row.getInt(ValueFields.AGGREGATION));
                bucket.setCount(row.getInt(ValueFields.COUNT));
                if (!row.isNull(ValueFields.SUM)) {
                    bucket.setSum(row.getDouble(ValueFields.SUM));
                }
                bucket.setCategory(new Dimension(DimensionType.Target), baseTable.getTargetCategory());
                for (Bundler bundler : bundlers) {
                    bundler.bundle(row, bucket);
                }
                context.addBucket(bucket);
            }
            callback.onSuccess(null);
        }
    });
}
Also used : SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) SqlQuery(com.bedatadriven.rebar.sql.client.query.SqlQuery) Bucket(org.activityinfo.legacy.shared.command.result.Bucket) 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) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow)

Example 3 with Bucket

use of org.activityinfo.legacy.shared.command.result.Bucket 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 4 with Bucket

use of org.activityinfo.legacy.shared.command.result.Bucket in project activityinfo by bedatadriven.

the class CustomerCalcIndicatorTest method fullPivot.

private PivotSites fullPivot(int activityId) {
    PivotSites pivot = new PivotSites();
    pivot.setDimensions(new Dimension(DimensionType.Indicator));
    pivot.setFilter(Filter.filter().onActivity(activityId));
    List<Bucket> buckets = execute(pivot).getBuckets();
    System.out.println(Joiner.on("\n").join(buckets));
    assertThat(buckets, hasItem(total("Expenditure", 225000)));
    assertThat(buckets, hasItem(total("Expenditure on water programme", 48500)));
    assertThat(buckets, hasItem(total("Value of Initial Cost - Not specified", 24250)));
    assertThat(buckets, hasItem(total("Value of Initial Cost - Cap Hard", 9700)));
    assertThat(buckets, hasItem(total("Value of Initial Cost – Cap Soft", 14550)));
    assertThat(buckets, hasItem(total("Total Value of Initial Cost", 48500)));
    return pivot;
}
Also used : Bucket(org.activityinfo.legacy.shared.command.result.Bucket) Dimension(org.activityinfo.legacy.shared.reports.model.Dimension) DateDimension(org.activityinfo.legacy.shared.reports.model.DateDimension) AttributeGroupDimension(org.activityinfo.legacy.shared.reports.model.AttributeGroupDimension)

Example 5 with Bucket

use of org.activityinfo.legacy.shared.command.result.Bucket in project activityinfo by bedatadriven.

the class PivotSitesHandlerTest method execute.

private void execute(int userId) {
    setUser(userId);
    try {
        PivotSites pivot = new PivotSites(dimensions, filter);
        pivot.setValueType(valueType);
        buckets = execute(pivot).getBuckets();
    } catch (CommandException e) {
        throw new RuntimeException(e);
    }
    System.out.println("Buckets = [");
    for (Bucket bucket : buckets) {
        System.out.print("  { Value: " + bucket.doubleValue());
        for (Dimension dim : bucket.dimensions()) {
            DimensionCategory cat = bucket.getCategory(dim);
            System.out.print("\n    " + dim.toString() + ": ");
            System.out.print("" + cat);
        }
        System.out.println("\n  }");
    }
    System.out.print("]\n");
}
Also used : Bucket(org.activityinfo.legacy.shared.command.result.Bucket) CommandException(org.activityinfo.legacy.shared.exception.CommandException)

Aggregations

Bucket (org.activityinfo.legacy.shared.command.result.Bucket)13 Dimension (org.activityinfo.legacy.shared.reports.model.Dimension)7 AdminDimension (org.activityinfo.legacy.shared.reports.model.AdminDimension)5 PivotSites (org.activityinfo.legacy.shared.command.PivotSites)4 EntityCategory (org.activityinfo.legacy.shared.reports.content.EntityCategory)4 AttributeGroupDimension (org.activityinfo.legacy.shared.reports.model.AttributeGroupDimension)4 ArrayList (java.util.ArrayList)3 DateDimension (org.activityinfo.legacy.shared.reports.model.DateDimension)3 PivotResult (org.activityinfo.legacy.shared.command.PivotSites.PivotResult)2 PivotTableData (org.activityinfo.legacy.shared.reports.content.PivotTableData)2 PivotTableReportElement (org.activityinfo.legacy.shared.reports.model.PivotTableReportElement)2 DispatcherSync (org.activityinfo.server.command.DispatcherSync)2 User (org.activityinfo.server.database.hibernate.entity.User)2 Test (org.junit.Test)2 SqlResultCallback (com.bedatadriven.rebar.sql.client.SqlResultCallback)1 SqlResultSet (com.bedatadriven.rebar.sql.client.SqlResultSet)1 SqlResultSetRow (com.bedatadriven.rebar.sql.client.SqlResultSetRow)1 SqlTransaction (com.bedatadriven.rebar.sql.client.SqlTransaction)1 SqlQuery (com.bedatadriven.rebar.sql.client.query.SqlQuery)1 GetPartnersDimension (org.activityinfo.legacy.shared.command.GetPartnersDimension)1