Search in sources :

Example 16 with Promise

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

Example 17 with Promise

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

the class UpdateMonthlyReportsAsync method executeUpdate.

private Promise<Void> executeUpdate(SqlTransaction tx, SqlUpdate update) {
    final Promise<Void> promise = new Promise<>();
    update.execute(tx, new SqlResultCallback() {

        @Override
        public void onSuccess(SqlTransaction tx, SqlResultSet results) {
            promise.resolve(null);
        }
    });
    return promise;
}
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 18 with Promise

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

the class FormWidgetCreator method createWidgets.

public Promise<Void> createWidgets(final FormClass formClass, final FieldUpdated fieldUpdated) {
    List<Promise<Void>> promises = Lists.newArrayList();
    for (final FormField field : formClass.getFields()) {
        if (field.getType() instanceof SubFormReferenceType) {
            FormClass subForm = model.getSubFormByOwnerFieldId(field.getId());
            if (subForm.getSubFormKind() != SubFormKind.REPEATING) {
                // for repeating we create it internally in sub SimpleFormPanel
                Promise<Void> subFormWidgetsPromise = createWidgets(subForm, fieldUpdated);
                promises.add(subFormWidgetsPromise);
            }
        } else {
            Promise<Void> promise = widgetFactory.createWidget(formClass, field, new FieldUpdater<FieldValue>() {

                @Override
                public void onInvalid(String errorMessage) {
                    containers.get(field.getId()).setInvalid(errorMessage);
                }

                @Override
                public void update(FieldValue value) {
                    containers.get(field.getId()).setValid();
                    fieldUpdated.onFieldUpdated(field, value);
                }
            }).then(new Function<FormFieldWidget, Void>() {

                @Override
                public Void apply(FormFieldWidget widget) {
                    FieldContainer fieldContainer = containerFactory.createContainer(field, widget, 4);
                    containers.put(field.getId(), fieldContainer);
                    model.addContainerOfClass(formClass.getId(), fieldContainer);
                    return null;
                }
            });
            promises.add(promise);
        }
    }
    return Promise.waitAll(promises);
}
Also used : FieldUpdater(org.activityinfo.ui.client.component.form.field.FieldUpdater) SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) Promise(org.activityinfo.promise.Promise) FormFieldWidget(org.activityinfo.ui.client.component.form.field.FormFieldWidget) FormClass(org.activityinfo.model.form.FormClass) FieldValue(org.activityinfo.model.type.FieldValue) FormField(org.activityinfo.model.form.FormField)

Example 19 with Promise

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

the class FormActions method save.

public Promise<List<FormInstance>> save() {
    model.getEventBus().fireEvent(new BeforeSaveEvent());
    final List<FormInstance> instancesToPersist = getInstancesToPersist();
    Promise<Void> persist = locator.persist(instancesToPersist);
    Promise<Void> remove = remove();
    final Promise<List<FormInstance>> result = new Promise<>();
    Promise.waitAll(persist, remove).then(new AsyncCallback<Void>() {

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

        @Override
        public void onSuccess(Void input) {
            model.getChangedInstances().clear();
            result.onSuccess(instancesToPersist);
        }
    });
    return result;
}
Also used : Promise(org.activityinfo.promise.Promise) BeforeSaveEvent(org.activityinfo.ui.client.component.form.event.BeforeSaveEvent) List(java.util.List) FormInstance(org.activityinfo.model.form.FormInstance)

Example 20 with Promise

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

the class FormActions method remove.

private Promise<Void> remove() {
    Promise<Void> remove = Promise.done();
    if (!model.getPersistedInstanceToRemoveByLocator().isEmpty()) {
        List<Promise<Void>> removePromises = Lists.newArrayList();
        for (FormInstance instance : model.getPersistedInstanceToRemoveByLocator()) {
            removePromises.add(locator.remove(instance.getFormId(), instance.getId()));
        }
        remove = Promise.waitAll(removePromises);
        remove.then(new Function<Void, Object>() {

            @Override
            public Object apply(Void input) {
                model.getPersistedInstanceToRemoveByLocator().clear();
                return null;
            }
        });
    }
    return remove;
}
Also used : Promise(org.activityinfo.promise.Promise) FormInstance(org.activityinfo.model.form.FormInstance)

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