← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 10086: Analytics, fixed bug related to filtering

 

------------------------------------------------------------
revno: 10086
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2013-03-08 17:18:21 +0100
message:
  Analytics, fixed bug related to filtering
modified:
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/Dimension.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultQueryPlanner.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManager.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java	2013-03-08 14:51:55 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/DataQueryParams.java	2013-03-08 16:18:21 +0000
@@ -83,8 +83,6 @@
     
     private List<Dimension> filters = new ArrayList<Dimension>();
 
-    private ListMap<String, Dimension> filterMap = new ListMap<String, Dimension>();
-    
     private AggregationType aggregationType;
     
     private Map<MeasureFilter, Double> measureCriteria = new HashMap<MeasureFilter, Double>();
@@ -131,7 +129,7 @@
      * can only be present if the data element dimension exists and the indicator
      * and data set dimensions do not exist.
      */
-    public void conform()
+    public DataQueryParams conform()
     {
         if ( !dimensions.contains( new Dimension( DATAELEMENT_DIM_ID ) ) ||
             dimensions.contains( new Dimension( INDICATOR_DIM_ID ) ) ||
@@ -139,6 +137,23 @@
         {
             removeDimension( CATEGORYOPTIONCOMBO_DIM_ID );
         }
+        
+        return this;
+    }
+    
+    /**
+     * Creates a mapping between the dimension names and the filter dimensions.
+     */
+    public ListMap<String, Dimension> getDimensionFilterMap()
+    {
+        ListMap<String, Dimension> map = new ListMap<String, Dimension>();
+        
+        for ( Dimension filter : filters )
+        {
+            map.putValue( filter.getDimension(), filter );
+        }
+        
+        return map;
     }
     
     /**
@@ -220,9 +235,21 @@
     /**
      * Removes the dimension with the given identifier.
      */
-    public void removeDimension( String dimension )
+    public DataQueryParams removeDimension( String dimension )
     {
         this.dimensions.remove( new Dimension( dimension ) );
+        
+        return this;
+    }
+
+    /**
+     * Removes the filter with the given identifier.
+     */
+    public DataQueryParams removeFilter( String filter )
+    {
+        this.filters.remove( new Dimension( filter ) );
+        
+        return this;
     }
     
     /**
@@ -489,7 +516,7 @@
     /**
      * Sets the options for the given dimension.
      */
-    public void setDimensionOptions( String dimension, DimensionType type, String dimensionName, List<IdentifiableObject> options )
+    public DataQueryParams setDimensionOptions( String dimension, DimensionType type, String dimensionName, List<IdentifiableObject> options )
     {
         int index = dimensions.indexOf( new Dimension( dimension ) );
         
@@ -501,6 +528,8 @@
         {
             dimensions.add( new Dimension( dimension, type, dimensionName, options ) );
         }
+        
+        return this;
     }
     
     /**
@@ -512,11 +541,21 @@
         
         return index != -1 ? filters.get( index ).getItems() : null;
     }
+
+    /**
+     * Retrieves the filter with the given filter identifier.
+     */
+    public Dimension getFilter( String filter )
+    {
+        int index = filters.indexOf( new Dimension( filter ) );
+        
+        return index != -1 ? filters.get( index ) : null;
+    }
     
     /**
      * Sets the options for the given filter.
      */
-    public void setFilterOptions( String filter, DimensionType type, String dimensionName, List<IdentifiableObject> options )
+    public DataQueryParams setFilterOptions( String filter, DimensionType type, String dimensionName, List<IdentifiableObject> options )
     {
         int index = filters.indexOf( new Dimension( filter ) );
         
@@ -528,6 +567,8 @@
         {
             filters.add( new Dimension( filter, type, dimensionName, options ) );
         }
+        
+        return this;
     }
     
     // -------------------------------------------------------------------------
@@ -709,16 +750,6 @@
         this.filters = filters;
     }
 
-    public ListMap<String, Dimension> getFilterMap()
-    {
-        return filterMap;
-    }
-
-    public void setFilterMap( ListMap<String, Dimension> filterMap )
-    {
-        this.filterMap = filterMap;
-    }
-
     public AggregationType getAggregationType()
     {
         return aggregationType;

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/Dimension.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/Dimension.java	2013-03-08 13:39:50 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/Dimension.java	2013-03-08 16:18:21 +0000
@@ -77,14 +77,30 @@
     // -------------------------------------------------------------------------
     
     /**
-     * Indicates whether this dimension should use all dimension options. All
+     * Indicates whether this dimension should use all dimension items. All
      * dimension options is represented as an option list of zero elements.
      */
-    public boolean isAllOptions()
+    public boolean isAllItems()
     {
         return items != null && items.isEmpty();
     }
+
+    /**
+     * Indicates whether this dimension has any dimension items.
+     */
+    public boolean hasItems()
+    {
+        return items != null && !items.isEmpty();
+    }
     
+    /**
+     * Returns dimension name with fall back to dimension.
+     */
+    public String getDimensionName()
+    {
+        return dimensionName != null ? dimensionName : dimension;
+    }
+
     // -------------------------------------------------------------------------
     // Get and set methods
     // -------------------------------------------------------------------------
@@ -109,14 +125,6 @@
         this.type = type;
     }
 
