← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 6967: Impl html, pdf, xls, json resource representation for dynamic report table data in web api

 

------------------------------------------------------------
revno: 6967
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2012-05-16 22:48:16 +0200
message:
  Impl html, pdf, xls, json resource representation for dynamic report table data in web api
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MetaDataController.java
  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/resources/templates/html/reportTable.xsl


--
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-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MetaDataController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MetaDataController.java	2012-05-16 09:54:13 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MetaDataController.java	2012-05-16 20:48:16 +0000
@@ -27,7 +27,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import org.hibernate.SessionFactory;
 import org.hisp.dhis.api.utils.ContextUtils;
 import org.hisp.dhis.api.utils.ContextUtils.CacheStrategy;
 import org.hisp.dhis.common.view.ExportView;

=== 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	2012-05-15 20:20:50 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ReportTableController.java	2012-05-16 20:48:16 +0000
@@ -81,6 +81,8 @@
 public class ReportTableController
 {
     public static final String RESOURCE_PATH = "/reportTables";
+    
+    private static final String DATA_NAME = "data";
 
     @Autowired
     public ReportTableService reportTableService;
@@ -103,9 +105,9 @@
     @Autowired
     private ContextUtils contextUtils;
 
-    //-------------------------------------------------------------------------------------------------------
+    //--------------------------------------------------------------------------
     // GET
-    //-------------------------------------------------------------------------------------------------------
+    //--------------------------------------------------------------------------
 
     @RequestMapping( method = RequestMethod.GET )
     public String getReportTables( IdentifiableObjectParams params, Model model, HttpServletRequest request )
@@ -157,6 +159,10 @@
         return "reportTable";
     }
 
+    //--------------------------------------------------------------------------
+    // GET - Dynamic data
+    //--------------------------------------------------------------------------
+
     @RequestMapping( value = "/data", method = RequestMethod.GET )
     public String getReportTableDynamicData( @RequestParam(required=false, value="in") List<String> indicators,
                                              @RequestParam(required=false, value="de") List<String> dataElements,
@@ -169,6 +175,81 @@
                                              Model model, 
                                              HttpServletResponse response ) throws Exception
     {
+        Grid grid = getReportTableDynamicGrid( indicators, dataElements, dataSets, 
+            orgUnits, crossTab, orgUnitIsParent, minimal, relatives, response );
+        
+        model.addAttribute( "model", grid );
+        model.addAttribute( "view", "detailed" );
+        
+        return grid != null ? "reportTableData" : null;
+    }
+
+    @RequestMapping( value = "/data.html", method = RequestMethod.GET )
+    public void getReportTableDynamicDataHtml( @RequestParam(required=false, value="in") List<String> indicators,
+                                             @RequestParam(required=false, value="de") List<String> dataElements,
+                                             @RequestParam(required=false, value="ds") List<String> dataSets,
+                                             @RequestParam(value="ou") List<String> orgUnits,
+                                             @RequestParam(required=false, value="crosstab") List<String> crossTab,
+                                             @RequestParam(required=false) boolean orgUnitIsParent,
+                                             @RequestParam(required=false) boolean minimal,
+                                             RelativePeriods relatives,
+                                             Model model, 
+                                             HttpServletResponse response ) throws Exception
+    {
+        Grid grid = getReportTableDynamicGrid( indicators, dataElements, dataSets, 
+            orgUnits, crossTab, orgUnitIsParent, minimal, relatives, response );
+
+        String filename = DATA_NAME + ".html";
+        contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_HTML, CacheStrategy.RESPECT_SYSTEM_SETTING, filename, false );
+
+        GridUtils.toHtml( grid, response.getWriter() );
+    }
+
+    @RequestMapping( value = "/data.pdf", method = RequestMethod.GET )
+    public void getReportTableDynamicDataPdf( @RequestParam(required=false, value="in") List<String> indicators,
+                                             @RequestParam(required=false, value="de") List<String> dataElements,
+                                             @RequestParam(required=false, value="ds") List<String> dataSets,
+                                             @RequestParam(value="ou") List<String> orgUnits,
+                                             @RequestParam(required=false, value="crosstab") List<String> crossTab,
+                                             @RequestParam(required=false) boolean orgUnitIsParent,
+                                             @RequestParam(required=false) boolean minimal,
+                                             RelativePeriods relatives,
+                                             Model model, 
+                                             HttpServletResponse response ) throws Exception
+    {
+        Grid grid = getReportTableDynamicGrid( indicators, dataElements, dataSets, 
+            orgUnits, crossTab, orgUnitIsParent, minimal, relatives, response );
+
+        String filename = DATA_NAME + ".pdf";
+        contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_PDF, CacheStrategy.RESPECT_SYSTEM_SETTING, filename, false );
+
+        GridUtils.toPdf( grid, response.getOutputStream() );
+    }
+
+    @RequestMapping( value = "/data.xls", method = RequestMethod.GET )
+    public void getReportTableDynamicDataXls( @RequestParam(required=false, value="in") List<String> indicators,
+                                             @RequestParam(required=false, value="de") List<String> dataElements,
+                                             @RequestParam(required=false, value="ds") List<String> dataSets,
+                                             @RequestParam(value="ou") List<String> orgUnits,
+                                             @RequestParam(required=false, value="crosstab") List<String> crossTab,
+                                             @RequestParam(required=false) boolean orgUnitIsParent,
+                                             @RequestParam(required=false) boolean minimal,
+                                             RelativePeriods relatives,
+                                             Model model, 
+                                             HttpServletResponse response ) throws Exception
+    {
+        Grid grid = getReportTableDynamicGrid( indicators, dataElements, dataSets, 
+            orgUnits, crossTab, orgUnitIsParent, minimal, relatives, response );
+        
+        String filename = DATA_NAME + ".xls";
+        contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_EXCEL, CacheStrategy.RESPECT_SYSTEM_SETTING, filename, true );
+
+        GridUtils.toXls( grid, response.getOutputStream() );
+    }
+
+    private Grid getReportTableDynamicGrid( List<String> indicators, List<String> dataElements, List<String> dataSets, 
+        List<String> orgUnits, List<String> crossTab, boolean orgUnitIsParent, boolean minimal, RelativePeriods relatives, HttpServletResponse response )  throws Exception
+    {
         List<Indicator> indicators_ = indicatorService.getIndicatorsByUid( indicators );
         List<DataElement> dataElements_ = dataElementService.getDataElementsByUid( dataElements );
         List<DataSet> dataSets_ = dataSetService.getDataSetsByUid( dataSets );
@@ -211,52 +292,33 @@
         table.setRelatives( relatives );
         
         Grid grid = reportTableService.getReportTableGrid( table, i18nManager.getI18nFormat(), new Date(), null, minimal );
+        
+        return grid;
+    }
+    
+    //--------------------------------------------------------------------------
+    // GET - Report table data
+    //--------------------------------------------------------------------------
 
