Generate Pdf option for reports
This commit is contained in:
+9
@@ -63,6 +63,15 @@ public class ApiParameterHelper {
|
|||||||
return exportCsv;
|
return exportCsv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static boolean exportPdf(final MultivaluedMap<String, String> queryParams) {
|
||||||
|
boolean exportPDF = false;
|
||||||
|
if (queryParams.getFirst("exportPDF") != null) {
|
||||||
|
String exportPdfValue = queryParams.getFirst("exportPDF");
|
||||||
|
exportPDF = "true".equalsIgnoreCase(exportPdfValue);
|
||||||
|
}
|
||||||
|
return exportPDF;
|
||||||
|
}
|
||||||
public static boolean parameterType(final MultivaluedMap<String, String> queryParams) {
|
public static boolean parameterType(final MultivaluedMap<String, String> queryParams) {
|
||||||
boolean parameterType = false;
|
boolean parameterType = false;
|
||||||
if (queryParams.getFirst("parameterType") != null) {
|
if (queryParams.getFirst("parameterType") != null) {
|
||||||
|
|||||||
+41
@@ -1,5 +1,6 @@
|
|||||||
package org.mifosplatform.infrastructure.dataqueries.api;
|
package org.mifosplatform.infrastructure.dataqueries.api;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@@ -13,6 +14,7 @@ import javax.ws.rs.core.Context;
|
|||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.MultivaluedMap;
|
import javax.ws.rs.core.MultivaluedMap;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
|
import javax.ws.rs.core.Response.ResponseBuilder;
|
||||||
import javax.ws.rs.core.StreamingOutput;
|
import javax.ws.rs.core.StreamingOutput;
|
||||||
import javax.ws.rs.core.UriInfo;
|
import javax.ws.rs.core.UriInfo;
|
||||||
|
|
||||||
@@ -51,6 +53,22 @@ public class ReportsApiResource {
|
|||||||
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
|
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
|
||||||
boolean exportCsv = ApiParameterHelper.exportCsv(uriInfo.getQueryParameters());
|
boolean exportCsv = ApiParameterHelper.exportCsv(uriInfo.getQueryParameters());
|
||||||
|
|
||||||
|
boolean exportPdf = ApiParameterHelper.exportPdf(uriInfo.getQueryParameters());
|
||||||
|
|
||||||
|
if(exportPdf)
|
||||||
|
|
||||||
|
{
|
||||||
|
String result = this.readExtraDataAndReportingService
|
||||||
|
.retrieveReportPDF(".", ".", extractedQueryParams);
|
||||||
|
|
||||||
|
return Response
|
||||||
|
.ok()
|
||||||
|
.entity(result)
|
||||||
|
.header("Content-Disposition",
|
||||||
|
"attachment;filename=ReportList.pdf").build();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
if (!exportCsv) {
|
if (!exportCsv) {
|
||||||
GenericResultsetData result = this.readExtraDataAndReportingService.retrieveGenericResultset(".", ".", extractedQueryParams);
|
GenericResultsetData result = this.readExtraDataAndReportingService.retrieveGenericResultset(".", ".", extractedQueryParams);
|
||||||
|
|
||||||
@@ -75,6 +93,7 @@ public class ReportsApiResource {
|
|||||||
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
|
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
|
||||||
boolean exportCsv = ApiParameterHelper.exportCsv(uriInfo.getQueryParameters());
|
boolean exportCsv = ApiParameterHelper.exportCsv(uriInfo.getQueryParameters());
|
||||||
boolean parameterType = ApiParameterHelper.parameterType(uriInfo.getQueryParameters());
|
boolean parameterType = ApiParameterHelper.parameterType(uriInfo.getQueryParameters());
|
||||||
|
boolean exportPdf = ApiParameterHelper.exportPdf(uriInfo.getQueryParameters());
|
||||||
|
|
||||||
checkUserPermissionForReport(reportName, parameterType);
|
checkUserPermissionForReport(reportName, parameterType);
|
||||||
|
|
||||||
@@ -90,6 +109,28 @@ public class ReportsApiResource {
|
|||||||
parameterTypeValue = "parameter";
|
parameterTypeValue = "parameter";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// PDF format
|
||||||
|
|
||||||
|
if(exportPdf)
|
||||||
|
{
|
||||||
|
|
||||||
|
Map<String, String> reportParams = getReportParams(queryParams, false);
|
||||||
|
String pdfFileName = this.readExtraDataAndReportingService
|
||||||
|
.retrieveReportPDF(reportName, parameterTypeValue, reportParams);
|
||||||
|
|
||||||
|
File file=new File(pdfFileName);
|
||||||
|
|
||||||
|
ResponseBuilder response=Response.ok(file);
|
||||||
|
response.header("Content-Disposition",
|
||||||
|
"attachment; filename=\""
|
||||||
|
+pdfFileName+"\"");
|
||||||
|
response.header("content-Type","application/pdf");
|
||||||
|
|
||||||
|
return response.build();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
if (!exportCsv) {
|
if (!exportCsv) {
|
||||||
|
|
||||||
Map<String, String> reportParams = getReportParams(queryParams, false);
|
Map<String, String> reportParams = getReportParams(queryParams, false);
|
||||||
|
|||||||
+3
@@ -15,5 +15,8 @@ public interface ReadReportingService {
|
|||||||
|
|
||||||
Response processPentahoRequest(String reportName, String outputType, Map<String, String> queryParams);
|
Response processPentahoRequest(String reportName, String outputType, Map<String, String> queryParams);
|
||||||
|
|
||||||
|
String retrieveReportPDF(String name, String type,Map<String, String> extractedQueryParams);
|
||||||
|
|
||||||
|
|
||||||
String getReportType(String reportName);
|
String getReportType(String reportName);
|
||||||
}
|
}
|
||||||
+92
-1
@@ -2,6 +2,8 @@ package org.mifosplatform.infrastructure.dataqueries.service;
|
|||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
@@ -18,6 +20,7 @@ import javax.ws.rs.core.StreamingOutput;
|
|||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
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.mifosng.platform.infrastructure.FileUtils;
|
||||||
import org.mifosplatform.infrastructure.dataqueries.data.GenericResultsetData;
|
import org.mifosplatform.infrastructure.dataqueries.data.GenericResultsetData;
|
||||||
import org.mifosplatform.infrastructure.dataqueries.data.ResultsetColumnHeader;
|
import org.mifosplatform.infrastructure.dataqueries.data.ResultsetColumnHeader;
|
||||||
import org.mifosplatform.infrastructure.dataqueries.data.ResultsetDataRow;
|
import org.mifosplatform.infrastructure.dataqueries.data.ResultsetDataRow;
|
||||||
@@ -41,6 +44,11 @@ import org.slf4j.LoggerFactory;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.lowagie.text.Document;
|
||||||
|
import com.lowagie.text.PageSize;
|
||||||
|
import com.lowagie.text.pdf.PdfPTable;
|
||||||
|
import com.lowagie.text.pdf.PdfWriter;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class ReadReportingServiceImpl implements ReadReportingService {
|
public class ReadReportingServiceImpl implements ReadReportingService {
|
||||||
|
|
||||||
@@ -135,6 +143,8 @@ public class ReadReportingServiceImpl implements ReadReportingService {
|
|||||||
return writer;
|
return writer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public GenericResultsetData retrieveGenericResultset(final String name, final String type, final Map<String, String> queryParams) {
|
public GenericResultsetData retrieveGenericResultset(final String name, final String type, final Map<String, String> queryParams) {
|
||||||
|
|
||||||
@@ -332,4 +342,85 @@ public class ReadReportingServiceImpl implements ReadReportingService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
@Override
|
||||||
|
public String retrieveReportPDF(final String reportName,final String type, final Map<String, String> queryParams) {
|
||||||
|
|
||||||
|
String fileLocation=FileUtils.MIFOSX_BASE_DIR+File.separator+"";
|
||||||
|
if(!new File(fileLocation).isDirectory())
|
||||||
|
{
|
||||||
|
new File(fileLocation).mkdirs();
|
||||||
|
}
|
||||||
|
|
||||||
|
String genaratePdf=fileLocation+File.separator+reportName+".pdf";
|
||||||
|
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
GenericResultsetData result = retrieveGenericResultset(
|
||||||
|
reportName, type, queryParams);
|
||||||
|
|
||||||
|
|
||||||
|
List<ResultsetColumnHeader> columnHeaders = result.getColumnHeaders();
|
||||||
|
List<ResultsetDataRow> data = result.getData();
|
||||||
|
List<String> row;
|
||||||
|
|
||||||
|
logger.info("NO. of Columns: " + columnHeaders.size());
|
||||||
|
Integer chSize = columnHeaders.size();
|
||||||
|
|
||||||
|
Document document = new Document(PageSize.B0.rotate());
|
||||||
|
|
||||||
|
PdfWriter.getInstance(document,
|
||||||
|
new FileOutputStream(new File(fileLocation+reportName+".pdf")));
|
||||||
|
document.open();
|
||||||
|
|
||||||
|
PdfPTable table = new PdfPTable(chSize);
|
||||||
|
table.setWidthPercentage(100);
|
||||||
|
|
||||||
|
for (int i = 0; i < chSize; i++) {
|
||||||
|
|
||||||
|
table.addCell(columnHeaders.get(i).getColumnName());
|
||||||
|
|
||||||
|
}
|
||||||
|
table.completeRow();
|
||||||
|
|
||||||
|
Integer rSize;
|
||||||
|
String currColType;
|
||||||
|
String currVal;
|
||||||
|
logger.info("NO. of Rows: " + data.size());
|
||||||
|
for (int i = 0; i < data.size(); i++) {
|
||||||
|
row = data.get(i).getRow();
|
||||||
|
rSize = row.size();
|
||||||
|
for (int j = 0; j < rSize; j++) {
|
||||||
|
currColType = columnHeaders.get(j).getColumnType();
|
||||||
|
currVal = row.get(j);
|
||||||
|
if (currVal != null) {
|
||||||
|
if (currColType.equals("DECIMAL")
|
||||||
|
|| currColType.equals("DOUBLE")
|
||||||
|
|| currColType.equals("BIGINT")
|
||||||
|
|| currColType.equals("SMALLINT")
|
||||||
|
|| currColType.equals("INT"))
|
||||||
|
{
|
||||||
|
|
||||||
|
table.addCell(currVal.toString());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
table.addCell(currVal.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
table.completeRow();
|
||||||
|
document.add(table);
|
||||||
|
document.close();
|
||||||
|
return genaratePdf;
|
||||||
|
}
|
||||||
|
|
||||||
|
catch (Exception e) {
|
||||||
|
throw new PlatformDataIntegrityException(
|
||||||
|
"error.msg.exception.error", e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user