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;
}
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;
}
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;
}
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;
}
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);
}
});
}
});
}
Aggregations