Search in sources :

Example 21 with SqlTransaction

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

the class GetActivityFormsHandler method execute.

@Override
public void execute(GetActivityForms command, final ExecutionContext context, final AsyncCallback<ActivityFormResults> callback) {
    composeQuery(command.getFilter()).execute(context.getTransaction(), new SqlResultCallback() {

        @Override
        public void onSuccess(SqlTransaction tx, final SqlResultSet results) {
            context.execute(new GetSchema(), new AsyncCallback<SchemaDTO>() {

                @Override
                public void onFailure(Throwable caught) {
                    callback.onFailure(caught);
                }

                @Override
                public void onSuccess(SchemaDTO schema) {
                    LOGGER.log(Level.INFO, "Forms matching filter: " + results.getRows().size());
                    final List<Promise<ActivityFormDTO>> pending = new ArrayList<>();
                    for (SqlResultSetRow row : results.getRows()) {
                        int activityId = row.getInt("activityId");
                        boolean visible = (schema.getActivityById(activityId) != null);
                        if (visible) {
                            pending.add(fetchForm(context, activityId));
                        }
                    }
                    LOGGER.log(Level.INFO, "Forms pending: " + pending.size());
                    Promise.waitAll(pending).then(new Function<Void, ActivityFormResults>() {

                        @Nullable
                        @Override
                        public ActivityFormResults apply(@Nullable Void aVoid) {
                            LOGGER.log(Level.INFO, "Form loading completed.");
                            List<ActivityFormDTO> forms = new ArrayList<>();
                            for (Promise<ActivityFormDTO> pendingForm : pending) {
                                forms.add(pendingForm.get());
                            }
                            return new ActivityFormResults(forms);
                        }
                    }).then(callback);
                }
            });
        }
    });
}
Also used : ActivityFormDTO(org.activityinfo.legacy.shared.model.ActivityFormDTO) AsyncCallback(com.google.gwt.user.client.rpc.AsyncCallback) ArrayList(java.util.ArrayList) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) ActivityFormResults(org.activityinfo.legacy.shared.command.result.ActivityFormResults) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow) SchemaDTO(org.activityinfo.legacy.shared.model.SchemaDTO) Promise(org.activityinfo.promise.Promise) Function(com.google.common.base.Function) SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback) Nullable(javax.annotation.Nullable)

Example 22 with SqlTransaction

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

the class GetAdminEntitiesHandler method execute.

@Override
public void execute(GetAdminEntities cmd, ExecutionContext context, final AsyncCallback<AdminEntityResult> callback) {
    SqlQuery query = SqlQuery.select("AdminEntity.adminEntityId", "AdminEntity.name", "AdminEntity.adminLevelId", "AdminEntity.adminEntityParentId", "x1", "y1", "x2", "y2").from(Tables.ADMIN_ENTITY, "AdminEntity").whereTrue("not AdminEntity.deleted");
    query.orderBy("AdminEntity.name");
    if (cmd.getLevelId() != null) {
        query.where("AdminEntity.AdminLevelId").equalTo(cmd.getLevelId());
    } else {
        query.leftJoin(Tables.ADMIN_LEVEL, "level").on("AdminEntity.AdminLevelID=level.AdminLevelId");
        query.appendColumn("level.name", "levelName");
    }
    if (cmd.getEntityIds() != null && !cmd.getEntityIds().isEmpty()) {
        query.where("AdminEntity.AdminEntityId").in(cmd.getEntityIds());
    }
    if (cmd.getParentId() != null) {
        if (cmd.getParentId() == GetAdminEntities.ROOT) {
            query.where("AdminEntity.AdminEntityParentId IS NULL");
        } else {
            query.where("AdminEntity.AdminEntityParentId").equalTo(cmd.getParentId());
        }
    }
    if (cmd.getFilter() != null) {
        Filter filter = cmd.getFilter();
        if (filter.isRestricted(DimensionType.Activity) || filter.isRestricted(DimensionType.Database) || filter.isRestricted(DimensionType.Indicator)) {
            SqlQuery subQuery = SqlQuery.select("link.AdminEntityId").from(Tables.SITE, "site").leftJoin(Tables.LOCATION, "Location").on("Location.LocationId = site.LocationId").leftJoin(Tables.LOCATION_ADMIN_LINK, "link").on("link.LocationId = Location.LocationId");
            if (filter.isRestricted(DimensionType.Activity)) {
                subQuery.where("site.ActivityId").in(filter.getRestrictions(DimensionType.Activity));
            }
            if (filter.isRestricted(DimensionType.Database)) {
                subQuery.leftJoin(Tables.ACTIVITY, "activity").on("site.ActivityId=activity.ActivityId").where("activity.DatabaseId").in(filter.getRestrictions(DimensionType.Database));
            }
            if (filter.isRestricted(DimensionType.Indicator)) {
                subQuery.leftJoin(Tables.REPORTING_PERIOD, "rp").on("site.siteId=rp.SiteId").leftJoin(Tables.INDICATOR_VALUE, "iv").on("iv.reportingPeriodId=rp.reportingPeriodId").where("iv.indicatorId").in(filter.getRestrictions(DimensionType.Indicator));
            }
            query.where("AdminEntity.AdminEntityId").in(subQuery);
        }
    }
    if (cmd.getFilter() != null && cmd.getFilter().isRestricted(DimensionType.AdminLevel)) {
        if (cmd.getLevelId() == null) {
            query.where("AdminEntityId").in(cmd.getFilter().getRestrictions(DimensionType.AdminLevel));
        } else {
            SqlQuery subQuery = SqlQuery.select("adminEntityId").from(Tables.ADMIN_ENTITY, "AdminEntity").where("AdminLevelId").equalTo(cmd.getLevelId()).where("AdminEntityId").in(cmd.getFilter().getRestrictions(DimensionType.AdminLevel));
            query.where("AdminEntity.AdminEntityId").in(subQuery);
        }
    }
    query.execute(context.getTransaction(), new SqlResultCallback() {

        @Override
        public void onSuccess(SqlTransaction tx, SqlResultSet results) {
            final List<AdminEntityDTO> entities = new ArrayList<AdminEntityDTO>();
            Set<String> names = Sets.newHashSet();
            Set<String> duplicates = Sets.newHashSet();
            for (SqlResultSetRow row : results.getRows()) {
                AdminEntityDTO entity = toEntity(row);
                if (!names.add(entity.getName())) {
                    duplicates.add(entity.getName());
                }
                entities.add(entity);
            }
            for (int i = 0; i != entities.size(); ++i) {
                if (duplicates.contains(entities.get(i).getName())) {
                    String levelName = results.getRow(i).getString("levelName");
                    entities.get(i).setName(entities.get(i).getName() + " [" + levelName + "]");
                }
            }
            callback.onSuccess(new AdminEntityResult(entities));
        }
    });
}
Also used : SqlQuery(com.bedatadriven.rebar.sql.client.query.SqlQuery) Set(java.util.Set) SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) AdminEntityDTO(org.activityinfo.legacy.shared.model.AdminEntityDTO) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow) SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) Filter(org.activityinfo.legacy.shared.command.Filter) AdminEntityResult(org.activityinfo.legacy.shared.command.result.AdminEntityResult) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback) ArrayList(java.util.ArrayList) List(java.util.List)

