wip mifosx-195 runreports api now used for running reports instead of reports - updated in api-docs along with report maintenance apis

This commit is contained in:
John Woodlock
2013-04-16 12:22:25 +01:00
parent 97d7104c93
commit 436f77c3a8
4 changed files with 411 additions and 218 deletions
@@ -5,12 +5,9 @@
*/
package org.mifosplatform.infrastructure.dataqueries.api;
import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.ws.rs.Consumes;
@@ -23,27 +20,18 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.StreamingOutput;
import javax.ws.rs.core.UriInfo;
import org.mifosplatform.commands.domain.CommandWrapper;
import org.mifosplatform.commands.service.CommandWrapperBuilder;
import org.mifosplatform.commands.service.PortfolioCommandSourceWritePlatformService;
import org.mifosplatform.infrastructure.core.api.ApiParameterHelper;
import org.mifosplatform.infrastructure.core.api.ApiRequestParameterHelper;
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
import org.mifosplatform.infrastructure.core.serialization.ApiRequestJsonSerializationSettings;
import org.mifosplatform.infrastructure.core.serialization.ToApiJsonSerializer;
import org.mifosplatform.infrastructure.dataqueries.data.GenericResultsetData;
import org.mifosplatform.infrastructure.dataqueries.data.ReportData;
import org.mifosplatform.infrastructure.dataqueries.service.GenericDataService;
import org.mifosplatform.infrastructure.dataqueries.service.ReadReportingService;
import org.mifosplatform.infrastructure.security.exception.NoAuthorizationException;
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
import org.mifosplatform.useradministration.domain.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@@ -58,11 +46,10 @@ public class ReportsApiResource {
"reportCategory", "description", "reportSql", "coreReport",
"useReport", "reportParameters"));
private final String resourceNameForPermissions = "FUND";
private final String resourceNameForPermissions = "REPORT";
private final PlatformSecurityContext context;
private final ToApiJsonSerializer<ReportData> toApiJsonSerializer;
private final ReadReportingService readExtraDataAndReportingService;
private final GenericDataService genericDataService;
private final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService;
private final ApiRequestParameterHelper apiRequestParameterHelper;
@@ -70,13 +57,11 @@ public class ReportsApiResource {
public ReportsApiResource(
final PlatformSecurityContext context,
final ReadReportingService readExtraDataAndReportingService,
final GenericDataService genericDataService,
final ToApiJsonSerializer<ReportData> toApiJsonSerializer,
PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService,
final ApiRequestParameterHelper apiRequestParameterHelper) {
this.context = context;
this.readExtraDataAndReportingService = readExtraDataAndReportingService;
this.genericDataService = genericDataService;
this.toApiJsonSerializer = toApiJsonSerializer;
this.commandsSourceWritePlatformService = commandsSourceWritePlatformService;
this.apiRequestParameterHelper = apiRequestParameterHelper;
@@ -100,7 +85,7 @@ public class ReportsApiResource {
}
@GET
@Path("wip/{id}")
@Path("{id}")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public String retrieveReport(@PathParam("id") final Long id,
@@ -163,143 +148,4 @@ public class ReportsApiResource {
return this.toApiJsonSerializer.serialize(result);
}
@GET
@Path("{reportName}")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON, "application/x-msdownload",
"application/vnd.ms-excel", "application/pdf", "text/html" })
public Response runReport(
@PathParam("reportName") final String reportName,
@Context final UriInfo uriInfo) {
MultivaluedMap<String, String> queryParams = uriInfo
.getQueryParameters();
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo
.getQueryParameters());
boolean exportCsv = ApiParameterHelper.exportCsv(uriInfo
.getQueryParameters());
boolean parameterType = ApiParameterHelper.parameterType(uriInfo
.getQueryParameters());
boolean exportPdf = ApiParameterHelper.exportPdf(uriInfo
.getQueryParameters());
checkUserPermissionForReport(reportName, parameterType);
String parameterTypeValue = null;
if (!parameterType) {
parameterTypeValue = "report";
if (this.readExtraDataAndReportingService.getReportType(reportName)
.equalsIgnoreCase("Pentaho")) {
Map<String, String> reportParams = getReportParams(queryParams,
true);
return this.readExtraDataAndReportingService
.processPentahoRequest(reportName,
queryParams.getFirst("output-type"),
reportParams);
}
} else {
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) {
Map<String, String> reportParams = getReportParams(queryParams,
false);
GenericResultsetData result = this.readExtraDataAndReportingService
.retrieveGenericResultset(reportName, parameterTypeValue,
reportParams);
String json = "";
final boolean genericResultSetIsPassed = ApiParameterHelper
.genericResultSetPassed(uriInfo.getQueryParameters());
final boolean genericResultSet = ApiParameterHelper
.genericResultSet(uriInfo.getQueryParameters());
if (genericResultSetIsPassed) {
if (genericResultSet) {
json = this.toApiJsonSerializer.serializePretty(
prettyPrint, result);
} else {
json = this.genericDataService
.generateJsonFromGenericResultsetData(result);
}
} else {
json = this.toApiJsonSerializer.serializePretty(prettyPrint,
result);
}
return Response.ok().entity(json).type(MediaType.APPLICATION_JSON)
.build();
}
// CSV Export
Map<String, String> reportParams = getReportParams(queryParams, false);
StreamingOutput result = this.readExtraDataAndReportingService
.retrieveReportCSV(reportName, parameterTypeValue, reportParams);
return Response
.ok()
.entity(result)
.type("application/x-msdownload")
.header("Content-Disposition",
"attachment;filename=" + reportName.replaceAll(" ", "")
+ ".csv").build();
}
private void checkUserPermissionForReport(final String reportName,
final boolean parameterType) {
// Anyone can run a 'report' that is simply getting possible parameter
// (dropdown listbox) values.
if (!parameterType) {
AppUser currentUser = context.authenticatedUser();
if (currentUser.hasNotPermissionForReport(reportName)) {
throw new NoAuthorizationException(
"Not authorised to run report: " + reportName);
}
}
}
private Map<String, String> getReportParams(
final MultivaluedMap<String, String> queryParams,
final Boolean isPentaho) {
Map<String, String> reportParams = new HashMap<String, String>();
Set<String> keys = queryParams.keySet();
String pKey;
String pValue;
for (String k : keys) {
if (k.startsWith("R_")) {
if (isPentaho)
pKey = k.substring(2);
else
pKey = "${" + k.substring(2) + "}";
pValue = queryParams.get(k).get(0);
reportParams.put(pKey, pValue);
}
}
return reportParams;
}
}
@@ -0,0 +1,198 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.mifosplatform.infrastructure.dataqueries.api;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.StreamingOutput;
import javax.ws.rs.core.UriInfo;
import org.mifosplatform.infrastructure.core.api.ApiParameterHelper;
import org.mifosplatform.infrastructure.core.serialization.ToApiJsonSerializer;
import org.mifosplatform.infrastructure.dataqueries.data.GenericResultsetData;
import org.mifosplatform.infrastructure.dataqueries.data.ReportData;
import org.mifosplatform.infrastructure.dataqueries.service.GenericDataService;
import org.mifosplatform.infrastructure.dataqueries.service.ReadReportingService;
import org.mifosplatform.infrastructure.security.exception.NoAuthorizationException;
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
import org.mifosplatform.useradministration.domain.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Path("/runreports")
@Component
@Scope("singleton")
public class RunreportsApiResource {
private final PlatformSecurityContext context;
private final ToApiJsonSerializer<ReportData> toApiJsonSerializer;
private final ReadReportingService readExtraDataAndReportingService;
private final GenericDataService genericDataService;
@Autowired
public RunreportsApiResource(final PlatformSecurityContext context,
final ReadReportingService readExtraDataAndReportingService,
final GenericDataService genericDataService,
final ToApiJsonSerializer<ReportData> toApiJsonSerializer) {
this.context = context;
this.readExtraDataAndReportingService = readExtraDataAndReportingService;
this.genericDataService = genericDataService;
this.toApiJsonSerializer = toApiJsonSerializer;
}
@GET
@Path("{reportName}")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON, "application/x-msdownload",
"application/vnd.ms-excel", "application/pdf", "text/html" })
public Response runReport(@PathParam("reportName") final String reportName,
@Context final UriInfo uriInfo) {
MultivaluedMap<String, String> queryParams = uriInfo
.getQueryParameters();
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo
.getQueryParameters());
boolean exportCsv = ApiParameterHelper.exportCsv(uriInfo
.getQueryParameters());
boolean parameterType = ApiParameterHelper.parameterType(uriInfo
.getQueryParameters());
boolean exportPdf = ApiParameterHelper.exportPdf(uriInfo
.getQueryParameters());
checkUserPermissionForReport(reportName, parameterType);
String parameterTypeValue = null;
if (!parameterType) {
parameterTypeValue = "report";
if (this.readExtraDataAndReportingService.getReportType(reportName)
.equalsIgnoreCase("Pentaho")) {
Map<String, String> reportParams = getReportParams(queryParams,
true);
return this.readExtraDataAndReportingService
.processPentahoRequest(reportName,
queryParams.getFirst("output-type"),
reportParams);
}
} else {
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) {
Map<String, String> reportParams = getReportParams(queryParams,
false);
GenericResultsetData result = this.readExtraDataAndReportingService
.retrieveGenericResultset(reportName, parameterTypeValue,
reportParams);
String json = "";
final boolean genericResultSetIsPassed = ApiParameterHelper
.genericResultSetPassed(uriInfo.getQueryParameters());
final boolean genericResultSet = ApiParameterHelper
.genericResultSet(uriInfo.getQueryParameters());
if (genericResultSetIsPassed) {
if (genericResultSet) {
json = this.toApiJsonSerializer.serializePretty(
prettyPrint, result);
} else {
json = this.genericDataService
.generateJsonFromGenericResultsetData(result);
}
} else {
json = this.toApiJsonSerializer.serializePretty(prettyPrint,
result);
}
return Response.ok().entity(json).type(MediaType.APPLICATION_JSON)
.build();
}
// CSV Export
Map<String, String> reportParams = getReportParams(queryParams, false);
StreamingOutput result = this.readExtraDataAndReportingService
.retrieveReportCSV(reportName, parameterTypeValue, reportParams);
return Response
.ok()
.entity(result)
.type("application/x-msdownload")
.header("Content-Disposition",
"attachment;filename=" + reportName.replaceAll(" ", "")
+ ".csv").build();
}
private void checkUserPermissionForReport(final String reportName,
final boolean parameterType) {
// Anyone can run a 'report' that is simply getting possible parameter
// (dropdown listbox) values.
if (!parameterType) {
AppUser currentUser = context.authenticatedUser();
if (currentUser.hasNotPermissionForReport(reportName)) {
throw new NoAuthorizationException(
"Not authorised to run report: " + reportName);
}
}
}
private Map<String, String> getReportParams(
final MultivaluedMap<String, String> queryParams,
final Boolean isPentaho) {
Map<String, String> reportParams = new HashMap<String, String>();
Set<String> keys = queryParams.keySet();
String pKey;
String pValue;
for (String k : keys) {
if (k.startsWith("R_")) {
if (isPentaho)
pKey = k.substring(2);
else
pKey = "${" + k.substring(2) + "}";
pValue = queryParams.get(k).get(0);
reportParams.put(pKey, pValue);
}
}
return reportParams;
}
}
@@ -185,30 +185,7 @@ public class ReadReportingServiceImpl implements ReadReportingService {
long startTime = System.currentTimeMillis();
logger.info("STARTING REPORT: " + name + " Type: " + type);
String sql;
if (name.equals(".")) {
// this is to support api /reports - which isn't an important
// call. It isn't used in the default reporting UI. But there is a
// need to provide an api that does bring back 'permitted' reports
// PERMITTED REPORTS SQL
sql = "select r.report_id, r.report_name, r.report_type, r.report_subtype, r.report_category,"
+ " rp.parameter_id, rp.report_parameter_name, p.parameter_name"
+ " from stretchy_report r"
+ " left join stretchy_report_parameter rp on rp.report_id = r.report_id"
+ " left join stretchy_parameter p on p.parameter_id = rp.parameter_id"
+ " where exists"
+ " (select 'f'"
+ " from m_appuser_role ur "
+ " join m_role r on r.id = ur.role_id"
+ " left join m_role_permission rp on rp.role_id = r.id"
+ " left join m_permission p on p.id = rp.permission_id"
+ " where ur.appuser_id = "
+ context.authenticatedUser().getId()
+ " and (p.code in ('ALL_FUNCTIONS', 'ALL_FUNCTIONS_READ') or p.code = concat('READ_', r.report_name))) "
+ " order by r.report_name, rp.parameter_id";
} else {
sql = getSQLtoRun(name, type, queryParams);
}
String sql = getSQLtoRun(name, type, queryParams);
GenericResultsetData result = genericDataService
.fillGenericResultSet(sql);