Search in sources :

Example 1 with PivotSites

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

the class PivotTableBinder method executeLoad.

@Override
protected void executeLoad(final AsyncCallback<PivotContent> callback) {
    final Filter filter = new Filter(getModel().getFilter());
    final Set<Dimension> dimensions = new HashSet<>(getModel().allDimensions());
    if (filter.equals(cachedFilter) && dimensions.equals(cachedDimensions)) {
        try {
            callback.onSuccess(generateElement());
        } catch (Exception e) {
            callback.onFailure(e);
        }
    } else {
        PivotSites query = new PivotSites(dimensions, filter);
        if (query.isTooBroad()) {
            callback.onSuccess(new PivotContent());
            return;
        }
        dispatcher.execute(query, new AsyncCallback<PivotSites.PivotResult>() {

            @Override
            public void onFailure(Throwable throwable) {
                callback.onFailure(throwable);
            }

            @Override
            public void onSuccess(PivotSites.PivotResult pivotResult) {
                cachedFilter = filter;
                cachedDimensions = dimensions;
                cachedResult = pivotResult;
                try {
                    callback.onSuccess(generateElement());
                } catch (Exception e) {
                    callback.onFailure(e);
                }
            }
        });
    }
}
Also used : PivotSites(org.activityinfo.legacy.shared.command.PivotSites) Filter(org.activityinfo.legacy.shared.command.Filter) PivotContent(org.activityinfo.legacy.shared.reports.content.PivotContent) Dimension(org.activityinfo.legacy.shared.reports.model.Dimension) HashSet(java.util.HashSet)

Example 2 with PivotSites

use of org.activityinfo.legacy.shared.command.PivotSites 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 3 with PivotSites

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

the class CubeResource method pivot.

@GET
@Timed(name = "api.rest.sites.pivot")
@Produces("application/json")
public List<Bucket> pivot(@QueryParam("dimension") List<String> dimensions, @QueryParam("form") List<Integer> forms, @QueryParam("month") String monthName) {
    Filter filter = new Filter();
    if (forms.size() == 0) {
        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Must specify at least one ?form={formId}").build());
    }
    filter.addRestriction(DimensionType.Activity, forms);
    if (monthName != null) {
        Month month = Month.parseMonth(monthName);
        filter.setEndDateRange(new DateUtilCalendarImpl().monthRange(month));
    }
    Set<Dimension> pivotDimensions = Sets.newHashSet();
    if (forms.size() == 0) {
        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Must specify at least one ?dimension={indicator|form|database|...}").build());
    }
    for (String dimension : dimensions) {
        switch(dimension) {
            case "indicator":
                pivotDimensions.add(new Dimension(DimensionType.Indicator));
                break;
            case "site":
                pivotDimensions.add(new Dimension(DimensionType.Site));
                break;
            case "month":
                pivotDimensions.add(new DateDimension(DateUnit.MONTH));
                break;
            case "partner":
                pivotDimensions.add(new Dimension(DimensionType.Partner));
                break;
            default:
                throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Invalid dimension '" + dimension + "'").build());
        }
    }
    PivotSites query = new PivotSites();
    query.setFilter(filter);
    query.setDimensions(pivotDimensions);
    if (query.isTooBroad()) {
        return Lists.newArrayList();
    }
    PivotSites.PivotResult result = dispatcherSync.execute(query);
    return result.getBuckets();
}
Also used : Month(org.activityinfo.model.type.time.Month) PivotSites(org.activityinfo.legacy.shared.command.PivotSites) WebApplicationException(javax.ws.rs.WebApplicationException) Filter(org.activityinfo.legacy.shared.command.Filter) DateUtilCalendarImpl(org.activityinfo.server.report.util.DateUtilCalendarImpl) Dimension(org.activityinfo.legacy.shared.reports.model.Dimension) DateDimension(org.activityinfo.legacy.shared.reports.model.DateDimension) DateDimension(org.activityinfo.legacy.shared.reports.model.DateDimension) Produces(javax.ws.rs.Produces) Timed(org.activityinfo.server.util.monitoring.Timed) GET(javax.ws.rs.GET)

Example 4 with PivotSites

use of org.activityinfo.legacy.shared.command.PivotSites 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 5 with PivotSites

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

the class DrillDownProxy method load.

@Override
protected void load(Object loadConfig, final AsyncCallback<List<DrillDownRow>> callback) {
    PivotSites query = new PivotSites(dims, filter);
    if (query.isTooBroad()) {
        callback.onSuccess(Lists.<DrillDownRow>newArrayList());
        return;
    }
    dispatcher.execute(query, new AsyncCallback<PivotSites.PivotResult>() {

        @Override
        public void onFailure(Throwable caught) {
            callback.onFailure(caught);
        }

        @Override
        public void onSuccess(PivotSites.PivotResult result) {
            try {
                callback.onSuccess(toRows(result));
            } catch (Throwable caught) {
                Log.error(caught.getMessage(), caught);
                callback.onFailure(caught);
            }
        }
    });
}
Also used : PivotSites(org.activityinfo.legacy.shared.command.PivotSites)

Aggregations

PivotSites (org.activityinfo.legacy.shared.command.PivotSites)6 Dimension (org.activityinfo.legacy.shared.reports.model.Dimension)3 Filter (org.activityinfo.legacy.shared.command.Filter)2 PivotResult (org.activityinfo.legacy.shared.command.PivotSites.PivotResult)2 Bucket (org.activityinfo.legacy.shared.command.result.Bucket)2 EntityCategory (org.activityinfo.legacy.shared.reports.content.EntityCategory)2 HashSet (java.util.HashSet)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 GetPartnersDimension (org.activityinfo.legacy.shared.command.GetPartnersDimension)1 PartnerResult (org.activityinfo.legacy.shared.command.result.PartnerResult)1 PivotTableDataBuilder (org.activityinfo.legacy.shared.impl.pivot.PivotTableDataBuilder)1 PartnerDTO (org.activityinfo.legacy.shared.model.PartnerDTO)1 AdminMarker (org.activityinfo.legacy.shared.reports.content.AdminMarker)1 PivotContent (org.activityinfo.legacy.shared.reports.content.PivotContent)1 AdminDimension (org.activityinfo.legacy.shared.reports.model.AdminDimension)1 DateDimension (org.activityinfo.legacy.shared.reports.model.DateDimension)1 Month (org.activityinfo.model.type.time.Month)1 DateUtilCalendarImpl (org.activityinfo.server.report.util.DateUtilCalendarImpl)1