diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/ReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/ReadPlatformServiceImpl.java index fbc84fe63..9e808899f 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/ReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/ReadPlatformServiceImpl.java @@ -1287,20 +1287,22 @@ public class ReadPlatformServiceImpl implements ReadPlatformService { } @Override - public ExtraDatasets retrieveExtraDatasetNames(String datasetType) { - - if (datasetType == null) { - logger.info("Extra Data Dataset Type Not Found"); - return null; - } + public ExtraDatasets retrieveExtraDatasetNames(String type) { List names = new ArrayList(); Connection db_connection; try { db_connection = dataSource.getConnection(); Statement db_statement = db_connection.createStatement(); - String sql = "select d.`name` as datasetName from stretchydata_dataset d join stretchydata_datasettype t on t.id = d.datasettype_id where t.`name` = '" - + datasetType + "' order by d.`name`"; + + String whereClause; + if (type == null) { + whereClause = ""; + } else { + whereClause = "where t.`name` = '" + type + "'"; + } + String sql = "select d.`name` as datasetName from stretchydata_dataset d join stretchydata_datasettype t on t.id = d.datasettype_id " + + whereClause + " order by d.`name`"; //logger.info("extra tables sql: " + sql); ResultSet rs = db_statement.executeQuery(sql); diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/ExtraDataApiResource.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/ExtraDataApiResource.java new file mode 100644 index 000000000..eb8fab5ec --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/ExtraDataApiResource.java @@ -0,0 +1,126 @@ +package org.mifosng.platform.api; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +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.Status; +import javax.ws.rs.core.UriInfo; + +import org.mifosng.data.EntityIdentifier; +import org.mifosng.data.ErrorResponse; +import org.mifosng.data.ErrorResponseList; +import org.mifosng.data.ExtraDatasets; +import org.mifosng.data.reports.GenericResultset; +import org.mifosng.platform.InvalidSqlException; +import org.mifosng.platform.ReadPlatformService; +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; +@Path("/v1/extradata") +@Component +@Scope("singleton") +public class ExtraDataApiResource { + + private final static Logger logger = LoggerFactory + .getLogger(ExtraDataApiResource.class); + + @Autowired + private ReadPlatformService readPlatformService; + +// @GET +// @Path("{dataSetPrefix}") +// @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) +// @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) +// public Response extraDataNames(@PathParam("dataSetPrefix") final String dataSetPrefix) { +// +// List result = this.readPlatformService.retrieveExtraDataTableNames(dataSetPrefix); +// +// return Response.ok().entity(result).build(); +// } + + @GET + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + @Produces({ MediaType.APPLICATION_JSON}) + public Response datasets(@Context UriInfo uriInfo) { + + MultivaluedMap queryParams = uriInfo.getQueryParameters(); + String type = queryParams.getFirst("type"); + + ExtraDatasets result = this.readPlatformService.retrieveExtraDatasetNames(type); + return Response.ok().entity(result).build(); + } + /* + @GET + @Path("{datasetType}/{datasetName}/{datasetPKValue}") + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + public Response extraData(@PathParam("datasetType") final String datasetType,@PathParam("datasetName") final String datasetName, @PathParam("datasetPKValue") final String datasetPKValue, @Context UriInfo uriInfo) { + + try { + GenericResultset result = this.readPlatformService.retrieveExtraData(datasetType, datasetName, datasetPKValue); + + return Response.ok().entity(result).build(); + } catch (InvalidSqlException e) { + List allErrors = new ArrayList(); + + ErrorResponse err = new ErrorResponse("extradata.invalid.sql", "sql", e.getSql()); + allErrors.add(err); + + throw new WebApplicationException(Response + .status(Status.BAD_REQUEST) + .entity(new ErrorResponseList(allErrors)).build()); + } + } + + @POST + @Path("{datasetType}/{datasetName}/{datasetPKValue}") + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + public Response saveExtraData(@PathParam("datasetType") final String datasetType,@PathParam("datasetName") final String datasetName, @PathParam("datasetPKValue") final String datasetPKValue, @Context UriInfo uriInfo) { + + try { + + MultivaluedMap incomingParams = uriInfo.getQueryParameters(); + Map queryParams = new HashMap(); + + Set keys = incomingParams.keySet(); + for (String key : keys) { + String pValue = incomingParams.get(key).get(0); + queryParams.put(key, pValue); + logger.info(key + " - " + pValue); + } + + + this.readPlatformService.tempSaveExtraData(datasetType, datasetName, datasetPKValue, queryParams); + + EntityIdentifier entityIdentifier = new EntityIdentifier(Long.valueOf(datasetPKValue)); + + return Response.ok().entity(entityIdentifier).build(); + } catch (InvalidSqlException e) { + List allErrors = new ArrayList(); + + ErrorResponse err = new ErrorResponse("extradata.invalid.sql", "sql", e.getSql()); + allErrors.add(err); + + throw new WebApplicationException(Response + .status(Status.BAD_REQUEST) + .entity(new ErrorResponseList(allErrors)).build()); + } + }*/ +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/FlexibleReportingApiResource.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/FlexibleReportingApiResource.java new file mode 100644 index 000000000..dc6fc2c60 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/FlexibleReportingApiResource.java @@ -0,0 +1,155 @@ +package org.mifosng.platform.api; + +import java.io.IOException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.OPTIONS; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +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.Response.Status; +import javax.ws.rs.core.UriInfo; + +import org.mifosng.data.ErrorResponse; +import org.mifosng.data.ErrorResponseList; +import org.mifosng.data.reports.GenericResultset; +import org.mifosng.platform.ReadPlatformService; +import org.mifosng.platform.exceptions.ApplicationDomainRuleException; +import org.mifosng.platform.exceptions.ClientNotAuthenticatedException; +import org.mifosng.platform.exceptions.NewDataValidationException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Scope; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.stereotype.Component; + +@Path("/v1/flexiblereporting") +@Component +@Scope("singleton") +public class FlexibleReportingApiResource { + + private final static Logger logger = LoggerFactory.getLogger(FlexibleReportingApiResource.class); + + @Autowired + private ReadPlatformService readPlatformService; + + private String _corsHeaders; + + private Response makeCORS(ResponseBuilder req, String returnMethod) { + ResponseBuilder rb = req + .header("Access-Control-Allow-Origin", "*") + .header("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); + + if (!"".equals(returnMethod)) { + rb.header("Access-Control-Allow-Headers", returnMethod); + } + + return rb.build(); + } + + private Response makeCORS(ResponseBuilder req) { + return makeCORS(req, _corsHeaders); + } + + // This OPTIONS request/response is necessary + // if you consumes other format than text/plain or + // if you use other HTTP verbs than GET and POST + @OPTIONS + @Path("/flexireport") + public Response corsMyResource(@HeaderParam("Access-Control-Request-Headers") String requestH) { + _corsHeaders = requestH; + return makeCORS(Response.ok(), requestH); + } + + @GET + @Path("flexireport") + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public Response retrieveReport(@Context UriInfo uriInfo, @Context HttpServletResponse httpServletResponse) { + + MultivaluedMap queryParams = uriInfo.getQueryParameters(); + String rptDB = queryParams.getFirst("MRP_rptDB"); + String name = queryParams.getFirst("MRP_Name"); + String type = queryParams.getFirst("MRP_Type"); + + Map extractedQueryParams = new HashMap(); + + Set keys = queryParams.keySet(); + String pKey; + String pValue; + for (String k : keys) { + + if (k.startsWith("MRP_")) + { + pKey = "${" + k.substring(4) + "}"; + pValue = queryParams.get(k).get(0); + + extractedQueryParams.put(pKey, pValue); + } + } + logger.info("BEGINNING REQUEST FOR: " + name); + GenericResultset result = this.readPlatformService.retrieveGenericResultset(rptDB, name, type, extractedQueryParams); + +// JSONWithPadding paddedResult = new JSONWithPadding(result, +// callbackName); + + return makeCORS(Response.ok().entity(result)); + } + + @GET + @Path("/exportcsv/{reportDb}/{reportName}/{reportType}/office/{officeId}/currency/{currencyId}") + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public Response exportCsvReport( + @PathParam("reportDb") String rptDB, + @PathParam("reportName") String name, + @PathParam("reportType") String type, + @PathParam("officeId") Long officeId, @PathParam("currencyId") Long currencyId) throws IOException { + + try { + Map extractedQueryParams = new HashMap(); + + if (officeId != null && officeId > 0) { + extractedQueryParams.put("${officeId}", officeId.toString()); + } + + if (currencyId != null) { + extractedQueryParams.put("${currencyId}", currencyId.toString()); + } + + GenericResultset result = this.readPlatformService.retrieveGenericResultset(rptDB, name, type, extractedQueryParams); + + return Response.ok().entity(result).build(); + } catch (ClientNotAuthenticatedException e) { + throw new WebApplicationException(Response.status(Status.UNAUTHORIZED).build()); + } catch (AccessDeniedException e) { + ErrorResponse errorResponse = new ErrorResponse("error.msg.no.permission", "id"); + ErrorResponseList list = new ErrorResponseList(Arrays.asList(errorResponse)); + throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(list).build()); + } catch (ApplicationDomainRuleException e) { + throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(new ErrorResponseList(e.getErrors())).build()); + } catch (NewDataValidationException e) { + throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(new ErrorResponseList(e.getValidationErrors())).build()); + } + } + + @GET + @Path("forceauth") + public Response hackToForceAuthentication() { + return Response.ok().build(); + } +} \ No newline at end of file