← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 10118: Split long method

 

------------------------------------------------------------
revno: 10118
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2013-03-10 15:53:20 +0100
message:
  Split long method
modified:
  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/data/JdbcAnalyticsManager.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManager.java	2013-03-10 14:26:48 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManager.java	2013-03-10 14:53:20 +0000
@@ -27,15 +27,19 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import static org.hisp.dhis.analytics.AggregationType.AVERAGE_BOOL;
 import static org.hisp.dhis.analytics.AggregationType.AVERAGE_INT;
-import static org.hisp.dhis.analytics.AggregationType.AVERAGE_BOOL;
 import static org.hisp.dhis.analytics.AggregationType.AVERAGE_INT_DISAGGREGATION;
 import static org.hisp.dhis.analytics.AggregationType.COUNT;
 import static org.hisp.dhis.analytics.DataQueryParams.DIMENSION_SEP;
 import static org.hisp.dhis.analytics.DataQueryParams.VALUE_ID;
+import static org.hisp.dhis.analytics.MeasureFilter.EQ;
+import static org.hisp.dhis.analytics.MeasureFilter.GE;
+import static org.hisp.dhis.analytics.MeasureFilter.GT;
+import static org.hisp.dhis.analytics.MeasureFilter.LE;
+import static org.hisp.dhis.analytics.MeasureFilter.LT;
 import static org.hisp.dhis.common.IdentifiableObjectUtils.getUids;
 import static org.hisp.dhis.system.util.TextUtils.getQuotedCommaDelimitedString;
-import static org.hisp.dhis.analytics.MeasureFilter.*;
 
 import java.util.Collection;
 import java.util.HashMap;
@@ -52,9 +56,9 @@
 import org.hisp.dhis.analytics.Dimension;
 import org.hisp.dhis.analytics.MeasureFilter;
 import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.common.ListMap;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodType;
-import org.hisp.dhis.common.ListMap;
 import org.hisp.dhis.system.util.MathUtils;
 import org.hisp.dhis.system.util.SqlHelper;
 import org.hisp.dhis.system.util.TextUtils;
@@ -94,66 +98,11 @@
         
         params.replaceAggregationPeriodsWithDataPeriods( dataPeriodAggregationPeriodMap );
         
-        List<Dimension> dimensions = params.getQueryDimensions();
-
-        SqlHelper sqlHelper = new SqlHelper();
-
-        String sql = "select " + getCommaDelimitedString( dimensions ) + ", ";
-        
-        if ( params.isAggregationType( AVERAGE_INT ) )
-        {
-            int days = PeriodType.getPeriodTypeByName( params.getPeriodType() ).getFrequencyOrder();
-            
-            sql += "sum(daysxvalue) / " + days;
-        }
-        else if ( params.isAggregationType( AVERAGE_BOOL ) )
-        {
-            sql += "sum(daysxvalue) / sum(daysno) * 100";
-        }
-        else if ( params.isAggregationType( COUNT ) )
-        {
-            sql += "count(value)";
-        }
-        else // SUM, AVERAGE_DISAGGREGATION and undefined //TODO
-        {
-            sql += "sum(value)";
-        }
-        
-        sql += " as value from ";
-        
-        sql += params.getTableName() + " ";
-        
-        for ( Dimension dim : dimensions )
-        {
-            if ( !dim.isAllItems() )
-            {
-                sql += sqlHelper.whereAnd() + " " + dim.getDimensionName() + " in (" + getQuotedCommaDelimitedString( getUids( dim.getItems() ) ) + ") ";
-            }
-        }
-
-        ListMap<String, Dimension> filterMap = params.getDimensionFilterMap();
-        
-        for ( String dimension : filterMap.keySet() )
-        {
-            List<Dimension> filters = filterMap.get( dimension );
-            
-            if ( DataQueryParams.anyDimensionHasItems( filters ) )
-            {
-                sql += sqlHelper.whereAnd() + " (";
-                
-                for ( Dimension filter : filters )
-                {
-                    if ( filter.hasItems() )
-                    {
-                        sql += filter.getDimensionName() + " in (" + getQuotedCommaDelimitedString( getUids( filter.getItems() ) ) + ") or ";
-                    }
-                }
-            }
-            
-            sql = sql.substring( 0, sql.length() - " or ".length() ) + ") ";
-        }
-                
-        sql += "group by " + getCommaDelimitedString( dimensions );
+        String sql = getSelectClause( params );
+        
+        sql += getFromWhereClause( params );
+        
+        sql += getGroupByClause( params );
     
         log.info( sql );
 