Example 23 with SqlTransaction

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

the class GetSiteAttachmentsHandler method execute.

@Override
public void execute(GetSiteAttachments command, ExecutionContext context, final AsyncCallback<SiteAttachmentResult> callback) {
    dtos = new ArrayList<SiteAttachmentDTO>();
    SqlQuery.selectAll().from(Tables.SITE_ATTACHMENT, "s").where("s.siteid").equalTo(command.getSiteId()).execute(context.getTransaction(), new SqlResultCallback() {

        @Override
        public void onSuccess(SqlTransaction tx, SqlResultSet results) {
            for (SqlResultSetRow row : results.getRows()) {
                SiteAttachmentDTO dto = new SiteAttachmentDTO();
                dto.setSiteId(row.getInt("siteid"));
                dto.setBlobId(row.getString("blobid"));
                dto.setFileName(row.getString("filename"));
                dto.setUploadedBy(row.getInt("uploadedby"));
                dto.setBlobSize(row.getInt("blobsize"));
                dto.setContentType(row.getString("contenttype"));
                dtos.add(dto);
            }
            callback.onSuccess(new SiteAttachmentResult(dtos));
        }
    });
}
Also used : SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) SiteAttachmentDTO(org.activityinfo.legacy.shared.model.SiteAttachmentDTO) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow) SiteAttachmentResult(org.activityinfo.legacy.shared.command.result.SiteAttachmentResult)

Example 24 with SqlTransaction

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

the class LocalDispatcher method executeOffline.

/**
 * Begins a new transaction, initializes a new ExecutionContext, and
 * executes the root command.
 *
 * @param command
 * @param callback
 */
private <R extends CommandResult> void executeOffline(final Command<R> command, final AsyncCallback<R> callback) {
    Log.debug("Executing command " + command + " OFFLINE.");
    try {
        final Collector<R> commandResult = Collector.newCollector();
        database.transaction(new SqlTransactionCallback() {

            @Override
            public void begin(SqlTransaction tx) {
                LocalExecutionContext context = new LocalExecutionContext(auth, tx, registry, commandQueue);
                context.execute(command, commandResult);
            }

            @Override
            public void onError(SqlException e) {
                Log.error("OFFLINE EXECUTION FAILED: " + command, e);
                callback.onFailure(e);
            }

            @Override
            public void onSuccess() {
                callback.onSuccess(commandResult.getResult());
            }
        });
    } catch (Exception e) {
        callback.onFailure(e);
    }
}
Also used : SqlTransactionCallback(com.bedatadriven.rebar.sql.client.SqlTransactionCallback) SqlException(com.bedatadriven.rebar.sql.client.SqlException) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) SqlException(com.bedatadriven.rebar.sql.client.SqlException)

Example 25 with SqlTransaction

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

the class UpdateMonthlyReportsAsync method queryPeriodMap.

private Promise<Map<Month, Integer>> queryPeriodMap(UpdateMonthlyReports command, ExecutionContext context) {
    final Promise<Map<Month, Integer>> promise = new Promise<>();
    SqlQuery.select("reportingPeriodId", "Date2").from(Tables.REPORTING_PERIOD, "rp").where("siteId").equalTo(command.getSiteId()).execute(context.getTransaction(), new SqlResultCallback() {

        @Override
        public void onSuccess(SqlTransaction tx, SqlResultSet results) {
            Map<Month, Integer> periodMap = Maps.newHashMap();
            for (SqlResultSetRow row : results.getRows()) {
                Date endDate = row.getDate("Date2");
                Month month = Month.of(endDate);
                periodMap.put(month, row.getInt("reportingPeriodId"));
            }
            promise.resolve(periodMap);
        }
    });
    return promise;
}
Also used : Promise(org.activityinfo.promise.Promise) Month(org.activityinfo.model.type.time.Month) SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow) Map(java.util.Map) Date(java.util.Date)

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