Search in sources :

Example 1 with DimensionType

use of org.activityinfo.legacy.shared.command.DimensionType in project activityinfo by bedatadriven.

the class OldGetSitesHandler method applyFilter.

private void applyFilter(SqlQuery query, GetSites command) {
    Filter filter = command.getFilter();
    if (filter != null) {
        if (filter.getRestrictedDimensions() != null && filter.getRestrictedDimensions().size() > 0) {
            query.onlyWhere(" AND (");
            boolean isFirst = true;
            boolean isRestricted = false;
            for (DimensionType type : filter.getRestrictedDimensions()) {
                if (isQueryableType(type)) {
                    addJoint(query, isFirst);
                    isRestricted = true;
                }
                if (type == DimensionType.Activity) {
                    query.onlyWhere("activity.ActivityId").in(filter.getRestrictions(type));
                } else if (type == DimensionType.Database) {
                    query.onlyWhere("activity.DatabaseId").in(filter.getRestrictions(type));
                } else if (type == DimensionType.Partner) {
                    query.onlyWhere("site.PartnerId").in(filter.getRestrictions(type));
                } else if (type == DimensionType.Project) {
                    query.onlyWhere("site.ProjectId").in(filter.getRestrictions(type));
                } else if (type == DimensionType.AdminLevel) {
                    query.onlyWhere("site.LocationId").in(SqlQuery.select("Link.LocationId").from(Tables.LOCATION_ADMIN_LINK, "Link").where("Link.AdminEntityId").in(filter.getRestrictions(type)));
                } else if (type == DimensionType.Site) {
                    query.onlyWhere("site.SiteId").in(filter.getRestrictions(type));
                } else if (type == DimensionType.Attribute) {
                    Set<Integer> attributes = filter.getRestrictions(DimensionType.Attribute);
                    boolean isFirstAttr = true;
                    for (Integer attribute : attributes) {
                        SqlQuery attributefilter = SqlQuery.select().appendColumn("1", "__VAL_EXISTS").from("attributevalue", "av").whereTrue("av.value=1").and("av.SiteId = site.SiteId").where("av.AttributeId").equalTo(attribute);
                        addJoint(query, isFirstAttr);
                        if (isFirstAttr) {
                            isFirstAttr = false;
                        }
                        query.onlyWhere("EXISTS (" + attributefilter.sql() + ") ");
                        query.appendParameter(attribute);
                    }
                } else if (type == DimensionType.Location) {
                    query.onlyWhere("location.locationId").in(filter.getRestrictions(type));
                }
                if (isQueryableType(type) && isFirst) {
                    isFirst = false;
                }
            }
            if (!isRestricted) {
                query.onlyWhere(" 1=1 ");
            }
            query.onlyWhere(")");
        }
        if (!command.isFetchAllReportingPeriods()) {
            // it does not make sense to filter monthly activity by site dates (filter by reporting period instead)
            applyDateRangeFilter("site.Date1", filter.getStartDateRange(), query);
            applyDateRangeFilter("site.Date2", filter.getEndDateRange(), query);
        }
    }
}
Also used : DimensionType(org.activityinfo.legacy.shared.command.DimensionType) SqlQuery(com.bedatadriven.rebar.sql.client.query.SqlQuery) Filter(org.activityinfo.legacy.shared.command.Filter)

Example 2 with DimensionType

use of org.activityinfo.legacy.shared.command.DimensionType in project activityinfo by bedatadriven.

the class CalculatedIndicatorsQuery method composeSiteFilter.

private Filter composeSiteFilter() {
    Filter siteFilter = new Filter();
    siteFilter.addRestriction(DimensionType.Activity, activityIds);
    siteFilter.setEndDateRange(query.getFilter().getEndDateRange());
    for (DimensionType type : query.getFilter().getRestrictedDimensions()) {
        if (type != DimensionType.Activity && type != DimensionType.Database && type != DimensionType.Indicator) {
            siteFilter.addRestriction(type, query.getFilter().getRestrictions(type));
        }
    }
    return siteFilter;
}
Also used : DimensionType(org.activityinfo.legacy.shared.command.DimensionType) Filter(org.activityinfo.legacy.shared.command.Filter)

