← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16114: Impl method Grid.getUniqueValues

 

------------------------------------------------------------
revno: 16114
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2014-07-13 11:53:10 +0200
message:
  Impl method Grid.getUniqueValues
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventAnalyticsService.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/chart/impl/DefaultChartService.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	2014-07-12 22:07:01 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java	2014-07-13 09:53:10 +0000
@@ -31,6 +31,7 @@
 import java.sql.ResultSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import org.springframework.jdbc.support.rowset.SqlRowSet;
 
@@ -98,6 +99,13 @@
     List<GridHeader> getVisibleHeaders();
     
     /**
+     * Returns the index of the header with the given name.
+     * 
+     * @param name the name of the grid header.
+     */
+    int getIndexOfHeader( String name );
+    
+    /**
      * Adds a header value.
      */
     Grid addHeader( GridHeader header );
@@ -300,12 +308,18 @@
     
     /**
      * Returns indexes of the meta grid headers.
-     * 
-     * @return List of indexes of meta headers.
      */
     List<Integer> getMetaColumnIndexes();
     
     /**
+     * Returns the unique set of values from the grid column with the given name.
+     * The name refers to the name of the grid header of the column.
+     * 
+     * @param columnName name of the column grid header.
+     */
+    Set<Object> getUniqueValues( String columnName );
+    
+    /**
      * 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-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventAnalyticsService.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventAnalyticsService.java	2014-07-12 22:07:01 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/EventAnalyticsService.java	2014-07-13 09:53:10 +0000
@@ -28,7 +28,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.Map;
 import java.util.Set;
 
 import org.hisp.dhis.analytics.SortOrder;
@@ -45,9 +44,7 @@
     
     Grid getAggregatedEventData( EventQueryParams params );
     
-    Map<String, Double> getAggregatedEventDataMappping( EventQueryParams params );
-    
-    Map<String, Double> getAggregatedEventDataMappping( BaseAnalyticalObject object, I18nFormat format );
+    Grid getAggregatedEventData( BaseAnalyticalObject object, I18nFormat format );
     
     Grid getEvents( EventQueryParams params );
 

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java	2014-07-13 09:35:14 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/DefaultEventAnalyticsService.java	2014-07-13 09:53:10 +0000
@@ -77,7 +77,6 @@
 import org.hisp.dhis.program.ProgramService;
 import org.hisp.dhis.program.ProgramStage;
 import org.hisp.dhis.program.ProgramStageService;
-import org.hisp.dhis.system.grid.GridUtils;
 import org.hisp.dhis.system.grid.ListGrid;
 import org.hisp.dhis.system.util.DateUtils;
 import org.hisp.dhis.system.util.ListUtils;
@@ -218,18 +217,11 @@
         return grid;
     }
     
-    public Map<String, Double> getAggregatedEventDataMappping( EventQueryParams params )
-    {        
-        Grid grid = getAggregatedEventData( params );
-        int valueIndex = grid.getWidth() - 1;
-        return GridUtils.getMetaValueMapping( grid, valueIndex );
-    }
-    
-    public Map<String, Double> getAggregatedEventDataMappping( BaseAnalyticalObject object, I18nFormat format )
+    public Grid getAggregatedEventData( BaseAnalyticalObject object, I18nFormat format )
     {
         EventQueryParams params = getFromAnalyticalObject( object, format );
         
-        return getAggregatedEventDataMappping( params );
+        return getAggregatedEventData( params );
     }
 
     public Grid getEvents( EventQueryParams params )

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/chart/impl/DefaultChartService.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/chart/impl/DefaultChartService.java	2014-07-12 22:07:01 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/chart/impl/DefaultChartService.java	2014-07-13 09:53:10 +0000
@@ -63,6 +63,7 @@
 import org.hisp.dhis.common.AnalyticalObjectStore;
 import org.hisp.dhis.common.AnalyticsType;
 import org.hisp.dhis.common.DimensionalObject;
+import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.NameableObject;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
@@ -79,6 +80,7 @@
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodService;
 import org.hisp.dhis.period.RelativePeriods;
+import org.hisp.dhis.system.grid.GridUtils;
 import org.hisp.dhis.system.util.Filter;
 import org.hisp.dhis.system.util.FilterUtils;
 import org.hisp.dhis.system.util.MathUtils;
@@ -732,7 +734,9 @@
         }
         else if ( chart.isAnalyticsType( AnalyticsType.EVENT ) )
         {
-            valueMap = eventAnalyticsService.getAggregatedEventDataMappping( chart, chart.getFormat() );
+            Grid grid = eventAnalyticsService.getAggregatedEventData( chart, chart.getFormat() );
+            
+            valueMap = GridUtils.getMetaValueMapping( grid, ( grid.getWidth() - 1 ) );
         }
 
         DefaultCategoryDataset regularDataSet = new DefaultCategoryDataset();

=== 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	2014-07-12 22:07:01 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java	2014-07-13 09:53:10 +0000
@@ -37,8 +37,10 @@
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import net.sf.jasperreports.engine.JRException;
 import net.sf.jasperreports.engine.JRField;
@@ -219,7 +221,12 @@
 
         return tempHeaders;
     }
-
+    
+    public int getIndexOfHeader( String name )
+    {
+        return headers.indexOf( new GridHeader( name, null ) );
+    }
+    
     @JsonProperty
     @JsonView( { DetailedView.class } )
     public int getHeight()
@@ -673,6 +680,21 @@
         
         return indexes;
     }
+    
+    public Set<Object> getUniqueValues( String columnName )
+    {
+        int columnIndex = getIndexOfHeader( columnName );
+                
+        Set<Object> values = new HashSet<>();
+        
+        if ( columnIndex != -1 )
+        {
+            List<Object> column = getColumn( columnIndex );
+            values.addAll( column );            
+        }
+        
+        return values;
+    }
             
     // -------------------------------------------------------------------------
     // 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	2014-07-12 22:07:01 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/GridTest.java	2014-07-13 09:53:10 +0000
@@ -36,8 +36,10 @@
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.GridHeader;
@@ -568,6 +570,23 @@
         
         assertEquals( expected, gridA.getMetaColumnIndexes() );
     }
+
+    @Test
+    public void testGetUniqueValues()
+    {
+        gridA.addRow();
+        gridA.addValue( 11 );
+        gridA.addValue( 12 );
+        gridA.addValue( 13 );
+        
+        Set<Object> expected = new HashSet<>();
+        expected.add( 12 );
+        expected.add( 22 );
+        expected.add( 32 );
+        expected.add( 42 );
+        
+        assertEquals( expected, gridA.getUniqueValues( "ColB" ) );
+    }
     
     @Test
     public void testJRDataSource() throws Exception