← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 5413: Improved report params handling in ReportTableController

 

------------------------------------------------------------
revno: 5413
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2011-12-14 14:18:25 +0100
message:
  Improved report params handling in ReportTableController
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/reporttable/ReportTable.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ReportTableController.java
  dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/ContextUtils.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/ReportTable.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/reporttable/ReportTable.java	2011-12-14 10:03:38 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/reporttable/ReportTable.java	2011-12-14 13:18:25 +0000
@@ -666,6 +666,14 @@
     {
         return topLimit != null ? topLimit : 0;
     }
+    
+    /**
+     * Tests whether this report table has report params.
+     */
+    public boolean hasReportParams()
+    {
+        return reportParams != null;
+    }
 
     // -------------------------------------------------------------------------
     // Supportive methods

=== 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 12:52:07 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ReportTableController.java	2011-12-14 13:18:25 +0000
@@ -27,13 +27,22 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import static org.hisp.dhis.system.util.CodecUtils.filenameEncode;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Date;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
 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;
@@ -45,18 +54,11 @@
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.HttpRequestMethodNotSupportedException;
-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.io.PrintWriter;
-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;
+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.RequestParam;
+import org.springframework.web.bind.annotation.ResponseStatus;
 
 @Controller
 @RequestMapping( value = ReportTableController.RESOURCE_PATH )
@@ -71,9 +73,6 @@
     private OrganisationUnitService organisationUnitService;
 
     @Autowired
-    private PeriodService periodService;
-
-    @Autowired
     private I18nManager i18nManager;
 
     //-------------------------------------------------------------------------------------------------------
@@ -122,21 +121,16 @@
     }
 
     @RequestMapping( value = "/{uid}/data", method = RequestMethod.GET )
-    public String getReportTableDATA( @PathVariable( "uid" ) String uid, Model model,
+    public String getReportTableData( @PathVariable( "uid" ) String uid, Model model,
                                       @RequestParam( value = "organisationUnit", required = false ) String organisationUnitUid,
                                       @RequestParam( value = "period", required = false ) String period,
                                       HttpServletResponse response ) throws I18nManagerException, IOException
     {
-        if ( organisationUnitUid == null && period == null )
+        ReportTable reportTable = reportTableService.getReportTable( uid );
+        
+        if ( organisationUnitUid == null && reportTable.hasReportParams() && reportTable.getReportParams().isOrganisationUnitSet() )
         {
-            response.setStatus( HttpServletResponse.SC_BAD_REQUEST );
-            response.setContentType( "text/plain" );
-
-            PrintWriter writer = new PrintWriter( response.getOutputStream() );
-            writer.println( "GRID needs either organisationUnit or period parameter." );
-            writer.flush();
-
-            return "grid";
+            organisationUnitUid = organisationUnitService.getRootOrganisationUnits().iterator().next().getUid();
         }
 
         Date date = period != null ? DateUtils.getMediumDate( period ) : new Date();
@@ -149,85 +143,70 @@
     }
 
     @RequestMapping( value = "/{uid}/data.pdf", method = RequestMethod.GET )
-    public void getReportTablePDF( @PathVariable( "uid" ) String uid,
+    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 )
+        ReportTable reportTable = reportTableService.getReportTable( uid );
+        
+        if ( organisationUnitUid == null && reportTable.hasReportParams() && reportTable.getReportParams().isOrganisationUnitSet() )
         {
-            response.setStatus( HttpServletResponse.SC_BAD_REQUEST );
-            response.setContentType( "text/plain" );
-
-            PrintWriter writer = new PrintWriter( response.getOutputStream() );
-            writer.println( "PDF needs either organisationUnit or period parameter." );
-            writer.flush();
-
-            return;
+            organisationUnitUid = organisationUnitService.getRootOrganisationUnits().iterator().next().getUid();
         }
 
         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";
+        String filename = filenameEncode( grid.getTitle() ) + ".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,
+    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 )
+        ReportTable reportTable = reportTableService.getReportTable( uid );
+        
+        if ( organisationUnitUid == null && reportTable.hasReportParams() && reportTable.getReportParams().isOrganisationUnitSet() )
         {
-            response.setStatus( HttpServletResponse.SC_BAD_REQUEST );
-            response.setContentType( "text/plain" );
-
-            PrintWriter writer = new PrintWriter( response.getOutputStream() );
-            writer.println( "XLS needs either organisationUnit or period parameter." );
-            writer.flush();
-
-            return;
+            organisationUnitUid = organisationUnitService.getRootOrganisationUnits().iterator().next().getUid();
         }
 
         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 );
+        String filename = filenameEncode( grid.getTitle() ) + ".xls";
+        ContextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_EXCEL, true, filename, true );
 
         GridUtils.toXls( grid, response.getOutputStream() );
     }
 
     @RequestMapping( value = "/{uid}/data.csv", method = RequestMethod.GET )
-    public void getReportTableCSV( @PathVariable( "uid" ) String uid,
+    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 )
+        ReportTable reportTable = reportTableService.getReportTable( uid );
+        
+        if ( organisationUnitUid == null && reportTable.hasReportParams() && reportTable.getReportParams().isOrganisationUnitSet() )
         {
-            response.setStatus( HttpServletResponse.SC_BAD_REQUEST );
-            response.setContentType( "text/plain" );
-
-            PrintWriter writer = new PrintWriter( response.getOutputStream() );
-            writer.println( "CSV needs either organisationUnit or period parameter." );
-            writer.flush();
-
-            return;
+            organisationUnitUid = organisationUnitService.getRootOrganisationUnits().iterator().next().getUid();
         }
 
         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_CSV, true, filename, false );
+        String filename = filenameEncode( grid.getTitle() ) + ".csv";
+        ContextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_CSV, true, filename, true );
 
         GridUtils.toCsv( grid, response.getOutputStream() );
     }

=== modified file 'dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/ContextUtils.java'
--- dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/ContextUtils.java	2011-10-12 08:34:07 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/ContextUtils.java	2011-12-14 13:18:25 +0000
@@ -27,6 +27,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.io.IOException;
+import java.io.PrintWriter;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
@@ -148,4 +150,15 @@
  
         return null;
     }
+    
+    public static void errorResponse( HttpServletResponse response, String message )
+        throws IOException
+    {
+        response.setStatus( HttpServletResponse.SC_BAD_REQUEST );
+        response.setContentType( CONTENT_TYPE_TEXT );
+
+        PrintWriter writer = new PrintWriter( response.getOutputStream() );
+        writer.println( message );
+        writer.flush();
+    }
 }