-    /**
-     * Returns dimension name with fall back to dimension.
-     */
-    public String getDimensionName()
-    {
-        return dimensionName != null ? dimensionName : dimension;
-    }
-
     public void setDimensionName( String dimensionName )
     {
         this.dimensionName = dimensionName;

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultQueryPlanner.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultQueryPlanner.java	2013-03-08 14:51:55 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultQueryPlanner.java	2013-03-08 16:18:21 +0000
@@ -302,8 +302,11 @@
     }
     
     /**
-     * Groups the given query into sub queries based on the period type of its
-     * periods. Sets the period type name on each query.
+     * If periods appear as dimensions in the given query; groups the query into 
+     * sub queries based on the period type of the periods. Sets the period type 
+     * name on each query. If periods appear as filters; replaces the period filter
+     * with one filter for each period type. Sets the dimension names and filter
+     * names respectively.
      */
     private List<DataQueryParams> groupByPeriodType( DataQueryParams params )
     {
@@ -327,15 +330,18 @@
         }
         else if ( params.getFilterPeriods() != null && !params.getFilterPeriods().isEmpty() )
         {
-            ListMap<String, IdentifiableObject> periodTypePeriodMap = getPeriodTypePeriodMap( params.getFilterPeriods() );
+            Dimension filter = params.getFilter( PERIOD_DIM_ID );
+            
+            ListMap<String, IdentifiableObject> periodTypePeriodMap = getPeriodTypePeriodMap( filter.getItems() );
+            
+            params.removeFilter( PERIOD_DIM_ID ).setPeriodType( periodTypePeriodMap.keySet().iterator().next() ); // Using first period type
             
             for ( String periodType : periodTypePeriodMap.keySet() )
             {
-                DataQueryParams query = new DataQueryParams( params );
-                query.setFilterOptions( PERIOD_DIM_ID, DimensionType.PERIOD, periodType, periodTypePeriodMap.get( periodType ) );
-                query.setPeriodType( periodType );
-                queries.add( query );
+                params.getFilters().add( new Dimension( filter.getDimension(), filter.getType(), periodType, periodTypePeriodMap.get( periodType ) ) );
             }
+            
+            queries.add( params );
         }
         else
         {
@@ -346,8 +352,11 @@
     }
     
     /**
-     * Groups the given query into sub queries based on the level of its organisation 
-     * units. Sets the organisation unit level on each query.
+     * If organisation units appear as dimensions; groups the given query into 
+     * sub queries based on the level of the organisation units. Sets the organisation 
+     * unit level on each query. If organisation units appear as filter; replaces
+     * the organisation unit filter with one filter for each level. Sets the dimension
+     * names and filter names respectively.
      */
     private List<DataQueryParams> groupByOrgUnitLevel( DataQueryParams params )
     {
@@ -366,14 +375,18 @@
         }
         else if ( params.getFilterOrganisationUnits() != null && !params.getFilterOrganisationUnits().isEmpty() )
         {
+            Dimension filter = params.getFilter( ORGUNIT_DIM_ID );
+            
             ListMap<Integer, IdentifiableObject> levelOrgUnitMap = getLevelOrgUnitMap( params.getFilterOrganisationUnits() );
             
+            params.removeFilter( ORGUNIT_DIM_ID );
+            
             for ( Integer level : levelOrgUnitMap.keySet() )
             {
-                DataQueryParams query = new DataQueryParams( params );
-                query.setFilterOptions( ORGUNIT_DIM_ID, DimensionType.ORGANISATIONUNIT, LEVEL_PREFIX + level, levelOrgUnitMap.get( level ) );
-                queries.add( query );
+                params.getFilters().add( new Dimension( filter.getDimension(), filter.getType(), LEVEL_PREFIX + level, levelOrgUnitMap.get( level ) ) );
             }
+            
+            queries.add( params );
         }
         else
         {

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManager.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManager.java	2013-03-08 13:39:50 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManager.java	2013-03-08 16:18:21 +0000
@@ -122,20 +122,31 @@
         
         for ( Dimension dim : dimensions )
         {
-            if ( !dim.isAllOptions() )
+            if ( !dim.isAllItems() )
             {
-                sql += sqlHelper.whereAnd() + " " + dim.getDimensionName() + " in (" + getQuotedCommaDelimitedString( getUids( dim.getItems() ) ) + " ) ";
+                sql += sqlHelper.whereAnd() + " " + dim.getDimensionName() + " in (" + getQuotedCommaDelimitedString( getUids( dim.getItems() ) ) + ") ";
             }
         }
 
-        for ( Dimension filter : params.getFilters() )
+        ListMap<String, Dimension> filters = params.getDimensionFilterMap();
+        
+        //TODO check if any items for all filters
+        
+        for ( String dimension : filters.keySet() )
         {
-            if ( !filter.isAllOptions() )
+            sql += sqlHelper.whereAnd() + " (";
+            
+            for ( Dimension filter : filters.get( dimension ) )
             {
-                sql += sqlHelper.whereAnd() + " " + filter.getDimensionName() + " in (" + getQuotedCommaDelimitedString( getUids( filter.getItems() ) ) + " ) ";
+                if ( filter != null && filter.hasItems() )
+                {
+                    sql += filter.getDimensionName() + " in (" + getQuotedCommaDelimitedString( getUids( filter.getItems() ) ) + ") or ";
+                }
             }
+            
+            sql = sql.substring( 0, sql.length() - " or ".length() ) + ") ";
         }
-        
+                
         sql += "group by " + getCommaDelimitedString( dimensions );
     
         log.info( sql );