← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 6199: Made grid utils more robust against weird input

 

------------------------------------------------------------
revno: 6199
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2012-03-07 12:53:00 +0100
message:
  Made grid utils more robust against weird input
modified:
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/GridUtils.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/resources/struts.xml


--
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-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/GridUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/GridUtils.java	2012-02-10 17:32:13 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/GridUtils.java	2012-03-07 11:53:00 +0000
@@ -106,11 +106,14 @@
      */
     public static void toPdf( Grid grid, OutputStream out )
     {
-        Document document = openDocument( out );
-        
-        toPdfInternal( grid, document, 0F );
-
-        closeDocument( document );
+        if ( isNonEmptyGrid( grid ) )
+        {
+            Document document = openDocument( out );
+            
+            toPdfInternal( grid, document, 0F );
+    
+            closeDocument( document );
+        }
     }
 
     /**
@@ -118,18 +121,26 @@
      */
     public static void toPdf( List<Grid> grids, OutputStream out )
     {
-        Document document = openDocument( out );
-        
-        for ( Grid grid : grids )
+        if ( hasNonEmptyGrid( grids ) )
         {
-            toPdfInternal( grid, document, 40F );
+            Document document = openDocument( out );
+            
+            for ( Grid grid : grids )
+            {
+                toPdfInternal( grid, document, 40F );
+            }
+            
+            closeDocument( document );
         }
-        
-        closeDocument( document );
     }
     
     private static void toPdfInternal( Grid grid, Document document, float spacing )
     {
+        if ( grid == null || grid.getVisibleWidth() == 0 )
+        {
+            return;
+        }
+        
         PdfPTable table = new PdfPTable( grid.getVisibleWidth() );
 
         table.setHeaderRows( 1 );
@@ -203,6 +214,11 @@
     private static void toXlsInternal( Grid grid, WritableWorkbook workbook, String sheetName, int sheetNo )
         throws Exception
     {
+        if ( grid == null )
+        {
+            return;
+        }
+        
         WritableSheet sheet = workbook.createSheet( sheetName, sheetNo );
 
         int rowNumber = 1;
@@ -255,6 +271,11 @@
     public static void toCsv( Grid grid, OutputStream out )
         throws Exception
     {
+        if ( grid == null )
+        {
+            return;
+        }
+        
         Iterator<GridHeader> headers = grid.getHeaders().iterator();
         
         while ( headers.hasNext() )
@@ -293,6 +314,11 @@
     public static void toJasperReport( Grid grid, Map<String, Object> params, OutputStream out )
         throws Exception
     {
+        if ( grid == null )
+        {
+            return;
+        }
+        
         final StringWriter writer = new StringWriter();
         
         render( grid, params, writer );
@@ -333,4 +359,32 @@
         
         new VelocityManager().getEngine().getTemplate( TEMPLATE ).merge( context, writer );
     }
+    
+    /**
+     * Indicates whether the given list of grids have at least one grid which is 
+     * not null and has more than zero visible columns.
+     */
+    private static boolean hasNonEmptyGrid( List<Grid> grids )
+    {
+        if ( grids != null && grids.size() > 0 )
+        {
+            for ( Grid grid : grids )
+            {
+                if ( isNonEmptyGrid( grid ) )
+                {
+                    return true;
+                }
+            }
+        }
+        
+        return false;
+    }
+    
+    /**
+     * Indicates whether grid is not null and has more than zero visible columns.
+     */
+    private static boolean isNonEmptyGrid( Grid grid )
+    {
+        return grid != null && grid.getVisibleWidth() > 0;
+    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/resources/struts.xml'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/resources/struts.xml	2012-03-02 12:39:20 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/resources/struts.xml	2012-03-07 11:53:00 +0000
@@ -126,7 +126,6 @@
       <result name="csv" type="gridCsvResult" />
       <result name="xls" type="gridXlsResult" />
       <result name="pdf" type="gridPdfResult" />
-      <result name="error" type="velocity-json">/dhis-web-commons/ajax/jsonResponseError.vm</result>
     </action>
 
     <!-- Organisation unit merge -->