From 6fd79e3f7dcea91123f58deadf84a05b254a8ab4 Mon Sep 17 00:00:00 2001 From: John Woodlock Date: Fri, 15 Jun 2012 11:35:13 +1000 Subject: [PATCH] WIP tidy up pentaho use --- mifosng-provider/build.gradle | 21 ++++ .../ReadExtraDataAndReportingService.java | 2 + .../ReadExtraDataAndReportingServiceImpl.java | 46 ++++++++ .../platform/api/ReportingApiResource.java | 110 ++++++++++++++++-- .../ApiJSONFormattingServiceImpl.java | 2 +- 5 files changed, 172 insertions(+), 9 deletions(-) diff --git a/mifosng-provider/build.gradle b/mifosng-provider/build.gradle index 9f485fb7e..65df0243f 100644 --- a/mifosng-provider/build.gradle +++ b/mifosng-provider/build.gradle @@ -112,6 +112,27 @@ dependencies { [group: 'ch.qos.logback', name: 'logback-classic', version: '1.0.0'], [group: 'mysql', name: 'mysql-connector-java', version: '5.1.18'], + + [name: 'bsf', version: '2.4.0'], + [name: 'bsh', version: '1.3.0'], + [name: 'itext', version: '2.1.7'], + [name: 'libbase', version: '1.2.2'], + [name: 'libdocbundle', version: '1.2.3'], + [name: 'libfonts', version: '1.2.3'], + [name: 'libformat', version: '1.2.4'], + [name: 'libformula', version: '1.2.3'], + [name: 'libloader', version: '1.2.3'], + [name: 'librepository', version: '1.2.3'], + [name: 'libserializer', version: '1.2.2'], + [name: 'libsparkline', version: '1.2.1'], + [name: 'libxml', version: '1.2.2'], + [name: 'pentaho-reporting-engine-classic-core', version: '3.8.3-GA'], + [name: 'pentaho-reporting-engine-classic-extensions', version: '3.8.3-GA'], + [name: 'pentaho-reporting-engine-wizard-core', version: '3.8.3-GA'], + [name: 'pentaho-reporting-engine-wizard-xul', version: '3.8.3-GA'], + [name: 'poi', version: '3.6'], + [name: 'poi-ooxml', version: '3.6'], + [name: 'poi-ooxml-schemas', version: '3.6'], ) testCompile 'junit:junit:4.10', diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/ReadExtraDataAndReportingService.java b/mifosng-provider/src/main/java/org/mifosng/platform/ReadExtraDataAndReportingService.java index 4f1cff375..cedab6e89 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/ReadExtraDataAndReportingService.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/ReadExtraDataAndReportingService.java @@ -23,4 +23,6 @@ public interface ReadExtraDataAndReportingService { void updateExtraData(String type, String set, Long id, Map queryParams); + + String getReportType(String reportName); } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/ReadExtraDataAndReportingServiceImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/ReadExtraDataAndReportingServiceImpl.java index b6dadf11c..cdce8418b 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/ReadExtraDataAndReportingServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/ReadExtraDataAndReportingServiceImpl.java @@ -23,6 +23,7 @@ import org.mifosng.platform.api.data.ResultsetDataRow; import org.mifosng.platform.exceptions.AdditionalFieldsNotFoundException; import org.mifosng.platform.exceptions.PlatformDataIntegrityException; import org.mifosng.platform.exceptions.ReportNotFoundException; +import org.pentaho.reporting.engine.classic.core.ClassicEngineBoot; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -40,6 +41,7 @@ public class ReadExtraDataAndReportingServiceImpl implements @Autowired public ReadExtraDataAndReportingServiceImpl(final DataSource dataSource) { + ClassicEngineBoot.getInstance().start(); try { this.dataSource = dataSource; Connection db_connection = dataSource.getConnection(); @@ -653,4 +655,48 @@ public class ReadExtraDataAndReportingServiceImpl implements } return saveSql; } + + @Override + public String getReportType(String reportName) { + String sql = "SELECT ifnull(report_type,'') as report_type FROM `stretchy_report` where report_name = '" + + reportName + "'"; + String reportType = ""; + logger.info("get reportType: " + sql); + + try { + Connection db_connection = dataSource.getConnection(); + Statement db_statement = db_connection.createStatement(); + ResultSet rs = db_statement.executeQuery(sql); + + if (rs.next()) { + reportType = rs.getString("report_type"); + } else { + dbClose(db_statement, db_connection); + throw new ReportNotFoundException(sql); + } + dbClose(db_statement, db_connection); + } catch (SQLException e) { + throw new PlatformDataIntegrityException("error.msg.sql.error", + e.getMessage(), "Report Name: " + reportName + " Sql: " + + sql); + } + + return reportType; + } + + private void dbClose(Statement db_statement, Connection db_connection) { + try { + if (db_statement != null) { + db_statement.close(); + db_statement = null; + } + if (db_connection != null) { + db_connection.close(); + db_connection = null; + } + } catch (SQLException e) { + throw new PlatformDataIntegrityException("error.msg.sql.error", + e.getMessage(), "Error closing database connection"); + } + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/ReportingApiResource.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/ReportingApiResource.java index eead3aa9d..e6e64a433 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/ReportingApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/ReportingApiResource.java @@ -1,5 +1,7 @@ package org.mifosng.platform.api; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Set; @@ -19,6 +21,15 @@ import javax.ws.rs.core.UriInfo; import org.mifosng.platform.ReadExtraDataAndReportingService; import org.mifosng.platform.api.data.GenericResultset; import org.mifosng.platform.api.infrastructure.ApiJSONFormattingService; +import org.pentaho.reporting.engine.classic.core.MasterReport; +import org.pentaho.reporting.engine.classic.core.ReportProcessingException; +import org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.PdfReportUtil; +import org.pentaho.reporting.engine.classic.core.modules.output.table.csv.CSVReportUtil; +import org.pentaho.reporting.engine.classic.core.modules.output.table.html.HtmlReportUtil; +import org.pentaho.reporting.engine.classic.core.modules.output.table.xls.ExcelReportUtil; +import org.pentaho.reporting.libraries.resourceloader.Resource; +import org.pentaho.reporting.libraries.resourceloader.ResourceException; +import org.pentaho.reporting.libraries.resourceloader.ResourceManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -37,7 +48,7 @@ public class ReportingApiResource { private String filterName = "myFilter"; @Autowired - private ReadExtraDataAndReportingService ReadExtraDataAndReportingService; + private ReadExtraDataAndReportingService readExtraDataAndReportingService; @Autowired private ApiJSONFormattingService jsonFormattingService; @@ -54,7 +65,7 @@ public class ReportingApiResource { String exportCSV = queryParams.getFirst("exportCSV"); if ((exportCSV == null) || (!(exportCSV.equalsIgnoreCase("true")))) { - GenericResultset result = this.ReadExtraDataAndReportingService + GenericResultset result = this.readExtraDataAndReportingService .retrieveGenericResultset(".", ".", extractedQueryParams); String selectedFields = ""; String json = this.jsonFormattingService.convertRequest(result, @@ -63,7 +74,7 @@ public class ReportingApiResource { return Response.ok().entity(json).build(); } - StreamingOutput result = this.ReadExtraDataAndReportingService + StreamingOutput result = this.readExtraDataAndReportingService .retrieveReportCSV(".", ".", extractedQueryParams); return Response @@ -76,7 +87,8 @@ public class ReportingApiResource { @GET @Path("{reportName}") @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) - @Produces({ MediaType.APPLICATION_JSON, "application/x-msdownload" }) + @Produces({ MediaType.APPLICATION_JSON, "application/x-msdownload", + "application/vnd.ms-excel", "application/pdf", "text/html" }) public Response retrieveReport( @PathParam("reportName") final String reportName, @Context UriInfo uriInfo) { @@ -90,7 +102,6 @@ public class ReportingApiResource { } else { parameterType = "parameter"; } - String exportCSV = queryParams.getFirst("exportCSV"); Map extractedQueryParams = new HashMap(); @@ -107,8 +118,89 @@ public class ReportingApiResource { } logger.info("BEGINNING REQUEST FOR: " + reportName); + if (this.readExtraDataAndReportingService.getReportType(reportName) + .equalsIgnoreCase("Pentaho")) { + + String reportPath = "C:\\dev\\apache-tomcat-7.0.25\\webapps\\ROOT\\PentahoReports\\" + + reportName + ".prpt"; + // String reportPath = + // "/var/lib/tomcat6/webapps/ROOT/PentahoReports/" + // + reportName + ".prpt"; + logger.info("Report path: " + reportPath); + + // load report definition + ResourceManager manager = new ResourceManager(); + manager.registerDefaults(); + Resource res; + + String outputType = queryParams.getFirst("output-type"); + logger.info("outputType: " + outputType); + try { + res = manager.createDirectly(reportPath, MasterReport.class); + MasterReport masterReport = (MasterReport) res.getResource(); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + if ("PDF".equalsIgnoreCase(outputType)) { + logger.info("PDF: " + outputType); + PdfReportUtil.createPDF(masterReport, baos); + return Response.ok().entity(baos.toByteArray()) + .type("application/pdf").build(); + /* + * .header("Content-Disposition", "attachment;filename=" + + * reportName.replaceAll(" ", "") + ".pdf").build(); + */ + } + + if ("XLS".equalsIgnoreCase(outputType)) { + logger.info("XLS: " + outputType); + ExcelReportUtil.createXLS(masterReport, baos); + return Response + .ok() + .entity(baos.toByteArray()) + .type("application/vnd.ms-excel") + .header("Content-Disposition", + "attachment;filename=" + + reportName.replaceAll(" ", "") + + ".xls").build(); + } + + if ("CSV".equalsIgnoreCase(outputType)) { + logger.info("CSV: " + outputType); + + CSVReportUtil.createCSV(masterReport, baos, "UTF-8"); + return Response + .ok() + .entity(baos.toByteArray()) + .type("application/x-msdownload") + .header("Content-Disposition", + "attachment;filename=" + + reportName.replaceAll(" ", "") + + ".csv").build(); + } + + if ("HTML".equalsIgnoreCase(outputType)) { + logger.info("HTML: " + outputType); + + HtmlReportUtil.createStreamHTML(masterReport, baos); + return Response.ok().entity(baos.toByteArray()).build(); + } + } catch (ResourceException e) { + e.printStackTrace(); + } catch (ReportProcessingException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + logger.info("No matching Output Type: " + outputType); + return Response.ok().build(); + } + + String exportCSV = queryParams.getFirst("exportCSV"); + if ((exportCSV == null) || (!(exportCSV.equalsIgnoreCase("true")))) { - GenericResultset result = this.ReadExtraDataAndReportingService + GenericResultset result = this.readExtraDataAndReportingService .retrieveGenericResultset(reportName, parameterType, extractedQueryParams); @@ -116,16 +208,18 @@ public class ReportingApiResource { String json = this.jsonFormattingService.convertRequest(result, filterName, allowedFieldList, selectedFields, uriInfo.getQueryParameters()); - return Response.ok().entity(json).build(); + return Response.ok().entity(json).type(MediaType.APPLICATION_JSON) + .build(); } - StreamingOutput result = this.ReadExtraDataAndReportingService + StreamingOutput result = this.readExtraDataAndReportingService .retrieveReportCSV(reportName, parameterType, extractedQueryParams); return Response .ok() .entity(result) + .type("application/x-msdownload") .header("Content-Disposition", "attachment;filename=" + reportName.replaceAll(" ", "") + ".csv").build(); diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiJSONFormattingServiceImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiJSONFormattingServiceImpl.java index 35e27c7b1..6e3829e21 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiJSONFormattingServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiJSONFormattingServiceImpl.java @@ -86,7 +86,7 @@ public class ApiJSONFormattingServiceImpl implements ApiJSONFormattingService { // logger.info("fieldList to be processed is: " + fieldList // + " filter type is: " + filterType); - + logger.info("query params: " + queryParams.toString()); return convertDataObjectJSON(dataObject, filterName, filterType, fieldList, isTrue(queryParams.getFirst("pretty"))); }