Search in sources :

Example 6 with Promise

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

the class MonthlyReportsPanel method save.

public Promise<Void> save() {
    ArrayList<UpdateMonthlyReports.Change> changes = new ArrayList<>();
    for (Record record : store.getModifiedRecords()) {
        IndicatorRowDTO report = (IndicatorRowDTO) record.getModel();
        for (String property : record.getChanges().keySet()) {
            UpdateMonthlyReports.Change change = new UpdateMonthlyReports.Change();
            change.setIndicatorId(report.getIndicatorId());
            change.setMonth(IndicatorRowDTO.monthForProperty(property));
            change.setValue(report.get(property));
            changes.add(change);
        }
    }
    final Promise<Void> promise = new Promise<>();
    service.execute(new UpdateMonthlyReports(currentSiteId, changes), new MaskingAsyncMonitor(this, I18N.CONSTANTS.saving()), new AsyncCallback<VoidResult>() {

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

        @Override
        public void onSuccess(VoidResult result) {
            store.commitChanges();
            promise.onSuccess(null);
        }
    });
    return promise;
}
Also used : UpdateMonthlyReports(org.activityinfo.legacy.shared.command.UpdateMonthlyReports) VoidResult(org.activityinfo.legacy.shared.command.result.VoidResult) ArrayList(java.util.ArrayList) Promise(org.activityinfo.promise.Promise) MaskingAsyncMonitor(org.activityinfo.ui.client.dispatch.monitor.MaskingAsyncMonitor) Record(com.extjs.gxt.ui.client.store.Record)

Example 7 with Promise

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

the class SchemaImporterV3 method persist.

@Override
public void persist(final AsyncCallback<Void> callback) {
    List<PromiseExecutionOperation> operations = new ArrayList<>();
    for (final FormClass formClass : toSave()) {
        operations.add(new PromiseExecutionOperation() {

            @Nullable
            @Override
            public Promise<Void> apply(@Nullable Void aVoid) {
                return locator.persist(formClass);
            }
        });
    }
    PromisesExecutionGuard.newInstance().executeSerially(operations).then(callback);
}
Also used : PromiseExecutionOperation(org.activityinfo.promise.PromiseExecutionOperation) Promise(org.activityinfo.promise.Promise) FormClass(org.activityinfo.model.form.FormClass) Nullable(javax.annotation.Nullable)

Example 8 with Promise

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

the class OldGetSitesHandler method joinIndicatorValues.

private Promise<Void> joinIndicatorValues(final GetSites command, SqlTransaction tx, final Multimap<Integer, SiteDTO> siteMap, final Map<Integer, SiteDTO> periodMap) {
    final Promise<Void> complete = new Promise<>();
    Log.trace("Starting joinIndicatorValues()");
    SqlQuery query = SqlQuery.select().appendColumn("P.SiteId", "SiteId").appendColumn("V.IndicatorId", "SourceIndicatorId").appendColumn("I.ActivityId", "SourceActivityId").appendColumn("D.IndicatorId", "DestIndicatorId").appendColumn("D.ActivityId", "DestActivityId").appendColumn("I.Type").appendColumn("I.Expression").appendColumn("V.Value").appendColumn("V.TextValue").appendColumn("V.DateValue").appendColumn("P.ReportingPeriodId", "PeriodId").from(Tables.REPORTING_PERIOD, "P").innerJoin(Tables.INDICATOR_VALUE, "V").on("P.ReportingPeriodId = V.ReportingPeriodId").innerJoin(Tables.INDICATOR, "I").on("I.IndicatorId = V.IndicatorId").leftJoin(Tables.INDICATOR_LINK, "L").on("L.SourceIndicatorId=I.IndicatorId").leftJoin(Tables.INDICATOR, "D").on("L.DestinationIndicatorId=D.IndicatorId").whereTrue("I.dateDeleted IS NULL");
    if (weAreFetchingAllSitesForAnActivityAndThereAreNoLinkedSites(command, siteMap)) {
        query.where("I.ActivityId").in(command.getFilter().getRestrictions(DimensionType.Activity));
    } else {
        query.where("P.SiteId").in(siteMap.keySet());
    }
    query.execute(tx, new SqlResultCallback() {

        @Override
        public void onSuccess(SqlTransaction tx, SqlResultSet results) {
            Log.trace("Received results for join indicators");
            for (final SqlResultSetRow row : results.getRows()) {
                FieldTypeClass indicatorType = FormFieldType.valueOf(row.getString("Type"));
                String expression = row.getString("Expression");
                boolean isCalculatedIndicator = !Strings.isNullOrEmpty(expression);
                Object indicatorValue = null;
                if (isCalculatedIndicator) {
                // ignore -> see joinCalculatedIndicatorValues
                } else {
                    // if indicator is no calculated then assign value directly
                    if (indicatorType == FieldTypeClass.QUANTITY) {
                        if (!row.isNull("Value")) {
                            indicatorValue = row.getDouble("Value");
                        }
                    } else if (indicatorType == FieldTypeClass.FREE_TEXT || indicatorType == FieldTypeClass.NARRATIVE || indicatorType == ReferenceType.TYPE_CLASS || indicatorType == AttachmentType.TYPE_CLASS) {
                        if (!row.isNull("TextValue")) {
                            indicatorValue = row.getString("TextValue");
                        }
                    } else if (indicatorType == FieldTypeClass.LOCAL_DATE) {
                        indicatorValue = row.getDate("DateValue");
                    } else if (indicatorType == FieldTypeClass.BOOLEAN) {
                        if (!row.isNull("BooleanValue")) {
                            indicatorValue = row.getBoolean("BooleanValue");
                        }
                    }
                }
                int sourceActivityId = row.getInt("SourceActivityId");
                if (command.isFetchAllReportingPeriods()) {
                    SiteDTO site = periodMap.get(row.getInt("PeriodId"));
                    if (site != null) {
                        site.setIndicatorValue(row.getInt("SourceIndicatorId"), indicatorValue);
                    }
                } else {
                    for (SiteDTO site : siteMap.get(row.getInt("SiteId"))) {
                        if (sourceActivityId == site.getActivityId()) {
                            int indicatorId = row.getInt("SourceIndicatorId");
                            site.setIndicatorValue(indicatorId, indicatorValue);
                        } else if (!row.isNull("DestActivityId")) {
                            int destActivityId = row.getInt("DestActivityId");
                            if (site.getActivityId() == destActivityId) {
                                int indicatorId = row.getInt("DestIndicatorId");
                                site.setIndicatorValue(indicatorId, indicatorValue);
                            }
                        }
                    }
                }
            }
            Log.trace("Done populating dtos for join indicators");
            // after normal indicators are evaluated try to calculate indicators with expression
            joinCalculatedIndicatorValues(complete, tx, siteMap);
        }
    });
    return complete;
}
Also used : SqlQuery(com.bedatadriven.rebar.sql.client.query.SqlQuery) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow) Promise(org.activityinfo.promise.Promise) SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) FieldTypeClass(org.activityinfo.model.type.FieldTypeClass) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback)

