← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 10949: Centralized code

 

------------------------------------------------------------
revno: 10949
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2013-05-23 11:23:19 +0200
message:
  Centralized code
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseAnalyticalObject.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java
  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-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.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/common/BaseAnalyticalObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseAnalyticalObject.java	2013-05-22 16:37:59 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseAnalyticalObject.java	2013-05-23 09:23:19 +0000
@@ -406,6 +406,23 @@
         return categoryDims;
     }
     
+    public static String getId( List<NameableObject> column, List<NameableObject> row )
+    {
+        StringBuilder id = new StringBuilder();
+        
+        for ( NameableObject item : column )
+        {
+            id.append( item.getUid() ).append( "-" );
+        }
+        
+        for ( NameableObject item : row )
+        {
+            id.append( item.getUid() ).append( "-" );
+        }
+        
+        return id.substring( 0, id.length() - 1 );
+    }
+    
     public void mergeWith( BaseAnalyticalObject other )
     {
         super.mergeWith( other );

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java	2013-04-15 18:04:45 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java	2013-05-23 09:23:19 +0000
@@ -238,6 +238,14 @@
     Grid addRegressionColumn( int columnIndex, boolean addHeader );
     
     /**
+     * Adds columns with regression values to the given grid.
+     *
+     * @param startColumnIndex the index of the first data column.
+     * @param numberOfColumns  the number of data columns.
+     */
+    Grid addRegressionToGrid( int startColumnIndex, int numberOfColumns );
+    
+    /**
      * Adds a cumulative column to the grid. Column must hold numeric data.
      * 
      * @param columnIndex the index of the base column.
@@ -246,6 +254,14 @@
     Grid addCumulativeColumn( int columnIndex, boolean addHeader );
     
     /**
+     * Adds columns with cumulative values to the given grid.
+     *
+     * @param startColumnIndex the index of the first data column.
+     * @param numberOfColumns  the number of data columns.
+     */
+    Grid addCumulativesToGrid( int startColumnIndex, int numberOfColumns );
+    
+    /**
      * Substitutes the values in the meta columns with the mapped value in the
      * meta-data map.
      * 

=== 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	2013-05-22 16:37:59 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/reporttable/ReportTable.java	2013-05-23 09:23:19 +0000
@@ -38,11 +38,14 @@
 import java.util.List;
 import java.util.Map;
 
+import org.apache.commons.lang.StringUtils;
 import org.hisp.dhis.common.BaseAnalyticalObject;
 import org.hisp.dhis.common.BaseNameableObject;
 import org.hisp.dhis.common.CombinationGenerator;
 import org.hisp.dhis.common.DimensionalObject;
 import org.hisp.dhis.common.DxfNamespaces;
+import org.hisp.dhis.common.Grid;
+import org.hisp.dhis.common.GridHeader;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.NameableObject;
 import org.hisp.dhis.common.view.DetailedView;
@@ -519,6 +522,26 @@
     }
 
     /**
+     * Checks whether the given List of IdentifiableObjects contains an object
+     * which is an OrganisationUnit and has the currentParent property set to
+     * true.
+     *
+     * @param objects the List of IdentifiableObjects.
+     */
+    public static boolean isCurrentParent( List<? extends IdentifiableObject> objects )
+    {
+        for ( IdentifiableObject object : objects )
+        {
+            if ( object != null && object instanceof OrganisationUnit && ((OrganisationUnit) object).isCurrentParent() )
+            {
+                return true;
+            }
+        }
+
+        return false;
+    }
+    
+    /**
      * Returns null-safe sort order, none if null.
      */
     public int sortOrder()
@@ -595,6 +618,117 @@
         }
     }
 
