← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2822: Simplifactions in report table

 

Merge authors:
  Lars Helge Øverland (larshelge)
------------------------------------------------------------
revno: 2822 [merge]
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2011-02-08 01:05:22 +0100
message:
  Simplifactions in report table
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/reporttable/ReportTable.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/reporttable/impl/DefaultReportTableService.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/reporttable/jdbc/JDBCReportTableManager.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/reporttable/statement/GetReportTableDataStatement.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-api/src/main/java/org/hisp/dhis/reporttable/ReportTable.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/reporttable/ReportTable.java	2011-01-31 19:47:04 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/reporttable/ReportTable.java	2011-02-07 08:43:29 +0000
@@ -112,6 +112,18 @@
     private static final String TABLE_PREFIX = "_report_";
     private static final String REGEX_NUMERIC = "([0-9]*)";
 
+    private static final Map<String, String> MODE_ID_MAP = new HashMap<String, String>() { {
+        put( MODE_INDICATORS, INDICATOR_ID );
+        put( MODE_DATAELEMENTS, DATAELEMENT_ID );
+        put( MODE_DATASETS, DATASET_ID );
+    } };
+    
+    private static final Map<String, String> MODE_NAME_MAP = new HashMap<String, String>() { {
+        put( MODE_INDICATORS, INDICATOR_NAME );
+        put( MODE_DATAELEMENTS, DATAELEMENT_NAME );
+        put( MODE_DATASETS, DATASET_NAME );
+    } };
+        
     // -------------------------------------------------------------------------
     // Persisted properties
     // -------------------------------------------------------------------------
@@ -459,7 +471,7 @@
             crossTabIndicators.addAll( dataElements );
             crossTabIndicators.addAll( dataSets );
             reportIndicators.add( null );
-            selectColumns.add( getIdentifier( mode ) );
+            selectColumns.add( MODE_ID_MAP.get( mode ) );
         }
         else
         {
@@ -468,8 +480,8 @@
             reportIndicators.addAll( indicators );
             reportIndicators.addAll( dataElements );
             reportIndicators.addAll( dataSets );
-            indexColumns.add( getIdentifier( mode ) );
-            indexNameColumns.add( getName( mode ) );
+            indexColumns.add( MODE_ID_MAP.get( mode ) );
+            indexNameColumns.add( MODE_NAME_MAP.get( mode ) );
         }
         
         if ( isDimensional() ) // Category options will be crosstab if dimensional