-        model.addAttribute( "model", grid );
-        model.addAttribute( "view", "detailed" );
-        
-        return "reportTableData";
-    }
-        
     @RequestMapping( value = "/{uid}/data", method = RequestMethod.GET )
     public String getReportTableData( @PathVariable( "uid" ) String uid, Model model,
                                       @RequestParam( value = "ou", required = false ) String organisationUnitUid,
                                       @RequestParam( value = "pe", required = false ) String period,
                                       HttpServletResponse response ) throws Exception
     {
-        ReportTable reportTable = reportTableService.getReportTable( uid );
-
-        if ( organisationUnitUid == null && reportTable.hasReportParams() && reportTable.getReportParams().isOrganisationUnitSet() )
-        {
-            organisationUnitUid = organisationUnitService.getRootOrganisationUnits().iterator().next().getUid();
-        }
-
-        Date date = period != null ? DateUtils.getMediumDate( period ) : new Cal().now().subtract( Calendar.MONTH, 1 ).time();
-
-        Grid grid = reportTableService.getReportTableGrid( uid, i18nManager.getI18nFormat(), date, organisationUnitUid );
-
-        model.addAttribute( "model", grid );
+        model.addAttribute( "model", getReportTableGrid( uid, organisationUnitUid, period ) );
         model.addAttribute( "view", "detailed" );
-
+        
         return "grid";
     }