Example 3 with DimensionType

use of org.activityinfo.legacy.shared.command.DimensionType in project activityinfo by bedatadriven.

the class PivotQuery method appendDimensionRestrictions.

private void appendDimensionRestrictions() {
    if (filter != null) {
        if (filter.getRestrictedDimensions() != null && filter.getRestrictedDimensions().size() > 0) {
            query.where("(");
            boolean isFirst = true;
            for (DimensionType type : filter.getRestrictedDimensions()) {
                addJoint(query, isFirst);
                if (isFirst) {
                    isFirst = false;
                }
                if (type == DimensionType.AdminLevel) {
                    query.onlyWhere(baseTable.getDimensionIdColumn(DimensionType.Location)).in(SqlQuery.select("Link.LocationId").from(Tables.LOCATION_ADMIN_LINK, "Link").where("Link.AdminEntityId").in(filter.getRestrictions(DimensionType.AdminLevel)));
                } else if (type == DimensionType.Attribute) {
                    Set<Integer> attributes = filter.getRestrictions(DimensionType.Attribute);
                    boolean isFirstAttr = true;
                    for (Integer attribute : attributes) {
                        SqlQuery attributefilter = SqlQuery.select().appendColumn("1", "__VAL_EXISTS").from("attributevalue", "v").whereTrue("v.value=1").and("v.SiteId = Site.SiteId").where("v.AttributeId").equalTo(attribute);
                        addJoint(query, isFirstAttr);
                        if (isFirstAttr) {
                            isFirstAttr = false;
                        }
                        query.onlyWhere("EXISTS (" + attributefilter.sql() + ") ");
                        query.appendParameter(attribute);
                    }
                } else {
                    query.onlyWhere(baseTable.getDimensionIdColumn(type)).in(filter.getRestrictions(type));
                }
            }
            query.onlyWhere(")");
        }
    }
}
Also used : DimensionType(org.activityinfo.legacy.shared.command.DimensionType) Set(java.util.Set) SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) SqlQuery(com.bedatadriven.rebar.sql.client.query.SqlQuery)

Example 4 with DimensionType

use of org.activityinfo.legacy.shared.command.DimensionType in project activityinfo by bedatadriven.

the class FilterAdapter method marshal.

@Override
public FilterElement marshal(Filter filter) throws Exception {
    FilterElement element = new FilterElement();
    element.startDateRange = filter.getStartDateRange();
    element.endDateRange = filter.getEndDateRange();
    for (DimensionType t : filter.getRestrictedDimensions()) {
        Restriction r = new Restriction();
        r.dimension = t.toString().toLowerCase();
        for (Integer id : filter.getRestrictions(t)) {
            r.categories.add(id.toString());
        }
        element.restrictions.add(r);
    }
    return element;
}
Also used : DimensionType(org.activityinfo.legacy.shared.command.DimensionType)

Example 5 with DimensionType

use of org.activityinfo.legacy.shared.command.DimensionType in project activityinfo by bedatadriven.

the class MapGenerator method generate.

