← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 3807: Implemented export to PDF and Excel for dataset report.

 

Merge authors:
  Lars Helge Øverland (larshelge)
------------------------------------------------------------
revno: 3807 [merge]
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2011-05-31 13:39:53 +0200
message:
  Implemented export to PDF and Excel for dataset report.
modified:
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/GridUtils.java
  dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/result/GridPdfResult.java
  dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/result/GridXlsResult.java
  dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/SessionUtils.java
  dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/dataset/action/GenerateDataSetReportAction.java
  dhis-2/dhis-web/dhis-web-reporting/src/main/resources/struts.xml
  dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/javascript/dataSetReport.js
  dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/renderDefaultDataSetReportForm.vm
  dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/renderSectionDataSetReportForm.vm


--
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	2011-05-30 19:10:22 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/grid/GridUtils.java	2011-05-31 11:39:19 +0000
@@ -102,7 +102,7 @@
     {
         Document document = openDocument( out );
         
-        toPdfInternal( grid, document );
+        toPdfInternal( grid, document, 0F );
 
         closeDocument( document );
     }
@@ -116,23 +116,28 @@
         
         for ( Grid grid : grids )
         {
-            toPdfInternal( grid, document );
+            toPdfInternal( grid, document, 40F );
         }
         
         closeDocument( document );
     }
     
-    private static void toPdfInternal( Grid grid, Document document )
+    private static void toPdfInternal( Grid grid, Document document, float spacing )
     {
         PdfPTable table = new PdfPTable( grid.getVisibleWidth() );
 
         table.setHeaderRows( 1 );
-        table.setWidthPercentage( 100f );
+        table.setWidthPercentage( 100F );
         table.setKeepTogether( false );
+        table.setSpacingAfter( spacing );
 
-        table.addCell( resetPaddings( getTitleCell( grid.getTitle(), grid.getVisibleWidth() ), 0, 45, 0, 0 ) );
-        table.addCell( getSubtitleCell( grid.getSubtitle(), grid.getVisibleWidth() ) );
-        table.addCell( getEmptyCell( grid.getVisibleWidth(), 30 ) );
+        table.addCell( resetPaddings( getTitleCell( grid.getTitle(), grid.getVisibleWidth() ), 0, 30, 0, 0 ) );
+        
+        if ( StringUtils.isNotEmpty( grid.getSubtitle() ) )
+        {
+            table.addCell( getSubtitleCell( grid.getSubtitle(), grid.getVisibleWidth() ) );
+            table.addCell( getEmptyCell( grid.getVisibleWidth(), 30 ) );
+        }        
 
         for ( GridHeader header : grid.getVisibleHeaders() )
         {

=== modified file 'dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/result/GridPdfResult.java'
--- dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/result/GridPdfResult.java	2011-05-05 21:15:45 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/result/GridPdfResult.java	2011-05-31 11:18:52 +0000
@@ -27,21 +27,26 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import static org.apache.commons.lang.StringUtils.defaultIfEmpty;
+import static org.hisp.dhis.system.util.CodecUtils.filenameEncode;
+
 import java.io.OutputStream;
+import java.util.List;
 
 import javax.servlet.http.HttpServletResponse;
 
-import org.apache.commons.lang.StringUtils;
 import org.apache.struts2.ServletActionContext;
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.system.grid.GridUtils;
-import org.hisp.dhis.system.util.CodecUtils;
 import org.hisp.dhis.util.ContextUtils;
 
 import com.opensymphony.xwork2.ActionInvocation;
 import com.opensymphony.xwork2.Result;
 
 /**
+ * Creates a PDF representation of the given Grid or list of Grids and writes
+ * it to the servlet outputstream. One of the grid or grids arguments must be set.
+ * 
  * @author Lars Helge Overland
  */
 public class GridPdfResult
@@ -52,7 +57,7 @@
      */
     private static final long serialVersionUID = 6613101138470779866L;
 
-    private static final String DEFAULT_FILENAME = "Grid";
+    private static final String DEFAULT_NAME = "Grid";
 
     // -------------------------------------------------------------------------
     // Input
@@ -65,11 +70,19 @@
         this.grid = grid;
     }
 
+    private List<Grid> grids;
+
+    public void setGrids( List<Grid> grids )
+    {
+        this.grids = grids;
+    }
+
     // -------------------------------------------------------------------------
     // Result implementation
     // -------------------------------------------------------------------------
 
     @Override
+    @SuppressWarnings( "unchecked" )
     public void execute( ActionInvocation invocation )
         throws Exception
     {
@@ -81,6 +94,10 @@
 
         grid = _grid != null ? _grid : grid;
 
+        List<Grid> _grids = (List<Grid>) invocation.getStack().findValue( "grids" );
+        
+        grids = _grids != null ? _grids : grids;
+        
         // ---------------------------------------------------------------------
         // Configure response
         // ---------------------------------------------------------------------
@@ -89,14 +106,21 @@
 
         OutputStream out = response.getOutputStream();
 
-        String filename = CodecUtils.filenameEncode( StringUtils.defaultIfEmpty( grid.getTitle(), DEFAULT_FILENAME ) ) + ".pdf";
-
+        String filename = filenameEncode( defaultIfEmpty( grid != null ? grid.getTitle() : grids.iterator().next().getTitle(), DEFAULT_NAME ) ) + ".pdf";;
+        
         ContextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_PDF, true, filename, false );
 
         // ---------------------------------------------------------------------
         // Write PDF to output stream
         // ---------------------------------------------------------------------
 
-        GridUtils.toPdf( grid, out );
+        if ( grid != null )
+        {
+            GridUtils.toPdf( grid, out );
+        }
+        else
+        {
+            GridUtils.toPdf( grids, out );
+        }
     }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/result/GridXlsResult.java'
--- dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/result/GridXlsResult.java	2011-05-05 21:15:45 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/result/GridXlsResult.java	2011-05-31 11:39:19 +0000
@@ -27,21 +27,26 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import static org.apache.commons.lang.StringUtils.defaultIfEmpty;
+import static org.hisp.dhis.system.util.CodecUtils.filenameEncode;
+
 import java.io.OutputStream;
+import java.util.List;
 
 import javax.servlet.http.HttpServletResponse;
 
-import org.apache.commons.lang.StringUtils;
 import org.apache.struts2.ServletActionContext;
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.system.grid.GridUtils;
-import org.hisp.dhis.system.util.CodecUtils;
 import org.hisp.dhis.util.ContextUtils;
 
 import com.opensymphony.xwork2.ActionInvocation;
 import com.opensymphony.xwork2.Result;
 
 /**
+ * Creates an XLS representation of the given Grid or list of Grids and writes
+ * it to the servlet outputstream. One of the grid or grids arguments must be set.
+ * 
  * @author Lars Helge Overland
  */
 public class GridXlsResult
@@ -52,7 +57,7 @@
      */
     private static final long serialVersionUID = 3030165635768899728L;
 
-    private static final String DEFAULT_FILENAME = "Grid.xls";
+    private static final String DEFAULT_NAME = "Grid.xls";
     
     // -------------------------------------------------------------------------
     // Input
@@ -65,11 +70,19 @@
         this.grid = grid;
     }
 
+    private List<Grid> grids;
+
+    public void setGrids( List<Grid> grids )
+    {
+        this.grids = grids;
+    }
+
     // -------------------------------------------------------------------------
     // Result implementation
     // -------------------------------------------------------------------------
 
     @Override
+    @SuppressWarnings( "unchecked" )
     public void execute( ActionInvocation invocation )
         throws Exception
     {
@@ -81,6 +94,10 @@
         
         grid = _grid != null ? _grid : grid; 
 
+        List<Grid> _grids = (List<Grid>) invocation.getStack().findValue( "grids" );
+        
+        grids = _grids != null ? _grids : grids;
+        
         // ---------------------------------------------------------------------
         // Configure response
         // ---------------------------------------------------------------------
@@ -89,14 +106,21 @@
 
         OutputStream out = response.getOutputStream();
 
-        String filename = CodecUtils.filenameEncode( StringUtils.defaultIfEmpty( grid.getTitle(), DEFAULT_FILENAME ) ) + ".xls";
-                
+        String filename = filenameEncode( defaultIfEmpty( grid != null ? grid.getTitle() : grids.iterator().next().getTitle(), DEFAULT_NAME ) ) + ".xls";;
+        
         ContextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_EXCEL, true, filename, true );
         
         // ---------------------------------------------------------------------
         // Create workbook and write to output stream
         // ---------------------------------------------------------------------
 
-        GridUtils.toXls( grid, out );
+        if ( grid != null )
+        {
+            GridUtils.toXls( grid, out );
+        }
+        else
+        {
+            GridUtils.toXls( grids, out );
+        }
     }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/SessionUtils.java'
--- dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/SessionUtils.java	2011-01-06 12:40:57 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/SessionUtils.java	2011-05-31 11:18:52 +0000
@@ -39,6 +39,7 @@
     public static final String KEY_PREVIEW_STATUS = "previewStatus";    
     public static final String KEY_CURRENT_YEAR = "currentYear";
     public static final String KEY_REPORT_TABLE_GRID = "lastReportTableGrid";
+    public static final String KEY_DATASET_REPORT_GRID = "lastDataSetReportGrid";
     
     public static Object getSessionVar( String name )
     {

=== modified file 'dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/dataset/action/GenerateDataSetReportAction.java'
--- dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/dataset/action/GenerateDataSetReportAction.java	2011-05-30 20:54:54 +0000
+++ dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/dataset/action/GenerateDataSetReportAction.java	2011-05-31 11:18:52 +0000
@@ -42,11 +42,13 @@
 import org.hisp.dhis.oust.manager.SelectionTreeManager;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodService;
+import org.hisp.dhis.util.SessionUtils;
 
 import com.opensymphony.xwork2.Action;
 import com.opensymphony.xwork2.ActionContext;
 
 import static org.hisp.dhis.dataset.DataSet.*;
+import static org.hisp.dhis.util.SessionUtils.*;
 
 /**
  * @author Chau Thu Tran
@@ -134,6 +136,20 @@
         this.selectedUnitOnly = selectedUnitOnly;
     }
 
+    private boolean useLast;
+
+    public void setUseLast( boolean useLast )
+    {
+        this.useLast = useLast;
+    }
+    
+    private String type;
+
+    public void setType( String type )
+    {
+        this.type = type;
+    }
+
     // -------------------------------------------------------------------------
     // Output
     // -------------------------------------------------------------------------
@@ -185,37 +201,42 @@
     // -------------------------------------------------------------------------
 
     @Override
+    @SuppressWarnings( "unchecked" )
     public String execute()
         throws Exception
     {
         selectedOrgunit = selectionTreeManager.getSelectedOrganisationUnit();
 
-        if ( dataSetId == null || periodId == null || selectedOrgunit == null )
-        {
-            return ERROR;
-        }
-
         selectedDataSet = dataSetService.getDataSet( dataSetId );
 
-        selectedPeriod = periodService.getPeriodByExternalId( periodId );
-
-        String type = selectedDataSet.getDataSetType();
+        if ( periodId != null )
+        {
+            selectedPeriod = periodService.getPeriodByExternalId( periodId );
+        }
         
-        if ( TYPE_CUSTOM.equals( type ) )
+        String dataSetType = selectedDataSet.getDataSetType();
+                
+        if ( TYPE_CUSTOM.equals( dataSetType ) )
         {
             customDataEntryFormCode = dataSetReportService.getCustomDataSetReport( selectedDataSet, selectedOrgunit, selectedPeriod, selectedUnitOnly, format );
         }
-        else if ( TYPE_SECTION.equals( type ) )
+        else if ( TYPE_SECTION.equals( dataSetType ) )
         {
-            grids = dataSetReportService.getSectionDataSetReport( selectedDataSet, selectedPeriod, selectedOrgunit, selectedUnitOnly, format, i18n );            
+            grids = useLast ? (List<Grid>) getSessionVar( KEY_DATASET_REPORT_GRID ) :
+                dataSetReportService.getSectionDataSetReport( selectedDataSet, selectedPeriod, selectedOrgunit, selectedUnitOnly, format, i18n );
+            
+            SessionUtils.setSessionVar( SessionUtils.KEY_DATASET_REPORT_GRID, grids );
         }
         else
         {
-            grid = dataSetReportService.getDefaultDataSetReport( selectedDataSet, selectedPeriod, selectedOrgunit, selectedUnitOnly, format, i18n );
+            grid = useLast ? (Grid) getSessionVar( KEY_DATASET_REPORT_GRID ) :
+                dataSetReportService.getDefaultDataSetReport( selectedDataSet, selectedPeriod, selectedOrgunit, selectedUnitOnly, format, i18n );
+            
+            SessionUtils.setSessionVar( SessionUtils.KEY_DATASET_REPORT_GRID, grid );
         }
-        
-        ActionContext.getContext().getActionInvocation().getStack().setValue( PARAM_PAGE, VIEW_MAP.get( type ) );
-        
-        return SUCCESS;
+                
+        ActionContext.getContext().getActionInvocation().getStack().setValue( PARAM_PAGE, VIEW_MAP.get( dataSetType ) );
+        
+        return useLast ? type : SUCCESS;
     }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-reporting/src/main/resources/struts.xml'
--- dhis-2/dhis-web/dhis-web-reporting/src/main/resources/struts.xml	2011-05-30 20:54:54 +0000
+++ dhis-2/dhis-web/dhis-web-reporting/src/main/resources/struts.xml	2011-05-31 11:18:52 +0000
@@ -278,8 +278,11 @@
     </action>	   
         
 	<action name="generateDataSetReport" class="org.hisp.dhis.reporting.dataset.action.GenerateDataSetReportAction">
+      <result name="xls" type="gridXlsResult"/>
+      <result name="pdf" type="gridPdfResult"/>
 	  <result name="success" type="velocity">/main.vm</result> <!-- Page param set in action -->
-	  <param name="menu">/dhis-web-reporting/menu.vm</param>	  
+	  <param name="menu">/dhis-web-reporting/menu.vm</param>	
+      <param name="javascripts">javascript/dataSetReport.js</param>  
 	</action>
 	
     <!-- Tally Sheet Forms Generator -->

=== modified file 'dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/javascript/dataSetReport.js'
--- dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/javascript/dataSetReport.js	2011-03-20 14:21:11 +0000
+++ dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/javascript/dataSetReport.js	2011-05-31 11:18:52 +0000
@@ -10,7 +10,7 @@
     selectedOrganisationUnitIds = ids;
 }
 
-if ( selectionTreeSelection )
+if ( typeof( selectionTreeSelection ) != "undefined" )
 {
     selectionTreeSelection.setListenerFunction( setSelectedOrganisationUnitIds );
 }
@@ -42,3 +42,9 @@
     
     document.getElementById( "reportForm" ).submit();
 }
+
+function exportDataSetReport( type )
+{
+	var url = "generateDataSetReport.action?useLast=true&dataSetId=" + $( "#dataSetId" ).val() + "&type=" + type;
+	window.location.href = url;
+}
\ No newline at end of file

=== modified file 'dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/renderDefaultDataSetReportForm.vm'
--- dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/renderDefaultDataSetReportForm.vm	2011-05-30 20:54:54 +0000
+++ dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/renderDefaultDataSetReportForm.vm	2011-05-31 11:18:52 +0000
@@ -1,6 +1,11 @@
 
 <h3>$i18n.getString('reporting_unit'): $encoder.htmlEncode( $selectedOrgunit.name ) &nbsp; $i18n.getString('reporting_period'): $format.formatPeriod( $selectedPeriod )</h3>
-<p><input type="button" value="$i18n.getString( 'back' )" style="width:100px" onclick="javascript:window.location.href='showDataSetReportForm.action'"/></p>
+
+<div style="margin-bottom:15px">
+<input type="button" value="$i18n.getString( 'get_report_as_xls' )" onclick="exportDataSetReport( 'xls' )" style="width:140px">
+<input type="button" value="$i18n.getString( 'get_report_as_pdf' )" onclick="exportDataSetReport( 'pdf' )" style="width:140px">
+<input type="button" value="$i18n.getString( 'back' )" style="width:100px" onclick="javascript:window.location.href='showDataSetReportForm.action'"/>
+</div>
 
 <div>
 #parse( "dhis-web-commons/ajax/htmlGrid.vm" )

=== modified file 'dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/renderSectionDataSetReportForm.vm'
--- dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/renderSectionDataSetReportForm.vm	2011-05-30 20:54:54 +0000
+++ dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/renderSectionDataSetReportForm.vm	2011-05-31 11:18:52 +0000
@@ -4,8 +4,15 @@
 }
 </style>
 
+<input type="hidden" id="dataSetId" value="${selectedDataSet.id}">
+
 <h3>$i18n.getString('reporting_unit'): $encoder.htmlEncode( $selectedOrgunit.name ) &nbsp; $i18n.getString('reporting_period'): $format.formatPeriod( $selectedPeriod )</h3>
-<p><input type="button" value="$i18n.getString( 'back' )" style="width:100px" onclick="javascript:window.location.href='showDataSetReportForm.action'"/></p>
+
+<div style="margin-bottom:15px">
+<input type="button" value="$i18n.getString( 'get_report_as_xls' )" onclick="exportDataSetReport( 'xls' )" style="width:140px">
+<input type="button" value="$i18n.getString( 'get_report_as_pdf' )" onclick="exportDataSetReport( 'pdf' )" style="width:140px">
+<input type="button" value="$i18n.getString( 'back' )" style="width:100px" onclick="javascript:window.location.href='showDataSetReportForm.action'"/>
+</div>
 
 #foreach( $grid in $grids )
 <div style="border:1px solid #c0c0c0; padding:10px; margin-bottom:30px;">