Search in sources :

Example 16 with SqlTransaction

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;
}
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 17 with SqlTransaction

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());
}
Also used : VoidResult(org.activityinfo.legacy.shared.command.result.VoidResult) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction)

Example 18 with SqlTransaction

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();
        }
    });
}
Also used : SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) Dimension(org.activityinfo.legacy.shared.reports.model.Dimension) AdminDimension(org.activityinfo.legacy.shared.reports.model.AdminDimension) DateDimension(org.activityinfo.legacy.shared.reports.model.DateDimension) AttributeGroupDimension(org.activityinfo.legacy.shared.reports.model.AttributeGroupDimension) AttributeGroupDimension(org.activityinfo.legacy.shared.reports.model.AttributeGroupDimension) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow) EntityCategory(org.activityinfo.legacy.shared.reports.content.EntityCategory)

Example 19 with SqlTransaction

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();
                }
            }
        }
    });
}
Also used : SimpleCategory(org.activityinfo.legacy.shared.reports.content.SimpleCategory) SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) SqlQuery(com.bedatadriven.rebar.sql.client.query.SqlQuery) Filter(org.activityinfo.legacy.shared.command.Filter) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow) EntityCategory(org.activityinfo.legacy.shared.reports.content.EntityCategory)

Example 20 with SqlTransaction

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

Aggregations

SqlTransaction (com.bedatadriven.rebar.sql.client.SqlTransaction)27 SqlResultCallback (com.bedatadriven.rebar.sql.client.SqlResultCallback)23 SqlResultSet (com.bedatadriven.rebar.sql.client.SqlResultSet)23 SqlResultSetRow (com.bedatadriven.rebar.sql.client.SqlResultSetRow)18 SqlQuery (com.bedatadriven.rebar.sql.client.query.SqlQuery)13 Promise (org.activityinfo.promise.Promise)10 ArrayList (java.util.ArrayList)4 List (java.util.List)4 SqlTransactionCallback (com.bedatadriven.rebar.sql.client.SqlTransactionCallback)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Filter (org.activityinfo.legacy.shared.command.Filter)2 LocationResult (org.activityinfo.legacy.shared.command.result.LocationResult)2 AdminEntityDTO (org.activityinfo.legacy.shared.model.AdminEntityDTO)2 LocationDTO (org.activityinfo.legacy.shared.model.LocationDTO)2 EntityCategory (org.activityinfo.legacy.shared.reports.content.EntityCategory)2 AdminDimension (org.activityinfo.legacy.shared.reports.model.AdminDimension)2 AttributeGroupDimension (org.activityinfo.legacy.shared.reports.model.AttributeGroupDimension)2 DateDimension (org.activityinfo.legacy.shared.reports.model.DateDimension)2