Search in sources :

Example 11 with Promise

use of org.activityinfo.promise.Promise in project activityinfo by bedatadriven.

the class GetMonthlyReportsHandlerAsync method queryIndicators.

private Promise<SqlResultSet> queryIndicators(final GetMonthlyReports command, ExecutionContext context) {
    final Promise<SqlResultSet> promise = new Promise<>();
    SqlQuery activityQuery = SqlQuery.select("activityId").from(Tables.SITE).where("siteId").equalTo(command.getSiteId());
    SqlQuery.select().appendColumn("i.indicatorId", "indicatorId").appendColumn("i.name", "indicatorName").appendColumn("i.category", "category").appendColumn("a.name", "activityName").appendColumn("a.activityId", "activityId").from(Tables.INDICATOR, "i").leftJoin(Tables.ACTIVITY, "a").on("i.activityId=a.activityId").where("a.activityId").in(activityQuery).orderBy("i.sortOrder").execute(context.getTransaction(), new SqlResultCallback() {

        @Override
        public void onSuccess(SqlTransaction tx, SqlResultSet indicatorResults) {
            promise.resolve(indicatorResults);
        }
    });
    return promise;
}
Also used : Promise(org.activityinfo.promise.Promise) 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)

Example 12 with Promise

use of org.activityinfo.promise.Promise in project activityinfo by bedatadriven.

the class GetMonthlyReportsHandlerAsync method queryPeriods.

private Promise<SqlResultSet> queryPeriods(final GetMonthlyReports command, final ExecutionContext context) {
    final Promise<SqlResultSet> result = new Promise<>();
    SqlQuery.select("rp.Date1", "rp.Date2", "v.indicatorId", "v.value").from(Tables.REPORTING_PERIOD, "rp").leftJoin(Tables.INDICATOR_VALUE, "v").on("rp.ReportingPeriodId=v.ReportingPeriodId").where("rp.SiteId").equalTo(command.getSiteId()).execute(context.getTransaction(), new SqlResultCallback() {

        @Override
        public void onSuccess(SqlTransaction tx, SqlResultSet periodResults) {
            result.resolve(periodResults);
        }
    });
    return result;
}
Also used : Promise(org.activityinfo.promise.Promise) SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction)

Example 13 with Promise

use of org.activityinfo.promise.Promise in project activityinfo by bedatadriven.

the class OldGetSitesHandler method joinAttributeValues.

private Promise<Void> joinAttributeValues(GetSites command, SqlTransaction tx, final Multimap<Integer, SiteDTO> siteMap) {
    Log.trace("Starting joinAttributeValues() ");
    final Promise<Void> complete = new Promise<>();
    SqlQuery sqlQuery = SqlQuery.select().appendColumn("v.AttributeId", "attributeId").appendColumn("a.Name", "attributeName").appendColumn("v.Value", "value").appendColumn("v.SiteId", "siteId").appendColumn("g.name", "groupName").from(Tables.ATTRIBUTE_VALUE, "v").leftJoin(Tables.ATTRIBUTE, "a").on("v.AttributeId = a.AttributeId").leftJoin(Tables.ATTRIBUTE_GROUP, "g").on("a.AttributeGroupId=g.AttributeGroupId").whereTrue("v.Value=1").and("g.dateDeleted IS NULL").orderBy("groupName, attributeName");
    if (weAreFetchingAllSitesForAnActivityAndThereAreNoLinkedSites(command, siteMap)) {
        sqlQuery.leftJoin(Tables.ATTRIBUTE_GROUP_IN_ACTIVITY, "ag").on("ag.attributeGroupId=g.attributeGroupId").where("ag.ActivityId").in(command.getFilter().getRestrictions(DimensionType.Activity));
    } else {
        sqlQuery.where("v.SiteId").in(siteMap.keySet());
    }
    sqlQuery.execute(tx, new SqlResultCallback() {

        @Override
        public void onSuccess(SqlTransaction tx, SqlResultSet results) {
            Log.trace("Received results for joinAttributeValues() ");
            for (SqlResultSetRow row : results.getRows()) {
                int attributeId = row.getInt("attributeId");
                boolean value = row.getBoolean("value");
                String groupName = row.getString("groupName");
                String attributeName = row.getString("attributeName");
                for (SiteDTO site : siteMap.get(row.getInt("siteId"))) {
                    site.setAttributeValue(attributeId, value);
                    if (value) {
                        site.addDisplayAttribute(groupName, attributeName);
                    }
                }
            }
            Log.trace("Done populating results for joinAttributeValues()");
            complete.onSuccess(null);
        }
    });
    return complete;
}
Also used : Promise(org.activityinfo.promise.Promise) 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)

