LoanProduct filtering changes

This commit is contained in:
John Woodlock
2012-05-25 21:10:40 +10:00
parent a0680ee34b
commit 3bee9582e1
3 changed files with 68 additions and 61 deletions
@@ -7,9 +7,11 @@ import java.util.List;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import org.codehaus.jackson.map.annotate.JsonFilter;
import org.joda.time.DateTime; import org.joda.time.DateTime;
@XmlRootElement(name = "loanProduct") @XmlRootElement(name = "loanProduct")
@JsonFilter("myFilter")
public class LoanProductData implements Serializable { public class LoanProductData implements Serializable {
private Long id; private Long id;
@@ -1,28 +0,0 @@
package org.mifosng.data;
import java.util.ArrayList;
import java.util.Collection;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class LoanProductList {
private Collection<LoanProductData> products = new ArrayList<LoanProductData>();
protected LoanProductList() {
//
}
public LoanProductList(final Collection<LoanProductData> products) {
this.products = products;
}
public Collection<LoanProductData> getProducts() {
return this.products;
}
public void setProducts(final Collection<LoanProductData> products) {
this.products = products;
}
}
@@ -11,14 +11,16 @@ import javax.ws.rs.PUT;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.mifosng.data.EntityIdentifier; import org.mifosng.data.EntityIdentifier;
import org.mifosng.data.LoanProductData; import org.mifosng.data.LoanProductData;
import org.mifosng.data.LoanProductList;
import org.mifosng.data.command.LoanProductCommand; import org.mifosng.data.command.LoanProductCommand;
import org.mifosng.platform.api.infrastructure.ApiDataConversionService; import org.mifosng.platform.api.infrastructure.ApiDataConversionService;
import org.mifosng.platform.api.infrastructure.ApiJSONFormattingService;
import org.mifosng.platform.loanproduct.service.LoanProductReadPlatformService; import org.mifosng.platform.loanproduct.service.LoanProductReadPlatformService;
import org.mifosng.platform.loanproduct.service.LoanProductWritePlatformService; import org.mifosng.platform.loanproduct.service.LoanProductWritePlatformService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -30,7 +32,10 @@ import org.springframework.stereotype.Component;
@Scope("singleton") @Scope("singleton")
public class LoanProductApiResource { public class LoanProductApiResource {
@Autowired private String defaultFieldList = "principalMoney,inArrearsTolerance,amortizationMethod,interestMethod,repaymentPeriodFrequency,interestRatePeriod,interestRateCalculatedInPeriod,repaidEvery,numberOfRepayments,interestRatePerPeriod,annualInterestRate,numberOfInterestFreePeriods,flexible,interestRebateAllowed";
private String allowedFieldList = "possibleCurrencies,possibleAmortizationOptions,possibleInterestOptions,possibleInterestRateCalculatedInPeriodOptions,repaymentFrequencyOptions,interestFrequencyOptions";
@Autowired
private LoanProductReadPlatformService loanProductReadPlatformService; private LoanProductReadPlatformService loanProductReadPlatformService;
@Autowired @Autowired
@@ -39,68 +44,96 @@ public class LoanProductApiResource {
@Autowired @Autowired
private ApiDataConversionService apiDataConversionService; private ApiDataConversionService apiDataConversionService;
@Autowired
private ApiJSONFormattingService jsonFormattingService;
@POST @POST
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON}) @Produces({ MediaType.APPLICATION_JSON })
public Response createLoanProduct(LoanProductCommand command) { public Response createLoanProduct(LoanProductCommand command) {
EntityIdentifier entityIdentifier = this.loanProductWritePlatformService.createLoanProduct(command); EntityIdentifier entityIdentifier = this.loanProductWritePlatformService
.createLoanProduct(command);
return Response.ok().entity(entityIdentifier).build(); return Response.ok().entity(entityIdentifier).build();
} }
@GET @GET
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({MediaType.APPLICATION_JSON}) @Produces({ MediaType.APPLICATION_JSON })
public Response retrieveAllLoanProducts() { public String retrieveAllLoanProducts(@Context UriInfo uriInfo) {
Collection<LoanProductData> products = this.loanProductReadPlatformService.retrieveAllLoanProducts(); Collection<LoanProductData> products = this.loanProductReadPlatformService
.retrieveAllLoanProducts();
return Response.ok().entity(new LoanProductList(products)).build(); String selectedFields = "";
} return this.jsonFormattingService.convertRequest(products,
allowedFieldList, selectedFields, uriInfo.getQueryParameters());
}
/* /*
* see http://stackoverflow.com/questions/5250074/what-uri-can-be-used-to-request-a-default-resource * see
* http://stackoverflow.com/questions/5250074/what-uri-can-be-used-to-request
* -a-default-resource
*/ */
@GET @GET
@Path("template") @Path("template")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON}) @Produces({ MediaType.APPLICATION_JSON })
public Response retrieveNewLoanProductDetails() { public String retrieveNewLoanProductDetails(@Context UriInfo uriInfo) {
LoanProductData loanProduct = this.loanProductReadPlatformService.retrieveNewLoanProductDetails(); LoanProductData loanProduct = this.loanProductReadPlatformService
.retrieveNewLoanProductDetails();
return Response.ok().entity(loanProduct).build(); String selectedFields = defaultFieldList + "," + allowedFieldList;
return this.jsonFormattingService.convertRequest(loanProduct,
allowedFieldList, selectedFields, uriInfo.getQueryParameters());
} }
@GET @GET
@Path("{productId}") @Path("{productId}")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON}) @Produces({ MediaType.APPLICATION_JSON })
public Response retrieveLoanProductDetails(@PathParam("productId") final Long productId) { public String retrieveLoanProductDetails(
@PathParam("productId") final Long productId,
@Context UriInfo uriInfo) {
LoanProductData loanProduct = this.loanProductReadPlatformService.retrieveLoanProduct(productId); LoanProductData loanProduct = this.loanProductReadPlatformService
.retrieveLoanProduct(productId);
return Response.ok().entity(loanProduct).build(); String selectedFields = "";
return this.jsonFormattingService.convertRequest(loanProduct,
allowedFieldList, selectedFields, uriInfo.getQueryParameters());
} }
@PUT @PUT
@Path("{productId}") @Path("{productId}")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON })
public Response updateLoanProduct(@PathParam("productId") final Long productId, final LoanProductCommand command) { public Response updateLoanProduct(
@PathParam("productId") final Long productId,
final LoanProductCommand command) {
// FIXME - pass in locale through query string or in 'request body' // FIXME - pass in locale through query string or in 'request body'
Locale clientApplicationLocale = Locale.UK; Locale clientApplicationLocale = Locale.UK;
BigDecimal principal = this.apiDataConversionService.convertFrom(command.getPrincipalFormatted(), "principalFormatted", clientApplicationLocale); BigDecimal principal = this.apiDataConversionService.convertFrom(
BigDecimal interestRatePerPeriod = this.apiDataConversionService.convertFrom(command.getInterestRatePerPeriodFormatted(), "interestRatePerPeriodFormatted", clientApplicationLocale); command.getPrincipalFormatted(), "principalFormatted",
BigDecimal inArrearsToleranceAmount = this.apiDataConversionService.convertFrom(command.getInArrearsToleranceAmountFormatted(), "inArrearsToleranceAmountFormatted", clientApplicationLocale); clientApplicationLocale);
BigDecimal interestRatePerPeriod = this.apiDataConversionService
.convertFrom(command.getInterestRatePerPeriodFormatted(),
"interestRatePerPeriodFormatted",
clientApplicationLocale);
BigDecimal inArrearsToleranceAmount = this.apiDataConversionService
.convertFrom(command.getInArrearsToleranceAmountFormatted(),
"inArrearsToleranceAmountFormatted",
clientApplicationLocale);
command.setPrincipal(principal); command.setPrincipal(principal);
command.setInterestRatePerPeriod(interestRatePerPeriod); command.setInterestRatePerPeriod(interestRatePerPeriod);
command.setInArrearsToleranceAmount(inArrearsToleranceAmount); command.setInArrearsToleranceAmount(inArrearsToleranceAmount);
command.setId(productId); command.setId(productId);
EntityIdentifier entityIdentifier = this.loanProductWritePlatformService.updateLoanProduct(command); EntityIdentifier entityIdentifier = this.loanProductWritePlatformService
.updateLoanProduct(command);
return Response.ok().entity(entityIdentifier).build(); return Response.ok().entity(entityIdentifier).build();
} }