@@ -174,7 +123,7 @@
         
         return new AsyncResult<Map<String, Double>>( map );   
     }
-
+    
     public void replaceDataPeriodsWithAggregationPeriods( Map<String, Double> dataValueMap, DataQueryParams params, ListMap<IdentifiableObject, IdentifiableObject> dataPeriodAggregationPeriodMap )
     {
         if ( params.isAggregationType( AVERAGE_INT_DISAGGREGATION ) )
@@ -217,6 +166,89 @@
     // -------------------------------------------------------------------------
 
     /**
+     * Generates the select clause of the query SQL.
+     */
+    private String getSelectClause( DataQueryParams params )
+    {
+        String sql = "select " + getCommaDelimitedString( params.getQueryDimensions() ) + ", ";
+        
+        if ( params.isAggregationType( AVERAGE_INT ) )
+        {
+            int days = PeriodType.getPeriodTypeByName( params.getPeriodType() ).getFrequencyOrder();
+            
+            sql += "sum(daysxvalue) / " + days;
+        }
+        else if ( params.isAggregationType( AVERAGE_BOOL ) )
+        {
+            sql += "sum(daysxvalue) / sum(daysno) * 100";
+        }
+        else if ( params.isAggregationType( COUNT ) )
+        {
+            sql += "count(value)";
+        }
+        else // SUM, AVERAGE_DISAGGREGATION and undefined //TODO
+        {
+            sql += "sum(value)";
+        }
+        
+        sql += " as value ";
+        
+        return sql;        
+    }
+    
+    /**
+     * Generates the from clause of the query SQL.
+     */
+    private String getFromWhereClause( DataQueryParams params )
+    {
+        SqlHelper sqlHelper = new SqlHelper();
+
+        String sql = "from " + params.getTableName() + " ";
+        
+        for ( Dimension dim : params.getQueryDimensions() )
+        {
+            if ( !dim.isAllItems() )
+            {
+                sql += sqlHelper.whereAnd() + " " + dim.getDimensionName() + " in (" + getQuotedCommaDelimitedString( getUids( dim.getItems() ) ) + ") ";
+            }
+        }
+
+        ListMap<String, Dimension> filterMap = params.getDimensionFilterMap();
+        
+        for ( String dimension : filterMap.keySet() )
+        {
+            List<Dimension> filters = filterMap.get( dimension );
+            
+            if ( DataQueryParams.anyDimensionHasItems( filters ) )
+            {
+                sql += sqlHelper.whereAnd() + " (";
+                
+                for ( Dimension filter : filters )
+                {
+                    if ( filter.hasItems() )
+                    {
+                        sql += filter.getDimensionName() + " in (" + getQuotedCommaDelimitedString( getUids( filter.getItems() ) ) + ") or ";
+                    }
+                }
+            }
+            
+            sql = sql.substring( 0, sql.length() - " or ".length() ) + ") ";
+        }
+        
+        return sql;
+    }
+    
+    /**
+     * Generates the group by clause of the query SQL.
+     */
+    private String getGroupByClause( DataQueryParams params )
+    {
+        String sql = "group by " + getCommaDelimitedString( params.getQueryDimensions() );
+        
+        return sql;
+    }
+
+    /**
      * Retrieves data from the database based on the given query and SQL and puts
      * into a value key and value mapping.
      */