+    /**
+     * Generates a grid for this report table based on the given aggregate value
+     * map.
+     *
+     * @param grid the grid, should be empty and not null.
+     * @param valueMap the mapping of identifiers to aggregate values.
+     * @param format the I18nFormat.
+     * @return a grid.
+     */
+    public Grid getGrid( Grid grid, Map<String, Double> valueMap )
+    {
+        final String subtitle = StringUtils.trimToEmpty( getParentOrganisationUnitName() ) + SPACE
+            + StringUtils.trimToEmpty( getReportingPeriodName() );
+
+        grid.setTitle( getName() + " - " + subtitle );
+
+        // ---------------------------------------------------------------------
+        // Headers
+        // ---------------------------------------------------------------------
+
+        for ( String row : getRowDimensions() )
+        {
+            String name = StringUtils.defaultIfEmpty( DimensionalObject.PRETTY_NAMES.get( row ), row );
+            
+            grid.addHeader( new GridHeader( name + " ID", row + "_id", String.class.getName(), true, true ) );
+            grid.addHeader( new GridHeader( name, row + "_name", String.class.getName(), false, true ) );
+            grid.addHeader( new GridHeader( name + " code", row + "_code", String.class.getName(), true, true ) );
+            grid.addHeader( new GridHeader( name + " description", row + "_description", String.class.getName(), true, true ) );
+        }
+        
+        grid.addHeader( new GridHeader( "Reporting month", REPORTING_MONTH_COLUMN_NAME,
+            String.class.getName(), true, true ) );
+        grid.addHeader( new GridHeader( "Organisation unit parameter",
+            PARAM_ORGANISATIONUNIT_COLUMN_NAME, String.class.getName(), true, true ) );
+        grid.addHeader( new GridHeader( "Organisation unit is parent",
+            ORGANISATION_UNIT_IS_PARENT_COLUMN_NAME, String.class.getName(), true, true ) );
+
+        final int startColumnIndex = grid.getHeaders().size();
+        final int numberOfColumns = getGridColumns().size();
+
+        for ( List<NameableObject> column : getGridColumns() )
+        {
+            grid.addHeader( new GridHeader( getPrettyColumnName( column ), getColumnName( column ), Double.class
+                .getName(), false, false ) );
+        }
+
+        // ---------------------------------------------------------------------
+        // Values
+        // ---------------------------------------------------------------------
+
+        for ( List<NameableObject> row : getGridRows() )
+        {
+            grid.addRow();
+
+            // -----------------------------------------------------------------
+            // Row meta data
+            // -----------------------------------------------------------------
+
+            for ( NameableObject object : row )
+            {
+                grid.addValue( object.getUid() );
+                grid.addValue( object.getName() );
+                grid.addValue( object.getCode() );
+                grid.addValue( object.getDescription() );
+            }
+            
+            grid.addValue( getReportingPeriodName() );
+            grid.addValue( getParentOrganisationUnitName() );
+            grid.addValue( isCurrentParent( row ) ? "Yes" : "No" );
+
+            // -----------------------------------------------------------------
+            // Row data values
+            // -----------------------------------------------------------------
+
+            for ( List<NameableObject> column : getGridColumns() )
+            {
+                String key = BaseAnalyticalObject.getId( column, row );
+                
+                Double value = valueMap.get( key );
+                
+                grid.addValue( value );
+            }
+        }
+
+        if ( isRegression() )
+        {
+            grid.addRegressionToGrid( startColumnIndex, numberOfColumns );
+        }
+
+        if ( isCumulative() )
+        {
+            grid.addCumulativesToGrid( startColumnIndex, numberOfColumns );
+        }
+
+        // ---------------------------------------------------------------------
+        // Sort and limit
+        // ---------------------------------------------------------------------
+
+        if ( sortOrder() != ReportTable.NONE )
+        {
+            grid.sortGrid( grid.getWidth(), sortOrder() );
+        }
+
+        if ( topLimit() > 0 )
+        {
+            grid.limitGrid( topLimit() );
+        }
+
+        return grid;
+    }
+    
     // -------------------------------------------------------------------------
     // Supportive methods
     // -------------------------------------------------------------------------

=== 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	2013-05-22 16:37:59 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/reporttable/impl/DefaultReportTableService.java	2013-05-23 09:23:19 +0000
@@ -27,28 +27,16 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import static org.hisp.dhis.reporttable.ReportTable.ORGANISATION_UNIT_IS_PARENT_COLUMN_NAME;
-import static org.hisp.dhis.reporttable.ReportTable.PARAM_ORGANISATIONUNIT_COLUMN_NAME;
-import static org.hisp.dhis.reporttable.ReportTable.REPORTING_MONTH_COLUMN_NAME;
-import static org.hisp.dhis.reporttable.ReportTable.SPACE;
-import static org.hisp.dhis.reporttable.ReportTable.getColumnName;
-import static org.hisp.dhis.reporttable.ReportTable.getPrettyColumnName;
-
 import java.util.Collection;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
-import org.apache.commons.lang.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.analytics.AnalyticsService;
-import org.hisp.dhis.common.DimensionalObject;
 import org.hisp.dhis.common.GenericIdentifiableObjectStore;
 import org.hisp.dhis.common.Grid;
