use of org.activityinfo.shared.command.handler.pivot.bundler.PointBundler in project activityinfo by bedatadriven.
the class PivotQuery method execute.
public void execute(final AsyncCallback<Void> callback) {
baseTable.setupQuery(command, query);
if (command.isPivotedBy(DimensionType.Location) || command.isPointRequested()) {
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));
}
if (command.isPointRequested()) {
if (command.isPivotedBy(DimensionType.Location)) {
query.appendColumn("Location.X", "LX");
query.appendColumn("Location.Y", "LY");
} else {
query.appendColumn("AVG(Location.X)", "LX");
query.appendColumn("AVG(Location.Y)", "LY");
}
// Build the derived table that identifies the MBR for each
// location using the admin MBRs
SqlQuery adminBoundsQuery = SqlQuery.select().appendColumn("link.LocationId", "LocationId").appendColumn("(MAX(X1)+MIN(X2))/2.0", "AX").appendColumn("(MAX(Y1)+MIN(Y2))/2.0", "AY").from(Tables.LOCATION_ADMIN_LINK, "link").leftJoin(Tables.ADMIN_ENTITY, "e").on("link.adminentityid=e.adminentityid").groupBy("link.locationid");
query.leftJoin(adminBoundsQuery, "ambr").on("Location.LocationId=ambr.LocationId");
query.appendColumn("ambr.AX", "AX");
query.appendColumn("ambr.AY", "AY");
System.out.println(adminBoundsQuery.sql());
bundlers.add(new PointBundler());
}
addDimensionBundlers();
// otherwise permissions have already been taken into account during synchronization
if (isRemote()) {
appendVisibilityFilter();
}
if (filter.getMinDate() != null) {
query.where(baseTable.getDateCompleteColumn()).greaterThanOrEqualTo(filter.getMinDate());
}
if (filter.getMaxDate() != null) {
query.where(baseTable.getDateCompleteColumn()).lessThanOrEqualTo(filter.getMaxDate());
}
appendDimensionRestrictions();
if (Log.isDebugEnabled()) {
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);
}
});
}
Aggregations