WIP tidy up pentaho use

This commit is contained in:
John Woodlock
2012-06-15 21:25:44 +10:00
parent 7dd7af2398
commit 1b97a0ac87
3 changed files with 117 additions and 106 deletions
@@ -3,6 +3,7 @@ package org.mifosng.platform;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput; import javax.ws.rs.core.StreamingOutput;
import org.mifosng.platform.api.data.AdditionalFieldsSet; import org.mifosng.platform.api.data.AdditionalFieldsSet;
@@ -24,5 +25,8 @@ public interface ReadExtraDataAndReportingService {
void updateExtraData(String type, String set, Long id, void updateExtraData(String type, String set, Long id,
Map<String, String> queryParams); Map<String, String> queryParams);
public Response processPentahoRequest(String reportName,
String outputType, Map<String, String> queryParams);
String getReportType(String reportName); String getReportType(String reportName);
} }
@@ -1,6 +1,8 @@
package org.mifosng.platform; package org.mifosng.platform;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.sql.Connection; import java.sql.Connection;
@@ -14,6 +16,7 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.sql.DataSource; import javax.sql.DataSource;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput; import javax.ws.rs.core.StreamingOutput;
import org.mifosng.platform.api.data.AdditionalFieldsSet; 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.PlatformDataIntegrityException;
import org.mifosng.platform.exceptions.ReportNotFoundException; import org.mifosng.platform.exceptions.ReportNotFoundException;
import org.pentaho.reporting.engine.classic.core.ClassicEngineBoot; 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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -41,7 +53,10 @@ public class ReadExtraDataAndReportingServiceImpl implements
@Autowired @Autowired
public ReadExtraDataAndReportingServiceImpl(final DataSource dataSource) { public ReadExtraDataAndReportingServiceImpl(final DataSource dataSource) {
//kick off pentaho reports server
ClassicEngineBoot.getInstance().start(); ClassicEngineBoot.getInstance().start();
try { try {
this.dataSource = dataSource; this.dataSource = dataSource;
Connection db_connection = dataSource.getConnection(); Connection db_connection = dataSource.getConnection();
@@ -699,4 +714,79 @@ public class ReadExtraDataAndReportingServiceImpl implements
e.getMessage(), "Error closing database connection"); e.getMessage(), "Error closing database connection");
} }
} }
@Override
public Response processPentahoRequest(String reportName,
String outputType, Map<String, String> 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);
}
} }
@@ -1,7 +1,5 @@
package org.mifosng.platform.api; package org.mifosng.platform.api;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@@ -21,18 +19,6 @@ import javax.ws.rs.core.UriInfo;
import org.mifosng.platform.ReadExtraDataAndReportingService; import org.mifosng.platform.ReadExtraDataAndReportingService;
import org.mifosng.platform.api.data.GenericResultset; import org.mifosng.platform.api.data.GenericResultset;
import org.mifosng.platform.api.infrastructure.ApiJSONFormattingService; 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.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -42,9 +28,6 @@ import org.springframework.stereotype.Component;
@Scope("singleton") @Scope("singleton")
public class ReportingApiResource { public class ReportingApiResource {
private final static Logger logger = LoggerFactory
.getLogger(ReportingApiResource.class);
private String allowedFieldList = ""; private String allowedFieldList = "";
private String filterName = "myFilter"; private String filterName = "myFilter";
@@ -96,6 +79,7 @@ public class ReportingApiResource {
MultivaluedMap<String, String> queryParams = uriInfo MultivaluedMap<String, String> queryParams = uriInfo
.getQueryParameters(); .getQueryParameters();
String parameterType = queryParams.getFirst("parameterType"); String parameterType = queryParams.getFirst("parameterType");
if ((parameterType == null) if ((parameterType == null)
|| (!(parameterType.equalsIgnoreCase("true")))) { || (!(parameterType.equalsIgnoreCase("true")))) {
@@ -104,34 +88,21 @@ public class ReportingApiResource {
parameterType = "parameter"; parameterType = "parameter";
} }
Map<String, String> extractedQueryParams = new HashMap<String, String>(); Map<String, String> reportParams = getReportParams(queryParams);
Set<String> 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);
if (this.readExtraDataAndReportingService.getReportType(reportName) if (this.readExtraDataAndReportingService.getReportType(reportName)
.equalsIgnoreCase("Pentaho")) { .equalsIgnoreCase("Pentaho")) {
return processPentahoRequest(reportName, return this.readExtraDataAndReportingService.processPentahoRequest(
queryParams.getFirst("output-type"), extractedQueryParams); reportName, queryParams.getFirst("output-type"),
reportParams);
} }
String exportCSV = queryParams.getFirst("exportCSV"); String exportCSV = queryParams.getFirst("exportCSV");
if ((exportCSV == null) || (!(exportCSV.equalsIgnoreCase("true")))) { if ((exportCSV == null) || (!(exportCSV.equalsIgnoreCase("true")))) {
// JSON
GenericResultset result = this.readExtraDataAndReportingService GenericResultset result = this.readExtraDataAndReportingService
.retrieveGenericResultset(reportName, parameterType, .retrieveGenericResultset(reportName, parameterType,
extractedQueryParams); reportParams);
String selectedFields = ""; String selectedFields = "";
String json = this.jsonFormattingService.convertRequest(result, String json = this.jsonFormattingService.convertRequest(result,
@@ -141,10 +112,9 @@ public class ReportingApiResource {
.build(); .build();
} }
// CSV Export
StreamingOutput result = this.readExtraDataAndReportingService StreamingOutput result = this.readExtraDataAndReportingService
.retrieveReportCSV(reportName, parameterType, .retrieveReportCSV(reportName, parameterType, reportParams);
extractedQueryParams);
return Response return Response
.ok() .ok()
.entity(result) .entity(result)
@@ -154,75 +124,22 @@ public class ReportingApiResource {
+ ".csv").build(); + ".csv").build();
} }
private Response processPentahoRequest(String reportName, private Map<String, String> getReportParams(
String outputType, Map<String, String> queryParams) { MultivaluedMap<String, String> queryParams) {
String reportPath = "C:\\dev\\apache-tomcat-7.0.25\\webapps\\ROOT\\PentahoReports\\"
+ reportName + ".prpt"; Map<String, String> reportParams = new HashMap<String, String>();
// String reportPath = Set<String> keys = queryParams.keySet();
// "/var/lib/tomcat6/webapps/ROOT/PentahoReports/" String pKey;
// + reportName + ".prpt"; String pValue;
logger.info("Report path: " + reportPath); for (String k : keys) {
// load report definition if (k.startsWith("R_")) {
ResourceManager manager = new ResourceManager(); pKey = "${" + k.substring(2) + "}";
manager.registerDefaults(); pValue = queryParams.get(k).get(0);
Resource res; reportParams.put(pKey, pValue);
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());
} }
return reportParams;
throw new PlatformDataIntegrityException(
"error.msg.invalid.outputType", "No matching Output Type: "
+ outputType);
} }
} }