use of org.activityinfo.shared.command.handler.pivot.bundler.AttributeBundler in project activityinfo by bedatadriven.
the class PivotQuery method defineAttributeDimension.
/**
* Defines an a dimension based on an Attribute Group defined
* by the user. This is essentially a custom dimension
* @param dimension
*/
private void defineAttributeDimension(AttributeGroupDimension dim) {
// this pivots the data by a single-valued attribute group
String valueQueryAlias = "attributeValues" + dim.getAttributeGroupId();
// this first query gives us the single chosen attribute for
// each site, arbitrarily taking the attribute with the minimum
// id if more than one attribute has been selected (i.e db is inconsistent)
// note that we select attributes by NAME rather than
// the attribute group id itself. This permits merging
// of attributes from other activities/dbs with the same name
SqlQuery groupNameQuery = SqlQuery.select().appendColumn("name").from(Tables.ATTRIBUTE_GROUP).whereTrue("AttributeGroupId=" + dim.getAttributeGroupId());
SqlQuery derivedValueQuery = SqlQuery.select().appendColumn("v.siteId", "siteId").appendColumn("min(a.name)", "value").appendColumn("min(a.sortOrder)", "sortOrder").from("attributevalue", "v").leftJoin("attribute", "a").on("v.AttributeId = a.AttributeId").leftJoin("attributegroup", "g").on("a.AttributeGroupId=g.AttributeGroupId").whereTrue("v.value=1").where("g.name").in(groupNameQuery).groupBy("v.siteId");
query.leftJoin(derivedValueQuery, valueQueryAlias).on(baseTable.getDimensionIdColumn(DimensionType.Site) + "=" + valueQueryAlias + ".SiteId");
String valueAlias = appendDimColumn(valueQueryAlias + ".value");
String sortOrderAlias = appendDimColumn(valueQueryAlias + ".sortOrder");
bundlers.add(new AttributeBundler(dim, valueAlias, sortOrderAlias));
}
Aggregations