← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11635: Pushed rendering of HTML based standard reports to ReportService. Made HTML based reports availab...

 

------------------------------------------------------------
revno: 11635
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-08-12 15:07:42 +0200
message:
  Pushed rendering of HTML based standard reports to ReportService. Made HTML based reports available in Web API.
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-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ReportController.java
  dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/reportviewer/action/GetReportAndParamsAction.java
  dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/renderHtmlReport.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-api/src/main/java/org/hisp/dhis/report/ReportService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/report/ReportService.java	2013-07-23 07:51:07 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/report/ReportService.java	2013-08-12 13:07:42 +0000
@@ -28,6 +28,7 @@
  */
 
 import java.io.OutputStream;
+import java.io.Writer;
 import java.util.Collection;
 import java.util.List;
 
@@ -78,6 +79,17 @@
      */
     JasperPrint renderReport( OutputStream out, String reportUid, Period period,
         String organisationUnitUid, String type, I18nFormat format );
+    
+    /**
+     * Renders and writes a HTML-based standard report to the given Writer.
+     * 
+     * @param writer the Writer.
+     * @param uid the report uid.
+     * @param pe the period iso identifier.
+     * @param ou the organisation unit uid.
+     * @param format the I18nFormat.
+     */
+    void renderHtmlReport( Writer writer, String uid, String pe, String ou, I18nFormat format );
 
     /**
      * Saves a Report.

=== 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	2013-07-23 07:51:07 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/report/impl/DefaultReportService.java	2013-08-12 13:07:42 +0000
@@ -31,7 +31,9 @@
 import static org.hisp.dhis.system.util.TextUtils.getCommaDelimitedString;
 
 import java.io.OutputStream;
+import java.io.Writer;
 import java.sql.Connection;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Date;
 import java.util.HashMap;
@@ -46,23 +48,28 @@
 import net.sf.jasperreports.engine.JasperReport;
 import net.sf.jasperreports.engine.util.JRProperties;
 
+import org.apache.velocity.VelocityContext;
 import org.hisp.dhis.common.GenericIdentifiableObjectStore;
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.common.IdentifiableObjectUtils;
 import org.hisp.dhis.constant.ConstantService;
+import org.hisp.dhis.i18n.I18n;
 import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodService;
+import org.hisp.dhis.period.PeriodType;
 import org.hisp.dhis.report.Report;
 import org.hisp.dhis.report.ReportService;
 import org.hisp.dhis.reporttable.ReportTable;
 import org.hisp.dhis.reporttable.ReportTableService;
+import org.hisp.dhis.system.util.Encoder;
 import org.hisp.dhis.system.util.Filter;
 import org.hisp.dhis.system.util.FilterUtils;
 import org.hisp.dhis.system.util.JRExportUtils;
 import org.hisp.dhis.system.util.StreamUtils;
+import org.hisp.dhis.system.velocity.VelocityManager;
 import org.springframework.jdbc.datasource.DataSourceUtils;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -76,6 +83,8 @@
 {
     public static final String ORGUNIT_LEVEL_COLUMN_PREFIX = "idlevel";
     public static final String ORGUNIT_UID_LEVEL_COLUMN_PREFIX = "uidlevel";
+
+    private static final Encoder ENCODER = new Encoder();
     
     // -------------------------------------------------------------------------
     // Dependencies
@@ -217,6 +226,58 @@
         
         return print;
     }
+    
+    public void renderHtmlReport( Writer writer, String uid, String pe, String ou, I18nFormat format )
+    {
+        Report report = getReport( uid );        
+        OrganisationUnit organisationUnit = null;
+        List<OrganisationUnit> organisationUnitHierarchy = new ArrayList<OrganisationUnit>();
+        List<OrganisationUnit> organisationUnitChildren = new ArrayList<OrganisationUnit>();
+        List<Period> periods = new ArrayList<Period>();
+        
+        if ( ou != null )
+        {
+            organisationUnit = organisationUnitService.getOrganisationUnit( ou );
+            
+            if ( organisationUnit != null )
+            {
+                organisationUnitHierarchy.add( organisationUnit );
+                
+                OrganisationUnit parent = organisationUnit;
+                
+                while ( parent.getParent() != null )
+                {
+                    parent = parent.getParent();
+                    organisationUnitHierarchy.add( parent );
+                }
+                
+                organisationUnitChildren.addAll( organisationUnit.getChildren() );
+            }
+        }
+        
+        Date date = new Date();
+        
+        if ( pe != null )
+        {
+            date = PeriodType.getPeriodFromIsoString( pe ).getStartDate();
+        }
+        
+        if ( report != null && report.hasRelativePeriods() )
+        {
+            periods = report.getRelatives().getRelativePeriods( date, format, true );
+        }
+        
+        final VelocityContext context = new VelocityContext();
+        context.put( "report", report );
+        context.put( "organisationUnit", organisationUnit );
+        context.put( "organisationUnitHierarchy", organisationUnitHierarchy );
+        context.put( "organisationUnitChildren", organisationUnitChildren );
+        context.put( "periods", periods );
+        context.put( "format", format );
+        context.put( "encoder", ENCODER );
+        
+        new VelocityManager().getEngine().getTemplate( "html-report.vm" ).merge( context, writer );
+    }
 
     public int saveReport( Report report )
     {

=== 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	2013-06-09 21:26:26 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ReportController.java	2013-08-12 13:07:42 +0000
@@ -27,6 +27,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import static org.hisp.dhis.system.util.CodecUtils.filenameEncode;
+
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
@@ -36,6 +38,7 @@
 
 import org.hisp.dhis.api.utils.ContextUtils;
 import org.hisp.dhis.api.utils.ContextUtils.CacheStrategy;
+import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.i18n.I18nManager;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.period.MonthlyPeriodType;
@@ -53,8 +56,6 @@
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 
-import static org.hisp.dhis.system.util.CodecUtils.filenameEncode;
-
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  * @author Lars Helge Overland
@@ -102,9 +103,7 @@
         @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( BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, print );
+        getReport( request, response, uid, organisationUnitUid, period, "html", ContextUtils.CONTENT_TYPE_HTML, false );
     }
     
     @RequestMapping( value = "/{uid}/design", method = RequestMethod.PUT )
@@ -170,15 +169,17 @@
     // Supportive methods
     // -------------------------------------------------------------------------
 
-    private JasperPrint getReport( HttpServletRequest request, HttpServletResponse response, String uid, String organisationUnitUid, String isoPeriod,
+    private void getReport( HttpServletRequest request, HttpServletResponse response, String uid, String organisationUnitUid, String isoPeriod,
         String type, String contentType, boolean attachment ) throws Exception
     {
         Report report = reportService.getReport( uid );
+        
+        I18nFormat format = i18nManager.getI18nFormat();
 
         if ( report == null )
         {
             ContextUtils.notFoundResponse( response, "Report does not exist: " + uid );
-            return null;
+            return;
         }
         
         if ( organisationUnitUid == null && report.hasReportTable() && report.getReportTable().hasReportParams()
@@ -189,12 +190,24 @@
 
         Period period = isoPeriod != null ? PeriodType.getPeriodFromIsoString( isoPeriod ) : new MonthlyPeriodType().createPeriod();
         
-        String filename = CodecUtils.filenameEncode( report.getName() ) + "." + type;
-        contextUtils.configureResponse( response, contentType, CacheStrategy.RESPECT_SYSTEM_SETTING, filename, attachment );
-
-        JasperPrint print = reportService.renderReport( response.getOutputStream(), uid, period, organisationUnitUid, type,
-            i18nManager.getI18nFormat() );
-        
-        return print;
+        if ( report.isTypeHtml() )
+        {
+            contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_HTML, CacheStrategy.RESPECT_SYSTEM_SETTING );
+            
+            reportService.renderHtmlReport( response.getWriter(), uid, isoPeriod, organisationUnitUid, format );
+        }
+        else
+        {
+            String filename = CodecUtils.filenameEncode( report.getName() ) + "." + type;
+            
+            contextUtils.configureResponse( response, contentType, CacheStrategy.RESPECT_SYSTEM_SETTING, filename, attachment );
+            
+            JasperPrint print = reportService.renderReport( response.getOutputStream(), uid, period, organisationUnitUid, type, format );
+            
+            if ( "html".equals( type ) )
+            {            
+                request.getSession().setAttribute( BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, print );
+            }
+        }
     }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/reportviewer/action/GetReportAndParamsAction.java'
--- dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/reportviewer/action/GetReportAndParamsAction.java	2013-04-30 11:25:21 +0000
+++ dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/reportviewer/action/GetReportAndParamsAction.java	2013-08-12 13:07:42 +0000
@@ -27,16 +27,11 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
+import java.io.StringWriter;
+import java.io.Writer;
 
 import org.hisp.dhis.i18n.I18nFormat;
-import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
-import org.hisp.dhis.period.Period;
-import org.hisp.dhis.period.PeriodType;
-import org.hisp.dhis.report.Report;
 import org.hisp.dhis.report.ReportService;
 import org.springframework.beans.factory.annotation.Autowired;
 
@@ -90,39 +85,11 @@
     // Output
     // -------------------------------------------------------------------------
 
-    private Report report;
-    
-    public Report getReport()
-    {
-        return report;
-    }
-    
-    private OrganisationUnit organisationUnit;
-
-    public OrganisationUnit getOrganisationUnit()
-    {
-        return organisationUnit;
-    }
-
-    private List<OrganisationUnit> organisationUnitHierarchy = new ArrayList<OrganisationUnit>();
-    
-    public List<OrganisationUnit> getOrganisationUnitHierarchy()
-    {
-        return organisationUnitHierarchy;
-    }
-    
-    private List<OrganisationUnit> organisationUnitChildren = new ArrayList<OrganisationUnit>();
-    
-    public List<OrganisationUnit> getOrganisationUnitChildren()
-    {
-        return organisationUnitChildren;
-    }
-
-    private List<Period> periods;
-    
-    public List<Period> getPeriods()
-    {
-        return periods;
+    private String content;
+
+    public String getContent()
+    {
+        return content;
     }
 
     // -------------------------------------------------------------------------
@@ -130,40 +97,13 @@
     // -------------------------------------------------------------------------
 
     public String execute()
+        throws Exception
     {
-        report = reportService.getReport( uid );
-        
-        if ( ou != null )
-        {
-            organisationUnit = organisationUnitService.getOrganisationUnit( ou );
-            
-            if ( organisationUnit != null )
-            {
-                organisationUnitHierarchy.add( organisationUnit );
-                
-                OrganisationUnit parent = organisationUnit;
-                
-                while ( parent.getParent() != null )
-                {
-                    parent = parent.getParent();
-                    organisationUnitHierarchy.add( parent );
-                }
-                
-                organisationUnitChildren.addAll( organisationUnit.getChildren() );
-            }
-        }
-        
-        Date date = new Date();
-        
-        if ( pe != null )
-        {
-            date = PeriodType.getPeriodFromIsoString( pe ).getStartDate();
-        }
-        
-        if ( report != null && report.hasRelativePeriods() )
-        {
-            periods = report.getRelatives().getRelativePeriods( date, format, true );
-        }
+        Writer writer = new StringWriter();
+        
+        reportService.renderHtmlReport( writer, uid, pe, ou, format );
+        
+        content = writer.toString();
         
         return SUCCESS;
     }

=== modified file 'dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/renderHtmlReport.vm'
--- dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/renderHtmlReport.vm	2013-04-29 13:14:35 +0000
+++ dhis-2/dhis-web/dhis-web-reporting/src/main/webapp/dhis-web-reporting/renderHtmlReport.vm	2013-08-12 13:07:42 +0000
@@ -1,57 +1,6 @@
-<script type="text/javascript">
-
-dhis2.util.namespace( 'dhis2.report' );
-
-dhis2.report.organisationUnit = {
-#if( $organisationUnit )
-    "id": "$!encoder.htmlEncode( ${organisationUnit.uid} )",
-    "name": "$!encoder.htmlEncode( ${organisationUnit.name} )",
-    "code": "$!encoder.htmlEncode( ${organisationUnit.code} )"
-#end
-};
-
-dhis2.report.organisationUnitHierarchy = [
-#if( $organisationUnitHierarchy )
-#set( $size = $organisationUnitHierarchy.size() )
-#foreach( $ou in $organisationUnitHierarchy )
-{
-    "id": "$!encoder.htmlEncode( ${ou.uid} )",
-    "name": "$!encoder.htmlEncode( ${ou.name} )",
-    "code": "$!encoder.htmlEncode( ${ou.code} )"
-}#if( $velocityCount < $size ),#end
-#end
-#end
-];
-
-dhis2.report.periods = [
-#if( $periods )
-#set( $size = $periods.size() )
-#foreach( $period in $periods )
-"$period.getIsoDate()"#if( $velocityCount < $size ),#end
-#end
-#end
-];
-
-dhis2.report.organisationUnitChildren = [
-#if( $organisationUnitChildren )
-#set( $size = $organisationUnitChildren.size() )
-#foreach( $ou in $organisationUnitChildren )
-{
-    "id": "$!encoder.htmlEncode( ${ou.uid} )",
-    "name": "$!encoder.htmlEncode( ${ou.name} )",
-    "code": "$!encoder.htmlEncode( ${ou.code} )"
-}#if( $velocityCount < $size ),#end
-#end
-#end
-];
-
-</script>
-
 <div style="margin-bottom:16px" class="hideInPrint">
 <input type="button" value="$i18n.getString( 'print' )" onclick="window.print()" id="printButton" style="width:140px">
-<input type="button" value="$i18n.getString( 'back' )" onclick="javascript:window.location.href='displayViewReportForm.action'" style="width:140px">
+<input type="button" value="$i18n.getString( 'back' )" onclick="javascript:window.location.href='displayViewReportForm.action'" style="width:140px">
 </div>
 
-<div>
-${report.designContent}
-</div>
\ No newline at end of file
+${content}
\ No newline at end of file