← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2875: Impl support for limit and sort in ReportTable / Grid

 

------------------------------------------------------------
revno: 2875
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2011-02-18 20:27:56 +0100
message:
  Impl support for limit and sort in ReportTable / Grid
modified:
  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-services/dhis-service-reporting/src/main/resources/org/hisp/dhis/reporttable/hibernate/ReportTable.hbm.xml
  dhis-2/dhis-services/dhis-service-reporting/src/test/java/org/hisp/dhis/reporttable/ReportTableGridTest.java
  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/test/java/org/hisp/dhis/system/grid/GridTest.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/Grid.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java	2011-02-17 22:21:12 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java	2011-02-18 19:27:56 +0000
@@ -167,7 +167,7 @@
     /**
      * Limits the grid from top by the given argument number.
      * 
-     * @param limit the top limit.
+     * @param limit the top limit, must be greater than zero to have an effect.
      */
     Grid limitGrid( int limit );
     
@@ -175,9 +175,10 @@
      * Sorts the grid ascending on the column at the given columnIndex.
      * 
      * @param columnIndex the column index, starting on 1.
-     * @param descending indicates whether to sort ascending or descending.
+     * @param order a negative value indicates ascending order, a positive value 
+     *        indicates descending order, zero value indicates no sorting.
      */
-    Grid sortGrid( int columnIndex, boolean descending );
+    Grid sortGrid( int columnIndex, int order );
     
     /**
      * Adds a regression column to the grid. Column must hold numeric data.

=== 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-02-17 22:48:17 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/reporttable/ReportTable.java	2011-02-18 19:27:56 +0000
@@ -88,6 +88,10 @@
     
     public static final String REGRESSION_COLUMN_PREFIX = "regression_";
     
+    public static final int ASC = -1;
+    public static final int DESC = 1;
+    public static final int NONE = 0;
+    
     public static final Map<String, String> PRETTY_COLUMNS = new HashMap<String, String>() { {
         put( DATAELEMENT_ID, "Data element ID" );
         put( DATAELEMENT_NAME, "Data element" );
@@ -197,6 +201,16 @@
      * The ReportParams of the ReportTable.
      */
     private ReportParams reportParams;
+    
+    /**
+     * The sort order if any applied to the last column of the table.
+     */
+    private Integer sortOrder;
+    
+    /**
+     * Inidicates whether the table should be limited from top by this value.
+     */
+    private Integer topLimit;
 
     // -------------------------------------------------------------------------
     // Transient properties
@@ -607,6 +621,22 @@
         return string;
     }
     
+    /**
+     * Returns null-safe sort order, none if null.
+     */
+    public int sortOrder()
+    {
+        return sortOrder != null ? sortOrder : NONE;
+    }
+    
+    /**
+     * Returns null-safe top limit, 0 if null;
+     */
+    public int topLimit()
+    {
+        return topLimit != null ? topLimit : 0;
+    }
+    
     // -------------------------------------------------------------------------
     // Supportive methods
     // -------------------------------------------------------------------------
@@ -924,7 +954,27 @@
     {
         this.reportParams = reportParams;
     }
-    
+
+    public Integer getSortOrder()
+    {
+        return sortOrder;
+    }
+
+    public void setSortOrder( Integer sortOrder )
+    {
+        this.sortOrder = sortOrder;
+    }
+
+    public Integer getTopLimit()
+    {
+        return topLimit;
+    }
+
+    public void setTopLimit( Integer topLimit )
+    {
+        this.topLimit = topLimit;
+    }
+
     // -------------------------------------------------------------------------
     // Get- and set-methods for transient properties
     // -------------------------------------------------------------------------

=== 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-17 16:51:39 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/reporttable/impl/DefaultReportTableService.java	2011-02-18 19:27:56 +0000
@@ -520,6 +520,20 @@
                 grid.addValue( toString( map.get( getIdentifier( row ) ) ) ); // Only category option combo is crosstab when total, row identifier will return total
             }
         }
+
+        // ---------------------------------------------------------------------
+        // Sort and limit
+        // ---------------------------------------------------------------------
+
+        if ( reportTable.topLimit() > 0 )
+        {
+            grid = grid.limitGrid( reportTable.topLimit() );
+        }
+        
+        if ( reportTable.sortOrder() != ReportTable.NONE )
+        {
+            grid.sortGrid( grid.getWidth(), reportTable.sortOrder() );
+        }
         
         return grid;
     }

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/resources/org/hisp/dhis/reporttable/hibernate/ReportTable.hbm.xml'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/resources/org/hisp/dhis/reporttable/hibernate/ReportTable.hbm.xml	2011-02-17 15:37:19 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/resources/org/hisp/dhis/reporttable/hibernate/ReportTable.hbm.xml	2011-02-18 19:27:56 +0000
@@ -82,5 +82,9 @@
       <property name="paramOrganisationUnit"/>
     </component>
     
+    <property name="sortOrder"/>
+    
+    <property name="topLimit"/>
+    
   </class>
 </hibernate-mapping>

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/test/java/org/hisp/dhis/reporttable/ReportTableGridTest.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/test/java/org/hisp/dhis/reporttable/ReportTableGridTest.java	2011-02-17 18:48:30 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/test/java/org/hisp/dhis/reporttable/ReportTableGridTest.java	2011-02-18 19:27:56 +0000
@@ -902,4 +902,50 @@
         assertEquals( String.valueOf( 17.0 ), grid.getRow( 6 ).get( 9 ) );
         assertEquals( String.valueOf( 18.0 ), grid.getRow( 7 ).get( 9 ) );
     }