-
+    
     @RequestMapping( value = "/{uid}/data.html", method = RequestMethod.GET )
     public void getReportTableHtml( @PathVariable( "uid" ) String uid,
                                    @RequestParam( value = "ou", required = false ) String organisationUnitUid,
                                    @RequestParam( value = "pe", required = false ) String period,
                                    HttpServletResponse response ) throws Exception
     {
-        ReportTable reportTable = reportTableService.getReportTable( uid );
-
-        if ( organisationUnitUid == null && reportTable.hasReportParams() && reportTable.getReportParams().isOrganisationUnitSet() )
-        {
-            organisationUnitUid = organisationUnitService.getRootOrganisationUnits().iterator().next().getUid();
-        }
-
-        Date date = period != null ? DateUtils.getMediumDate( period ) : new Cal().now().subtract( Calendar.MONTH, 1 ).time();
-
-        Grid grid = reportTableService.getReportTableGrid( uid, i18nManager.getI18nFormat(), date, organisationUnitUid );
+        Grid grid = getReportTableGrid( uid, organisationUnitUid, period );
 
         String filename = filenameEncode( grid.getTitle() ) + ".html";
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_HTML, CacheStrategy.RESPECT_SYSTEM_SETTING, filename, false );
@@ -270,17 +332,8 @@
                                    @RequestParam( value = "pe", required = false ) String period,
                                    HttpServletResponse response ) throws Exception
     {
-        ReportTable reportTable = reportTableService.getReportTable( uid );
-
-        if ( organisationUnitUid == null && reportTable.hasReportParams() && reportTable.getReportParams().isOrganisationUnitSet() )
-        {
-            organisationUnitUid = organisationUnitService.getRootOrganisationUnits().iterator().next().getUid();
-        }
-
-        Date date = period != null ? DateUtils.getMediumDate( period ) : new Cal().now().subtract( Calendar.MONTH, 1 ).time();
-
-        Grid grid = reportTableService.getReportTableGrid( uid, i18nManager.getI18nFormat(), date, organisationUnitUid );
-
+        Grid grid = getReportTableGrid( uid, organisationUnitUid, period );
+        
         String filename = filenameEncode( grid.getTitle() ) + ".xml";
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_XML, CacheStrategy.RESPECT_SYSTEM_SETTING, filename, false );
 
@@ -293,16 +346,7 @@
                                    @RequestParam( value = "pe", required = false ) String period,
                                    HttpServletResponse response ) throws Exception
     {
-        ReportTable reportTable = reportTableService.getReportTable( uid );
-
-        if ( organisationUnitUid == null && reportTable.hasReportParams() && reportTable.getReportParams().isOrganisationUnitSet() )
-        {
-            organisationUnitUid = organisationUnitService.getRootOrganisationUnits().iterator().next().getUid();
-        }
-
-        Date date = period != null ? DateUtils.getMediumDate( period ) : new Cal().now().subtract( Calendar.MONTH, 1 ).time();
-
-        Grid grid = reportTableService.getReportTableGrid( uid, i18nManager.getI18nFormat(), date, organisationUnitUid );
+        Grid grid = getReportTableGrid( uid, organisationUnitUid, period );
 
         String filename = filenameEncode( grid.getTitle() ) + ".pdf";
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_PDF, CacheStrategy.RESPECT_SYSTEM_SETTING, filename, false );
@@ -316,16 +360,7 @@
                                    @RequestParam( value = "pe", required = false ) String period,
                                    HttpServletResponse response ) throws Exception
     {
-        ReportTable reportTable = reportTableService.getReportTable( uid );
-
-        if ( organisationUnitUid == null && reportTable.hasReportParams() && reportTable.getReportParams().isOrganisationUnitSet() )
-        {
-            organisationUnitUid = organisationUnitService.getRootOrganisationUnits().iterator().next().getUid();
-        }
-
-        Date date = period != null ? DateUtils.getMediumDate( period ) : new Cal().now().subtract( Calendar.MONTH, 1 ).time();
-
-        Grid grid = reportTableService.getReportTableGrid( uid, i18nManager.getI18nFormat(), date, organisationUnitUid );
+        Grid grid = getReportTableGrid( uid, organisationUnitUid, period );
 
         String filename = filenameEncode( grid.getTitle() ) + ".xls";
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_EXCEL, CacheStrategy.RESPECT_SYSTEM_SETTING, filename, true );
@@ -339,16 +374,7 @@
                                    @RequestParam( value = "pe", required = false ) String period,
                                    HttpServletResponse response ) throws Exception
     {
-        ReportTable reportTable = reportTableService.getReportTable( uid );
-
-        if ( organisationUnitUid == null && reportTable.hasReportParams() && reportTable.getReportParams().isOrganisationUnitSet() )
-        {
-            organisationUnitUid = organisationUnitService.getRootOrganisationUnits().iterator().next().getUid();
-        }
-
-        Date date = period != null ? DateUtils.getMediumDate( period ) : new Cal().now().subtract( Calendar.MONTH, 1 ).time();
-
-        Grid grid = reportTableService.getReportTableGrid( uid, i18nManager.getI18nFormat(), date, organisationUnitUid );
+        Grid grid = getReportTableGrid( uid, organisationUnitUid, period );
 
         String filename = filenameEncode( grid.getTitle() ) + ".csv";
         contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_CSV, CacheStrategy.RESPECT_SYSTEM_SETTING, filename, true );