Example 14 with Promise

use of org.activityinfo.promise.Promise in project activityinfo by bedatadriven.

the class GetActivityFormHandler method applyPermissions.

private Promise<ActivityFormDTO> applyPermissions(final ExecutionContext context, final ActivityFormDTO form) {
    final Promise<ActivityFormDTO> result = new Promise<>();
    SqlQuery.selectAll().appendColumn("allowView").appendColumn("allowViewAll").appendColumn("allowEdit").appendColumn("allowEditAll").appendColumn("allowDesign").appendColumn("partnerId").from(Tables.USER_PERMISSION, "p").where("p.UserId").equalTo(context.getUser().getId()).where("p.DatabaseId").equalTo(form.getDatabaseId()).execute(context.getTransaction(), new SqlResultCallback() {

        @Override
        public void onSuccess(SqlTransaction tx, SqlResultSet results) {
            if (results.getRows().isEmpty()) {
                if (form.getPublished() == Published.ALL_ARE_PUBLISHED.getIndex()) {
                    result.resolve(form);
                } else {
                    result.reject(new IllegalAccessCommandException("User " + context.getUser().getId() + " does not have access to form " + form.getId()));
                }
                return;
            }
            SqlResultSetRow row = results.getRow(0);
            if (!row.getBoolean("allowView")) {
                if (form.getPublished() == Published.ALL_ARE_PUBLISHED.getIndex()) {
                    result.resolve(form);
                } else {
                    result.reject(new IllegalAccessCommandException("User " + context.getUser().getId() + " does not have access to form " + form.getId()));
                }
                return;
            }
            form.setEditAllowed(row.getBoolean("allowEdit"));
            form.setEditAllAllowed(row.getBoolean("allowEditAll"));
            form.setDesignAllowed(row.getBoolean("allowDesign"));
            form.setCurrentPartnerId(row.getInt("partnerId"));
            result.resolve(form);
        }
    });
    return result;
}
Also used : Promise(org.activityinfo.promise.Promise) SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) IllegalAccessCommandException(org.activityinfo.legacy.shared.exception.IllegalAccessCommandException) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow)

Example 15 with Promise

use of org.activityinfo.promise.Promise 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)

Aggregations

Promise (org.activityinfo.promise.Promise)21 SqlResultCallback (com.bedatadriven.rebar.sql.client.SqlResultCallback)10 SqlResultSet (com.bedatadriven.rebar.sql.client.SqlResultSet)10 SqlTransaction (com.bedatadriven.rebar.sql.client.SqlTransaction)10 SqlResultSetRow (com.bedatadriven.rebar.sql.client.SqlResultSetRow)6 SqlQuery (com.bedatadriven.rebar.sql.client.query.SqlQuery)4 ArrayList (java.util.ArrayList)4 Function (com.google.common.base.Function)2 List (java.util.List)2 Nullable (javax.annotation.Nullable)2 UpdateMonthlyReports (org.activityinfo.legacy.shared.command.UpdateMonthlyReports)2 FormClass (org.activityinfo.model.form.FormClass)2 FormField (org.activityinfo.model.form.FormField)2 FormInstance (org.activityinfo.model.form.FormInstance)2 ColumnSet (org.activityinfo.model.query.ColumnSet)2 QueryModel (org.activityinfo.model.query.QueryModel)2 ResourceId (org.activityinfo.model.resource.ResourceId)2 FieldValue (org.activityinfo.model.type.FieldValue)2 Record (com.extjs.gxt.ui.client.store.Record)1 Optional (com.google.common.base.Optional)1