use of com.bedatadriven.rebar.sql.client.SqlTransaction 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 com.bedatadriven.rebar.sql.client.SqlTransaction in project activityinfo by bedatadriven.
the class UpdateSiteHandlerAsync method execute.
@Override
public void execute(final UpdateSite command, ExecutionContext context, final AsyncCallback<VoidResult> callback) {
final Map<String, Object> changes = command.getChanges().getTransientMap();
SqlTransaction tx = context.getTransaction();
updateSiteProperties(tx, command, changes);
updateAttributeValues(tx, command.getSiteId(), changes);
updateReportingPeriod(tx, command.getSiteId(), changes);
callback.onSuccess(new VoidResult());
}
use of com.bedatadriven.rebar.sql.client.SqlTransaction in project activityinfo by bedatadriven.
the class CalculatedIndicatorsQuery method queryAttributeGroups.
private void queryAttributeGroups() {
Set<Integer> groupIds = Sets.newHashSet();
for (Dimension dim : query.getDimensions()) {
if (dim instanceof AttributeGroupDimension) {
AttributeGroupDimension groupDim = (AttributeGroupDimension) dim;
groupIds.add(groupDim.getAttributeGroupId());
}
}
SqlQuery.select().appendColumn("g.attributeGroupId", "groupId").appendColumn("g.name", "groupName").appendColumn("a.attributeId").appendColumn("a.name").appendColumn("a.sortOrder").from(Tables.ATTRIBUTE, "a").leftJoin(Tables.ATTRIBUTE_GROUP, "g").on("a.attributeGroupId=g.attributeGroupId").where("a.attributeGroupId").in(groupIds).execute(queryContext.getExecutionContext().getTransaction(), new SqlResultCallback() {
@Override
public void onSuccess(SqlTransaction tx, SqlResultSet results) {
for (SqlResultSetRow row : results.getRows()) {
int groupId = row.getInt("groupId");
int attributeId = row.getInt("attributeId");
int sortOrder = row.getInt("sortOrder");
String attributeName = row.getString("name");
attributes.put(groupId, new EntityCategory(attributeId, attributeName, sortOrder));
}
querySites();
}
});
}
use of com.bedatadriven.rebar.sql.client.SqlTransaction in project activityinfo by bedatadriven.
the class CalculatedIndicatorsQuery method execute.
@Override
public void execute(final AsyncCallback<Void> callback) {
this.callback = callback;
final SqlQuery query = SqlQuery.selectDistinct().appendColumn("i.indicatorId", "indicatorId").appendColumn("i.name", "indicatorName").appendColumn("i.activityId", "activityId").appendColumn("i.sortOrder", "indicatorOrder").appendColumn("i.aggregation", "aggregation").appendColumn("a.name", "activityName").appendColumn("a.category", "activityCategory").appendColumn("a.sortOrder", "activityOrder").appendColumn("db.DatabaseId", "databaseId").appendColumn("db.name", "databaseName").from(Tables.INDICATOR, "i").leftJoin(Tables.ACTIVITY, "a").on("a.activityId=i.activityId").leftJoin(Tables.USER_DATABASE, "db").on("a.databaseId=db.databaseId").whereTrue("i.calculatedAutomatically=1 and i.Expression is not null");
Filter filter = this.query.getFilter();
if (filter.isRestricted(DimensionType.Indicator)) {
query.where("i.indicatorId").in(filter.getRestrictions(DimensionType.Indicator));
} else if (filter.isRestricted(DimensionType.Activity)) {
query.where("i.activityId").in(filter.getRestrictions(DimensionType.Activity));
} else if (filter.isRestricted(DimensionType.Database)) {
query.where("a.databaseId").in(filter.getRestrictions(DimensionType.Database));
} else {
// too broad
callback.onSuccess(null);
return;
}
// enforce visibility rules
query.whereTrue(visibilityRules());
query.execute(queryContext.getExecutionContext().getTransaction(), new SqlResultCallback() {
@Override
public void onSuccess(SqlTransaction tx, SqlResultSet results) {
if (results.getRows().isEmpty()) {
callback.onSuccess(null);
} else {
boolean hasSumAggregation = false;
for (SqlResultSetRow row : results.getRows()) {
LOGGER.finer("row = " + row);
int activityId = row.getInt("activityId");
int indicatorId = row.getInt("indicatorId");
activityIds.add(activityId);
indicatorIds.add(indicatorId);
activityMap.put(activityId, new EntityCategory(activityId, row.getString("activityName"), row.getInt("activityOrder")));
activityCategoryMap.put(activityId, new SimpleCategory(row.getString("activityCategory")));
activityToDatabaseMap.put(activityId, new EntityCategory(row.getInt("databaseId"), row.getString("databaseName")));
indicatorMap.put(indicatorId, new EntityCategory(indicatorId, row.getString("indicatorName"), row.getInt("indicatorOrder")));
indicatorAggregationMap.put(indicatorId, row.getInt("aggregation"));
if (!hasSumAggregation) {
hasSumAggregation = row.getInt("aggregation") == 0;
}
}
// set aggregation to Sum for all indicators if at least one indicator has different aggregation
if (hasSumAggregation) {
for (Map.Entry<Integer, Integer> entry : indicatorAggregationMap.entrySet()) {
// set to Sum
entry.setValue(0);
}
}
if (queryContext.getCommand().isPivotedBy(DimensionType.AttributeGroup)) {
queryAttributeGroups();
} else {
querySites();
}
}
}
});
}
use of com.bedatadriven.rebar.sql.client.SqlTransaction 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;
}
Aggregations