← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 5408: added pdf/xls/csv rendering of reportTables, current at /api/reportTables/uid/data{.pdf, .xls, .c...

 

------------------------------------------------------------
revno: 5408
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2011-12-14 12:29:43 +0100
message:
  added pdf/xls/csv rendering of reportTables, current at /api/reportTables/uid/data{.pdf, .xls, .cvs}?{organisationUnit=uid|period=periodString}
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/reporttable/ReportTableService.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/reporttable/impl/DefaultReportTableService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ReportTableController.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/reporttable/ReportTableService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/reporttable/ReportTableService.java	2011-12-14 10:03:38 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/reporttable/ReportTableService.java	2011-12-14 11:29:43 +0000
@@ -27,12 +27,12 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.common.Grid;
+import org.hisp.dhis.i18n.I18nFormat;
+
 import java.util.Collection;
 import java.util.Date;
 
-import org.hisp.dhis.common.Grid;
-import org.hisp.dhis.i18n.I18nFormat;
-
 /**
  * @author Lars Helge Overland
  * @version $Id$
@@ -43,46 +43,46 @@
 
     final String MODE_REPORT = "report";
     final String MODE_REPORT_TABLE = "table";
-    
+
     /**
      * Populated data mart with the relevant aggregated data for this ReportTable.
-     * 
-     * @param id the identifier.
-     * @param mode the mode, can be <l>dataelements</i>, <i>indicators</i>, and <i>datasets</i>.
-     * @param reportingPeriod the number of months back in time which will be used
-     *        as basis for the generation of relative periods.
+     *
+     * @param id                 the identifier.
+     * @param mode               the mode, can be <l>dataelements</i>, <i>indicators</i>, and <i>datasets</i>.
+     * @param reportingPeriod    the number of months back in time which will be used
+     *                           as basis for the generation of relative periods.
      * @param organisationUnitId the identifier of the organisation unit of the
-     *        report parameter, bot parent organisation unit and organisation unit.
-     * @param format the I18nFormat to use.
+     *                           report parameter, bot parent organisation unit and organisation unit.
+     * @param format             the I18nFormat to use.
      */
-    void populateReportTableDataMart( int id, String mode, Date reportingPeriod, 
-        Integer organisationUnitId, I18nFormat format );
-    
+    void populateReportTableDataMart( int id, String mode, Date reportingPeriod,
+                                      Integer organisationUnitId, I18nFormat format );
+
     /**
      * Saves a ReportTable.
-     * 
+     *
      * @param reportTable the ReportTable to save.
      * @return the generated identifier.
      */
     int saveReportTable( ReportTable reportTable );
-    
+
     /**
      * Updates a ReportTable.
-     * 
+     *
      * @param reportTable the ReportTable to update.
      */
     void updateReportTable( ReportTable reportTable );
-    
+
     /**
      * Deletes a ReportTable.
-     * 
+     *
      * @param reportTable the ReportTable to delete.
      */
     void deleteReportTable( ReportTable reportTable );
-    
+
     /**
      * Retrieves the ReportTable with the given identifier.
-     * 
+     *
      * @param id the identifier of the ReportTable to retrieve.
      * @return the ReportTable.
      */
@@ -90,65 +90,77 @@
 
     /**
      * Retrieves the ReportTable with the given uid.
-     * 
+     *
      * @param uid the uid of the ReportTable to retrieve.
      * @return the ReportTable.
      */
     ReportTable getReportTable( String uid );
-    
+
     /**
      * Retrieves a Collection of all ReportTables.
-     * 
+     *
      * @return a Collection of ReportTables.
      */
     Collection<ReportTable> getAllReportTables();
-    
+
     /**
      * Retrieves ReportTables with the given identifiers.
-     * 
+     *
      * @param reportTables the identfiers of the ReportTables to retrieve.
      * @return a Collection of ReportTables.
      */
     Collection<ReportTable> getReportTables( Collection<Integer> reportTables );
-    
+
     /**
      * Retrieves the ReportTable with the given name.
-     * 
+     *
      * @param name the name of the ReportTable.
      * @return the ReportTable.
      */
     ReportTable getReportTableByName( String name );
 
     /**
-     * Instantiates and populates a Grid populated with data from the ReportTable 
+     * Instantiates and populates a Grid populated with data from the ReportTable
      * with the given identifier.
-     * 
-     * @param id the ReportTable identifier.
-     * @param format the I18nFormat.
-     * @param reportingPeriod the reporting period number.
-     * @param organisationUnitId the organisation unit number. 
+     *
+     * @param id                 the ReportTable identifier.
+     * @param format             the I18nFormat.
+     * @param reportingPeriod    the reporting period number.
+     * @param organisationUnitId the organisation unit number.
      * @return a Grid.
      */
     Grid getReportTableGrid( int id, I18nFormat format, Date reportingPeriod, Integer organisationUnitId );
-    
+
+    /**
+     * Instantiates and populates a Grid populated with data from the ReportTable
+     * with the given identifier.
+     *
+     * @param uid                 the ReportTable unique identifier.
+     * @param format              the I18nFormat.
+     * @param reportingPeriod     the reporting period number.
+     * @param organisationUnitUid the organisation unit number.
+     * @return a Grid.
+     */
+    Grid getReportTableGrid( String uid, I18nFormat format, Date reportingPeriod, String organisationUnitUid );
+
     /**
      * If report table mode, this method will return the report table with the
      * given identifier. If report mode, this method will return the report
      * tables associated with the report.
-     * 
-     * @param id the identifier.
+     *
+     * @param id   the identifier.
      * @param mode the mode.
      */
     ReportTable getReportTable( Integer id, String mode );
-    
+
     Collection<ReportTable> getReportTablesBetween( int first, int max );
-    
+
     Collection<ReportTable> getReportTablesBetweenByName( String name, int first, int max );
-    
+
     int getReportTableCount();
-    
+
     int getReportTableCountByName( String name );
-    
+
     // -------------------------------------------------------------------------
     // ReportTableGroup
     // -------------------------------------------------------------------------
@@ -161,7 +173,7 @@
 
     ReportTableGroup getReportTableGroup( int id );
 
-    ReportTableGroup getReportTableGroup( String uid);
+    ReportTableGroup getReportTableGroup( String uid );
 
     ReportTableGroup getReportTableGroupByName( String name );
 

=== 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-12-14 10:03:38 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/reporttable/impl/DefaultReportTableService.java	2011-12-14 11:29:43 +0000
@@ -27,34 +27,10 @@
  * 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.PRETTY_COLUMNS;
-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.TOTAL_COLUMN_NAME;
-import static org.hisp.dhis.reporttable.ReportTable.TOTAL_COLUMN_PRETTY_NAME;
-import static org.hisp.dhis.reporttable.ReportTable.columnEncode;
-import static org.hisp.dhis.reporttable.ReportTable.getColumnName;
-import static org.hisp.dhis.reporttable.ReportTable.getIdentifier;
-import static org.hisp.dhis.reporttable.ReportTable.getPrettyColumnName;
-import static org.hisp.dhis.system.util.ConversionUtils.getIdentifiers;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Date;
-import java.util.Iterator;
-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.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.common.*;
 import org.hisp.dhis.completeness.DataSetCompletenessService;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementCategoryOption;
@@ -76,6 +52,11 @@
 import org.hisp.dhis.system.util.FilterUtils;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.util.*;
+
+import static org.hisp.dhis.reporttable.ReportTable.*;
+import static org.hisp.dhis.system.util.ConversionUtils.getIdentifiers;
+
 /**
  * @author Lars Helge Overland
  * @version $Id$
@@ -155,7 +136,7 @@
     // -------------------------------------------------------------------------
 
     public void populateReportTableDataMart( int id, String mode, Date reportingPeriod, Integer organisationUnitId,
-        I18nFormat format )
+                                             I18nFormat format )
     {
         ReportTable reportTable = getReportTable( id, mode );
 
@@ -172,10 +153,18 @@
         {
             completenessService.exportDataSetCompleteness( getIdentifiers( DataSet.class, reportTable.getDataSets() ),
                 getIdentifiers( Period.class, reportTable.getAllPeriods() ), getIdentifiers( OrganisationUnit.class,
-                    reportTable.getAllUnits() ) );
+                reportTable.getAllUnits() ) );
         }
     }
 
+    public Grid getReportTableGrid( String uid, I18nFormat format, Date reportingPeriod, String organisationUnitUid )
+    {
+        ReportTable reportTable = getReportTable( uid );
+        OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( organisationUnitUid );
+
+        return getReportTableGrid( reportTable.getId(), format, reportingPeriod, organisationUnit.getId() );
+    }
+
     public Grid getReportTableGrid( int id, I18nFormat format, Date reportingPeriod, Integer organisationUnitId )
     {
         ReportTable reportTable = getReportTable( id );
@@ -250,7 +239,7 @@
     {
         return reportTableStore.getByName( name );
     }
-    
+
     public Collection<ReportTable> getReportTablesBetweenByName( String name, int first, int max )
     {
         return reportTableStore.getBetweenByName( name, first, max );
@@ -295,7 +284,7 @@
         return reportTableGroupStore.get( id );
     }
 
-    public ReportTableGroup getReportTableGroup( String uid)
+    public ReportTableGroup getReportTableGroup( String uid )
     {
         return reportTableGroupStore.getByUid( uid );
     }
@@ -369,15 +358,15 @@
     /**
      * Populates the report table with dynamic meta objects originating from
      * report table parameters.
-     * 
-     * @param reportTable the report table.
-     * @param reportingPeriod the reporting period number.
+     *
+     * @param reportTable        the report table.
+     * @param reportingPeriod    the reporting period number.
      * @param organisationUnitId the organisation unit identifier.
-     * @param format the I18n format.
+     * @param format             the I18n format.
      * @return a report table.
      */
     private ReportTable initDynamicMetaObjects( ReportTable reportTable, Date reportingPeriod,
-        Integer organisationUnitId, I18nFormat format )
+                                                Integer organisationUnitId, I18nFormat format )
     {
         // ---------------------------------------------------------------------
         // Reporting period report parameter / current reporting period
@@ -412,15 +401,15 @@
             reportTable.getRelativeUnits().addAll(
                 new ArrayList<OrganisationUnit>( organisationUnitService.getLeafOrganisationUnits( organisationUnitId ) ) );
             reportTable.setOrganisationUnitName( organisationUnit.getName() );
-            
+
             log.info( "Leaf parent organisation unit: " + organisationUnit.getName() );
         }
