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