Search in sources :

Example 11 with SqlTransaction

use of com.bedatadriven.rebar.sql.client.SqlTransaction in project activityinfo by bedatadriven.

the class GetDimensionLabelsHandler method execute.

@Override
public void execute(GetDimensionLabels command, ExecutionContext context, final AsyncCallback<DimensionLabels> callback) {
    SqlQuery query = composeQuery(command);
    query.execute(context.getTransaction(), new SqlResultCallback() {

        @Override
        public void onSuccess(SqlTransaction tx, SqlResultSet results) {
            Map<Integer, String> labels = Maps.newHashMap();
            for (SqlResultSetRow row : results.getRows()) {
                labels.put(row.getInt("id"), row.getString("name"));
            }
            callback.onSuccess(new DimensionLabels(labels));
        }
    });
}
Also used : SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) SqlQuery(com.bedatadriven.rebar.sql.client.query.SqlQuery) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow) DimensionLabels(org.activityinfo.legacy.shared.command.GetDimensionLabels.DimensionLabels) GetDimensionLabels(org.activityinfo.legacy.shared.command.GetDimensionLabels) Map(java.util.Map)

Example 12 with SqlTransaction

use of com.bedatadriven.rebar.sql.client.SqlTransaction 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);
        }
    });
}
Also used : SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) SqlQuery(com.bedatadriven.rebar.sql.client.query.SqlQuery) Bucket(org.activityinfo.legacy.shared.command.result.Bucket) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) Dimension(org.activityinfo.legacy.shared.reports.model.Dimension) AdminDimension(org.activityinfo.legacy.shared.reports.model.AdminDimension) DateDimension(org.activityinfo.legacy.shared.reports.model.DateDimension) AttributeGroupDimension(org.activityinfo.legacy.shared.reports.model.AttributeGroupDimension) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow)

Example 13 with SqlTransaction

use of com.bedatadriven.rebar.sql.client.SqlTransaction in project activityinfo by bedatadriven.

the class CommandQueueTest method testUpdateSite.

@Test
public void testUpdateSite() {
    Map<String, Object> changes = new HashMap<String, Object>();
    changes.put("anInt", 34);
    changes.put("aString", "testing");
    final UpdateSite cmd = new UpdateSite(99, changes);
    db.transaction(new SqlTransactionCallback() {

        @Override
        public void begin(SqlTransaction tx) {
            queue.queue(tx, cmd);
        }
    });
    Collector<CommandQueue.QueueEntry> reread = Collector.newCollector();
    queue.peek(reread);
    assertThat(reread.getResult(), not(nullValue()));
    assertThat(cmd, equalTo(reread.getResult().getCommand()));
}
Also used : SqlTransactionCallback(com.bedatadriven.rebar.sql.client.SqlTransactionCallback) HashMap(java.util.HashMap) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) UpdateSite(org.activityinfo.legacy.shared.command.UpdateSite) Test(org.junit.Test)

Example 14 with SqlTransaction

use of com.bedatadriven.rebar.sql.client.SqlTransaction in project activityinfo by bedatadriven.

the class CommandQueueTest method testCreateSite.

@Test
public void testCreateSite() {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("anInt", 34);
    properties.put("aString", "testing");
    properties.put("aDouble", 3.0);
    properties.put("aBoolean", true);
    properties.put("anotherBoolean", false);
    properties.put("aTime", new Date());
    properties.put("aDate", new LocalDate(2011, 3, 15));
    final CreateSite cmd = new CreateSite(properties);
    db.transaction(new SqlTransactionCallback() {

        @Override
        public void begin(SqlTransaction tx) {
            queue.queue(tx, cmd);
        }
    });
    Collector<CommandQueue.QueueEntry> reread = Collector.newCollector();
    queue.peek(reread);
    assertThat(reread.getResult(), not(nullValue()));
    assertThat(cmd, equalTo(reread.getResult().getCommand()));
    Collector<Void> deleted = Collector.newCollector();
    queue.remove(reread.getResult(), deleted);
    Collector<CommandQueue.QueueEntry> entry2 = Collector.newCollector();
    queue.peek(entry2);
    assertThat(entry2.getResult(), is(nullValue()));
}
Also used : SqlTransactionCallback(com.bedatadriven.rebar.sql.client.SqlTransactionCallback) HashMap(java.util.HashMap) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) LocalDate(com.bedatadriven.rebar.time.calendar.LocalDate) Date(java.util.Date) LocalDate(com.bedatadriven.rebar.time.calendar.LocalDate) CreateSite(org.activityinfo.legacy.shared.command.CreateSite) Test(org.junit.Test)

