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);
}
}
});
}
}
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);
}
});
}
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();
}
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();
}
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);
}
}
});
}
Aggregations