Example 9 with Promise

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

the class OldGetSitesHandler method queryTotalLength.

private Promise<Void> queryTotalLength(SqlTransaction tx, GetSites command, ExecutionContext context, final SiteResult result) {
    final Promise<Void> promise = new Promise<>();
    if (isMySql()) {
        tx.executeSql("SELECT FOUND_ROWS() site_count", new SqlResultCallback() {

            @Override
            public void onSuccess(SqlTransaction tx, SqlResultSet results) {
                result.setTotalLength(results.getRow(0).getInt("site_count"));
                promise.resolve(null);
            }
        });
    } else {
        // otherwise we have to execute the whole thing again
        SqlQuery query = countQuery(command, context);
        query.execute(tx, new SqlResultCallback() {

            @Override
            public void onSuccess(SqlTransaction tx, SqlResultSet results) {
                result.setTotalLength(results.getRow(0).getInt("site_count"));
                promise.resolve(null);
            }
        });
    }
    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 10 with Promise

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

the class OldGetSitesHandler method joinEntities.

private Promise<Void> joinEntities(SqlTransaction tx, final Multimap<Integer, SiteDTO> siteMap) {
    final Promise<Void> complete = new Promise<>();
    Log.trace("Starting joinEntities()");
    SqlQuery.select("site.SiteId", "Link.adminEntityId", "e.name", "e.adminLevelId", "e.adminEntityParentId", "x1", "y1", "x2", "y2").from(Tables.SITE).innerJoin(Tables.LOCATION).on("location.LocationId = site.LocationId").innerJoin(Tables.LOCATION_ADMIN_LINK, "Link").on("Link.LocationId = location.LocationId").innerJoin(Tables.ADMIN_ENTITY, "e").on("Link.AdminEntityId = e.AdminEntityId").where("site.SiteId").in(siteMap.keySet()).execute(tx, new SqlResultCallback() {

        @Override
        public void onSuccess(SqlTransaction tx, SqlResultSet results) {
            Log.trace("Received results for joinEntities()");
            Map<Integer, AdminEntityDTO> entities = Maps.newHashMap();
            for (SqlResultSetRow row : results.getRows()) {
                int adminEntityId = row.getInt("adminEntityId");
                AdminEntityDTO entity = entities.get(adminEntityId);
                if (entity == null) {
                    entity = GetAdminEntitiesHandler.toEntity(row);
                    entities.put(adminEntityId, entity);
                }
                for (SiteDTO site : siteMap.get(row.getInt("SiteId"))) {
                    site.setAdminEntity(entity.getLevelId(), entity);
                }
            }
            Log.trace("Done populating results for joinEntities");
            complete.onSuccess(null);
        }
    });
    return complete;
}
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) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow)

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