← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2983: Implemented export to Excel (xls) for standard report

 

------------------------------------------------------------
revno: 2983
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2011-03-08 02:55:01 +0100
message:
  Implemented export to Excel (xls) for standard report
added:
  dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/JRExportUtils.java
modified:
  dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/ContextUtils.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/pom.xml
  dhis-2/dhis-web/dhis-web-reporting/pom.xml
  dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/reportviewer/action/RenderReportAction.java
  dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/inputReportParamsForm.vm
  dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/javascript/generateReport.js
  dhis-2/pom.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-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-01-27 16:15:25 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/ContextUtils.java	2011-03-08 01:55:01 +0000
@@ -56,6 +56,24 @@
     private static final String PORT_SEPARATOR = ":";    
     private static final String PROTOCOL = "http://";;
     
+    private static final Map<String, String> CONTENT_TYPE_MAP = new HashMap<String, String>() { {
+        put( "pdf", CONTENT_TYPE_PDF );
+        put( "zip", CONTENT_TYPE_ZIP );
+        put( "json", CONTENT_TYPE_JSON );
+        put( "html", CONTENT_TYPE_HTML );
+        put( "txt", CONTENT_TYPE_TEXT );
+        put( "xml", CONTENT_TYPE_XML );
+        put( "csv", CONTENT_TYPE_CSV );
+        put( "png", CONTENT_TYPE_PNG );
+        put( "xls", CONTENT_TYPE_EXCEL );
+    } };
+    
+    public static String getContentType( String type, String defaultType )
+    {
+        String contentType = CONTENT_TYPE_MAP.get( type );
+        return contentType != null ? contentType : defaultType;
+    }
+    
     @SuppressWarnings( "unchecked" )
     public static Map<String, String> getParameterMap( HttpServletRequest request )
     {

=== added file 'dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/JRExportUtils.java'
--- dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/JRExportUtils.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/JRExportUtils.java	2011-03-08 01:55:01 +0000
@@ -0,0 +1,93 @@
+package org.hisp.dhis.util;
+
+/*
+ * Copyright (c) 2004-2010, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import java.io.OutputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import net.sf.jasperreports.engine.JRAbstractExporter;
+import net.sf.jasperreports.engine.JRException;
+import net.sf.jasperreports.engine.JRExporterParameter;
+import net.sf.jasperreports.engine.JasperPrint;
+import net.sf.jasperreports.engine.export.JRPdfExporter;
+import net.sf.jasperreports.engine.export.JRXlsExporter;
+import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
+
+/**
+ * Supports PDF and XLS exports.
+ * 
+ * @author Lars Helge Overland
+ */
+public class JRExportUtils
+{
+    public static String TYPE_XLS = "xls";
+    public static String TYPE_PDF = "pdf";
+    
+    private static final Map<String, JRExportProvider> exporters = new HashMap<String, JRExportProvider>() { {
+        put( TYPE_XLS, new JRXlsExportProvider() );
+        put( TYPE_PDF, new JRPdfExportProvider() );
+    } };
+    
+    public static void export( String type, OutputStream out, JasperPrint jasperPrint )
+        throws JRException
+    {
+        JRAbstractExporter exporter = exporters.get( type ).provide();
+        
+        exporter.setParameter( JRExporterParameter.OUTPUT_STREAM, out );
+        exporter.setParameter( JRExporterParameter.JASPER_PRINT, jasperPrint );
+        exporter.exportReport();
+    }
+    
+    private interface JRExportProvider
+    {
+        JRAbstractExporter provide();
+    }
+    
+    private static class JRXlsExportProvider implements JRExportProvider
+    {
+        public JRAbstractExporter provide()
+        {
+            JRXlsExporter exporter = new JRXlsExporter();
+            exporter.setParameter( JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE );
+            exporter.setParameter( JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE );
+            exporter.setParameter( JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE );
+            return exporter;
+        }
+    }
+    
+    private static class JRPdfExportProvider implements JRExportProvider
+    {
+        public JRAbstractExporter provide()
+        {
+            return new JRPdfExporter();
+        }
+    }
+}
+
+

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/pom.xml'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/pom.xml	2011-03-02 00:23:51 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/pom.xml	2011-03-08 01:55:01 +0000
@@ -47,8 +47,7 @@
 	
 	<dependency>
 		<groupId>org.apache.poi</groupId>
-		<artifactId>poi</artifactId>		
-		<version>3.5-FINAL</version>
+		<artifactId>poi</artifactId>
 	</dependency>
 	
      <!-- Other -->

=== modified file 'dhis-2/dhis-web/dhis-web-reporting/pom.xml'
--- dhis-2/dhis-web/dhis-web-reporting/pom.xml	2011-03-02 00:23:51 +0000
+++ dhis-2/dhis-web/dhis-web-reporting/pom.xml	2011-03-08 01:55:01 +0000
@@ -57,6 +57,10 @@
       <groupId>jfree</groupId>
       <artifactId>jfreechart</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.apache.poi</groupId>
+      <artifactId>poi</artifactId>
+    </dependency>
     
     <!-- Other -->
        

=== modified file 'dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/reportviewer/action/RenderReportAction.java'
--- dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/reportviewer/action/RenderReportAction.java	2011-03-01 00:49:30 +0000
+++ dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/reportviewer/action/RenderReportAction.java	2011-03-08 01:55:01 +0000
@@ -27,13 +27,14 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import static org.apache.commons.lang.StringUtils.defaultIfEmpty;
+
 import java.io.OutputStream;
 import java.sql.Connection;
 
 import javax.servlet.http.HttpServletResponse;
 
 import net.sf.jasperreports.engine.JasperCompileManager;
-import net.sf.jasperreports.engine.JasperExportManager;
 import net.sf.jasperreports.engine.JasperFillManager;
 import net.sf.jasperreports.engine.JasperPrint;
 import net.sf.jasperreports.engine.JasperReport;
@@ -48,6 +49,7 @@
 import org.hisp.dhis.system.util.CodecUtils;
 import org.hisp.dhis.system.util.StreamUtils;
 import org.hisp.dhis.util.ContextUtils;
+import org.hisp.dhis.util.JRExportUtils;
 import org.hisp.dhis.util.StreamActionSupport;
 
 /**
@@ -57,7 +59,7 @@
 public class RenderReportAction
     extends StreamActionSupport
 {
-    private static final String EXT_PDF = ".pdf";
+    private static final String DEFAULT_TYPE = "pdf";
     
     // -------------------------------------------------------------------------
     // Dependencies
@@ -115,6 +117,13 @@
     {
         this.organisationUnitId = organisationUnitId;
     }
+    
+    private String type;
+
+    public void setType( String type )
+    {
+        this.type = type;
+    }
 
     // -------------------------------------------------------------------------
     // Action implementation
@@ -124,6 +133,8 @@
     protected String execute( HttpServletResponse response, OutputStream out )
         throws Exception
     {
+        type = defaultIfEmpty( type, DEFAULT_TYPE );
+        
         Report report = reportService.getReport( id );
         
         JasperReport jasperReport = JasperCompileManager.compileReport( StreamUtils.getInputStream( report.getDesignContent() ) );
@@ -154,7 +165,7 @@
         
         if ( print != null )
         {
-            JasperExportManager.exportReportToPdfStream( print, out );
+            JRExportUtils.export( type, out, print );
         }
         
         return SUCCESS;
@@ -163,7 +174,7 @@
     @Override
     protected String getContentType()
     {
-        return ContextUtils.CONTENT_TYPE_PDF;
+        return ContextUtils.getContentType( type, ContextUtils.CONTENT_TYPE_PDF );
     }
 
     @Override
@@ -171,7 +182,7 @@
     {
         Report report = reportService.getReport( id );
         
-        return CodecUtils.filenameEncode( report.getName() ) + EXT_PDF;
+        return CodecUtils.filenameEncode( report.getName() ) + "." + defaultIfEmpty( type, DEFAULT_TYPE );
     }
     
     @Override
@@ -183,6 +194,6 @@
     @Override
     protected boolean attachment()
     {
-        return false;
+        return !defaultIfEmpty( type, DEFAULT_TYPE ).equals( DEFAULT_TYPE );
     }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/inputReportParamsForm.vm'
--- dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/inputReportParamsForm.vm	2011-02-22 23:05:45 +0000
+++ dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/inputReportParamsForm.vm	2011-03-08 01:55:01 +0000
@@ -77,11 +77,13 @@
         <td>
         	#if( $!reportParams.isSet() )
         	
-            <input type="button" value="$i18n.getString( 'get_report' )" onclick="generateReport()" style="width:140px">
         	#if ( $mode == "table" )
+            <input type="button" value="$i18n.getString( 'get_report' )" onclick="generateReport( '' )" style="width:140px">        	
             <input type="button" value="$i18n.getString( 'back' )" onclick="javascript:window.location.href='displayManageTableForm.action'" style="width:140px">
         	#elseif ( $mode == "report" )
-            <input type="button" value="$i18n.getString( 'back' )" onclick="javascript:window.location.href='displayViewReportForm.action'" style="width:140px">
+            <input type="button" value="$i18n.getString( 'get_report_as_pdf' )" onclick="generateReport( 'pdf' )" style="width:140px">
+            <input type="button" value="$i18n.getString( 'back' )" onclick="javascript:window.location.href='displayViewReportForm.action'" style="width:140px"><br>
+            <input type="button" value="$i18n.getString( 'get_report_as_xls' )" onclick="generateReport( 'xls' )" style="width:140px">
         	#end
         	
             #end
@@ -100,6 +102,6 @@
 
 #if( !$!reportParams.isSet() )
 <script type="text/javascript">
-generateReport();
+generateReport( '' );
 </script>
 #end

=== modified file 'dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/javascript/generateReport.js'
--- dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/javascript/generateReport.js	2011-03-07 23:37:36 +0000
+++ dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/javascript/generateReport.js	2011-03-08 01:55:01 +0000
@@ -2,6 +2,8 @@
 var MODE_REPORT = "report";
 var MODE_TABLE = "table";
 
+var reportType;
+
 // -----------------------------------------------------------------------------
 // Report params
 // -----------------------------------------------------------------------------
@@ -32,8 +34,10 @@
 // Report
 // -----------------------------------------------------------------------------
 
-function generateReport()
+function generateReport( type )
 {
+	reportType = type != null && type != "" ? type : "pdf";
+	
 	if ( validationError() )
 	{
 		return false;
@@ -74,7 +78,7 @@
     
     if ( finished == "true" )
     {
-        setMessage( i18n_process_completed );        
+        setMessage( i18n_process_completed );
     	viewReport();
     }
     else
@@ -87,9 +91,11 @@
 {
 	var mode = $( "#mode" ).val();
 	
+	setMessage( i18n_process_completed );
+	
     if ( mode == MODE_REPORT )
     {
-    	window.location.href = "renderReport.action?" + getUrlParams();
+    	window.location.href = "renderReport.action?type=" + reportType + "&" + getUrlParams();
     }
     else // MODE_TABLE
     {

=== modified file 'dhis-2/pom.xml'
--- dhis-2/pom.xml	2011-03-02 00:23:51 +0000
+++ dhis-2/pom.xml	2011-03-08 01:55:01 +0000
@@ -373,6 +373,11 @@
         <version>1.2</version>
       </dependency>
       <dependency>
+        <groupId>org.apache.poi</groupId>
+	<artifactId>poi</artifactId>		
+	<version>3.7</version>
+     </dependency>	
+      <dependency>
         <groupId>org.amplecode</groupId>
         <artifactId>quick</artifactId>
         <version>1.3</version>
@@ -557,7 +562,7 @@
       <dependency>
         <groupId>net.sf.jasperreports</groupId>
         <artifactId>jasperreports</artifactId>
-        <version>4.0.0</version>
+        <version>4.0.1</version>
         <exclusions>
           <exclusion>
             <groupId>xalan</groupId>