+
+    @Test
+    public void testGetIndicatorReportTableTopLimit()
+    {
+        ReportTable reportTable = new ReportTable( "Embezzlement", false,
+            new ArrayList<DataElement>(), indicators, new ArrayList<DataSet>(), periods, relativePeriods, units, new ArrayList<OrganisationUnit>(), 
+            null, false, false, true, new RelativePeriods(), null, i18nFormat, "january_2000" );
+        reportTable.setTopLimit( 2 );
+        
+        int id = reportTableService.saveReportTable( reportTable );
+
+        Grid grid = reportTableService.getReportTableGrid( id, i18nFormat, 0, 0 );
+
+        assertEquals( 2, grid.getHeight() );
+        
+        assertEquals( String.valueOf( 11.0 ), grid.getRow( 0 ).get( 7 ) );
+        assertEquals( String.valueOf( 12.0 ), grid.getRow( 0 ).get( 8 ) );
+        
+        assertEquals( String.valueOf( 13.0 ), grid.getRow( 1 ).get( 7 ) );
+        assertEquals( String.valueOf( 14.0 ), grid.getRow( 1 ).get( 8 ) );
+    }
+
+    @Test
+    public void testGetIndicatorReportTableSortOrder()
+    {
+        ReportTable reportTable = new ReportTable( "Embezzlement", false,
+            new ArrayList<DataElement>(), indicators, new ArrayList<DataSet>(), periods, relativePeriods, units, new ArrayList<OrganisationUnit>(), 
+            null, false, false, true, new RelativePeriods(), null, i18nFormat, "january_2000" );
+        reportTable.setSortOrder( ReportTable.DESC );
+        
+        int id = reportTableService.saveReportTable( reportTable );
+
+        Grid grid = reportTableService.getReportTableGrid( id, i18nFormat, 0, 0 );
+
+        assertEquals( String.valueOf( 17.0 ), grid.getRow( 0 ).get( 7 ) );
+        assertEquals( String.valueOf( 18.0 ), grid.getRow( 0 ).get( 8 ) );
+
+        assertEquals( String.valueOf( 15.0 ), grid.getRow( 1 ).get( 7 ) );
+        assertEquals( String.valueOf( 16.0 ), grid.getRow( 1 ).get( 8 ) );
+
+        assertEquals( String.valueOf( 13.0 ), grid.getRow( 2 ).get( 7 ) );
+        assertEquals( String.valueOf( 14.0 ), grid.getRow( 2 ).get( 8 ) );
+        
+        assertEquals( String.valueOf( 11.0 ), grid.getRow( 3 ).get( 7 ) );
+        assertEquals( String.valueOf( 12.0 ), grid.getRow( 3 ).get( 8 ) );
+    }
 }

=== 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	2011-02-17 22:23:32 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java	2011-02-18 19:27:56 +0000
@@ -305,12 +305,15 @@
     
     public Grid limitGrid( int limit )
     {
-        grid = grid.subList( 0, limit );
+        if ( limit > 0 )
+        {
+            grid = grid.subList( 0, limit );
+        }
         
         return this;
     }
     
-    public Grid sortGrid( int columnIndex, boolean descending )
+    public Grid sortGrid( int columnIndex, int order )
     {
         columnIndex--;
         
@@ -319,7 +322,7 @@
             return this; // Silently ignore
         }
         
-        Collections.sort( grid, new GridRowComparator( columnIndex, descending ) );
+        Collections.sort( grid, new GridRowComparator( columnIndex, order ) );
         
         return this;
     }
@@ -462,18 +465,23 @@
         implements Comparator<List<String>>
     {
         private int columnIndex;
-        private boolean descending;
+        private int order;
 
-        protected GridRowComparator( int columnIndex, boolean descending )
+        protected GridRowComparator( int columnIndex, int order )
         {
             this.columnIndex = columnIndex;
-            this.descending = descending;
+            this.order = order;
         }
         
         @Override
         public int compare( List<String> list1, List<String> list2 )
         {
-            return descending ? list2.get( columnIndex ).compareTo( 
+            if ( order == 0 )
+            {
+                return 0;
+            }
+            
+            return order > 0 ? list2.get( columnIndex ).compareTo( 
                 list1.get( columnIndex ) ) : list1.get( columnIndex ).compareTo( list2.get( columnIndex ) );
         }
     }

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/GridTest.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/GridTest.java	2011-02-17 22:21:12 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/GridTest.java	2011-02-18 19:27:56 +0000
@@ -230,6 +230,10 @@
 
         List<String> rowB = grid.getRow( 1 );        
         assertTrue( rowB.contains( "21" ) );
+        
+        grid.limitGrid( 0 );
+        
+        assertEquals( 2, grid.getRows().size() );
     }
     
     @Test
@@ -241,7 +245,7 @@
         grid.addRow().addValue( "2" ).addValue( "b" );
         grid.addRow().addValue( "3" ).addValue( "c" );
         
-        grid.sortGrid( 2, true );
+        grid.sortGrid( 2, 1 );
 
         List<String> rowA = grid.getRow( 0 );
         assertTrue( rowA.contains( "c" ) );
@@ -259,7 +263,7 @@
         grid.addRow().addValue( "2" ).addValue( "b" );
         grid.addRow().addValue( "1" ).addValue( "c" );
         
-        grid.sortGrid( 1, false );
+        grid.sortGrid( 1, -1 );
 
         List<String> rowA = grid.getRow( 0 );
         assertTrue( rowA.contains( "1" ) );