From dc23dec063d1ab093e59641443b0d27afd3c8629 Mon Sep 17 00:00:00 2001 From: John Woodlock Date: Fri, 20 Jul 2012 18:47:27 +1000 Subject: [PATCH] loan permissions --- .../platform/api/LoansApiResource.java | 125 +++++++++++------- .../loan/service/LoanReadPlatformService.java | 3 +- .../service/LoanReadPlatformServiceImpl.java | 60 ++++----- 3 files changed, 102 insertions(+), 86 deletions(-) diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/LoansApiResource.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/LoansApiResource.java index 05a4a71af..7595be782 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/LoansApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/LoansApiResource.java @@ -72,14 +72,15 @@ public class LoansApiResource { @GET @Path("template") - @Consumes({MediaType.APPLICATION_JSON}) - @Produces({MediaType.APPLICATION_JSON}) + @Consumes({ MediaType.APPLICATION_JSON }) + @Produces({ MediaType.APPLICATION_JSON }) public String retrieveDetailsForNewLoanApplicationStepOne( @QueryParam("clientId") final Long clientId, @QueryParam("productId") final Long productId, @Context final UriInfo uriInfo) { - NewLoanData workflowData = this.loanReadPlatformService.retrieveClientAndProductDetails(clientId, productId); + NewLoanData workflowData = this.loanReadPlatformService + .retrieveClientAndProductDetails(clientId, productId); String selectedFields = ""; return this.jsonFormattingService.convertRequest(workflowData, @@ -89,21 +90,32 @@ public class LoansApiResource { @GET @Path("{loanId}") - @Consumes({MediaType.APPLICATION_JSON}) - @Produces({MediaType.APPLICATION_JSON}) + @Consumes({ MediaType.APPLICATION_JSON }) + @Produces({ MediaType.APPLICATION_JSON }) public String retrieveLoanAccountDetails( - @PathParam("loanId") final Long loanId, @Context final UriInfo uriInfo) { + @PathParam("loanId") final Long loanId, + @Context final UriInfo uriInfo) { LoanBasicDetailsData loanBasicDetails = this.loanReadPlatformService .retrieveLoanAccountDetails(loanId); - - Collection loanRepayments = this.loanReadPlatformService.retrieveLoanPayments(loanId); - Collection repaymentSchedule = this.loanReadPlatformService.retrieveRepaymentSchedule(loanId); - - LoanAccountSummaryData summary = this.loanReadPlatformService.retrieveSummary(loanBasicDetails.getPrincipal(), repaymentSchedule, loanRepayments); - LoanPermissionData permissions = this.loanReadPlatformService.retrieveLoanPermissions(loanBasicDetails, summary.isWaiveAllowed(loanBasicDetails.getInArrearsTolerance())); - - LoanAccountData loanAccount = new LoanAccountData(loanBasicDetails, summary, repaymentSchedule, loanRepayments, permissions); + + Collection loanRepayments = this.loanReadPlatformService + .retrieveLoanPayments(loanId); + Collection repaymentSchedule = this.loanReadPlatformService + .retrieveRepaymentSchedule(loanId); + + LoanAccountSummaryData summary = this.loanReadPlatformService + .retrieveSummary(loanBasicDetails.getPrincipal(), + repaymentSchedule, loanRepayments); + + LoanPermissionData permissions = this.loanReadPlatformService + .retrieveLoanPermissions(loanBasicDetails, summary + .isWaiveAllowed(loanBasicDetails + .getInArrearsTolerance()), loanRepayments + .size()); + + LoanAccountData loanAccount = new LoanAccountData(loanBasicDetails, + summary, repaymentSchedule, loanRepayments, permissions); String selectedFields = ""; String associatedFields = "summary,repaymentSchedule,loanRepayments,permissions,convenienceData"; @@ -113,16 +125,19 @@ public class LoansApiResource { } @POST - @Consumes({MediaType.APPLICATION_JSON}) - @Produces({MediaType.APPLICATION_JSON}) + @Consumes({ MediaType.APPLICATION_JSON }) + @Produces({ MediaType.APPLICATION_JSON }) public Response calculateLoanScheduleOrSubmitLoanApplication( @QueryParam("command") final String commandParam, final String jsonRequestBody) { - SubmitLoanApplicationCommand command = this.apiDataConversionService.convertJsonToSubmitLoanApplicationCommand(jsonRequestBody); - - CalculateLoanScheduleCommand calculateLoanScheduleCommand = command.toCalculateLoanScheduleCommand(); - LoanSchedule loanSchedule = this.calculationPlatformService.calculateLoanSchedule(calculateLoanScheduleCommand); + SubmitLoanApplicationCommand command = this.apiDataConversionService + .convertJsonToSubmitLoanApplicationCommand(jsonRequestBody); + + CalculateLoanScheduleCommand calculateLoanScheduleCommand = command + .toCalculateLoanScheduleCommand(); + LoanSchedule loanSchedule = this.calculationPlatformService + .calculateLoanSchedule(calculateLoanScheduleCommand); // for now just auto generating the loan schedule and setting support // for 'manual' loan schedule creation later. @@ -132,31 +147,36 @@ public class LoansApiResource { return Response.ok().entity(loanSchedule).build(); } - EntityIdentifier identifier = this.loanWritePlatformService.submitLoanApplication(command); + EntityIdentifier identifier = this.loanWritePlatformService + .submitLoanApplication(command); return Response.ok().entity(identifier).build(); } @DELETE @Path("{loanId}") - @Consumes({MediaType.APPLICATION_JSON}) - @Produces({MediaType.APPLICATION_JSON}) + @Consumes({ MediaType.APPLICATION_JSON }) + @Produces({ MediaType.APPLICATION_JSON }) public Response deleteLoanApplication(@PathParam("loanId") final Long loanId) { - EntityIdentifier identifier = this.loanWritePlatformService.deleteLoan(loanId); + EntityIdentifier identifier = this.loanWritePlatformService + .deleteLoan(loanId); return Response.ok().entity(identifier).build(); } @POST @Path("{loanId}") - @Consumes({MediaType.APPLICATION_JSON}) - @Produces({MediaType.APPLICATION_JSON}) + @Consumes({ MediaType.APPLICATION_JSON }) + @Produces({ MediaType.APPLICATION_JSON }) public Response stateTransitions(@PathParam("loanId") final Long loanId, - @QueryParam("command") final String commandParam, final String jsonRequestBody) { + @QueryParam("command") final String commandParam, + final String jsonRequestBody) { + + LoanStateTransitionCommand command = this.apiDataConversionService + .convertJsonToLoanStateTransitionCommand(loanId, + jsonRequestBody); - LoanStateTransitionCommand command = this.apiDataConversionService.convertJsonToLoanStateTransitionCommand(loanId, jsonRequestBody); - Response response = null; if (is(commandParam, "reject")) { @@ -177,8 +197,9 @@ public class LoansApiResource { response = Response.ok().entity(identifier).build(); } - UndoStateTransitionCommand undoCommand = new UndoStateTransitionCommand(loanId, command.getNote()); - + UndoStateTransitionCommand undoCommand = new UndoStateTransitionCommand( + loanId, command.getNote()); + if (is(commandParam, "undoapproval")) { EntityIdentifier identifier = this.loanWritePlatformService .undoLoanApproval(undoCommand); @@ -203,21 +224,25 @@ public class LoansApiResource { @POST @Path("{loanId}/transactions") - @Consumes({MediaType.APPLICATION_JSON}) - @Produces({MediaType.APPLICATION_JSON}) + @Consumes({ MediaType.APPLICATION_JSON }) + @Produces({ MediaType.APPLICATION_JSON }) public Response executeLoanTransaction( @PathParam("loanId") final Long loanId, - @QueryParam("command") final String commandParam, final String jsonRequestBody) { + @QueryParam("command") final String commandParam, + final String jsonRequestBody) { + + final LoanTransactionCommand command = this.apiDataConversionService + .convertJsonToLoanTransactionCommand(loanId, jsonRequestBody); - final LoanTransactionCommand command = this.apiDataConversionService.convertJsonToLoanTransactionCommand(loanId, jsonRequestBody); - Response response = null; if (is(commandParam, "repayment")) { - EntityIdentifier identifier = this.loanWritePlatformService.makeLoanRepayment(command); + EntityIdentifier identifier = this.loanWritePlatformService + .makeLoanRepayment(command); response = Response.ok().entity(identifier).build(); } else if (is(commandParam, "waiver")) { - EntityIdentifier identifier = this.loanWritePlatformService.waiveLoanAmount(command); + EntityIdentifier identifier = this.loanWritePlatformService + .waiveLoanAmount(command); response = Response.ok().entity(identifier).build(); } @@ -230,8 +255,8 @@ public class LoansApiResource { @GET @Path("{loanId}/transactions/template") - @Consumes({MediaType.APPLICATION_JSON}) - @Produces({MediaType.APPLICATION_JSON}) + @Consumes({ MediaType.APPLICATION_JSON }) + @Produces({ MediaType.APPLICATION_JSON }) public String retrieveNewRepaymentDetails( @PathParam("loanId") final Long loanId, @QueryParam("command") final String commandParam, @@ -262,8 +287,8 @@ public class LoansApiResource { @GET @Path("{loanId}/transactions/{transactionId}") - @Consumes({MediaType.APPLICATION_JSON}) - @Produces({MediaType.APPLICATION_JSON}) + @Consumes({ MediaType.APPLICATION_JSON }) + @Produces({ MediaType.APPLICATION_JSON }) public String retrieveTransaction(@PathParam("loanId") final Long loanId, @PathParam("transactionId") final Long transactionId, @Context final UriInfo uriInfo) { @@ -279,15 +304,19 @@ public class LoansApiResource { @POST @Path("{loanId}/transactions/{transactionId}") - @Consumes({MediaType.APPLICATION_JSON}) - @Produces({MediaType.APPLICATION_JSON}) + @Consumes({ MediaType.APPLICATION_JSON }) + @Produces({ MediaType.APPLICATION_JSON }) public Response adjustLoanTransaction( @PathParam("loanId") final Long loanId, - @PathParam("transactionId") final Long transactionId, final String jsonRequestBody) { + @PathParam("transactionId") final Long transactionId, + final String jsonRequestBody) { - final AdjustLoanTransactionCommand command = this.apiDataConversionService.convertJsonToAdjustLoanTransactionCommand(loanId, transactionId, jsonRequestBody); - - EntityIdentifier identifier = this.loanWritePlatformService.adjustLoanTransaction(command); + final AdjustLoanTransactionCommand command = this.apiDataConversionService + .convertJsonToAdjustLoanTransactionCommand(loanId, + transactionId, jsonRequestBody); + + EntityIdentifier identifier = this.loanWritePlatformService + .adjustLoanTransaction(command); return Response.ok().entity(identifier).build(); } diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanReadPlatformService.java b/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanReadPlatformService.java index 8785993b6..7b638e9de 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanReadPlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanReadPlatformService.java @@ -24,7 +24,8 @@ public interface LoanReadPlatformService { Collection loanRepayments); LoanPermissionData retrieveLoanPermissions( - LoanBasicDetailsData loanBasicDetails, boolean isWaiverAllowed); + LoanBasicDetailsData loanBasicDetails, boolean isWaiverAllowed, + int repaymentAndWaiveCount); NewLoanData retrieveClientAndProductDetails(Long clientId, Long productId); diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanReadPlatformServiceImpl.java index bed3d12ce..129e5b967 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/loan/service/LoanReadPlatformServiceImpl.java @@ -5,20 +5,17 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; -import java.util.List; import org.joda.time.LocalDate; import org.mifosng.platform.api.data.ClientData; import org.mifosng.platform.api.data.CurrencyData; import org.mifosng.platform.api.data.EnumOptionData; -import org.mifosng.platform.api.data.LoanAccountData; import org.mifosng.platform.api.data.LoanAccountSummaryData; import org.mifosng.platform.api.data.LoanBasicDetailsData; import org.mifosng.platform.api.data.LoanPermissionData; import org.mifosng.platform.api.data.LoanProductData; import org.mifosng.platform.api.data.LoanProductLookup; import org.mifosng.platform.api.data.LoanRepaymentPeriodDatajpw; -import org.mifosng.platform.api.data.LoanRepaymentScheduleData; import org.mifosng.platform.api.data.LoanTransactionData; import org.mifosng.platform.api.data.LoanTransactionDatajpw; import org.mifosng.platform.api.data.MoneyData; @@ -40,8 +37,6 @@ import org.mifosng.platform.loan.domain.LoanTransactionType; import org.mifosng.platform.loanproduct.service.LoanEnumerations; import org.mifosng.platform.loanproduct.service.LoanProductReadPlatformService; import org.mifosng.platform.security.PlatformSecurityContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; @@ -51,9 +46,6 @@ import org.springframework.stereotype.Service; @Service public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { - private final static Logger logger = LoggerFactory - .getLogger(LoanReadPlatformServiceImpl.class); - private final JdbcTemplate jdbcTemplate; private final PlatformSecurityContext context; private final LoanRepository loanRepository; @@ -204,39 +196,33 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { @Override public LoanPermissionData retrieveLoanPermissions( - LoanBasicDetailsData loanBasicDetails, boolean isWaiverAllowed) { - /* - boolean waiveAllowed = isWaiverAllowed && (loanBasicDetails.getClosedOnDate() == null); - - - - - boolean undoDisbursalAllowed = isDisbursed() - && isOpenWithNoRepaymentMade(); - boolean makeRepaymentAllowed = isDisbursed() && isNotClosed(); + LoanBasicDetailsData loanBasicDetails, boolean isWaiverAllowed, + int repaymentAndWaiveCount) { - boolean rejectAllowed = isNotApproved() && isNotDisbursed() - && isNotClosed(); - boolean withdrawnByApplicantAllowed = isNotDisbursed() && isNotClosed(); - boolean undoApprovalAllowed = isApproved() && isNotClosed(); - boolean disbursalAllowed = isApproved() && isNotDisbursed() - && isNotClosed(); + boolean pendingApproval = (loanBasicDetails.getLifeCycleStatusId() + .equals(100)); + boolean waitingForDisbursal = (loanBasicDetails.getLifeCycleStatusId() + .equals(200)); + boolean isActive = (loanBasicDetails.getLifeCycleStatusId().equals(300)); - return new LoanPermissionData(waiveAllowed, - makeRepaymentAllowed, rejectAllowed, - withdrawnByApplicantAllowed, undoApprovalAllowed, - undoDisbursalAllowed, disbursalAllowed, - isSubmittedAndPendingApproval(), isWaitingForDisbursal()); - */ - + boolean waiveAllowed = isWaiverAllowed && isActive; + boolean makeRepaymentAllowed = isActive; - return new LoanPermissionData(true, - true, true, - true, true, - true, true, - true, true); + boolean rejectAllowed = pendingApproval; + boolean withdrawnByApplicantAllowed = waitingForDisbursal + || pendingApproval; + + boolean undoApprovalAllowed = waitingForDisbursal; + + boolean undoDisbursalAllowed = isActive && (repaymentAndWaiveCount == 0); + + boolean disbursalAllowed = waitingForDisbursal; + + return new LoanPermissionData(waiveAllowed, makeRepaymentAllowed, + rejectAllowed, withdrawnByApplicantAllowed, + undoApprovalAllowed, undoDisbursalAllowed, disbursalAllowed, + pendingApproval, waitingForDisbursal); } - @Override public NewLoanData retrieveClientAndProductDetails(final Long clientId,