Search in sources :

Example 11 with SqlResultSetRow

use of com.bedatadriven.rebar.sql.client.SqlResultSetRow 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 12 with SqlResultSetRow

use of com.bedatadriven.rebar.sql.client.SqlResultSetRow 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 13 with SqlResultSetRow

use of com.bedatadriven.rebar.sql.client.SqlResultSetRow 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 14 with SqlResultSetRow

use of com.bedatadriven.rebar.sql.client.SqlResultSetRow 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)

Example 15 with SqlResultSetRow

use of com.bedatadriven.rebar.sql.client.SqlResultSetRow 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);
                }
            });
        }
    });
}
Also used : ActivityFormDTO(org.activityinfo.legacy.shared.model.ActivityFormDTO) AsyncCallback(com.google.gwt.user.client.rpc.AsyncCallback) ArrayList(java.util.ArrayList) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) ActivityFormResults(org.activityinfo.legacy.shared.command.result.ActivityFormResults) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow) SchemaDTO(org.activityinfo.legacy.shared.model.SchemaDTO) Promise(org.activityinfo.promise.Promise) Function(com.google.common.base.Function) SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback) Nullable(javax.annotation.Nullable)

Aggregations

SqlResultSet (com.bedatadriven.rebar.sql.client.SqlResultSet)19 SqlResultSetRow (com.bedatadriven.rebar.sql.client.SqlResultSetRow)19 SqlResultCallback (com.bedatadriven.rebar.sql.client.SqlResultCallback)18 SqlTransaction (com.bedatadriven.rebar.sql.client.SqlTransaction)18 SqlQuery (com.bedatadriven.rebar.sql.client.query.SqlQuery)11 Promise (org.activityinfo.promise.Promise)6 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Map (java.util.Map)3 Function (com.google.common.base.Function)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 Nullable (javax.annotation.Nullable)2 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