← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19451: Impl Grid.getAsMap

 

------------------------------------------------------------
revno: 19451
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-06-18 14:53:41 +0200
message:
  Impl Grid.getAsMap
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.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	2015-03-07 16:16:41 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java	2015-06-18 12:53:41 +0000
@@ -320,6 +320,14 @@
     Set<Object> getUniqueValues( String columnName );
     
     /**
+     * Returns a map of each row in the grid.
+     * 
+     * @param valueIndex the index of the column to use as map values.
+     * @param keySeparator the separator to use to concatenate the map key.
+     */
+    <T> Map<String, T> getAsMap( int valueIndex, String keySeparator );
+    
+    /**
      * Adds a set of headers based on the column names of the given SQL result set.
      * 
      * @param rs the result set.

=== 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	2015-06-13 21:26:59 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java	2015-06-18 12:53:41 +0000
@@ -45,6 +45,7 @@
 import net.sf.jasperreports.engine.JRException;
 import net.sf.jasperreports.engine.JRField;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.math3.stat.regression.SimpleRegression;
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.GridHeader;
@@ -738,7 +739,28 @@
         
         return values;
     }
-            
+    
+    @SuppressWarnings("unchecked")
+    public <T> Map<String, T> getAsMap( int valueIndex, String keySeparator )
+    {
+        Map<String, T> map = new HashMap<>();
+        
+        for ( List<Object> row : grid )
+        {
+            List<Object> metaDataRow = new ArrayList<>( row );
+            
+            metaDataRow.remove( valueIndex );
+            
+            String key = StringUtils.join( metaDataRow, keySeparator );
+            
+            T value = (T) row.get( valueIndex );
+            
+            map.put( key, value );
+        }
+        
+        return map;
+    }
+    
     // -------------------------------------------------------------------------
     // JRDataSource implementation
     // -------------------------------------------------------------------------

=== 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	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/GridTest.java	2015-06-18 12:53:41 +0000
@@ -589,6 +589,18 @@
     }
     
     @Test
+    public void testGetAsMap()
+    {
+        Map<String, Integer> map = gridA.getAsMap( 2, "-" );
+        
+        assertEquals( 4, map.size() );        
+        assertEquals( Integer.valueOf( 13 ), map.get( "11-12" ) );
+        assertEquals( Integer.valueOf( 23 ), map.get( "21-22" ) );
+        assertEquals( Integer.valueOf( 33 ), map.get( "31-32" ) );
+        assertEquals( Integer.valueOf( 43 ), map.get( "41-42" ) );
+    }
+    
+    @Test
     public void testJRDataSource() throws Exception
     {
         assertTrue( gridA.next() );