@@ -542,8 +554,8 @@
                 {
                     for ( OrganisationUnit unit : crossTabUnits )
                     {
-                        String columnName = getColumnName( indicator, categoryOptionCombo, period, unit );
-                        String prettyColumnName = getPrettyColumnName( indicator, categoryOptionCombo, period, unit );
+                        String columnName = getColumnName( indicator, categoryOptionCombo, period, unit, i18nFormat );
+                        String prettyColumnName = getPrettyColumnName( indicator, categoryOptionCombo, period, unit, i18nFormat );
                         String columnIdentifier = getColumnIdentifier( indicator, categoryOptionCombo, period, unit );
                         
                         if ( columnName != null && !columnName.isEmpty() )
@@ -719,57 +731,15 @@
     /**
      * Generates a prefixed, database encoded name.
      */
-    private String generateTableName( String name )
+    private static String generateTableName( String name )
     {
         return TABLE_PREFIX + databaseEncode( name );
     }
-
-    /**
-     * Returns a mode identifier.
-     */
-    private String getIdentifier( String mode )
-    {
-        if ( mode == null || mode.equals( MODE_INDICATORS ) )
-        {
-            return INDICATOR_ID;
-        }
-        else if ( mode.equals( MODE_DATAELEMENTS ) )
-        {
-            return DATAELEMENT_ID;
-        }
-        else if ( mode.equals( MODE_DATASETS ) )
-        {
-            return DATASET_ID;
-        }
-        
-        return null;
-    }
-    
-    /**
-     * Returns a mode name.
-     */
-    private String getName( String mode )
-    {
-        if ( mode == null || mode.equals( MODE_INDICATORS ) )
-        {
-            return INDICATOR_NAME;
-        }
-        else if ( mode.equals( MODE_DATAELEMENTS ) )
-        {
-            return DATAELEMENT_NAME;
-        }
-        else if ( mode.equals( MODE_DATASETS ) )
-        {
-            return DATASET_NAME;
-        }
-        
-        return null;
-    }
     
     /**
      * Returns the number of empty lists among the argument lists.
      */
-    private int nonEmptyLists( List<?>... lists )
+    private static int nonEmptyLists( List<?>... lists )
     {
         int nonEmpty = 0;
         
@@ -787,7 +757,7 @@
     /**
      * Tests whether the argument list is not null and has no elements.
      */
-    private boolean listIsNonEmpty( List<?> list )
+    private static boolean listIsNonEmpty( List<?> list )
     {
         return list != null && list.size() > 0;
     }
@@ -796,7 +766,7 @@
      * Generates a pretty-print column name based on short-names of the argument
      * objects. Null arguments are ignored in the name.
      */
-    private String getPrettyColumnName( IdentifiableObject metaObject, DataElementCategoryOptionCombo categoryOptionCombo, Period period, OrganisationUnit unit )
+    private static String getPrettyColumnName( IdentifiableObject metaObject, DataElementCategoryOptionCombo categoryOptionCombo, Period period, OrganisationUnit unit, I18nFormat format )
     {
         StringBuffer buffer = new StringBuffer();
         
@@ -810,7 +780,7 @@
         }
         if ( period != null )
         {
-            String periodName = i18nFormat == null ? period.getName() : i18nFormat.formatPeriod( period );
+            String periodName = format == null ? period.getName() : format.formatPeriod( period );
             
             buffer.append( periodName + SPACE );
         }
@@ -826,7 +796,7 @@
      * Generates a column name based on short-names of the argument objects. Null 
      * arguments are ignored in the name.
      */
-    private String getColumnName( IdentifiableObject metaObject, DataElementCategoryOptionCombo categoryOptionCombo, Period period, OrganisationUnit unit )
+    private static String getColumnName( IdentifiableObject metaObject, DataElementCategoryOptionCombo categoryOptionCombo, Period period, OrganisationUnit unit, I18nFormat format )
     {
         StringBuffer buffer = new StringBuffer();
         
@@ -840,7 +810,7 @@
         }
         if ( period != null )
         {
-            String periodName = period.getName() != null ? period.getName() : i18nFormat.formatPeriod( period );
+            String periodName = period.getName() != null ? period.getName() : format.formatPeriod( period );
             
             buffer.append( periodName + SEPARATOR );
         }
@@ -858,27 +828,15 @@
      * Generates a column identifier based on the internal identifiers of the
      * argument objects. Null arguments are ignored in the identifier. 
      */
-    private String getColumnIdentifier( IdentifiableObject metaObject, DataElementCategoryOptionCombo categoryOptionCombo, Period period, OrganisationUnit unit )
+    private static String getColumnIdentifier( IdentifiableObject... objects )
     {
         StringBuffer buffer = new StringBuffer();
 
-        if ( metaObject != null )
-        {
-            buffer.append( metaObject.getId() + SEPARATOR );
-        }
-        if ( categoryOptionCombo != null )
-        {
-            buffer.append( categoryOptionCombo.getId() + SEPARATOR );
-        }
-        if ( period != null )
-        {
-            buffer.append( period.getId() + SEPARATOR );
-        }
-        if ( unit != null )
-        {
-            buffer.append( unit.getId() + SEPARATOR );
-        }
-
+        for ( IdentifiableObject object : objects )
+        {
+            buffer.append( object != null ? ( object.getId() + SEPARATOR ) : EMPTY );
+        }
+        
         return buffer.length() > 0 ? buffer.substring( 0, buffer.lastIndexOf( SEPARATOR ) ) : buffer.toString();
     }
     
@@ -887,7 +845,7 @@
      * string, prefixes the string if it starts with a numeric character and
      * truncates the string if it is longer than 255 characters.
      */
-    private String databaseEncode( String string )
+    private static String databaseEncode( String string )
     {
         if ( string != null )
         {
@@ -938,7 +896,7 @@
     /**
      * Removes duplicates from the given list while maintaining the order.
      */
-    private <T> List<T> removeDuplicates( List<T> list )
+    private static <T> List<T> removeDuplicates( List<T> list )
     {
         final List<T> temp = new ArrayList<T>( list );
         Collections.reverse( temp );        
@@ -958,7 +916,7 @@
     /**
      * Supportive method.
      */
-    private void verify( boolean expression, String falseMessage )
+    private static void verify( boolean expression, String falseMessage )
     {
         if ( !expression )
         {

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/reporttable/impl/DefaultReportTableService.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/reporttable/impl/DefaultReportTableService.java	2011-02-03 17:03:02 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/reporttable/impl/DefaultReportTableService.java	2011-02-06 14:55:33 +0000
@@ -545,8 +545,7 @@
                         // Values
                         // -----------------------------------------------------
 
-                        Map<String, Double> map = reportTableManager.getAggregatedValueMap( 
-                            reportTable, metaObject, (DataElementCategoryOptionCombo) categoryOptionCombo, period, unit );
+                        Map<String, Double> map = reportTableManager.getAggregatedValueMap( reportTable, metaObject, categoryOptionCombo, period, unit );
                         
                         for ( String identifier : reportTable.getCrossTabIdentifiers() )
                         {

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/reporttable/jdbc/JDBCReportTableManager.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/reporttable/jdbc/JDBCReportTableManager.java	2011-01-29 07:57:17 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/reporttable/jdbc/JDBCReportTableManager.java	2011-02-06 14:55:33 +0000
@@ -149,8 +149,6 @@
             
             log.debug( "Get values statement: " + statement.getStatement() );
             
-            int columnCount = resultSet.getMetaData().getColumnCount(); //TODO improve performance?
-            
             Map<String, Double> map = new HashMap<String, Double>();
 
             // -----------------------------------------------------------------
@@ -163,13 +161,11 @@
             {
                 final double value = resultSet.getDouble( 1 );
                 
-                final StringBuffer identifier = new StringBuffer();
+                final StringBuffer identifier = new StringBuffer(); // Identifies a row in the report table
                 
-                for ( int i = 0; i < columnCount - 1; i++ )
-                {
-                    int columnIndex = i + 2;
-                    
-                    identifier.append( resultSet.getInt( columnIndex ) + SEPARATOR );                    
+                for ( String col : reportTable.getSelectColumns() )
+                {                    
+                    identifier.append( resultSet.getInt( col ) + SEPARATOR );                    
                 }
                 
                 final String key = identifier.substring( 0, identifier.lastIndexOf( SEPARATOR ) );

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/reporttable/statement/GetReportTableDataStatement.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/reporttable/statement/GetReportTableDataStatement.java	2010-04-12 21:23:33 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/reporttable/statement/GetReportTableDataStatement.java	2011-02-06 14:55:33 +0000
@@ -32,6 +32,9 @@
 import org.hisp.dhis.reporttable.ReportTable;
 
 /**
+ * Report table can contain data elements and indicators only when crosstabbing
+ * on indicators.
+ *
  * @author Lars Helge Overland
  * @version $Id$
  */
@@ -56,7 +59,7 @@
     {
         StringBuffer buffer = new StringBuffer();
         
-        buffer.append( "SELECT SUM(value), " );
+        buffer.append( "SELECT SUM(value), " ); // Sum required for getting sum of category option combos
         
         Iterator<String> selectColumns = reportTable.getSelectColumns().iterator();