@Override
public void generate(User user, MapReportElement element, Filter inheritedFilter, DateRange dateRange) {
    Filter filter = GeneratorUtils.resolveElementFilter(element, dateRange);
    Filter effectiveFilter = inheritedFilter == null ? filter : new Filter(inheritedFilter, filter);
    MapContent content = new MapContent();
    content.setFilterDescriptions(generateFilterDescriptions(filter, Collections.<DimensionType>emptySet(), user));
    Map<Integer, Indicator> indicators = queryIndicators(element);
    // Set up layer generators
    List<LayerGenerator> layerGenerators = new ArrayList<LayerGenerator>();
    for (MapLayer layer : element.getLayers()) {
        if (layer.isVisible()) {
            LayerGenerator layerGtor = createGenerator(layer, indicators);
            layerGtor.query(getDispatcher(), effectiveFilter);
            layerGenerators.add(layerGtor);
        }
    }
    // FIRST PASS: calculate extents and margins
    int width = element.getWidth();
    int height = element.getHeight();
    AiLatLng center;
    int zoom;
    Extents extents = Extents.emptyExtents();
    Margins margins = new Margins(0);
    for (LayerGenerator layerGtor : layerGenerators) {
        extents.grow(layerGtor.calculateExtents());
        margins.grow(layerGtor.calculateMargins());
    }
    content.setExtents(extents);
    if (element.getCenter() == null) {
        // Now we're ready to calculate the zoom level
        // and the projection
        zoom = TileMath.zoomLevelForExtents(extents, width, height);
        center = extents.center();
    } else {
        center = element.getCenter();
        zoom = element.getZoomLevel();
    }
    content.setCenter(center);
    // Retrieve the basemap and clamp zoom level
    BaseMap baseMap = findBaseMap(element, indicators.values());
    if (zoom < baseMap.getMinZoom()) {
        zoom = baseMap.getMinZoom();
    }
    if (zoom > baseMap.getMaxZoom()) {
        zoom = baseMap.getMaxZoom();
    }
    if (zoom > element.getMaximumZoomLevel()) {
        zoom = element.getMaximumZoomLevel();
    }
    TiledMap map = new TiledMap(width, height, center, zoom);
    content.setBaseMap(baseMap);
    content.setZoomLevel(zoom);
    // Generate the actual content
    for (LayerGenerator layerGtor : layerGenerators) {
        layerGtor.generate(map, content);
    }
    content.setIndicators(toDTOs(indicators.values()));
    element.setContent(content);
}
Also used : DimensionType(org.activityinfo.legacy.shared.command.DimensionType) MapContent(org.activityinfo.legacy.shared.reports.content.MapContent) Extents(org.activityinfo.model.type.geo.Extents) Indicator(org.activityinfo.server.database.hibernate.entity.Indicator) BaseMap(org.activityinfo.legacy.shared.model.BaseMap) TileBaseMap(org.activityinfo.legacy.shared.model.TileBaseMap) GoogleBaseMap(org.activityinfo.legacy.shared.reports.content.GoogleBaseMap) Filter(org.activityinfo.legacy.shared.command.Filter) AiLatLng(org.activityinfo.model.type.geo.AiLatLng)

Aggregations

DimensionType (org.activityinfo.legacy.shared.command.DimensionType)10 Filter (org.activityinfo.legacy.shared.command.Filter)5 SqlQuery (com.bedatadriven.rebar.sql.client.query.SqlQuery)2 SqlResultSet (com.bedatadriven.rebar.sql.client.SqlResultSet)1 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 GetDimensionLabels (org.activityinfo.legacy.shared.command.GetDimensionLabels)1 DimensionLabels (org.activityinfo.legacy.shared.command.GetDimensionLabels.DimensionLabels)1 BaseMap (org.activityinfo.legacy.shared.model.BaseMap)1 SiteDTO (org.activityinfo.legacy.shared.model.SiteDTO)1 TileBaseMap (org.activityinfo.legacy.shared.model.TileBaseMap)1 FilterDescription (org.activityinfo.legacy.shared.reports.content.FilterDescription)1 GoogleBaseMap (org.activityinfo.legacy.shared.reports.content.GoogleBaseMap)1 MapContent (org.activityinfo.legacy.shared.reports.content.MapContent)1 ReportContent (org.activityinfo.legacy.shared.reports.content.ReportContent)1 TableContent (org.activityinfo.legacy.shared.reports.content.TableContent)1 TableData (org.activityinfo.legacy.shared.reports.content.TableData)1 AiLatLng (org.activityinfo.model.type.geo.AiLatLng)1 Extents (org.activityinfo.model.type.geo.Extents)1 Indicator (org.activityinfo.server.database.hibernate.entity.Indicator)1