From 3f201d7ffaed8249993fd5881ea913fc3a1939b2 Mon Sep 17 00:00:00 2001 From: John Woodlock Date: Wed, 18 Jul 2012 18:40:22 +1000 Subject: [PATCH] WIP get loan data via sql --- .../platform/api/LoansApiResource.java | 6 +- .../platform/api/data/LoanAccountData.java | 111 +++++--- .../api/data/LoanBasicDetailsData.java | 155 +++++++---- .../mifosng/platform/loan/domain/Loan.java | 16 +- .../loan/service/LoanReadPlatformService.java | 11 +- .../service/LoanReadPlatformServiceImpl.java | 250 ++++++++++++++---- 6 files changed, 384 insertions(+), 165 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 e91ec3014..91475bae5 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 @@ -22,6 +22,7 @@ import org.mifosng.platform.api.commands.SubmitLoanApplicationCommand; import org.mifosng.platform.api.commands.UndoStateTransitionCommand; import org.mifosng.platform.api.data.EntityIdentifier; import org.mifosng.platform.api.data.LoanAccountData; +import org.mifosng.platform.api.data.LoanBasicDetailsData; import org.mifosng.platform.api.data.LoanSchedule; import org.mifosng.platform.api.data.LoanTransactionData; import org.mifosng.platform.api.data.NewLoanData; @@ -87,7 +88,10 @@ public class LoansApiResource { public String retrieveLoanAccountDetails( @PathParam("loanId") final Long loanId, @Context final UriInfo uriInfo) { - LoanAccountData loanAccount = this.loanReadPlatformService.retrieveLoanAccountDetails(loanId); + LoanBasicDetailsData loanBasicDetails = this.loanReadPlatformService + .retrieveLoanAccountDetails(loanId); + + LoanAccountData loanAccount = new LoanAccountData(loanBasicDetails, null, null, null, null ); String selectedFields = ""; String associatedFields = "summary,repaymentSchedule,loanRepayments,permissions,convenienceData"; diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanAccountData.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanAccountData.java index a35fd87f8..d1b0be035 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanAccountData.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanAccountData.java @@ -11,7 +11,9 @@ public class LoanAccountData { private Long id; private String externalId; - private FundData fund; + private Long fundId; + private String fundName; + private Long loanProductId; private String loanProductName; private LocalDate submittedOnDate; @@ -22,11 +24,11 @@ public class LoanAccountData { private LocalDate interestChargedFromDate; private LocalDate closedOnDate; private LocalDate expectedMaturityDate; - + private MoneyData principal; private MoneyData inArrearsTolerance; - - private Integer numberOfRepayments; + + private Integer numberOfRepayments; private Integer repaymentEvery; private BigDecimal interestRatePerPeriod; private BigDecimal annualInterestRate; @@ -36,18 +38,19 @@ public class LoanAccountData { private EnumOptionData amortizationType; private EnumOptionData interestType; private EnumOptionData interestCalculationPeriodType; - + + private Integer lifeCycleStatusId; private String lifeCycleStatusText; private LocalDate lifeCycleStatusDate; - + private LoanAccountSummaryData summary; private LoanRepaymentScheduleData repaymentSchedule; private List loanRepayments; - + private LoanPermissionData permissions; - + private LoanConvenienceData convenienceData; - + protected LoanAccountData() { // } @@ -55,35 +58,47 @@ public class LoanAccountData { public LoanAccountData(LoanBasicDetailsData basicDetails, LoanAccountSummaryData summary, LoanRepaymentScheduleData repaymentSchedule, - List loanRepayments, LoanPermissionData permissions) { + List loanRepayments, + LoanPermissionData permissions) { this.summary = summary; this.repaymentSchedule = repaymentSchedule; this.loanRepayments = loanRepayments; this.permissions = permissions; - - int maxSubmittedOnOffsetFromToday = basicDetails.getMaxSubmittedOnOffsetFromToday(); - int maxApprovedOnOffsetFromToday = basicDetails.getMaxApprovedOnOffsetFromToday(); - int maxDisbursedOnOffsetFromToday = basicDetails.getMaxDisbursedOnOffsetFromToday(); + + int maxSubmittedOnOffsetFromToday = basicDetails + .getMaxSubmittedOnOffsetFromToday(); + int maxApprovedOnOffsetFromToday = basicDetails + .getMaxApprovedOnOffsetFromToday(); + int maxDisbursedOnOffsetFromToday = basicDetails + .getMaxDisbursedOnOffsetFromToday(); int expectedLoanTermInDays = basicDetails.getLoanTermInDays(); int expectedLoanTermInMonths = basicDetails.getLoanTermInMonths(); int actualLoanTermInDays = basicDetails.getActualLoanTermInDays(); int actualLoanTermInMonths = basicDetails.getActualLoanTermInMonths(); - - this.convenienceData = new LoanConvenienceData(maxSubmittedOnOffsetFromToday, maxApprovedOnOffsetFromToday, maxDisbursedOnOffsetFromToday, - expectedLoanTermInDays, actualLoanTermInDays, expectedLoanTermInMonths, actualLoanTermInMonths); - + + this.convenienceData = new LoanConvenienceData( + maxSubmittedOnOffsetFromToday, maxApprovedOnOffsetFromToday, + maxDisbursedOnOffsetFromToday, expectedLoanTermInDays, + actualLoanTermInDays, expectedLoanTermInMonths, + actualLoanTermInMonths); + this.id = basicDetails.getId(); this.externalId = basicDetails.getExternalId(); - this.fund = basicDetails.getFund(); + this.fundId = basicDetails.getFundId(); + this.fundName = basicDetails.getFundName(); + this.loanProductId = basicDetails.getLoanProductId(); this.loanProductName = basicDetails.getLoanProductName(); this.submittedOnDate = basicDetails.getSubmittedOnDate(); this.approvedOnDate = basicDetails.getApprovedOnDate(); - this.expectedDisbursementDate = basicDetails.getExpectedDisbursementDate(); + this.expectedDisbursementDate = basicDetails + .getExpectedDisbursementDate(); this.actualDisbursementDate = basicDetails.getActualDisbursementDate(); this.closedOnDate = basicDetails.getClosedOnDate(); this.expectedMaturityDate = basicDetails.getExpectedMaturityDate(); - this.expectedFirstRepaymentOnDate = basicDetails.getExpectedFirstRepaymentOnDate(); - this.interestChargedFromDate = basicDetails.getInterestChargedFromDate(); + this.expectedFirstRepaymentOnDate = basicDetails + .getExpectedFirstRepaymentOnDate(); + this.interestChargedFromDate = basicDetails + .getInterestChargedFromDate(); this.principal = basicDetails.getPrincipal(); this.inArrearsTolerance = basicDetails.getInArrearsTolerance(); this.numberOfRepayments = basicDetails.getNumberOfRepayments(); @@ -91,14 +106,17 @@ public class LoanAccountData { this.interestRatePerPeriod = basicDetails.getInterestRatePerPeriod(); this.annualInterestRate = basicDetails.getAnnualInterestRate(); this.repaymentFrequencyType = basicDetails.getRepaymentFrequencyType(); - this.interestRateFrequencyType = basicDetails.getInterestRateFrequencyType(); + this.interestRateFrequencyType = basicDetails + .getInterestRateFrequencyType(); this.amortizationType = basicDetails.getAmortizationType(); this.interestType = basicDetails.getInterestType(); - this.interestCalculationPeriodType = basicDetails.getInterestCalculationPeriodType(); + this.interestCalculationPeriodType = basicDetails + .getInterestCalculationPeriodType(); this.lifeCycleStatusText = basicDetails.getLifeCycleStatusText(); + this.lifeCycleStatusId = basicDetails.getLifeCycleStatusId(); this.lifeCycleStatusDate = basicDetails.getLifeCycleStatusDate(); } - + public LoanConvenienceData getConvenienceData() { return convenienceData; } @@ -114,7 +132,7 @@ public class LoanAccountData { public void setPermissions(LoanPermissionData permissions) { this.permissions = permissions; } - + public LoanAccountSummaryData getSummary() { return summary; } @@ -154,14 +172,6 @@ public class LoanAccountData { public void setExternalId(String externalId) { this.externalId = externalId; } - - public FundData getFund() { - return fund; - } - - public void setFund(FundData fund) { - this.fund = fund; - } public String getLoanProductName() { return loanProductName; @@ -341,4 +351,37 @@ public class LoanAccountData { public void setLifeCycleStatusDate(LocalDate lifeCycleStatusDate) { this.lifeCycleStatusDate = lifeCycleStatusDate; } + + public Long getFundId() { + return fundId; + } + + public void setFundId(Long fundId) { + this.fundId = fundId; + } + + public String getFundName() { + return fundName; + } + + public void setFundName(String fundName) { + this.fundName = fundName; + } + + public Long getLoanProductId() { + return loanProductId; + } + + public void setLoanProductId(Long loanProductId) { + this.loanProductId = loanProductId; + } + + public Integer getLifeCycleStatusId() { + return lifeCycleStatusId; + } + + public void setLifeCycleStatusId(Integer lifeCycleStatusId) { + this.lifeCycleStatusId = lifeCycleStatusId; + } + } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanBasicDetailsData.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanBasicDetailsData.java index 19d5acec0..0f94da3b5 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanBasicDetailsData.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/LoanBasicDetailsData.java @@ -11,7 +11,9 @@ public class LoanBasicDetailsData { private Long id; private String externalId; - private FundData fund; + private Long fundId; + private String fundName; + private Long loanProductId; private String loanProductName; private LocalDate submittedOnDate; @@ -22,11 +24,11 @@ public class LoanBasicDetailsData { private LocalDate interestChargedFromDate; private LocalDate closedOnDate; private LocalDate expectedMaturityDate; - + private MoneyData principal; private MoneyData inArrearsTolerance; - - private Integer numberOfRepayments; + + private Integer numberOfRepayments; private Integer repaymentEvery; private BigDecimal interestRatePerPeriod; private BigDecimal annualInterestRate; @@ -36,36 +38,42 @@ public class LoanBasicDetailsData { private EnumOptionData amortizationType; private EnumOptionData interestType; private EnumOptionData interestCalculationPeriodType; - + + private Integer lifeCycleStatusId; private String lifeCycleStatusText; private LocalDate lifeCycleStatusDate; - + protected LoanBasicDetailsData() { // } - - public LoanBasicDetailsData(Long id, String externalId, - String loanProductName, FundData fund, LocalDate closedOnDate, - LocalDate submittedOnDate, LocalDate approvedOnDate, - LocalDate expectedDisbursedOnLocalDate, - LocalDate acutalDisbursedOnLocalDate, - LocalDate expectedMaturityDate, + + public LoanBasicDetailsData(Long id, String externalId, Long loanProductId, + String loanProductName, Long fundId, String fundName, + LocalDate closedOnDate, LocalDate submittedOnDate, + LocalDate approvedOnDate, LocalDate expectedDisbursementDate, + LocalDate actualDisbursementDate, LocalDate expectedMaturityDate, LocalDate expectedFirstRepaymentOnDate, - LocalDate interestChargedFromDate, - MoneyData principal, MoneyData inArrearsTolerance, Integer numberOfRepayments, Integer repaymentEvery, - BigDecimal interestRatePerPeriod, BigDecimal annualInterestRate, - EnumOptionData repaymentFrequencyType, EnumOptionData interestRateFrequencyType, EnumOptionData amortizationType, - EnumOptionData interestType, EnumOptionData interestCalculationPeriodType, - String lifeCycleStatusText, LocalDate lifeCycleStatusDate) { + LocalDate interestChargedFromDate, MoneyData principal, + MoneyData inArrearsTolerance, Integer numberOfRepayments, + Integer repaymentEvery, BigDecimal interestRatePerPeriod, + BigDecimal annualInterestRate, + EnumOptionData repaymentFrequencyType, + EnumOptionData interestRateFrequencyType, + EnumOptionData amortizationType, EnumOptionData interestType, + EnumOptionData interestCalculationPeriodType, + Integer lifeCycleStatusId, String lifeCycleStatusText, + LocalDate lifeCycleStatusDate) { this.id = id; this.externalId = externalId; + this.loanProductId = loanProductId; this.loanProductName = loanProductName; - this.fund = fund; + this.fundId = fundId; + this.fundName = fundName; this.closedOnDate = closedOnDate; this.submittedOnDate = submittedOnDate; this.approvedOnDate = approvedOnDate; - this.expectedDisbursementDate = expectedDisbursedOnLocalDate; - actualDisbursementDate = acutalDisbursedOnLocalDate; + this.expectedDisbursementDate = expectedDisbursementDate; + this.actualDisbursementDate = actualDisbursementDate; this.expectedMaturityDate = expectedMaturityDate; this.expectedFirstRepaymentOnDate = expectedFirstRepaymentOnDate; this.interestChargedFromDate = interestChargedFromDate; @@ -80,6 +88,7 @@ public class LoanBasicDetailsData { this.amortizationType = amortizationType; this.interestType = interestType; this.interestCalculationPeriodType = interestCalculationPeriodType; + this.lifeCycleStatusId = lifeCycleStatusId; this.lifeCycleStatusText = lifeCycleStatusText; this.lifeCycleStatusDate = lifeCycleStatusDate; } @@ -91,81 +100,86 @@ public class LoanBasicDetailsData { } public int getMaxApprovedOnOffsetFromToday() { - + int offset = 0; if (this.getApprovedOnDate() != null) { - offset = Days.daysBetween(new DateTime(), + offset = Days.daysBetween(new DateTime(), this.getApprovedOnDate().toDateMidnight().toDateTime()) .getDays(); } - + return offset; } public int getMaxDisbursedOnOffsetFromToday() { - + int offset = 0; if (this.getActualDisbursementDate() != null) { - offset = Days.daysBetween(new DateTime(), - this.getActualDisbursementDate().toDateMidnight().toDateTime()) - .getDays(); + offset = Days.daysBetween( + new DateTime(), + this.getActualDisbursementDate().toDateMidnight() + .toDateTime()).getDays(); } - + return offset; } - + public int getActualLoanTermInDays() { - + LocalDate dateToUse = getExpectedDisbursementDate(); if (getActualDisbursementDate() != null) { dateToUse = getActualDisbursementDate(); } - + LocalDate closingDateToUse = getExpectedMaturityDate(); if (getClosedOnDate() != null) { closingDateToUse = getClosedOnDate(); } - - return Days.daysBetween(dateToUse.toDateMidnight().toDateTime(), closingDateToUse.toDateMidnight().toDateTime()).getDays(); + + return Days.daysBetween(dateToUse.toDateMidnight().toDateTime(), + closingDateToUse.toDateMidnight().toDateTime()).getDays(); } - + public int getActualLoanTermInMonths() { - + LocalDate dateToUse = getExpectedDisbursementDate(); if (getActualDisbursementDate() != null) { dateToUse = getActualDisbursementDate(); } - + LocalDate closingDateToUse = getExpectedMaturityDate(); if (getClosedOnDate() != null) { closingDateToUse = getClosedOnDate(); } - - return Months.monthsBetween(dateToUse.toDateMidnight().toDateTime(), closingDateToUse.toDateMidnight().toDateTime()).getMonths(); + + return Months.monthsBetween(dateToUse.toDateMidnight().toDateTime(), + closingDateToUse.toDateMidnight().toDateTime()).getMonths(); } - + public int getLoanTermInDays() { - + LocalDate dateToUse = getExpectedDisbursementDate(); if (getActualDisbursementDate() != null) { dateToUse = getActualDisbursementDate(); } - + LocalDate closingDateToUse = getExpectedMaturityDate(); - - return Days.daysBetween(dateToUse.toDateMidnight().toDateTime(), closingDateToUse.toDateMidnight().toDateTime()).getDays(); + + return Days.daysBetween(dateToUse.toDateMidnight().toDateTime(), + closingDateToUse.toDateMidnight().toDateTime()).getDays(); } - + public int getLoanTermInMonths() { - + LocalDate dateToUse = getExpectedDisbursementDate(); if (getActualDisbursementDate() != null) { dateToUse = getActualDisbursementDate(); } - + LocalDate closingDateToUse = getExpectedMaturityDate(); - - return Months.monthsBetween(dateToUse.toDateMidnight().toDateTime(), closingDateToUse.toDateMidnight().toDateTime()).getMonths(); + + return Months.monthsBetween(dateToUse.toDateMidnight().toDateTime(), + closingDateToUse.toDateMidnight().toDateTime()).getMonths(); } public Long getId() { @@ -183,14 +197,6 @@ public class LoanBasicDetailsData { public void setExternalId(String externalId) { this.externalId = externalId; } - - public FundData getFund() { - return fund; - } - - public void setFund(FundData fund) { - this.fund = fund; - } public String getLoanProductName() { return loanProductName; @@ -370,4 +376,37 @@ public class LoanBasicDetailsData { public void setLifeCycleStatusDate(LocalDate lifeCycleStatusDate) { this.lifeCycleStatusDate = lifeCycleStatusDate; } + + public Long getFundId() { + return fundId; + } + + public void setFundId(Long fundId) { + this.fundId = fundId; + } + + public String getFundName() { + return fundName; + } + + public void setFundName(String fundName) { + this.fundName = fundName; + } + + public Long getLoanProductId() { + return loanProductId; + } + + public void setLoanProductId(Long loanProductId) { + this.loanProductId = loanProductId; + } + + public Integer getLifeCycleStatusId() { + return lifeCycleStatusId; + } + + public void setLifeCycleStatusId(Integer lifeCycleStatusId) { + this.lifeCycleStatusId = lifeCycleStatusId; + } + } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/loan/domain/Loan.java b/mifosng-provider/src/main/java/org/mifosng/platform/loan/domain/Loan.java index 312d9405e..ec4179ce2 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/loan/domain/Loan.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/loan/domain/Loan.java @@ -27,15 +27,6 @@ import org.joda.time.DateTime; import org.joda.time.LocalDate; import org.mifosng.platform.api.data.CurrencyData; import org.mifosng.platform.api.data.DerivedLoanData; -import org.mifosng.platform.api.data.EnumOptionData; -import org.mifosng.platform.api.data.FundData; -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.LoanRepaymentScheduleData; -import org.mifosng.platform.api.data.LoanTransactionData; -import org.mifosng.platform.api.data.MoneyData; import org.mifosng.platform.client.domain.Client; import org.mifosng.platform.currency.domain.MonetaryCurrency; import org.mifosng.platform.currency.domain.Money; @@ -43,7 +34,6 @@ import org.mifosng.platform.exceptions.InvalidLoanStateTransitionException; import org.mifosng.platform.exceptions.InvalidLoanTransactionTypeException; import org.mifosng.platform.fund.domain.Fund; import org.mifosng.platform.infrastructure.AbstractAuditableCustom; -import org.mifosng.platform.loanproduct.service.LoanEnumerations; import org.mifosng.platform.user.domain.AppUser; @Entity @@ -964,7 +954,7 @@ public class Loan extends AbstractAuditableCustom { return statusSinceDate; } - +/* jpw will be deleting this shortly public LoanBasicDetailsData toBasicDetailsData(final CurrencyData currencyData, final FundData fundData) { Money loanPrincipal = this.loanRepaymentScheduleDetail.getPrincipal(); @@ -986,11 +976,12 @@ public class Loan extends AbstractAuditableCustom { this.loanRepaymentScheduleDetail.getNominalInterestRatePerPeriod(), this.loanRepaymentScheduleDetail.getAnnualNominalInterestRate(), repaymentFrequencyType, interestRateFrequencyType, amortizationType, interestType, interestCalculationPeriodType, getLoanStatusDisplayName(), getLoanStatusSinceDate()); } - +*/ public String getCurrencyCode() { return this.loanRepaymentScheduleDetail.getPrincipal().getCurrencyCode(); } +/* jpw will be deleting this shortly public LoanAccountData toLoanAccountData(LoanAccountSummaryData summary, LoanRepaymentScheduleData repaymentSchedule, List loanRepayments, CurrencyData currencyData, FundData fundData) { @@ -1020,4 +1011,5 @@ public class Loan extends AbstractAuditableCustom { return new LoanAccountData(basicDetails, summary, repaymentSchedule, loanRepayments, permissions); } + */ } \ No newline at end of file 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 ab16a1bc5..33a5751db 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 @@ -1,18 +1,19 @@ package org.mifosng.platform.loan.service; -import org.mifosng.platform.api.data.LoanAccountData; +import org.mifosng.platform.api.data.LoanBasicDetailsData; import org.mifosng.platform.api.data.LoanTransactionData; import org.mifosng.platform.api.data.NewLoanData; public interface LoanReadPlatformService { - LoanAccountData retrieveLoanAccountDetails(Long loanId); - + LoanBasicDetailsData retrieveLoanAccountDetails(Long loanId); + NewLoanData retrieveClientAndProductDetails(Long clientId, Long productId); - + LoanTransactionData retrieveNewLoanRepaymentDetails(Long loanId); LoanTransactionData retrieveNewLoanWaiverDetails(Long loanId); - LoanTransactionData retrieveLoanTransactionDetails(Long loanId, Long transactionId); + LoanTransactionData retrieveLoanTransactionDetails(Long loanId, + Long transactionId); } \ No newline at end of file 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 207a49b4e..2ce4b6e9e 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 @@ -1,14 +1,16 @@ package org.mifosng.platform.loan.service; +import java.math.BigDecimal; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; 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.DerivedLoanData; -import org.mifosng.platform.api.data.FundData; -import org.mifosng.platform.api.data.LoanAccountData; +import org.mifosng.platform.api.data.EnumOptionData; +import org.mifosng.platform.api.data.LoanBasicDetailsData; import org.mifosng.platform.api.data.LoanProductData; import org.mifosng.platform.api.data.LoanProductLookup; import org.mifosng.platform.api.data.LoanTransactionData; @@ -21,7 +23,8 @@ import org.mifosng.platform.currency.domain.Money; import org.mifosng.platform.exceptions.CurrencyNotFoundException; import org.mifosng.platform.exceptions.LoanNotFoundException; import org.mifosng.platform.exceptions.LoanTransactionNotFoundException; -import org.mifosng.platform.fund.service.FundReadPlatformService; +import org.mifosng.platform.infrastructure.JdbcSupport; +import org.mifosng.platform.infrastructure.TenantAwareRoutingDataSource; import org.mifosng.platform.loan.domain.Loan; import org.mifosng.platform.loan.domain.LoanRepository; import org.mifosng.platform.loan.domain.LoanTransaction; @@ -31,18 +34,21 @@ import org.mifosng.platform.loanproduct.service.LoanEnumerations; import org.mifosng.platform.loanproduct.service.LoanProductReadPlatformService; import org.mifosng.platform.security.PlatformSecurityContext; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Service; @Service public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { + private final JdbcTemplate jdbcTemplate; private final PlatformSecurityContext context; private final LoanRepository loanRepository; private final ApplicationCurrencyRepository applicationCurrencyRepository; private final LoanProductReadPlatformService loanProductReadPlatformService; private final ClientReadPlatformService clientReadPlatformService; private final LoanTransactionRepository loanTransactionRepository; - private final FundReadPlatformService fundReadPlatformService; @Autowired public LoanReadPlatformServiceImpl( @@ -51,93 +57,98 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { final LoanTransactionRepository loanTransactionRepository, final ApplicationCurrencyRepository applicationCurrencyRepository, final LoanProductReadPlatformService loanProductReadPlatformService, - final FundReadPlatformService fundReadPlatformService, - final ClientReadPlatformService clientReadPlatformService) { + final ClientReadPlatformService clientReadPlatformService, + final TenantAwareRoutingDataSource dataSource) { this.context = context; this.loanRepository = loanRepository; this.loanTransactionRepository = loanTransactionRepository; this.applicationCurrencyRepository = applicationCurrencyRepository; this.loanProductReadPlatformService = loanProductReadPlatformService; - this.fundReadPlatformService = fundReadPlatformService; this.clientReadPlatformService = clientReadPlatformService; + this.jdbcTemplate = new JdbcTemplate(dataSource); } @Override - public LoanAccountData retrieveLoanAccountDetails(final Long loanId) { + public LoanBasicDetailsData retrieveLoanAccountDetails(final Long loanId) { - // TODO - OPTIMISE - prefer jdbc sql approach to return only what we - // need of loan information. - context.authenticatedUser(); + LoanBasicDetailsData selectedLoan = null; + try { + context.authenticatedUser(); - Loan loan = this.loanRepository.findOne(loanId); - if (loan == null) { + LoanMapper rm = new LoanMapper(); + String sql = "select " + rm.loanSchema() + " where l.id = ?"; + + selectedLoan = this.jdbcTemplate.queryForObject(sql, rm, + new Object[] { loanId }); + + } catch (EmptyResultDataAccessException e) { throw new LoanNotFoundException(loanId); } - - FundData fundData = null; - if (loan.getFund() != null) { - fundData = this.fundReadPlatformService.retrieveFund(loan.getFund().getId()); - } - - final String currencyCode = loan.getCurrencyCode(); - ApplicationCurrency currency = this.applicationCurrencyRepository.findOneByCode(currencyCode); - if (currency == null) { - throw new CurrencyNotFoundException(currencyCode); - } - CurrencyData currencyData = new CurrencyData(currency.getCode(), - currency.getName(), currency.getDecimalPlaces(), - currency.getDisplaySymbol(), currency.getNameCode()); - - return convertToData(loan, currencyData, fundData); - } - - private LoanAccountData convertToData(final Loan realLoan, CurrencyData currencyData, FundData fundData) { - - DerivedLoanData loanData = realLoan.deriveLoanData(currencyData); - - return realLoan.toLoanAccountData(loanData.getSummary(), loanData.getRepaymentSchedule(), loanData.getLoanRepayments(), currencyData, fundData); + return selectedLoan; + // return convertToData(loan, currencyData, fundData); } + /* + * check if this is used after jpw private LoanAccountData + * convertToData(final Loan realLoan, CurrencyData currencyData, FundData + * fundData) { + * + * DerivedLoanData loanData = realLoan.deriveLoanData(currencyData); + * + * return realLoan.toLoanAccountData(loanData.getSummary(), + * loanData.getRepaymentSchedule(), loanData.getLoanRepayments(), + * currencyData, fundData); } + */ @Override - public NewLoanData retrieveClientAndProductDetails(final Long clientId, final Long productId) { + public NewLoanData retrieveClientAndProductDetails(final Long clientId, + final Long productId) { context.authenticatedUser(); NewLoanData workflowData = new NewLoanData(); - Collection loanProducts = this.loanProductReadPlatformService.retrieveAllLoanProductsForLookup(); - workflowData.setAllowedProducts(new ArrayList(loanProducts)); + Collection loanProducts = this.loanProductReadPlatformService + .retrieveAllLoanProductsForLookup(); + workflowData.setAllowedProducts(new ArrayList( + loanProducts)); if (loanProducts.size() == 1) { - Long allowedProductId = workflowData.getAllowedProducts().get(0).getId(); - LoanProductData selectedProduct = this.loanProductReadPlatformService.retrieveLoanProduct(allowedProductId); + 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); - + LoanProductData selectedProduct = findLoanProductById(loanProducts, + productId); + workflowData.setProductId(selectedProduct.getId()); workflowData.setProductName(selectedProduct.getName()); workflowData.setSelectedProduct(selectedProduct); } - ClientData clientAccount = this.clientReadPlatformService.retrieveIndividualClient(clientId); + ClientData clientAccount = this.clientReadPlatformService + .retrieveIndividualClient(clientId); workflowData.setClientId(clientAccount.getId()); workflowData.setClientName(clientAccount.getDisplayName()); - + workflowData.setExpectedDisbursementDate(new LocalDate()); return workflowData; } - private LoanProductData findLoanProductById(Collection loanProducts, Long productId) { - LoanProductData match = this.loanProductReadPlatformService.retrieveNewLoanProductDetails(); + private LoanProductData findLoanProductById( + Collection loanProducts, Long productId) { + LoanProductData match = this.loanProductReadPlatformService + .retrieveNewLoanProductDetails(); for (LoanProductLookup loanProductLookup : loanProducts) { if (loanProductLookup.getId().equals(productId)) { - match = this.loanProductReadPlatformService.retrieveLoanProduct(loanProductLookup.getId()); + match = this.loanProductReadPlatformService + .retrieveLoanProduct(loanProductLookup.getId()); break; } } @@ -175,7 +186,8 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { possibleNextRepaymentAmount.getAmount()); LoanTransactionData newRepaymentDetails = new LoanTransactionData(); - newRepaymentDetails.setTransactionType(LoanEnumerations.transactionType(LoanTransactionType.REPAYMENT)); + newRepaymentDetails.setTransactionType(LoanEnumerations + .transactionType(LoanTransactionType.REPAYMENT)); newRepaymentDetails.setDate(earliestUnpaidInstallmentDate); newRepaymentDetails.setTotal(possibleNextRepayment); @@ -211,7 +223,8 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { totalOutstanding.getAmount()); LoanTransactionData newWaiverDetails = new LoanTransactionData(); - newWaiverDetails.setTransactionType(LoanEnumerations.transactionType(LoanTransactionType.WAIVED)); + newWaiverDetails.setTransactionType(LoanEnumerations + .transactionType(LoanTransactionType.WAIVED)); newWaiverDetails.setDate(new LocalDate()); newWaiverDetails.setTotal(totalOutstandingData); @@ -219,7 +232,8 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { } @Override - public LoanTransactionData retrieveLoanTransactionDetails(final Long loanId, final Long transactionId) { + public LoanTransactionData retrieveLoanTransactionDetails( + final Long loanId, final Long transactionId) { context.authenticatedUser(); @@ -236,11 +250,12 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { throw new CurrencyNotFoundException(currencyCode); } - LoanTransaction transaction = this.loanTransactionRepository.findOne(transactionId); + LoanTransaction transaction = this.loanTransactionRepository + .findOne(transactionId); if (transaction == null) { throw new LoanTransactionNotFoundException(transactionId); } - + if (transaction.isNotBelongingToLoanOf(loan)) { throw new LoanTransactionNotFoundException(transactionId, loanId); } @@ -252,11 +267,136 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { LocalDate date = transaction.getTransactionDate(); LoanTransactionData loanRepaymentData = new LoanTransactionData(); - loanRepaymentData.setTransactionType(LoanEnumerations.transactionType(transaction.getTypeOf())); + loanRepaymentData.setTransactionType(LoanEnumerations + .transactionType(transaction.getTypeOf())); loanRepaymentData.setId(transactionId); loanRepaymentData.setTotal(total); loanRepaymentData.setDate(date); return loanRepaymentData; } + + private static final class LoanMapper implements + RowMapper { + + public String loanSchema() { + return "l.id as id, l.external_id as externalId, l.fund_id as fundId, f.name as fundName, lp.id as loanProductId, lp.name as loanProductName, l.submittedon_date as submittedOnDate," + + " l.approvedon_date as approvedOnDate, l.expected_disbursedon_date as expectedDisbursementDate, l.disbursedon_date as actualDisbursementDate, l.expected_firstrepaymenton_date as expectedFirstRepaymentOnDate," + + " l.interest_calculated_from_date as interestChargedFromDate, l.closedon_date as closedOnDate, l.expected_maturedon_date as expectedMaturityDate, " + + " l.principal_amount as principal, l.arrearstolerance_amount as inArrearsTolerance, l.number_of_repayments as numberOfRepayments, l.repay_every as repaymentEvery," + + " l.nominal_interest_rate_per_period as interestRatePerPeriod, l.annual_nominal_interest_rate as annualInterestRate, " + + " l.repayment_period_frequency_enum as repaymentFrequencyType, l.interest_period_frequency_enum as interestRateFrequencyType, " + + " l.amortization_method_enum as amortizationType, l.interest_method_enum as interestType, l.interest_calculated_in_period_enum as interestCalculationPeriodType," + + " l.loan_status_id as lifeCycleStatusId, st.display_name as lifeCycleStatusText, " + + " l.currency_code as currencyCode, l.currency_digits as currencyDigits, rc.`name` as currencyName, rc.display_symbol as currencyDisplaySymbol, rc.internationalized_name_code as currencyNameCode" + + " from portfolio_loan l" + + " join portfolio_product_loan lp on lp.id = l.product_id" + + " join ref_currency rc on rc.`code` = l.currency_code" + + " join ref_loan_status st on st.id = l.loan_status_id" + + " left join org_fund f on f.id = l.fund_id"; + } + + @Override + public LoanBasicDetailsData mapRow(final ResultSet rs, final int rowNum) + throws SQLException { + + String currencyCode = rs.getString("currencyCode"); + String currencyName = rs.getString("currencyName"); + String currencyNameCode = rs.getString("currencyNameCode"); + String currencyDisplaySymbol = rs + .getString("currencyDisplaySymbol"); + Integer currencyDigits = JdbcSupport.getInteger(rs, + "currencyDigits"); + CurrencyData currencyData = new CurrencyData(currencyCode, + currencyName, currencyDigits, currencyDisplaySymbol, + currencyNameCode); + + Long id = rs.getLong("id"); + String externalId = rs.getString("externalId"); + Long fundId = JdbcSupport.getLong(rs, "fundId"); + String fundName = rs.getString("fundName"); + Long loanProductId = JdbcSupport.getLong(rs, "loanProductId"); + String loanProductName = rs.getString("loanProductName"); + LocalDate submittedOnDate = JdbcSupport.getLocalDate(rs, + "submittedOnDate"); + LocalDate approvedOnDate = JdbcSupport.getLocalDate(rs, + "approvedOnDate"); + LocalDate expectedDisbursementDate = JdbcSupport.getLocalDate(rs, + "expectedDisbursementDate"); + LocalDate actualDisbursementDate = JdbcSupport.getLocalDate(rs, + "actualDisbursementDate"); + LocalDate expectedFirstRepaymentOnDate = JdbcSupport.getLocalDate( + rs, "expectedFirstRepaymentOnDate"); + LocalDate interestChargedFromDate = JdbcSupport.getLocalDate(rs, + "interestChargedFromDate"); + LocalDate closedOnDate = JdbcSupport.getLocalDate(rs, + "closedOnDate"); + LocalDate expectedMaturityDate = JdbcSupport.getLocalDate(rs, + "expectedMaturityDate"); + + BigDecimal principalBD = rs.getBigDecimal("principal"); + MoneyData principal = MoneyData.of(currencyData, principalBD); + BigDecimal inArrearsToleranceBD = rs + .getBigDecimal("inArrearsTolerance"); + MoneyData inArrearsTolerance = MoneyData.of(currencyData, + inArrearsToleranceBD); + + Integer numberOfRepayments = JdbcSupport.getInteger(rs, + "numberOfRepayments"); + Integer repaymentEvery = JdbcSupport.getInteger(rs, + "repaymentEvery"); + BigDecimal interestRatePerPeriod = rs + .getBigDecimal("interestRatePerPeriod"); + BigDecimal annualInterestRate = rs + .getBigDecimal("annualInterestRate"); + + int repaymentFrequencyTypeInt = JdbcSupport.getInteger(rs, + "repaymentFrequencyType"); + int interestRateFrequencyTypeInt = JdbcSupport.getInteger(rs, + "interestRateFrequencyType"); + int amortizationTypeInt = JdbcSupport.getInteger(rs, + "amortizationType"); + int interestTypeInt = JdbcSupport.getInteger(rs, "interestType"); + int interestCalculationPeriodTypeInt = JdbcSupport.getInteger(rs, + "interestCalculationPeriodType"); + EnumOptionData repaymentFrequencyType = LoanEnumerations + .repaymentFrequencyType(repaymentFrequencyTypeInt); + EnumOptionData interestRateFrequencyType = LoanEnumerations + .interestRateFrequencyType(interestRateFrequencyTypeInt); + EnumOptionData amortizationType = LoanEnumerations + .amortizationType(amortizationTypeInt); + EnumOptionData interestType = LoanEnumerations + .interestType(interestTypeInt); + EnumOptionData interestCalculationPeriodType = LoanEnumerations + .interestCalculationPeriodType(interestCalculationPeriodTypeInt); + + Integer lifeCycleStatusId = JdbcSupport.getInteger(rs, + "lifeCycleStatusId"); + String lifeCycleStatusText = rs.getString("lifeCycleStatusText"); + + LocalDate lifeCycleStatusDate = submittedOnDate; + if (approvedOnDate != null) { + lifeCycleStatusDate = approvedOnDate; + } + if (actualDisbursementDate != null) { + lifeCycleStatusDate = actualDisbursementDate; + } + if (closedOnDate != null) { + lifeCycleStatusDate = closedOnDate; + } + + return new LoanBasicDetailsData(id, externalId, loanProductId, + loanProductName, fundId, fundName, closedOnDate, + submittedOnDate, approvedOnDate, expectedDisbursementDate, + actualDisbursementDate, expectedMaturityDate, + expectedFirstRepaymentOnDate, interestChargedFromDate, + principal, inArrearsTolerance, numberOfRepayments, + repaymentEvery, interestRatePerPeriod, annualInterestRate, + repaymentFrequencyType, interestRateFrequencyType, + amortizationType, interestType, + interestCalculationPeriodType, lifeCycleStatusId, + lifeCycleStatusText, lifeCycleStatusDate); + } + } + } \ No newline at end of file