-        
+
         // ---------------------------------------------------------------------
         // Grand parent organisation unit report parameter
         // ---------------------------------------------------------------------
 
-        if ( reportTable.getReportParams() != null && 
+        if ( reportTable.getReportParams() != null &&
             reportTable.getReportParams().isParamGrandParentOrganisationUnit() )
         {
             OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( organisationUnitId );
@@ -473,7 +462,7 @@
 
     /**
      * Generates a grid based on the given report table.
-     * 
+     *
      * @param reportTable the report table.
      * @return a grid.
      */
@@ -492,17 +481,17 @@
 
         for ( String column : reportTable.getIndexColumns() ) // Index columns
         {
-            grid.addHeader( new GridHeader( PRETTY_COLUMNS.get( column ), column, Integer.class.getName(), true, true ) ); 
+            grid.addHeader( new GridHeader( PRETTY_COLUMNS.get( column ), column, Integer.class.getName(), true, true ) );
         }
 
         for ( String column : reportTable.getIndexNameColumns() ) // Index name columns
         {
-            grid.addHeader( new GridHeader( PRETTY_COLUMNS.get( column ), column, String.class.getName(), false, true ) ); 
+            grid.addHeader( new GridHeader( PRETTY_COLUMNS.get( column ), column, String.class.getName(), false, true ) );
         }
 
         for ( String column : reportTable.getIndexCodeColumns() ) // Index code columns
         {
-            grid.addHeader( new GridHeader( PRETTY_COLUMNS.get( column ), column, String.class.getName(), true, true ) ); 
+            grid.addHeader( new GridHeader( PRETTY_COLUMNS.get( column ), column, String.class.getName(), true, true ) );
         }
 
         grid.addHeader( new GridHeader( PRETTY_COLUMNS.get( REPORTING_MONTH_COLUMN_NAME ), REPORTING_MONTH_COLUMN_NAME,
@@ -543,17 +532,17 @@
 
             for ( NameableObject object : row ) // Index columns
             {
-                grid.addValue( object.getId() ); 
+                grid.addValue( object.getId() );
             }
 
             for ( NameableObject object : row ) // Index name columns
             {
-                grid.addValue( object.getName() ); 
+                grid.addValue( object.getName() );
             }
 
             for ( NameableObject object : row ) // Index code columns
             {
-                grid.addValue( object.getCode() ); 
+                grid.addValue( object.getCode() );
             }
 
             grid.addValue( reportTable.getReportingPeriodName() );
@@ -562,7 +551,7 @@
 
             for ( List<NameableObject> column : reportTable.getColumns() ) // Values
             {
-                grid.addValue( map.get( getIdentifier( row, column ) ) ); 
+                grid.addValue( map.get( getIdentifier( row, column ) ) );
             }
 
             if ( reportTable.doSubTotals() )
@@ -580,8 +569,8 @@
                 // Only category option combo is crosstab when total, row
                 // identifier will return total
                 // -------------------------------------------------------------
-                
-                grid.addValue( map.get( getIdentifier( row ) ) ); 
+
+                grid.addValue( map.get( getIdentifier( row ) ) );
             }
         }
 
@@ -609,8 +598,8 @@
 
     /**
      * Adds columns with regression values to the given grid.
-     * 
-     * @param grid the grid.
+     *
+     * @param grid            the grid.
      * @param numberOfColumns the number of columns.
      */
     private Grid addRegressionToGrid( Grid grid, int numberOfColumns )
@@ -631,7 +620,7 @@
      * 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 )

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ReportTableController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ReportTableController.java	2011-12-14 10:03:38 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ReportTableController.java	2011-12-14 11:29:43 +0000
@@ -29,23 +29,33 @@
 
 import org.hisp.dhis.api.utils.IdentifiableObjectParams;
 import org.hisp.dhis.api.utils.WebLinkPopulator;
+import org.hisp.dhis.common.Grid;
+import org.hisp.dhis.i18n.I18nManager;
+import org.hisp.dhis.i18n.I18nManagerException;
+import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.period.PeriodService;
 import org.hisp.dhis.reporttable.ReportTable;
 import org.hisp.dhis.reporttable.ReportTableService;
 import org.hisp.dhis.reporttable.ReportTables;
+import org.hisp.dhis.system.grid.GridUtils;
+import org.hisp.dhis.system.util.DateUtils;
+import org.hisp.dhis.util.ContextUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.HttpRequestMethodNotSupportedException;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.ResponseStatus;
+import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Date;
+
+import static org.apache.commons.lang.StringUtils.defaultIfEmpty;
+import static org.hisp.dhis.system.util.CodecUtils.filenameEncode;
 
 @Controller
 @RequestMapping( value = ReportTableController.RESOURCE_PATH )
@@ -56,6 +66,15 @@
     @Autowired
     public ReportTableService reportTableService;
 
+    @Autowired
+    private OrganisationUnitService organisationUnitService;
+
+    @Autowired
+    private PeriodService periodService;
+
+    @Autowired
+    private I18nManager i18nManager;
+
     //-------------------------------------------------------------------------------------------------------
     // GET
     //-------------------------------------------------------------------------------------------------------
@@ -100,6 +119,73 @@
 
         return "reportTable";
     }
