From 1b97a0ac87e02fbd4bd28c04862f1d6bcedd9489 Mon Sep 17 00:00:00 2001 From: John Woodlock Date: Fri, 15 Jun 2012 21:25:44 +1000 Subject: [PATCH] WIP tidy up pentaho use --- .../ReadExtraDataAndReportingService.java | 4 + .../ReadExtraDataAndReportingServiceImpl.java | 90 ++++++++++++ .../platform/api/ReportingApiResource.java | 129 ++++-------------- 3 files changed, 117 insertions(+), 106 deletions(-) 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 cedab6e89..2b86bde02 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/ReadExtraDataAndReportingService.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/ReadExtraDataAndReportingService.java @@ -3,6 +3,7 @@ package org.mifosng.platform; import java.util.List; import java.util.Map; +import javax.ws.rs.core.Response; import javax.ws.rs.core.StreamingOutput; import org.mifosng.platform.api.data.AdditionalFieldsSet; @@ -24,5 +25,8 @@ public interface ReadExtraDataAndReportingService { void updateExtraData(String type, String set, Long id, Map queryParams); + public Response processPentahoRequest(String reportName, + String outputType, 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 cdce8418b..46cc3bd1f 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/ReadExtraDataAndReportingServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/ReadExtraDataAndReportingServiceImpl.java @@ -1,6 +1,8 @@ package org.mifosng.platform; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.sql.Connection; @@ -14,6 +16,7 @@ import java.util.Map; import java.util.Set; import javax.sql.DataSource; +import javax.ws.rs.core.Response; import javax.ws.rs.core.StreamingOutput; import org.mifosng.platform.api.data.AdditionalFieldsSet; @@ -24,6 +27,15 @@ 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.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; @@ -41,7 +53,10 @@ public class ReadExtraDataAndReportingServiceImpl implements @Autowired public ReadExtraDataAndReportingServiceImpl(final DataSource dataSource) { + //kick off pentaho reports server ClassicEngineBoot.getInstance().start(); + + try { this.dataSource = dataSource; Connection db_connection = dataSource.getConnection(); @@ -699,4 +714,79 @@ public class ReadExtraDataAndReportingServiceImpl implements e.getMessage(), "Error closing database connection"); } } + + + @Override + public Response processPentahoRequest(String reportName, + String outputType, Map queryParams) { + 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; + + logger.info("outputType: " + outputType); + try { + res = manager.createDirectly(reportPath, MasterReport.class); + MasterReport masterReport = (MasterReport) res.getResource(); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + if ("PDF".equalsIgnoreCase(outputType)) { + PdfReportUtil.createPDF(masterReport, baos); + return Response.ok().entity(baos.toByteArray()) + .type("application/pdf").build(); + } + + if ("XLS".equalsIgnoreCase(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)) { + 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)) { + HtmlReportUtil.createStreamHTML(masterReport, baos); + return Response.ok().entity(baos.toByteArray()).build(); + } + } catch (ResourceException e) { + throw new PlatformDataIntegrityException( + "error.msg.reporting.error", e.getMessage()); + } catch (ReportProcessingException e) { + throw new PlatformDataIntegrityException( + "error.msg.reporting.error", e.getMessage()); + } catch (IOException e) { + throw new PlatformDataIntegrityException( + "error.msg.reporting.error", e.getMessage()); + } + + throw new PlatformDataIntegrityException( + "error.msg.invalid.outputType", "No matching Output Type: " + + outputType); + + } + } \ 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 34af028ac..a7563828d 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,7 +1,5 @@ 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; @@ -21,18 +19,6 @@ 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.mifosng.platform.exceptions.PlatformDataIntegrityException; -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; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @@ -42,9 +28,6 @@ import org.springframework.stereotype.Component; @Scope("singleton") public class ReportingApiResource { - private final static Logger logger = LoggerFactory - .getLogger(ReportingApiResource.class); - private String allowedFieldList = ""; private String filterName = "myFilter"; @@ -96,6 +79,7 @@ public class ReportingApiResource { MultivaluedMap queryParams = uriInfo .getQueryParameters(); + String parameterType = queryParams.getFirst("parameterType"); if ((parameterType == null) || (!(parameterType.equalsIgnoreCase("true")))) { @@ -104,34 +88,21 @@ public class ReportingApiResource { parameterType = "parameter"; } - Map extractedQueryParams = new HashMap(); - - Set keys = queryParams.keySet(); - String pKey; - String pValue; - for (String k : keys) { - - if (k.startsWith("R_")) { - pKey = "${" + k.substring(2) + "}"; - pValue = queryParams.get(k).get(0); - extractedQueryParams.put(pKey, pValue); - } - } - logger.info("BEGINNING REQUEST FOR: " + reportName); - + Map reportParams = getReportParams(queryParams); if (this.readExtraDataAndReportingService.getReportType(reportName) .equalsIgnoreCase("Pentaho")) { - return processPentahoRequest(reportName, - queryParams.getFirst("output-type"), extractedQueryParams); + return this.readExtraDataAndReportingService.processPentahoRequest( + reportName, queryParams.getFirst("output-type"), + reportParams); } String exportCSV = queryParams.getFirst("exportCSV"); - if ((exportCSV == null) || (!(exportCSV.equalsIgnoreCase("true")))) { + // JSON GenericResultset result = this.readExtraDataAndReportingService .retrieveGenericResultset(reportName, parameterType, - extractedQueryParams); + reportParams); String selectedFields = ""; String json = this.jsonFormattingService.convertRequest(result, @@ -141,10 +112,9 @@ public class ReportingApiResource { .build(); } + // CSV Export StreamingOutput result = this.readExtraDataAndReportingService - .retrieveReportCSV(reportName, parameterType, - extractedQueryParams); - + .retrieveReportCSV(reportName, parameterType, reportParams); return Response .ok() .entity(result) @@ -154,75 +124,22 @@ public class ReportingApiResource { + ".csv").build(); } - private Response processPentahoRequest(String reportName, - String outputType, Map queryParams) { - 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); + private Map getReportParams( + MultivaluedMap queryParams) { + + Map reportParams = new HashMap(); + Set keys = queryParams.keySet(); + String pKey; + String pValue; + for (String k : keys) { - // load report definition - ResourceManager manager = new ResourceManager(); - manager.registerDefaults(); - Resource res; - - logger.info("outputType: " + outputType); - try { - res = manager.createDirectly(reportPath, MasterReport.class); - MasterReport masterReport = (MasterReport) res.getResource(); - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - if ("PDF".equalsIgnoreCase(outputType)) { - PdfReportUtil.createPDF(masterReport, baos); - return Response.ok().entity(baos.toByteArray()) - .type("application/pdf").build(); + if (k.startsWith("R_")) { + pKey = "${" + k.substring(2) + "}"; + pValue = queryParams.get(k).get(0); + reportParams.put(pKey, pValue); } - - if ("XLS".equalsIgnoreCase(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)) { - 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)) { - HtmlReportUtil.createStreamHTML(masterReport, baos); - return Response.ok().entity(baos.toByteArray()).build(); - } - } catch (ResourceException e) { - throw new PlatformDataIntegrityException( - "error.msg.reporting.error", e.getMessage()); - } catch (ReportProcessingException e) { - throw new PlatformDataIntegrityException( - "error.msg.reporting.error", e.getMessage()); - } catch (IOException e) { - throw new PlatformDataIntegrityException( - "error.msg.reporting.error", e.getMessage()); } - - throw new PlatformDataIntegrityException( - "error.msg.invalid.outputType", "No matching Output Type: " - + outputType); - + return reportParams; } + } \ No newline at end of file