-import org.hisp.dhis.common.GridHeader;
-import org.hisp.dhis.common.IdentifiableObject;
-import org.hisp.dhis.common.NameableObject;
 import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
@@ -63,7 +51,6 @@
 
 /**
  * @author Lars Helge Overland
- * @version $Id$
  */
 @Transactional
 public class DefaultReportTableService
@@ -71,10 +58,6 @@
 {
     private static final Log log = LogFactory.getLog( DefaultReportTableService.class );
 
-    private static final String YES = "Yes";
-
-    private static final String NO = "No";
-
     // ---------------------------------------------------------------------
     // Dependencies
     // ---------------------------------------------------------------------
@@ -129,7 +112,9 @@
                 
         reportTable.init( currentUserService.getCurrentUser(), reportingPeriod, organisationUnit, format );
 
-        return getGrid( reportTable, format );
+        Map<String, Double> valueMap = analyticsService.getAggregatedDataValueMapping( reportTable, format );
+
+        return reportTable.getGrid( new ListGrid(), valueMap );
     }
 
     @Override
@@ -222,195 +207,5 @@
     public List<ReportTable> getReportTablesBetween( int first, int max )
     {
         return reportTableStore.getAllOrderedName( first, max );
-    }
-
-    // -------------------------------------------------------------------------
-    // Supportive methods
-    // -------------------------------------------------------------------------
-
-    /**
-     * Generates a grid based on the given report table.
-     *
-     * @param reportTable the report table.
-     * @return a grid.
-     */
-    private Grid getGrid( ReportTable reportTable, I18nFormat format )
-    {
-        final String subtitle = StringUtils.trimToEmpty( reportTable.getParentOrganisationUnitName() ) + SPACE
-            + StringUtils.trimToEmpty( reportTable.getReportingPeriodName() );
-
-        final Grid grid = new ListGrid().setTitle( reportTable.getName() + " - " + subtitle );
-
-        Map<String, Double> map = analyticsService.getAggregatedDataValueMapping( reportTable, format );
-
-        // ---------------------------------------------------------------------
-        // Headers
-        // ---------------------------------------------------------------------
-
-        for ( String row : reportTable.getRowDimensions() )
-        {
-            String name = StringUtils.defaultIfEmpty( DimensionalObject.PRETTY_NAMES.get( row ), row );
-            
-            grid.addHeader( new GridHeader( name + " ID", row + "_id", String.class.getName(), true, true ) );
-            grid.addHeader( new GridHeader( name, row + "_name", String.class.getName(), false, true ) );
-            grid.addHeader( new GridHeader( name + " code", row + "_code", String.class.getName(), true, true ) );
-            grid.addHeader( new GridHeader( name + " description", row + "_description", String.class.getName(), true, true ) );
-        }
-        
-        grid.addHeader( new GridHeader( "Reporting month", REPORTING_MONTH_COLUMN_NAME,
-            String.class.getName(), true, true ) );
-        grid.addHeader( new GridHeader( "Organisation unit parameter",
-            PARAM_ORGANISATIONUNIT_COLUMN_NAME, String.class.getName(), true, true ) );
-        grid.addHeader( new GridHeader( "Organisation unit is parent",
-            ORGANISATION_UNIT_IS_PARENT_COLUMN_NAME, String.class.getName(), true, true ) );
-
-        final int startColumnIndex = grid.getHeaders().size();
-        final int numberOfColumns = reportTable.getGridColumns().size();
-
-        for ( List<NameableObject> column : reportTable.getGridColumns() )
-        {
-            grid.addHeader( new GridHeader( getPrettyColumnName( column ), getColumnName( column ), Double.class
-                .getName(), false, false ) );
-        }
-
-        // ---------------------------------------------------------------------
-        // Values
-        // ---------------------------------------------------------------------
-
-        for ( List<NameableObject> row : reportTable.getGridRows() )
-        {
-            grid.addRow();
-
-            // -----------------------------------------------------------------
-            // Row meta data
-            // -----------------------------------------------------------------
-
-            for ( NameableObject object : row )
-            {
-                grid.addValue( object.getUid() );
-                grid.addValue( object.getName() );
-                grid.addValue( object.getCode() );
-                grid.addValue( object.getDescription() );
-            }
-            
-            grid.addValue( reportTable.getReportingPeriodName() );
-            grid.addValue( reportTable.getParentOrganisationUnitName() );
-            grid.addValue( isCurrentParent( row ) ? YES : NO );
-
-            // -----------------------------------------------------------------
-            // Row data values
-            // -----------------------------------------------------------------
-
-            for ( List<NameableObject> column : reportTable.getGridColumns() )
-            {
-                String key = getId( column, row );
-                Double value = map.get( key );
-                
-                grid.addValue( value );
-            }
-
-            //TODO totals and sub totals
-        }
-
-        if ( reportTable.isRegression() )
-        {
-            addRegressionToGrid( grid, startColumnIndex, numberOfColumns );
-        }
-
-        if ( reportTable.isCumulative() )
-        {
-            addCumulativesToGrid( grid, startColumnIndex, numberOfColumns );
-        }
-
-        // ---------------------------------------------------------------------
-        // Sort and limit
-        // ---------------------------------------------------------------------
-
-        if ( reportTable.sortOrder() != ReportTable.NONE )
-        {
-            grid.sortGrid( grid.getWidth(), reportTable.sortOrder() );
-        }
-
-        if ( reportTable.topLimit() > 0 )
-        {
-            grid.limitGrid( reportTable.topLimit() );
-        }
-
-        return grid;
-    }
-    
-    private String getId( List<NameableObject> column, List<NameableObject> row )
-    {
-        StringBuilder id = new StringBuilder();
-        
-        for ( NameableObject item : column )
-        {
-            id.append( item.getUid() ).append( "-" );
-        }
-        
-        for ( NameableObject item : row )
-        {
-            id.append( item.getUid() ).append( "-" );
-        }
-        
-        return id.substring( 0, id.length() - 1 );
-    }
-
-    /**
-     * Adds columns with regression values to the given grid.
-     *
-     * @param grid             the grid.
-     * @param startColumnIndex the index of the first data column.
-     * @param numberOfColumns  the number of data columns.
-     */
-    private Grid addRegressionToGrid( Grid grid, int startColumnIndex, int numberOfColumns )
-    {
-        for ( int i = 0; i < numberOfColumns; i++ )
-        {
-            int columnIndex = i + startColumnIndex;
-
-            grid.addRegressionColumn( columnIndex, true );
-        }
-
-        return grid;
-    }
-
-    /**
-     * Adds columns with cumulative values to the given grid.
-     *
-     * @param grid             the grid.
-     * @param startColumnIndex the index of the first data column.
-     * @param numberOfColumns  the number of data columns.
-     */
-    private Grid addCumulativesToGrid( Grid grid, int startColumnIndex, int numberOfColumns )
-    {
-        for ( int i = 0; i < numberOfColumns; i++ )
-        {
-            int columnIndex = i + startColumnIndex;
-
-            grid.addCumulativeColumn( columnIndex, true );
-        }
-
-        return grid;
-    }
-
-    /**
-     * Checks whether the given List of IdentifiableObjects contains an object
-     * which is an OrganisationUnit and has the currentParent property set to
-     * true.
-     *
-     * @param objects the List of IdentifiableObjects.
-     */
-    private boolean isCurrentParent( List<? extends IdentifiableObject> objects )
-    {
-        for ( IdentifiableObject object : objects )
-        {
-            if ( object != null && object instanceof OrganisationUnit && ((OrganisationUnit) object).isCurrentParent() )
-            {
-                return true;
-            }
-        }
-
-        return false;
-    }
+    } 
 }

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java	2013-04-15 18:04:45 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java	2013-05-23 09:23:19 +0000
@@ -503,6 +503,18 @@
         return this;
     }
 
+    public Grid addRegressionToGrid( int startColumnIndex, int numberOfColumns )
+    {
+        for ( int i = 0; i < numberOfColumns; i++ )
+        {
+            int columnIndex = i + startColumnIndex;
+
+            this.addRegressionColumn( columnIndex, true );
+        }
+
+        return this;
+    }
+
     public Grid addCumulativeColumn( int columnIndex, boolean addHeader )
     {
         verifyGridState();
@@ -539,7 +551,19 @@
 
         return this;
     }
-    
+
+    public Grid addCumulativesToGrid( int startColumnIndex, int numberOfColumns )
+    {
+        for ( int i = 0; i < numberOfColumns; i++ )
+        {
+            int columnIndex = i + startColumnIndex;
+
+            this.addCumulativeColumn( columnIndex, true );
+        }
+
+        return this;
+    }
+
     public Grid substituteMetaData( Map<Object, Object> metaDataMap )
     {
         if ( metaDataMap == null || headers == null || headers.isEmpty() )