@@ -356,9 +382,26 @@
         GridUtils.toCsv( grid, response.getOutputStream() );
     }
 
-    //-------------------------------------------------------------------------------------------------------
+    private Grid getReportTableGrid( String uid, String organisationUnitUid, String period )
+        throws Exception
+    {
+        ReportTable reportTable = reportTableService.getReportTable( uid );
+
+        if ( organisationUnitUid == null && reportTable.hasReportParams() && reportTable.getReportParams().isOrganisationUnitSet() )
+        {
+            organisationUnitUid = organisationUnitService.getRootOrganisationUnits().iterator().next().getUid();
+        }
+
+        Date date = period != null ? DateUtils.getMediumDate( period ) : new Cal().now().subtract( Calendar.MONTH, 1 ).time();
+
+        Grid grid = reportTableService.getReportTableGrid( uid, i18nManager.getI18nFormat(), date, organisationUnitUid );
+
+        return grid;
+    }
+
+    //--------------------------------------------------------------------------
     // POST
-    //-------------------------------------------------------------------------------------------------------
+    //--------------------------------------------------------------------------
 
     @RequestMapping( method = RequestMethod.POST, headers = {"Content-Type=application/xml, text/xml"} )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_REPORTTABLE_ADD')" )
@@ -376,9 +419,9 @@
         throw new HttpRequestMethodNotSupportedException( RequestMethod.POST.toString() );
     }
 
-    //-------------------------------------------------------------------------------------------------------
+    //--------------------------------------------------------------------------
     // PUT
-    //-------------------------------------------------------------------------------------------------------
+    //--------------------------------------------------------------------------
 
     @RequestMapping( value = "/{uid}", method = RequestMethod.PUT, headers = {"Content-Type=application/xml, text/xml"} )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_REPORTTABLE_ADD')" )
@@ -396,9 +439,9 @@
         throw new HttpRequestMethodNotSupportedException( RequestMethod.PUT.toString() );
     }
 
-    //-------------------------------------------------------------------------------------------------------
+    //--------------------------------------------------------------------------
     // DELETE
-    //-------------------------------------------------------------------------------------------------------
+    //--------------------------------------------------------------------------
 
     @RequestMapping( value = "/{uid}", method = RequestMethod.DELETE )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_REPORTTABLE_DELETE')" )

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/resources/templates/html/reportTable.xsl'
--- dhis-2/dhis-web/dhis-web-api/src/main/resources/templates/html/reportTable.xsl	2012-05-15 15:28:44 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/resources/templates/html/reportTable.xsl	2012-05-16 20:48:16 +0000
@@ -21,19 +21,22 @@
 
       <tr>
         <td>ou</td>
-        <td colspan="4">organisation unit uid (opt)</td>
+        <td colspan="6">organisation unit uid (opt)</td>
       </tr>
 
       <tr>
         <td>pe</td>
-        <td colspan="4">period yyyy-MM-dd (opt)</td>
+        <td colspan="6">period yyyy-MM-dd (opt)</td>
       </tr>
 	  </table><br/>
 
 	  <table>
       <tr>
         <td>Dynamic Data</td>
+        <td><a href="data.html">html</a></td>
         <td><a href="data.json">json</a></td>
+        <td><a href="data.xls">xls</a></td>
+        <td><a href="data.pdf">pdf</a></td>
       </tr>
       <tr>
         <td>in</td>
@@ -57,7 +60,11 @@
       </tr>
       <tr>
         <td>orgUnitIsParent</td>
-        <td>org unit is parent</td>
+        <td>true/false</td>
+      </tr>
+      <tr>
+        <td>minimal</td>
+        <td>true/false</td>
       </tr>
       <tr>
         <td>[relative periods]</td>