=== 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 2011-07-21 03:29:35 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/report/ReportService.java 2011-09-18 14:36:11 +0000 @@ -27,10 +27,10 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import java.io.OutputStream; import java.util.Collection; -import org.hisp.dhis.report.Report; -import org.hisp.dhis.report.ReportGroup; +import org.hisp.dhis.i18n.I18nFormat; /** * @author Lars Helge Overland @@ -39,7 +39,13 @@ public interface ReportService { final String ID = ReportService.class.getName(); + + final String REPORTTYPE_PDF = "pdf"; + final String REPORTTYPE_XLS = "xls"; + public void renderReport( OutputStream out, Report report, Integer reportingPeriod, + Integer organisationUnitId, String type, I18nFormat format ); + /** * Saves a Report. * === modified file 'dhis-2/dhis-services/dhis-service-reporting/pom.xml' --- dhis-2/dhis-services/dhis-service-reporting/pom.xml 2011-08-28 17:52:59 +0000 +++ dhis-2/dhis-services/dhis-service-reporting/pom.xml 2011-09-16 16:54:03 +0000 @@ -38,6 +38,21 @@ dhis-service-mapping + + + + net.sf.jasperreports + jasperreports + + + jfree + jfreechart + + + org.apache.poi + poi + + === 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 2011-07-21 03:29:35 +0000 +++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/report/impl/DefaultReportService.java 2011-09-18 14:36:11 +0000 @@ -27,15 +27,33 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import java.io.OutputStream; +import java.sql.Connection; import java.util.Collection; +import java.util.HashMap; import java.util.Iterator; - +import java.util.Map; + +import net.sf.jasperreports.engine.JasperCompileManager; +import net.sf.jasperreports.engine.JasperFillManager; +import net.sf.jasperreports.engine.JasperPrint; +import net.sf.jasperreports.engine.JasperReport; + +import org.amplecode.quick.StatementManager; import org.hisp.dhis.common.GenericIdentifiableObjectStore; +import org.hisp.dhis.common.Grid; +import org.hisp.dhis.constant.ConstantService; +import org.hisp.dhis.i18n.I18nFormat; +import org.hisp.dhis.organisationunit.OrganisationUnitGroupService; import org.hisp.dhis.report.Report; import org.hisp.dhis.report.ReportGroup; import org.hisp.dhis.report.ReportService; +import org.hisp.dhis.reporttable.ReportTable; +import org.hisp.dhis.reporttable.ReportTableService; 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.springframework.transaction.annotation.Transactional; /** @@ -64,10 +82,86 @@ this.reportGroupStore = reportGroupStore; } - // ------------------------------------------------------------------------- - // Implements - // ------------------------------------------------------------------------- - + private ReportTableService reportTableService; + + public void setReportTableService( ReportTableService reportTableService ) + { + this.reportTableService = reportTableService; + } + + private ConstantService constantService; + + public void setConstantService( ConstantService constantService ) + { + this.constantService = constantService; + } + + private StatementManager statementManager; + + public void setStatementManager( StatementManager statementManager ) + { + this.statementManager = statementManager; + } + + private OrganisationUnitGroupService organisationUnitGroupService; + + public void setOrganisationUnitGroupService( OrganisationUnitGroupService organisationUnitGroupService ) + { + this.organisationUnitGroupService = organisationUnitGroupService; + } + + // ------------------------------------------------------------------------- + // ReportService implementation + // ------------------------------------------------------------------------- + + public void renderReport( OutputStream out, Report report, Integer reportingPeriod, + Integer organisationUnitId, String type, I18nFormat format ) + { + Map params = new HashMap(); + + params.putAll( constantService.getConstantParameterMap() ); + + try + { + JasperReport jasperReport = JasperCompileManager.compileReport( StreamUtils.getInputStream( report.getDesignContent() ) ); + + JasperPrint print = null; + + if ( report.hasReportTable() ) // Use JR data source + { + ReportTable reportTable = report.getReportTable(); + + params.putAll( reportTable.getOrganisationUnitGroupMap( organisationUnitGroupService.getCompulsoryOrganisationUnitGroupSets() ) ); + + Grid grid = reportTableService.getReportTableGrid( reportTable.getId(), format, reportingPeriod, organisationUnitId ); + + print = JasperFillManager.fillReport( jasperReport, params, grid ); + } + else // Assume SQL report and provide JDBC connection + { + Connection connection = statementManager.getHolder().getConnection(); + + try + { + print = JasperFillManager.fillReport( jasperReport, params, connection ); + } + finally + { + connection.close(); + } + } + + if ( print != null ) + { + JRExportUtils.export( type, out, print ); + } + } + catch ( Exception ex ) + { + throw new RuntimeException( "Failed to render report", ex ); + } + } + public int saveReport( Report report ) { return reportStore.save( report ); === modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/resources/META-INF/dhis/beans.xml' --- dhis-2/dhis-services/dhis-service-reporting/src/main/resources/META-INF/dhis/beans.xml 2011-08-25 08:52:40 +0000 +++ dhis-2/dhis-services/dhis-service-reporting/src/main/resources/META-INF/dhis/beans.xml 2011-09-16 16:54:03 +0000 @@ -56,6 +56,10 @@ + + + + === modified file 'dhis-2/dhis-support/dhis-support-system/pom.xml' --- dhis-2/dhis-support/dhis-support-system/pom.xml 2011-08-28 17:52:59 +0000 +++ dhis-2/dhis-support/dhis-support-system/pom.xml 2011-09-16 16:54:03 +0000 @@ -30,6 +30,21 @@ dhis-support-test + + + + net.sf.jasperreports + jasperreports + + + jfree + jfreechart + + + org.apache.poi + poi + + @@ -101,10 +116,6 @@ jxl - net.sf.jasperreports - jasperreports - - velocity velocity === renamed file 'dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/JRExportUtils.java' => 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/JRExportUtils.java' --- dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/JRExportUtils.java 2011-07-12 12:15:23 +0000 +++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/JRExportUtils.java 2011-09-16 16:54:03 +0000 @@ -1,4 +1,4 @@ -package org.hisp.dhis.util; +package org.hisp.dhis.system.util; /* * Copyright (c) 2004-2010, University of Oslo === added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/ResponseUtils.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/ResponseUtils.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/ResponseUtils.java 2011-09-18 14:36:11 +0000 @@ -0,0 +1,29 @@ +package org.hisp.dhis.web.api; + +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.ResponseBuilder; + +import org.hisp.dhis.system.util.DateUtils; + +public class ResponseUtils +{ + public static ResponseBuilder response( boolean disallowCache, + String filename, boolean attachment ) + { + ResponseBuilder builder = Response.ok(); + + if ( disallowCache ) + { + builder.header( "Cache-Control", "no-cache" ); + builder.header( "Expires", DateUtils.getExpiredHttpDateString() ); + } + + if ( filename != null ) + { + String type = attachment ? "attachment" : "inline"; + builder.header( "Content-Disposition", type + "; filename=\"" + filename + "\"" ); + } + + return builder; + } +} === modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/ChartResource.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/ChartResource.java 2011-09-16 15:59:35 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/ChartResource.java 2011-09-18 14:36:11 +0000 @@ -8,11 +8,15 @@ import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; import javax.ws.rs.core.StreamingOutput; +import org.hisp.dhis.chart.Chart; import org.hisp.dhis.chart.ChartService; import org.hisp.dhis.i18n.I18nManager; +import org.hisp.dhis.system.util.CodecUtils; import org.hisp.dhis.util.ContextUtils; +import org.hisp.dhis.web.api.ResponseUtils; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; @@ -32,22 +36,26 @@ { i18nManager = manager; } - + @GET @Produces( ContextUtils.CONTENT_TYPE_PNG ) - public StreamingOutput getChart( @PathParam("id") Integer id ) + public Response getChart( @PathParam("id") Integer id ) throws Exception { - final JFreeChart chart = chartService.getJFreeChart( id, i18nManager.getI18nFormat() ); - - return new StreamingOutput() + final JFreeChart jFreeChart = chartService.getJFreeChart( id, i18nManager.getI18nFormat() ); + + final Chart chart = chartService.getChart( id ); + + final String filename = CodecUtils.filenameEncode( chart.getTitle() + ".png" ); + + return ResponseUtils.response( true, filename, false ).entity( new StreamingOutput() { @Override public void write( OutputStream out ) throws IOException, WebApplicationException { - ChartUtilities.writeChartAsPNG( out, chart, 600, 400 ); + ChartUtilities.writeChartAsPNG( out, jFreeChart, 600, 400 ); } - }; + } ).build(); } } === added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/ReportResource.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/ReportResource.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/ReportResource.java 2011-09-18 14:36:11 +0000 @@ -0,0 +1,79 @@ +package org.hisp.dhis.web.api.resources; + +import java.io.IOException; +import java.io.OutputStream; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.StreamingOutput; + +import org.hisp.dhis.i18n.I18nFormat; +import org.hisp.dhis.i18n.I18nManager; +import org.hisp.dhis.organisationunit.OrganisationUnit; +import org.hisp.dhis.organisationunit.OrganisationUnitService; +import org.hisp.dhis.report.Report; +import org.hisp.dhis.report.ReportService; +import org.hisp.dhis.system.util.CodecUtils; +import org.hisp.dhis.util.ContextUtils; +import org.hisp.dhis.web.api.ResponseUtils; + +@Path( "/report/{report}/{orgUnit}" ) +public class ReportResource +{ + private ReportService reportService; + + public void setReportService( ReportService reportService ) + { + this.reportService = reportService; + } + + private OrganisationUnitService organisationUnitService; + + public void setOrganisationUnitService( OrganisationUnitService organisationUnitService ) + { + this.organisationUnitService = organisationUnitService; + } + + private I18nManager i18nManager; + + public void setI18nManager( I18nManager manager ) + { + i18nManager = manager; + } + + @GET + @Produces(ContextUtils.CONTENT_TYPE_PDF) + public Response renderReport( @PathParam("report") final Integer reportId, + @PathParam("orgUnit") final String orgUnitUuid ) + throws Exception + { + final Report report = reportService.getReport( reportId ); + + final OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( orgUnitUuid ); + + if ( report == null || organisationUnit == null ) + { + return null; + } + + final int organisationUnitId = organisationUnit.getId(); + + final I18nFormat format = i18nManager.getI18nFormat(); + + final String filename = CodecUtils.filenameEncode( report.getName() ) + "." + ReportService.REPORTTYPE_PDF; + + return ResponseUtils.response( true, filename, false ).entity( new StreamingOutput() + { + @Override + public void write( OutputStream out ) + throws IOException, WebApplicationException + { + reportService.renderReport( out, report, 1, organisationUnitId, ReportService.REPORTTYPE_PDF, format ); + } + } ).build(); + } +} === modified file 'dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/beans.xml' --- dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/beans.xml 2011-09-16 16:05:52 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/beans.xml 2011-09-17 04:39:45 +0000 @@ -42,6 +42,13 @@ + + + + + + === 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-09-14 12:46:13 +0000 +++ dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/reportviewer/action/RenderReportAction.java 2011-09-18 14:36:11 +0000 @@ -30,30 +30,14 @@ import static org.apache.commons.lang.StringUtils.defaultIfEmpty; import java.io.OutputStream; -import java.sql.Connection; -import java.util.HashMap; -import java.util.Map; import javax.servlet.http.HttpServletResponse; -import net.sf.jasperreports.engine.JasperCompileManager; -import net.sf.jasperreports.engine.JasperFillManager; -import net.sf.jasperreports.engine.JasperPrint; -import net.sf.jasperreports.engine.JasperReport; - -import org.amplecode.quick.StatementManager; -import org.hisp.dhis.common.Grid; -import org.hisp.dhis.constant.ConstantService; import org.hisp.dhis.i18n.I18nFormat; -import org.hisp.dhis.organisationunit.OrganisationUnitGroupService; 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.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; /** @@ -63,7 +47,7 @@ public class RenderReportAction extends StreamActionSupport { - private static final String DEFAULT_TYPE = "pdf"; + private static final String DEFAULT_TYPE = ReportService.REPORTTYPE_PDF; // ------------------------------------------------------------------------- // Dependencies @@ -76,34 +60,6 @@ this.reportService = reportService; } - private ReportTableService reportTableService; - - public void setReportTableService( ReportTableService reportTableService ) - { - this.reportTableService = reportTableService; - } - - private ConstantService constantService; - - public void setConstantService( ConstantService constantService ) - { - this.constantService = constantService; - } - - private StatementManager statementManager; - - public void setStatementManager( StatementManager statementManager ) - { - this.statementManager = statementManager; - } - - private OrganisationUnitGroupService organisationUnitGroupService; - - public void setOrganisationUnitGroupService( OrganisationUnitGroupService organisationUnitGroupService ) - { - this.organisationUnitGroupService = organisationUnitGroupService; - } - private I18nFormat format; public void setFormat( I18nFormat format ) @@ -155,42 +111,7 @@ Report report = reportService.getReport( id ); - Map params = new HashMap(); - - params.putAll( constantService.getConstantParameterMap() ); - - JasperReport jasperReport = JasperCompileManager.compileReport( StreamUtils.getInputStream( report.getDesignContent() ) ); - - JasperPrint print = null; - - if ( report.hasReportTable() ) // Use JR data source - { - ReportTable reportTable = report.getReportTable(); - - params.putAll( reportTable.getOrganisationUnitGroupMap( organisationUnitGroupService.getCompulsoryOrganisationUnitGroupSets() ) ); - - Grid grid = reportTableService.getReportTableGrid( reportTable.getId(), format, reportingPeriod, organisationUnitId ); - - print = JasperFillManager.fillReport( jasperReport, params, grid ); - } - else // Assume SQL report and provide JDBC connection - { - Connection connection = statementManager.getHolder().getConnection(); - - try - { - print = JasperFillManager.fillReport( jasperReport, params, connection ); - } - finally - { - connection.close(); - } - } - - if ( print != null ) - { - JRExportUtils.export( type, out, print ); - } + reportService.renderReport( out, report, reportingPeriod, organisationUnitId, type, format ); return SUCCESS; } === modified file 'dhis-2/dhis-web/dhis-web-reporting/src/main/resources/META-INF/dhis/beans.xml' --- dhis-2/dhis-web/dhis-web-reporting/src/main/resources/META-INF/dhis/beans.xml 2011-09-11 18:19:48 +0000 +++ dhis-2/dhis-web/dhis-web-reporting/src/main/resources/META-INF/dhis/beans.xml 2011-09-16 16:54:03 +0000 @@ -288,10 +288,6 @@ - - - -