← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9956: Analytics, added excel response format

 

------------------------------------------------------------
revno: 9956
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-03-04 11:30:59 +0100
message:
  Analytics, added excel response format
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java
  dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/organisationunit/hibernate/OrganisationUnit.hbm.xml
  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
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AnalyticsController.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	2013-02-19 16:12:47 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Grid.java	2013-03-04 10:30:59 +0000
@@ -77,12 +77,12 @@
     /**
      * Returns map of meta-data.
      */
-    Map<String, String> getMetaData();
+    Map<Object, String> getMetaData();
     
     /**
      * Sets map of meta-data.
      */
-    void setMetaData( Map<String, String> metaData );
+    void setMetaData( Map<Object, String> metaData );
     
     /**
      * Adds a key-value pair to meta-data.
@@ -244,6 +244,12 @@
     Grid addCumulativeColumn( int columnIndex, boolean addHeader );
     
     /**
+     * Substitutes the values in the meta columns with the mapped value in the
+     * meta-data map.
+     */
+    Grid substituteMetaData();
+    
+    /**
      * 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-core/src/main/resources/org/hisp/dhis/organisationunit/hibernate/OrganisationUnit.hbm.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/organisationunit/hibernate/OrganisationUnit.hbm.xml	2013-02-07 10:25:34 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/organisationunit/hibernate/OrganisationUnit.hbm.xml	2013-03-04 10:30:59 +0000
@@ -30,7 +30,6 @@
 
     <property name="uuid" unique="true" length="36"/>
 
-
     <property name="description" type="text" />
 
     <property name="openingDate" column="openingdate" type="date" />

=== 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-02-19 16:12:47 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/ListGrid.java	2013-03-04 10:30:59 +0000
@@ -79,7 +79,7 @@
     /**
      * A Map which can hold arbitrary meta-data.
      */
-    private Map<String, String> metaData;
+    private Map<Object, String> metaData;
 
     /**
      * A two dimensional List which simulates a grid where the first list
@@ -108,7 +108,7 @@
     public ListGrid()
     {
         headers = new ArrayList<GridHeader>();
-        metaData = new HashMap<String, String>();
+        metaData = new HashMap<Object, String>();
         grid = new ArrayList<List<Object>>();
     }
 
@@ -219,12 +219,12 @@
 
     @JsonProperty
     @JsonView( { DetailedView.class } )
-    public Map<String, String> getMetaData()
+    public Map<Object, String> getMetaData()
     {
         return metaData;
     }
     
-    public void setMetaData( Map<String, String> metaData )
+    public void setMetaData( Map<Object, String> metaData )
     {
         this.metaData = metaData;
     }
@@ -340,7 +340,7 @@
 
         return grid.get( rowIndex ).get( columnIndex );
     }
-
+    
     public Grid addColumn( List<Object> columnValues )
     {
         verifyGridState();
@@ -530,6 +530,36 @@
 
         return this;
     }
+    
+    public Grid substituteMetaData()
+    {
+        if ( metaData == null || headers == null || headers.isEmpty() )
+        {
+            return this;
+        }
+        
+        for ( int colIndex = 0; colIndex < headers.size(); colIndex++ )
+        {
+            if ( headers.get( colIndex ).isMeta() )
+            {
+                List<Object> col = getColumn( colIndex );
+                
+                for ( int rowIndex = 0; rowIndex < col.size(); rowIndex++ )
+                {
+                    Object object = col.get( rowIndex );
+                    
+                    Object meta = metaData.get( object );
+                    
+                    if ( meta != null )
+                    {
+                        grid.get( rowIndex ).set( colIndex, meta );
+                    }
+                }
+            }
+        }        
+        
+        return this;
+    }
 
     // -------------------------------------------------------------------------
     // 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	2012-12-21 12:59:39 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/grid/GridTest.java	2013-03-04 10:30:59 +0000
@@ -34,7 +34,9 @@
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.GridHeader;
@@ -58,8 +60,8 @@
     {
         grid = new ListGrid();
         
-        headerA = new GridHeader( "ColA", "colA", String.class.getName(), false, false );
-        headerB = new GridHeader( "ColB", "colB", String.class.getName(), false, false );
+        headerA = new GridHeader( "ColA", "colA", String.class.getName(), false, true );
+        headerB = new GridHeader( "ColB", "colB", String.class.getName(), false, true );
         headerC = new GridHeader( "ColC", "colC", String.class.getName(), true, false );
         
         grid.addHeader( headerA );
@@ -88,6 +90,30 @@
     }
     
     @Test
+    public void testSubstituteMetaData()
+    {
+        Map<Object, String> metaData = new HashMap<Object, String>();
+        metaData.put( 11, "Eleven" );
+        metaData.put( 12, "Twelve" );
+        metaData.put( 21, "TwentyOne" );
+        metaData.put( 22, "TwentyTwo" );
+        
+        grid.setMetaData( metaData );
+        
+        assertEquals( 11, grid.getValue( 0, 0 ) );
+        assertEquals( 12, grid.getValue( 0, 1 ) );
+        assertEquals( 21, grid.getValue( 1, 0 ) );
+        assertEquals( 22, grid.getValue( 1, 1 ) );
+        
+        grid.substituteMetaData();
+
+        assertEquals( "Eleven", grid.getValue( 0, 0 ) );
+        assertEquals( "Twelve", grid.getValue( 0, 1 ) );
+        assertEquals( "TwentyOne", grid.getValue( 1, 0 ) );
+        assertEquals( "TwentyTwo", grid.getValue( 1, 1 ) );
+    }
+    
+    @Test
     public void testGetHeight()
     {
         assertEquals( 4, grid.getHeight() );

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AnalyticsController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AnalyticsController.java	2013-02-05 08:50:26 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AnalyticsController.java	2013-03-04 10:30:59 +0000
@@ -114,7 +114,7 @@
 
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_CSV, CacheStrategy.RESPECT_SYSTEM_SETTING );
         Grid grid = analyticsService.getAggregatedDataValues( params );
-        GridUtils.toCsv( grid, response.getOutputStream() );
+        GridUtils.toCsv( grid.substituteMetaData(), response.getOutputStream() );
     }
     
     @RequestMapping( value = RESOURCE_PATH + ".html", method = RequestMethod.GET )
@@ -130,7 +130,23 @@
 
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_HTML, CacheStrategy.RESPECT_SYSTEM_SETTING );
         Grid grid = analyticsService.getAggregatedDataValues( params );
-        GridUtils.toHtml( grid, response.getWriter() );
+        GridUtils.toHtml( grid.substituteMetaData(), response.getWriter() );
+    }
+
+    @RequestMapping( value = RESOURCE_PATH + ".xls", method = RequestMethod.GET )
+    public void getXls( 
+        @RequestParam Set<String> dimension,
+        @RequestParam(required = false) Set<String> filter,
+        @RequestParam(required = false) AggregationType aggregationType,
+        @RequestParam(required = false) String measureCriteria,
+        Model model,
+        HttpServletResponse response ) throws Exception
+    {
+        DataQueryParams params = analyticsService.getFromUrl( dimension, filter, aggregationType, measureCriteria, i18nManager.getI18nFormat() );
+
+        contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_EXCEL, CacheStrategy.RESPECT_SYSTEM_SETTING );
+        Grid grid = analyticsService.getAggregatedDataValues( params );
+        GridUtils.toXls( grid.substituteMetaData(), response.getOutputStream() );
     }
 
     // -------------------------------------------------------------------------