move functionality around new loan application to loan resource.

This commit is contained in:
Keith Woodlock
2012-05-16 18:21:35 +01:00
parent 75054feeee
commit bf7783fc94
14 changed files with 218 additions and 236 deletions
@@ -3,15 +3,12 @@ package org.mifosng.platform;
import java.util.Collection;
import org.mifosng.data.LoanRepaymentData;
import org.mifosng.data.NewLoanWorkflowStepOneData;
import org.mifosng.data.OrganisationReadModel;
public interface ReadPlatformService {
Collection<OrganisationReadModel> retrieveAll();
NewLoanWorkflowStepOneData retrieveClientAndProductDetails(Long clientId, Long productId);
LoanRepaymentData retrieveNewLoanRepaymentDetails(Long loanId);
LoanRepaymentData retrieveNewLoanWaiverDetails(Long loanId);
@@ -196,58 +196,4 @@ public class ReadPlatformServiceImpl implements ReadPlatformService {
return loanRepaymentData;
}
@Override
public NewLoanWorkflowStepOneData retrieveClientAndProductDetails(
final Long clientId, final Long productId) {
AppUser currentUser = extractAuthenticatedUser();
NewLoanWorkflowStepOneData workflowData = new NewLoanWorkflowStepOneData();
workflowData.setOrganisationId(currentUser.getOrganisation().getId());
workflowData.setOrganisationName(currentUser.getOrganisation()
.getName());
Collection<LoanProductData> loanProducts = this.loanProductReadPlatformService
.retrieveAllLoanProducts();
workflowData.setAllowedProducts(new ArrayList<LoanProductData>(
loanProducts));
if (loanProducts.size() == 1) {
LoanProductData selectedProduct = this.loanProductReadPlatformService
.retrieveLoanProduct(workflowData.getAllowedProducts()
.get(0).getId());
workflowData.setProductId(selectedProduct.getId());
workflowData.setProductName(selectedProduct.getName());
workflowData.setSelectedProduct(selectedProduct);
} else {
LoanProductData selectedProduct = findLoanProductById(loanProducts,
productId);
workflowData.setProductId(selectedProduct.getId());
workflowData.setProductName(selectedProduct.getName());
workflowData.setSelectedProduct(selectedProduct);
}
ClientData clientAccount = this.clientReadPlatformService.retrieveIndividualClient(clientId);
workflowData.setClientId(clientAccount.getId());
workflowData.setClientName(clientAccount.getDisplayName());
return workflowData;
}
private LoanProductData findLoanProductById(
Collection<LoanProductData> loanProducts, Long productId) {
LoanProductData match = this.loanProductReadPlatformService
.retrieveNewLoanProductDetails();
for (LoanProductData loanProductData : loanProducts) {
if (loanProductData.getId().equals(productId)) {
match = this.loanProductReadPlatformService
.retrieveLoanProduct(loanProductData.getId());
break;
}
}
return match;
}
}
@@ -5,10 +5,12 @@ import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.mifosng.data.LoanAccountData;
import org.mifosng.data.NewLoanWorkflowStepOneData;
import org.mifosng.platform.ReadPlatformService;
import org.mifosng.platform.loan.service.LoanReadPlatformService;
import org.mifosng.platform.user.domain.AppUser;
@@ -43,6 +45,17 @@ public class LoansApiResource {
context.setAuthentication(auth);
}
@GET
@Path("template")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Produces({ MediaType.APPLICATION_JSON})
public Response retrieveDetailsForNewLoanApplicationStepOne(@QueryParam("clientId") final Long clientId, @QueryParam("productId") final Long productId) {
NewLoanWorkflowStepOneData workflowData = this.loanReadPlatformService.retrieveClientAndProductDetails(clientId, productId);
return Response.ok().entity(workflowData).build();
}
@GET
@Path("{loanId}")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@@ -1,8 +1,11 @@
package org.mifosng.platform.loan.service;
import org.mifosng.data.LoanAccountData;
import org.mifosng.data.NewLoanWorkflowStepOneData;
public interface LoanReadPlatformService {
LoanAccountData retrieveLoanAccountDetails(Long loanId);
NewLoanWorkflowStepOneData retrieveClientAndProductDetails(Long clientId, Long productId);
}
@@ -2,18 +2,26 @@ package org.mifosng.platform.loan.service;
import static org.mifosng.platform.Specifications.loansThatMatch;
import java.util.ArrayList;
import java.util.Collection;
import javax.sql.DataSource;
import org.joda.time.LocalDate;
import org.mifosng.data.ClientData;
import org.mifosng.data.CurrencyData;
import org.mifosng.data.DerivedLoanData;
import org.mifosng.data.LoanAccountData;
import org.mifosng.data.LoanProductData;
import org.mifosng.data.MoneyData;
import org.mifosng.data.NewLoanWorkflowStepOneData;
import org.mifosng.platform.client.service.ClientReadPlatformService;
import org.mifosng.platform.currency.domain.ApplicationCurrency;
import org.mifosng.platform.currency.domain.ApplicationCurrencyRepository;
import org.mifosng.platform.currency.domain.Money;
import org.mifosng.platform.loan.domain.Loan;
import org.mifosng.platform.loan.domain.LoanRepository;
import org.mifosng.platform.loanproduct.service.LoanProductReadPlatformService;
import org.mifosng.platform.security.PlatformSecurityContext;
import org.mifosng.platform.user.domain.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
@@ -27,13 +35,18 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService {
private final PlatformSecurityContext context;
private final LoanRepository loanRepository;
private final ApplicationCurrencyRepository applicationCurrencyRepository;
private final LoanProductReadPlatformService loanProductReadPlatformService;
private final ClientReadPlatformService clientReadPlatformService;
@Autowired
public LoanReadPlatformServiceImpl(final PlatformSecurityContext context, final DataSource dataSource,
final LoanRepository loanRepository, final ApplicationCurrencyRepository applicationCurrencyRepository) {
final LoanRepository loanRepository, final ApplicationCurrencyRepository applicationCurrencyRepository,
final LoanProductReadPlatformService loanProductReadPlatformService, final ClientReadPlatformService clientReadPlatformService) {
this.context = context;
this.loanRepository = loanRepository;
this.applicationCurrencyRepository = applicationCurrencyRepository;
this.loanProductReadPlatformService = loanProductReadPlatformService;
this.clientReadPlatformService = clientReadPlatformService;
this.jdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
@@ -145,4 +158,51 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService {
tolerance, loanData, waiveAllowed, interestRebateOwed);
}
@Override
public NewLoanWorkflowStepOneData retrieveClientAndProductDetails(final Long clientId, final Long productId) {
AppUser currentUser = context.authenticatedUser();
NewLoanWorkflowStepOneData workflowData = new NewLoanWorkflowStepOneData();
workflowData.setOrganisationId(currentUser.getOrganisation().getId());
workflowData.setOrganisationName(currentUser.getOrganisation().getName());
Collection<LoanProductData> loanProducts = this.loanProductReadPlatformService.retrieveAllLoanProducts();
workflowData.setAllowedProducts(new ArrayList<LoanProductData>(loanProducts));
if (loanProducts.size() == 1) {
Long allowedProductId = workflowData.getAllowedProducts().get(0).getId();
LoanProductData selectedProduct = this.loanProductReadPlatformService.retrieveLoanProduct(allowedProductId);
workflowData.setProductId(selectedProduct.getId());
workflowData.setProductName(selectedProduct.getName());
workflowData.setSelectedProduct(selectedProduct);
} else {
LoanProductData selectedProduct = findLoanProductById(loanProducts, productId);
workflowData.setProductId(selectedProduct.getId());
workflowData.setProductName(selectedProduct.getName());
workflowData.setSelectedProduct(selectedProduct);
}
ClientData clientAccount = this.clientReadPlatformService.retrieveIndividualClient(clientId);
workflowData.setClientId(clientAccount.getId());
workflowData.setClientName(clientAccount.getDisplayName());
return workflowData;
}
private LoanProductData findLoanProductById(
Collection<LoanProductData> loanProducts, Long productId) {
LoanProductData match = this.loanProductReadPlatformService
.retrieveNewLoanProductDetails();
for (LoanProductData loanProductData : loanProducts) {
if (loanProductData.getId().equals(productId)) {
match = this.loanProductReadPlatformService
.retrieveLoanProduct(loanProductData.getId());
break;
}
}
return match;
}
}
@@ -21,7 +21,6 @@ import org.mifosng.data.ErrorResponse;
import org.mifosng.data.ErrorResponseList;
import org.mifosng.data.LoanRepaymentData;
import org.mifosng.data.LoanSchedule;
import org.mifosng.data.NewLoanWorkflowStepOneData;
import org.mifosng.data.command.AdjustLoanTransactionCommand;
import org.mifosng.data.command.CalculateLoanScheduleCommand;
import org.mifosng.data.command.LoanStateTransitionCommand;
@@ -56,56 +55,9 @@ public class LoanResource {
@Autowired
private ReadPlatformService readPlatformService;
@Autowired
private WritePlatformService writePlatformService;
@GET
@Path("new/{clientId}/workflow/one")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response retrieveDetailsForNewLoanApplicationStepOne(@PathParam("clientId") final Long clientId) {
try {
NewLoanWorkflowStepOneData workflowData = this.readPlatformService.retrieveClientAndProductDetails(clientId, null);
return Response.ok().entity(workflowData).build();
} catch (UnAuthenticatedUserException 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("new/{clientId}/product/{productId}")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response retrieveDetailsForNewLoanApplicationStepOne(@PathParam("clientId") final Long clientId, @PathParam("productId") final Long productId) {
try {
NewLoanWorkflowStepOneData workflowData = this.readPlatformService.retrieveClientAndProductDetails(clientId, productId);
return Response.ok().entity(workflowData).build();
} catch (UnAuthenticatedUserException 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());
}
}
@POST
@Path("calculate")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })