Search in sources :

Example 1 with Bundler

use of org.activityinfo.shared.command.handler.pivot.bundler.Bundler 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);
        }
    });
}
Also used : SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) SqlQuery(com.bedatadriven.rebar.sql.client.query.SqlQuery) Bucket(org.activityinfo.shared.command.result.Bucket) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) Dimension(org.activityinfo.shared.report.model.Dimension) AttributeGroupDimension(org.activityinfo.shared.report.model.AttributeGroupDimension) AdminDimension(org.activityinfo.shared.report.model.AdminDimension) DateDimension(org.activityinfo.shared.report.model.DateDimension) PointBundler(org.activityinfo.shared.command.handler.pivot.bundler.PointBundler) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow) QuarterBundler(org.activityinfo.shared.command.handler.pivot.bundler.QuarterBundler) OrderedEntityBundler(org.activityinfo.shared.command.handler.pivot.bundler.OrderedEntityBundler) PointBundler(org.activityinfo.shared.command.handler.pivot.bundler.PointBundler) Bundler(org.activityinfo.shared.command.handler.pivot.bundler.Bundler) MySqlYearWeekBundler(org.activityinfo.shared.command.handler.pivot.bundler.MySqlYearWeekBundler) YearBundler(org.activityinfo.shared.command.handler.pivot.bundler.YearBundler) MonthBundler(org.activityinfo.shared.command.handler.pivot.bundler.MonthBundler) AttributeBundler(org.activityinfo.shared.command.handler.pivot.bundler.AttributeBundler) SimpleBundler(org.activityinfo.shared.command.handler.pivot.bundler.SimpleBundler) EntityBundler(org.activityinfo.shared.command.handler.pivot.bundler.EntityBundler)

Aggregations

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 AttributeBundler (org.activityinfo.shared.command.handler.pivot.bundler.AttributeBundler)1 Bundler (org.activityinfo.shared.command.handler.pivot.bundler.Bundler)1 EntityBundler (org.activityinfo.shared.command.handler.pivot.bundler.EntityBundler)1 MonthBundler (org.activityinfo.shared.command.handler.pivot.bundler.MonthBundler)1 MySqlYearWeekBundler (org.activityinfo.shared.command.handler.pivot.bundler.MySqlYearWeekBundler)1 OrderedEntityBundler (org.activityinfo.shared.command.handler.pivot.bundler.OrderedEntityBundler)1 PointBundler (org.activityinfo.shared.command.handler.pivot.bundler.PointBundler)1 QuarterBundler (org.activityinfo.shared.command.handler.pivot.bundler.QuarterBundler)1 SimpleBundler (org.activityinfo.shared.command.handler.pivot.bundler.SimpleBundler)1 YearBundler (org.activityinfo.shared.command.handler.pivot.bundler.YearBundler)1 Bucket (org.activityinfo.shared.command.result.Bucket)1 AdminDimension (org.activityinfo.shared.report.model.AdminDimension)1 AttributeGroupDimension (org.activityinfo.shared.report.model.AttributeGroupDimension)1 DateDimension (org.activityinfo.shared.report.model.DateDimension)1 Dimension (org.activityinfo.shared.report.model.Dimension)1