Example 15 with SqlTransaction

use of com.bedatadriven.rebar.sql.client.SqlTransaction in project activityinfo by bedatadriven.

the class OldGetSitesHandler method doQuery.

private void doQuery(final GetSites command, final ExecutionContext context, final AsyncCallback<SiteResult> callback) {
    // in order to pull in the linked queries, we want to
    // to create two queries that we union together.
    // for performance reasons, we want to apply all of the joins
    // and filters on both parts of the union query
    SqlQuery unioned;
    if (command.isFetchLinks()) {
        unioned = unionedQuery(context, command);
        unioned.appendAllColumns();
    } else {
        unioned = primaryQuery(context, command);
    }
    if (isMySql() && command.getLimit() >= 0) {
        // with this feature, MySQL will keep track of the total
        // number of rows regardless of our limit statement.
        // This way we don't have to execute the query twice to
        // get the total count
        // 
        // unfortunately, this is not available on sqlite
        unioned.appendKeyword("SQL_CALC_FOUND_ROWS");
    }
    applySort(unioned, command.getSortInfo());
    applyPaging(unioned, command);
    final Multimap<Integer, SiteDTO> siteMap = HashMultimap.create();
    final List<SiteDTO> sites = new ArrayList<SiteDTO>();
    final Map<Integer, SiteDTO> reportingPeriods = Maps.newHashMap();
    final SiteResult result = new SiteResult(sites);
    result.setOffset(command.getOffset());
    Log.trace("About to execute primary query: " + unioned.sql());
    unioned.execute(context.getTransaction(), new SqlResultCallback() {

        @Override
        public void onSuccess(SqlTransaction tx, SqlResultSet results) {
            Log.trace("Primary query returned " + results.getRows().size() + ", rows, starting to add to map");
            for (SqlResultSetRow row : results.getRows()) {
                SiteDTO site = toSite(command, row);
                sites.add(site);
                siteMap.put(site.getId(), site);
                if (command.isFetchAllReportingPeriods()) {
                    reportingPeriods.put(row.getInt("PeriodId"), site);
                }
            }
            Log.trace("Finished adding to map");
            List<Promise<Void>> queries = Lists.newArrayList();
            if (command.getLimit() <= 0) {
                result.setTotalLength(results.getRows().size());
            } else {
                queries.add(queryTotalLength(tx, command, context, result));
            }
            if (!sites.isEmpty()) {
                if (command.isFetchAdminEntities()) {
                    queries.add(joinEntities(tx, siteMap));
                }
                if (command.isFetchAttributes()) {
                    queries.add(joinAttributeValues(command, tx, siteMap));
                }
                if (command.fetchAnyIndicators()) {
                    queries.add(joinIndicatorValues(command, tx, siteMap, reportingPeriods));
                }
            }
            Promise.waitAll(queries).then(Functions.constant(result)).then(callback);
        }
    });
}
Also used : SqlQuery(com.bedatadriven.rebar.sql.client.query.SqlQuery) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow) SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) SiteResult(org.activityinfo.legacy.shared.command.result.SiteResult) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback)

Aggregations

SqlTransaction (com.bedatadriven.rebar.sql.client.SqlTransaction)27 SqlResultCallback (com.bedatadriven.rebar.sql.client.SqlResultCallback)23 SqlResultSet (com.bedatadriven.rebar.sql.client.SqlResultSet)23 SqlResultSetRow (com.bedatadriven.rebar.sql.client.SqlResultSetRow)18 SqlQuery (com.bedatadriven.rebar.sql.client.query.SqlQuery)13 Promise (org.activityinfo.promise.Promise)10 ArrayList (java.util.ArrayList)4 List (java.util.List)4 SqlTransactionCallback (com.bedatadriven.rebar.sql.client.SqlTransactionCallback)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Filter (org.activityinfo.legacy.shared.command.Filter)2 LocationResult (org.activityinfo.legacy.shared.command.result.LocationResult)2 AdminEntityDTO (org.activityinfo.legacy.shared.model.AdminEntityDTO)2 LocationDTO (org.activityinfo.legacy.shared.model.LocationDTO)2 EntityCategory (org.activityinfo.legacy.shared.reports.content.EntityCategory)2 AdminDimension (org.activityinfo.legacy.shared.reports.model.AdminDimension)2 AttributeGroupDimension (org.activityinfo.legacy.shared.reports.model.AttributeGroupDimension)2 DateDimension (org.activityinfo.legacy.shared.reports.model.DateDimension)2