+
+    @RequestMapping( value = "/{uid}/data.pdf", method = RequestMethod.GET )
+    public void getReportTablePDF( @PathVariable( "uid" ) String uid,
+                                   @RequestParam( value = "organisationUnit", required = false ) String organisationUnitUid,
+                                   @RequestParam( value = "period", required = false ) String period,
+                                   HttpServletResponse response ) throws I18nManagerException, IOException
+    {
+        if ( organisationUnitUid == null && period == null )
+        {
+            response.setStatus( HttpServletResponse.SC_BAD_REQUEST );
+            return;
+        }
+
+        Date date = period != null ? DateUtils.getMediumDate( period ) : new Date();
+
+        Grid grid = reportTableService.getReportTableGrid( uid, i18nManager.getI18nFormat(), date, organisationUnitUid );
+
+        String filename = filenameEncode( defaultIfEmpty( grid.getTitle(), "Grid" ) ) + ".pdf";
+        ContextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_PDF, true, filename, false );
+
+        GridUtils.toPdf( grid, response.getOutputStream() );
+    }
+
+    @RequestMapping( value = "/{uid}/data.xls", method = RequestMethod.GET )
+    public void getReportTableXLS( @PathVariable( "uid" ) String uid,
+                                   @RequestParam( value = "organisationUnit", required = false ) String organisationUnitUid,
+                                   @RequestParam( value = "period", required = false ) String period,
+                                   HttpServletResponse response ) throws Exception
+    {
+        if ( organisationUnitUid == null && period == null )
+        {
+            response.setStatus( HttpServletResponse.SC_BAD_REQUEST );
+            return;
+        }
+
+        Date date = period != null ? DateUtils.getMediumDate( period ) : new Date();
+
+        Grid grid = reportTableService.getReportTableGrid( uid, i18nManager.getI18nFormat(), date, organisationUnitUid );
+
+        String filename = filenameEncode( defaultIfEmpty( grid.getTitle(), "Grid" ) ) + ".xls";
+        ContextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_EXCEL, true, filename, false );
+
+        GridUtils.toXls( grid, response.getOutputStream() );
+    }
+
+    @RequestMapping( value = "/{uid}/data.csv", method = RequestMethod.GET )
+    public void getReportTableCSV( @PathVariable( "uid" ) String uid,
+                                   @RequestParam( value = "organisationUnit", required = false ) String organisationUnitUid,
+                                   @RequestParam( value = "period", required = false ) String period,
+                                   HttpServletResponse response ) throws Exception
+    {
+        if ( organisationUnitUid == null && period == null )
+        {
+            response.setStatus( HttpServletResponse.SC_BAD_REQUEST );
+            return;
+        }
+
+        Date date = period != null ? DateUtils.getMediumDate( period ) : new Date();
+
+        Grid grid = reportTableService.getReportTableGrid( uid, i18nManager.getI18nFormat(), date, organisationUnitUid );
+
+        String filename = filenameEncode( defaultIfEmpty( grid.getTitle(), "Grid" ) ) + ".csv";
+        ContextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_EXCEL, true, filename, false );
+
+        GridUtils.toCsv( grid, response.getOutputStream() );
+    }
+
     //-------------------------------------------------------------------------------------------------------
     // POST
     //-------------------------------------------------------------------------------------------------------