← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 8517: Impl jasper reports HTML rendering

 

------------------------------------------------------------
revno: 8517
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2012-10-14 16:39:23 +0200
message:
  Impl jasper reports HTML rendering
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/report/ReportService.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/report/impl/DefaultReportService.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/JRExportUtils.java
  dhis-2/dhis-web/dhis-web-api/pom.xml
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ReportController.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/report/ReportService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/report/ReportService.java	2012-10-01 10:50:34 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/report/ReportService.java	2012-10-14 14:39:23 +0000
@@ -30,6 +30,8 @@
 import java.io.OutputStream;
 import java.util.Collection;
 
+import net.sf.jasperreports.engine.JasperPrint;
+
 import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.period.Period;
 
@@ -70,7 +72,7 @@
      * @param type the type of the report, can be "xls" and "pdf".
      * @param format the I18nFormat to use.
      */
-    void renderReport( OutputStream out, String reportUid, Period period,
+    JasperPrint renderReport( OutputStream out, String reportUid, Period period,
                        String organisationUnitUid, String type, I18nFormat format );
 
     /**

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/report/impl/DefaultReportService.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/report/impl/DefaultReportService.java	2012-10-01 10:50:34 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/report/impl/DefaultReportService.java	2012-10-14 14:39:23 +0000
@@ -129,7 +129,7 @@
     // ReportService implementation
     // -------------------------------------------------------------------------
 
-    public void renderReport( OutputStream out, String reportUid, Period period,
+    public JasperPrint renderReport( OutputStream out, String reportUid, Period period,
         String organisationUnitUid, String type, I18nFormat format )
     {
         Report report = getReport( reportUid );
@@ -157,13 +157,13 @@
             params.put( PARAM_ORGANISATIONUNIT_LEVEL, level );
             params.put( PARAM_ORGANISATIONUNIT_LEVEL_COLUMN, ORGANISATIONUNIT_LEVEL_COLUMN_PREFIX + level );
         }
-        
+
+        JasperPrint print = null;
+
         try
         {
             JasperReport jasperReport = JasperCompileManager.compileReport( StreamUtils.getInputStream( report.getDesignContent() ) );
 
-            JasperPrint print = null;
-
             if ( report.hasReportTable() ) // Use JR data source
             {
                 ReportTable reportTable = report.getReportTable();
@@ -212,6 +212,8 @@
         {
             throw new RuntimeException( "Failed to render report", ex );
         }
+        
+        return print;
     }
 
     public int saveReport( Report report )

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/JRExportUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/JRExportUtils.java	2012-09-27 07:06:43 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/JRExportUtils.java	2012-10-14 14:39:23 +0000
@@ -35,6 +35,8 @@
 import net.sf.jasperreports.engine.JRException;
 import net.sf.jasperreports.engine.JRExporterParameter;
 import net.sf.jasperreports.engine.JasperPrint;
+import net.sf.jasperreports.engine.export.JRHtmlExporter;
+import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
 import net.sf.jasperreports.engine.export.JRPdfExporter;
 import net.sf.jasperreports.engine.export.JRXlsExporter;
 import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
@@ -48,11 +50,13 @@
 {
     public static final String TYPE_XLS = "xls";
     public static final String TYPE_PDF = "pdf";
+    public static final String TYPE_HTML = "html";
     
     private static final Map<String, JRExportProvider> exporters = new HashMap<String, JRExportProvider>() {    
     {
         put( TYPE_XLS, new JRXlsExportProvider() );
         put( TYPE_PDF, new JRPdfExportProvider() );
+        put( TYPE_HTML, new JRHtmlExportProvider() );
     } };
     
     public static void export( String type, OutputStream out, JasperPrint jasperPrint )
@@ -97,6 +101,17 @@
             return new JRPdfExporter();
         }
     }
+    
+    private static class JRHtmlExportProvider implements JRExportProvider
+    {
+        public JRAbstractExporter provide()
+        {
+            JRHtmlExporter exporter = new JRHtmlExporter();
+            exporter.setParameter( JRHtmlExporterParameter.IMAGES_URI, "../jasperReports/img?image=" );
+            
+            return exporter;
+        }
+    }
 }
 
 

=== modified file 'dhis-2/dhis-web/dhis-web-api/pom.xml'
--- dhis-2/dhis-web/dhis-web-api/pom.xml	2012-08-03 11:05:49 +0000
+++ dhis-2/dhis-web/dhis-web-api/pom.xml	2012-10-14 14:39:23 +0000
@@ -73,6 +73,10 @@
       <groupId>org.springframework</groupId>
       <artifactId>spring-oxm</artifactId>
     </dependency>
+    <dependency>
+      <groupId>net.sf.jasperreports</groupId>
+      <artifactId>jasperreports</artifactId>
+    </dependency>
   </dependencies>
 
   <properties>

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ReportController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ReportController.java	2012-09-27 09:28:54 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ReportController.java	2012-10-14 14:39:23 +0000
@@ -27,8 +27,12 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import net.sf.jasperreports.engine.JasperPrint;
+import net.sf.jasperreports.j2ee.servlets.ImageServlet;
+
 import org.hisp.dhis.api.utils.ContextUtils;
 import org.hisp.dhis.api.utils.ContextUtils.CacheStrategy;
 import org.hisp.dhis.i18n.I18nManager;
@@ -74,18 +78,29 @@
     public void getReportAsPdf( @PathVariable( "uid" ) String uid,
         @RequestParam( value = "ou", required = false ) String organisationUnitUid,
         @RequestParam( value = "pe", required = false ) String period,
-        HttpServletResponse response ) throws Exception
+        HttpServletRequest request, HttpServletResponse response ) throws Exception
     {
-        getReport( uid, organisationUnitUid, period, response, "pdf", ContextUtils.CONTENT_TYPE_PDF, false );
+        getReport( request, response, uid, organisationUnitUid, period, "pdf", ContextUtils.CONTENT_TYPE_PDF, false );
     }
 
     @RequestMapping( value = "/{uid}/data.xls", method = RequestMethod.GET )
     public void getReportAsXls( @PathVariable( "uid" ) String uid,
         @RequestParam( value = "ou", required = false ) String organisationUnitUid,
         @RequestParam( value = "pe", required = false ) String period,
-        HttpServletResponse response ) throws Exception
-    {
-        getReport( uid, organisationUnitUid, period, response, "xls", ContextUtils.CONTENT_TYPE_EXCEL, true );
+        HttpServletRequest request, HttpServletResponse response ) throws Exception
+    {
+        getReport( request, response, uid, organisationUnitUid, period, "xls", ContextUtils.CONTENT_TYPE_EXCEL, true );
+    }
+
+    @RequestMapping( value = "/{uid}/data.html", method = RequestMethod.GET )
+    public void getReportAsHtml( @PathVariable( "uid" ) String uid,
+        @RequestParam( value = "ou", required = false ) String organisationUnitUid,
+        @RequestParam( value = "pe", required = false ) String period,
+        HttpServletRequest request, HttpServletResponse response ) throws Exception
+    {
+        JasperPrint print = getReport( request, response, uid, organisationUnitUid, period, "html", ContextUtils.CONTENT_TYPE_HTML, false );
+        
+        request.getSession().setAttribute( ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, print );
     }
     
     @RequestMapping( value = "/{uid}/design", method = RequestMethod.PUT )
@@ -105,12 +120,24 @@
         reportService.saveReport( report );
     }
 
+    /**
+     * This methods wraps the Jasper image servlet to avoid having a separate
+     * servlet mapping around. Note that the path to images are relative to the
+     * reports path in this controller.
+     */
+    @RequestMapping( value = "/jasperReports/img", method = RequestMethod.GET )
+    public void getJasperImage( @RequestParam String image, 
+        HttpServletRequest request, HttpServletResponse response ) throws Exception
+    {
+        new ImageServlet().service( request, response );
+    }
+
     // -------------------------------------------------------------------------
     // Supportive methods
     // -------------------------------------------------------------------------
 
-    private void getReport( String uid, String organisationUnitUid, String isoPeriod,
-        HttpServletResponse response, String type, String contentType, boolean attachment ) throws Exception
+    private JasperPrint getReport( HttpServletRequest request, HttpServletResponse response, String uid, String organisationUnitUid, String isoPeriod,
+        String type, String contentType, boolean attachment ) throws Exception
     {
         Report report = reportService.getReport( uid );
 
@@ -125,7 +152,9 @@
         String filename = CodecUtils.filenameEncode( report.getName() ) + "." + type;
         contextUtils.configureResponse( response, contentType, CacheStrategy.RESPECT_SYSTEM_SETTING, filename, attachment );
 
-        reportService.renderReport( response.getOutputStream(), uid, period, organisationUnitUid, type,
+        JasperPrint print = reportService.renderReport( response.getOutputStream(), uid, period, organisationUnitUid, type,
             i18nManager.getI18nFormat() );
+        
+        return print;
     }
 }