WIP get loan data via sql

This commit is contained in:
John Woodlock
2012-07-18 20:38:46 +10:00
parent 1ee488e0ae
commit 3f201d7ffa
6 changed files with 384 additions and 165 deletions
@@ -22,6 +22,7 @@ import org.mifosng.platform.api.commands.SubmitLoanApplicationCommand;
import org.mifosng.platform.api.commands.UndoStateTransitionCommand; import org.mifosng.platform.api.commands.UndoStateTransitionCommand;
import org.mifosng.platform.api.data.EntityIdentifier; import org.mifosng.platform.api.data.EntityIdentifier;
import org.mifosng.platform.api.data.LoanAccountData; 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.LoanSchedule;
import org.mifosng.platform.api.data.LoanTransactionData; import org.mifosng.platform.api.data.LoanTransactionData;
import org.mifosng.platform.api.data.NewLoanData; import org.mifosng.platform.api.data.NewLoanData;
@@ -87,7 +88,10 @@ public class LoansApiResource {
public String retrieveLoanAccountDetails( public String retrieveLoanAccountDetails(
@PathParam("loanId") final Long loanId, @Context final UriInfo uriInfo) { @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 selectedFields = "";
String associatedFields = "summary,repaymentSchedule,loanRepayments,permissions,convenienceData"; String associatedFields = "summary,repaymentSchedule,loanRepayments,permissions,convenienceData";
@@ -11,7 +11,9 @@ public class LoanAccountData {
private Long id; private Long id;
private String externalId; private String externalId;
private FundData fund; private Long fundId;
private String fundName;
private Long loanProductId;
private String loanProductName; private String loanProductName;
private LocalDate submittedOnDate; private LocalDate submittedOnDate;
@@ -37,6 +39,7 @@ public class LoanAccountData {
private EnumOptionData interestType; private EnumOptionData interestType;
private EnumOptionData interestCalculationPeriodType; private EnumOptionData interestCalculationPeriodType;
private Integer lifeCycleStatusId;
private String lifeCycleStatusText; private String lifeCycleStatusText;
private LocalDate lifeCycleStatusDate; private LocalDate lifeCycleStatusDate;
@@ -55,35 +58,47 @@ public class LoanAccountData {
public LoanAccountData(LoanBasicDetailsData basicDetails, public LoanAccountData(LoanBasicDetailsData basicDetails,
LoanAccountSummaryData summary, LoanAccountSummaryData summary,
LoanRepaymentScheduleData repaymentSchedule, LoanRepaymentScheduleData repaymentSchedule,
List<LoanTransactionData> loanRepayments, LoanPermissionData permissions) { List<LoanTransactionData> loanRepayments,
LoanPermissionData permissions) {
this.summary = summary; this.summary = summary;
this.repaymentSchedule = repaymentSchedule; this.repaymentSchedule = repaymentSchedule;
this.loanRepayments = loanRepayments; this.loanRepayments = loanRepayments;
this.permissions = permissions; this.permissions = permissions;
int maxSubmittedOnOffsetFromToday = basicDetails.getMaxSubmittedOnOffsetFromToday(); int maxSubmittedOnOffsetFromToday = basicDetails
int maxApprovedOnOffsetFromToday = basicDetails.getMaxApprovedOnOffsetFromToday(); .getMaxSubmittedOnOffsetFromToday();
int maxDisbursedOnOffsetFromToday = basicDetails.getMaxDisbursedOnOffsetFromToday(); int maxApprovedOnOffsetFromToday = basicDetails
.getMaxApprovedOnOffsetFromToday();
int maxDisbursedOnOffsetFromToday = basicDetails
.getMaxDisbursedOnOffsetFromToday();
int expectedLoanTermInDays = basicDetails.getLoanTermInDays(); int expectedLoanTermInDays = basicDetails.getLoanTermInDays();
int expectedLoanTermInMonths = basicDetails.getLoanTermInMonths(); int expectedLoanTermInMonths = basicDetails.getLoanTermInMonths();
int actualLoanTermInDays = basicDetails.getActualLoanTermInDays(); int actualLoanTermInDays = basicDetails.getActualLoanTermInDays();
int actualLoanTermInMonths = basicDetails.getActualLoanTermInMonths(); int actualLoanTermInMonths = basicDetails.getActualLoanTermInMonths();
this.convenienceData = new LoanConvenienceData(maxSubmittedOnOffsetFromToday, maxApprovedOnOffsetFromToday, maxDisbursedOnOffsetFromToday, this.convenienceData = new LoanConvenienceData(
expectedLoanTermInDays, actualLoanTermInDays, expectedLoanTermInMonths, actualLoanTermInMonths); maxSubmittedOnOffsetFromToday, maxApprovedOnOffsetFromToday,
maxDisbursedOnOffsetFromToday, expectedLoanTermInDays,
actualLoanTermInDays, expectedLoanTermInMonths,
actualLoanTermInMonths);
this.id = basicDetails.getId(); this.id = basicDetails.getId();
this.externalId = basicDetails.getExternalId(); 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.loanProductName = basicDetails.getLoanProductName();
this.submittedOnDate = basicDetails.getSubmittedOnDate(); this.submittedOnDate = basicDetails.getSubmittedOnDate();
this.approvedOnDate = basicDetails.getApprovedOnDate(); this.approvedOnDate = basicDetails.getApprovedOnDate();
this.expectedDisbursementDate = basicDetails.getExpectedDisbursementDate(); this.expectedDisbursementDate = basicDetails
.getExpectedDisbursementDate();
this.actualDisbursementDate = basicDetails.getActualDisbursementDate(); this.actualDisbursementDate = basicDetails.getActualDisbursementDate();
this.closedOnDate = basicDetails.getClosedOnDate(); this.closedOnDate = basicDetails.getClosedOnDate();
this.expectedMaturityDate = basicDetails.getExpectedMaturityDate(); this.expectedMaturityDate = basicDetails.getExpectedMaturityDate();
this.expectedFirstRepaymentOnDate = basicDetails.getExpectedFirstRepaymentOnDate(); this.expectedFirstRepaymentOnDate = basicDetails
this.interestChargedFromDate = basicDetails.getInterestChargedFromDate(); .getExpectedFirstRepaymentOnDate();
this.interestChargedFromDate = basicDetails
.getInterestChargedFromDate();
this.principal = basicDetails.getPrincipal(); this.principal = basicDetails.getPrincipal();
this.inArrearsTolerance = basicDetails.getInArrearsTolerance(); this.inArrearsTolerance = basicDetails.getInArrearsTolerance();
this.numberOfRepayments = basicDetails.getNumberOfRepayments(); this.numberOfRepayments = basicDetails.getNumberOfRepayments();
@@ -91,11 +106,14 @@ public class LoanAccountData {
this.interestRatePerPeriod = basicDetails.getInterestRatePerPeriod(); this.interestRatePerPeriod = basicDetails.getInterestRatePerPeriod();
this.annualInterestRate = basicDetails.getAnnualInterestRate(); this.annualInterestRate = basicDetails.getAnnualInterestRate();
this.repaymentFrequencyType = basicDetails.getRepaymentFrequencyType(); this.repaymentFrequencyType = basicDetails.getRepaymentFrequencyType();
this.interestRateFrequencyType = basicDetails.getInterestRateFrequencyType(); this.interestRateFrequencyType = basicDetails
.getInterestRateFrequencyType();
this.amortizationType = basicDetails.getAmortizationType(); this.amortizationType = basicDetails.getAmortizationType();
this.interestType = basicDetails.getInterestType(); this.interestType = basicDetails.getInterestType();
this.interestCalculationPeriodType = basicDetails.getInterestCalculationPeriodType(); this.interestCalculationPeriodType = basicDetails
.getInterestCalculationPeriodType();
this.lifeCycleStatusText = basicDetails.getLifeCycleStatusText(); this.lifeCycleStatusText = basicDetails.getLifeCycleStatusText();
this.lifeCycleStatusId = basicDetails.getLifeCycleStatusId();
this.lifeCycleStatusDate = basicDetails.getLifeCycleStatusDate(); this.lifeCycleStatusDate = basicDetails.getLifeCycleStatusDate();
} }
@@ -155,14 +173,6 @@ public class LoanAccountData {
this.externalId = externalId; this.externalId = externalId;
} }
public FundData getFund() {
return fund;
}
public void setFund(FundData fund) {
this.fund = fund;
}
public String getLoanProductName() { public String getLoanProductName() {
return loanProductName; return loanProductName;
} }
@@ -341,4 +351,37 @@ public class LoanAccountData {
public void setLifeCycleStatusDate(LocalDate lifeCycleStatusDate) { public void setLifeCycleStatusDate(LocalDate lifeCycleStatusDate) {
this.lifeCycleStatusDate = 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;
}
} }
@@ -11,7 +11,9 @@ public class LoanBasicDetailsData {
private Long id; private Long id;
private String externalId; private String externalId;
private FundData fund; private Long fundId;
private String fundName;
private Long loanProductId;
private String loanProductName; private String loanProductName;
private LocalDate submittedOnDate; private LocalDate submittedOnDate;
@@ -37,6 +39,7 @@ public class LoanBasicDetailsData {
private EnumOptionData interestType; private EnumOptionData interestType;
private EnumOptionData interestCalculationPeriodType; private EnumOptionData interestCalculationPeriodType;
private Integer lifeCycleStatusId;
private String lifeCycleStatusText; private String lifeCycleStatusText;
private LocalDate lifeCycleStatusDate; private LocalDate lifeCycleStatusDate;
@@ -44,28 +47,33 @@ public class LoanBasicDetailsData {
// //
} }
public LoanBasicDetailsData(Long id, String externalId, public LoanBasicDetailsData(Long id, String externalId, Long loanProductId,
String loanProductName, FundData fund, LocalDate closedOnDate, String loanProductName, Long fundId, String fundName,
LocalDate submittedOnDate, LocalDate approvedOnDate, LocalDate closedOnDate, LocalDate submittedOnDate,
LocalDate expectedDisbursedOnLocalDate, LocalDate approvedOnDate, LocalDate expectedDisbursementDate,
LocalDate acutalDisbursedOnLocalDate, LocalDate actualDisbursementDate, LocalDate expectedMaturityDate,
LocalDate expectedMaturityDate,
LocalDate expectedFirstRepaymentOnDate, LocalDate expectedFirstRepaymentOnDate,
LocalDate interestChargedFromDate, LocalDate interestChargedFromDate, MoneyData principal,
MoneyData principal, MoneyData inArrearsTolerance, Integer numberOfRepayments, Integer repaymentEvery, MoneyData inArrearsTolerance, Integer numberOfRepayments,
BigDecimal interestRatePerPeriod, BigDecimal annualInterestRate, Integer repaymentEvery, BigDecimal interestRatePerPeriod,
EnumOptionData repaymentFrequencyType, EnumOptionData interestRateFrequencyType, EnumOptionData amortizationType, BigDecimal annualInterestRate,
EnumOptionData interestType, EnumOptionData interestCalculationPeriodType, EnumOptionData repaymentFrequencyType,
String lifeCycleStatusText, LocalDate lifeCycleStatusDate) { EnumOptionData interestRateFrequencyType,
EnumOptionData amortizationType, EnumOptionData interestType,
EnumOptionData interestCalculationPeriodType,
Integer lifeCycleStatusId, String lifeCycleStatusText,
LocalDate lifeCycleStatusDate) {
this.id = id; this.id = id;
this.externalId = externalId; this.externalId = externalId;
this.loanProductId = loanProductId;
this.loanProductName = loanProductName; this.loanProductName = loanProductName;
this.fund = fund; this.fundId = fundId;
this.fundName = fundName;
this.closedOnDate = closedOnDate; this.closedOnDate = closedOnDate;
this.submittedOnDate = submittedOnDate; this.submittedOnDate = submittedOnDate;
this.approvedOnDate = approvedOnDate; this.approvedOnDate = approvedOnDate;
this.expectedDisbursementDate = expectedDisbursedOnLocalDate; this.expectedDisbursementDate = expectedDisbursementDate;
actualDisbursementDate = acutalDisbursedOnLocalDate; this.actualDisbursementDate = actualDisbursementDate;
this.expectedMaturityDate = expectedMaturityDate; this.expectedMaturityDate = expectedMaturityDate;
this.expectedFirstRepaymentOnDate = expectedFirstRepaymentOnDate; this.expectedFirstRepaymentOnDate = expectedFirstRepaymentOnDate;
this.interestChargedFromDate = interestChargedFromDate; this.interestChargedFromDate = interestChargedFromDate;
@@ -80,6 +88,7 @@ public class LoanBasicDetailsData {
this.amortizationType = amortizationType; this.amortizationType = amortizationType;
this.interestType = interestType; this.interestType = interestType;
this.interestCalculationPeriodType = interestCalculationPeriodType; this.interestCalculationPeriodType = interestCalculationPeriodType;
this.lifeCycleStatusId = lifeCycleStatusId;
this.lifeCycleStatusText = lifeCycleStatusText; this.lifeCycleStatusText = lifeCycleStatusText;
this.lifeCycleStatusDate = lifeCycleStatusDate; this.lifeCycleStatusDate = lifeCycleStatusDate;
} }
@@ -94,7 +103,7 @@ public class LoanBasicDetailsData {
int offset = 0; int offset = 0;
if (this.getApprovedOnDate() != null) { if (this.getApprovedOnDate() != null) {
offset = Days.daysBetween(new DateTime(), offset = Days.daysBetween(new DateTime(),
this.getApprovedOnDate().toDateMidnight().toDateTime()) this.getApprovedOnDate().toDateMidnight().toDateTime())
.getDays(); .getDays();
} }
@@ -106,9 +115,10 @@ public class LoanBasicDetailsData {
int offset = 0; int offset = 0;
if (this.getActualDisbursementDate() != null) { if (this.getActualDisbursementDate() != null) {
offset = Days.daysBetween(new DateTime(), offset = Days.daysBetween(
this.getActualDisbursementDate().toDateMidnight().toDateTime()) new DateTime(),
.getDays(); this.getActualDisbursementDate().toDateMidnight()
.toDateTime()).getDays();
} }
return offset; return offset;
@@ -126,7 +136,8 @@ public class LoanBasicDetailsData {
closingDateToUse = getClosedOnDate(); 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() { public int getActualLoanTermInMonths() {
@@ -141,7 +152,8 @@ public class LoanBasicDetailsData {
closingDateToUse = getClosedOnDate(); 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() { public int getLoanTermInDays() {
@@ -153,7 +165,8 @@ public class LoanBasicDetailsData {
LocalDate closingDateToUse = getExpectedMaturityDate(); 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() { public int getLoanTermInMonths() {
@@ -165,7 +178,8 @@ public class LoanBasicDetailsData {
LocalDate closingDateToUse = getExpectedMaturityDate(); 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() { public Long getId() {
@@ -184,14 +198,6 @@ public class LoanBasicDetailsData {
this.externalId = externalId; this.externalId = externalId;
} }
public FundData getFund() {
return fund;
}
public void setFund(FundData fund) {
this.fund = fund;
}
public String getLoanProductName() { public String getLoanProductName() {
return loanProductName; return loanProductName;
} }
@@ -370,4 +376,37 @@ public class LoanBasicDetailsData {
public void setLifeCycleStatusDate(LocalDate lifeCycleStatusDate) { public void setLifeCycleStatusDate(LocalDate lifeCycleStatusDate) {
this.lifeCycleStatusDate = 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;
}
} }
@@ -27,15 +27,6 @@ import org.joda.time.DateTime;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
import org.mifosng.platform.api.data.CurrencyData; import org.mifosng.platform.api.data.CurrencyData;
import org.mifosng.platform.api.data.DerivedLoanData; 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.client.domain.Client;
import org.mifosng.platform.currency.domain.MonetaryCurrency; import org.mifosng.platform.currency.domain.MonetaryCurrency;
import org.mifosng.platform.currency.domain.Money; 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.exceptions.InvalidLoanTransactionTypeException;
import org.mifosng.platform.fund.domain.Fund; import org.mifosng.platform.fund.domain.Fund;
import org.mifosng.platform.infrastructure.AbstractAuditableCustom; import org.mifosng.platform.infrastructure.AbstractAuditableCustom;
import org.mifosng.platform.loanproduct.service.LoanEnumerations;
import org.mifosng.platform.user.domain.AppUser; import org.mifosng.platform.user.domain.AppUser;
@Entity @Entity
@@ -964,7 +954,7 @@ public class Loan extends AbstractAuditableCustom<AppUser, Long> {
return statusSinceDate; return statusSinceDate;
} }
/* jpw will be deleting this shortly
public LoanBasicDetailsData toBasicDetailsData(final CurrencyData currencyData, final FundData fundData) { public LoanBasicDetailsData toBasicDetailsData(final CurrencyData currencyData, final FundData fundData) {
Money loanPrincipal = this.loanRepaymentScheduleDetail.getPrincipal(); Money loanPrincipal = this.loanRepaymentScheduleDetail.getPrincipal();
@@ -986,11 +976,12 @@ public class Loan extends AbstractAuditableCustom<AppUser, Long> {
this.loanRepaymentScheduleDetail.getNominalInterestRatePerPeriod(), this.loanRepaymentScheduleDetail.getAnnualNominalInterestRate(), this.loanRepaymentScheduleDetail.getNominalInterestRatePerPeriod(), this.loanRepaymentScheduleDetail.getAnnualNominalInterestRate(),
repaymentFrequencyType, interestRateFrequencyType, amortizationType, interestType, interestCalculationPeriodType, getLoanStatusDisplayName(), getLoanStatusSinceDate()); repaymentFrequencyType, interestRateFrequencyType, amortizationType, interestType, interestCalculationPeriodType, getLoanStatusDisplayName(), getLoanStatusSinceDate());
} }
*/
public String getCurrencyCode() { public String getCurrencyCode() {
return this.loanRepaymentScheduleDetail.getPrincipal().getCurrencyCode(); return this.loanRepaymentScheduleDetail.getPrincipal().getCurrencyCode();
} }
/* jpw will be deleting this shortly
public LoanAccountData toLoanAccountData(LoanAccountSummaryData summary, LoanRepaymentScheduleData repaymentSchedule, public LoanAccountData toLoanAccountData(LoanAccountSummaryData summary, LoanRepaymentScheduleData repaymentSchedule,
List<LoanTransactionData> loanRepayments, List<LoanTransactionData> loanRepayments,
CurrencyData currencyData, FundData fundData) { CurrencyData currencyData, FundData fundData) {
@@ -1020,4 +1011,5 @@ public class Loan extends AbstractAuditableCustom<AppUser, Long> {
return new LoanAccountData(basicDetails, summary, repaymentSchedule, loanRepayments, permissions); return new LoanAccountData(basicDetails, summary, repaymentSchedule, loanRepayments, permissions);
} }
*/
} }
@@ -1,12 +1,12 @@
package org.mifosng.platform.loan.service; 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.LoanTransactionData;
import org.mifosng.platform.api.data.NewLoanData; import org.mifosng.platform.api.data.NewLoanData;
public interface LoanReadPlatformService { public interface LoanReadPlatformService {
LoanAccountData retrieveLoanAccountDetails(Long loanId); LoanBasicDetailsData retrieveLoanAccountDetails(Long loanId);
NewLoanData retrieveClientAndProductDetails(Long clientId, Long productId); NewLoanData retrieveClientAndProductDetails(Long clientId, Long productId);
@@ -14,5 +14,6 @@ public interface LoanReadPlatformService {
LoanTransactionData retrieveNewLoanWaiverDetails(Long loanId); LoanTransactionData retrieveNewLoanWaiverDetails(Long loanId);
LoanTransactionData retrieveLoanTransactionDetails(Long loanId, Long transactionId); LoanTransactionData retrieveLoanTransactionDetails(Long loanId,
Long transactionId);
} }
@@ -1,14 +1,16 @@
package org.mifosng.platform.loan.service; 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.ArrayList;
import java.util.Collection; import java.util.Collection;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
import org.mifosng.platform.api.data.ClientData; import org.mifosng.platform.api.data.ClientData;
import org.mifosng.platform.api.data.CurrencyData; 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.LoanBasicDetailsData;
import org.mifosng.platform.api.data.LoanAccountData;
import org.mifosng.platform.api.data.LoanProductData; import org.mifosng.platform.api.data.LoanProductData;
import org.mifosng.platform.api.data.LoanProductLookup; import org.mifosng.platform.api.data.LoanProductLookup;
import org.mifosng.platform.api.data.LoanTransactionData; 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.CurrencyNotFoundException;
import org.mifosng.platform.exceptions.LoanNotFoundException; import org.mifosng.platform.exceptions.LoanNotFoundException;
import org.mifosng.platform.exceptions.LoanTransactionNotFoundException; 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.Loan;
import org.mifosng.platform.loan.domain.LoanRepository; import org.mifosng.platform.loan.domain.LoanRepository;
import org.mifosng.platform.loan.domain.LoanTransaction; 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.loanproduct.service.LoanProductReadPlatformService;
import org.mifosng.platform.security.PlatformSecurityContext; import org.mifosng.platform.security.PlatformSecurityContext;
import org.springframework.beans.factory.annotation.Autowired; 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; import org.springframework.stereotype.Service;
@Service @Service
public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { public class LoanReadPlatformServiceImpl implements LoanReadPlatformService {
private final JdbcTemplate jdbcTemplate;
private final PlatformSecurityContext context; private final PlatformSecurityContext context;
private final LoanRepository loanRepository; private final LoanRepository loanRepository;
private final ApplicationCurrencyRepository applicationCurrencyRepository; private final ApplicationCurrencyRepository applicationCurrencyRepository;
private final LoanProductReadPlatformService loanProductReadPlatformService; private final LoanProductReadPlatformService loanProductReadPlatformService;
private final ClientReadPlatformService clientReadPlatformService; private final ClientReadPlatformService clientReadPlatformService;
private final LoanTransactionRepository loanTransactionRepository; private final LoanTransactionRepository loanTransactionRepository;
private final FundReadPlatformService fundReadPlatformService;
@Autowired @Autowired
public LoanReadPlatformServiceImpl( public LoanReadPlatformServiceImpl(
@@ -51,80 +57,82 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService {
final LoanTransactionRepository loanTransactionRepository, final LoanTransactionRepository loanTransactionRepository,
final ApplicationCurrencyRepository applicationCurrencyRepository, final ApplicationCurrencyRepository applicationCurrencyRepository,
final LoanProductReadPlatformService loanProductReadPlatformService, final LoanProductReadPlatformService loanProductReadPlatformService,
final FundReadPlatformService fundReadPlatformService, final ClientReadPlatformService clientReadPlatformService,
final ClientReadPlatformService clientReadPlatformService) { final TenantAwareRoutingDataSource dataSource) {
this.context = context; this.context = context;
this.loanRepository = loanRepository; this.loanRepository = loanRepository;
this.loanTransactionRepository = loanTransactionRepository; this.loanTransactionRepository = loanTransactionRepository;
this.applicationCurrencyRepository = applicationCurrencyRepository; this.applicationCurrencyRepository = applicationCurrencyRepository;
this.loanProductReadPlatformService = loanProductReadPlatformService; this.loanProductReadPlatformService = loanProductReadPlatformService;
this.fundReadPlatformService = fundReadPlatformService;
this.clientReadPlatformService = clientReadPlatformService; this.clientReadPlatformService = clientReadPlatformService;
this.jdbcTemplate = new JdbcTemplate(dataSource);
} }
@Override @Override
public LoanAccountData retrieveLoanAccountDetails(final Long loanId) { public LoanBasicDetailsData retrieveLoanAccountDetails(final Long loanId) {
// TODO - OPTIMISE - prefer jdbc sql approach to return only what we LoanBasicDetailsData selectedLoan = null;
// need of loan information. try {
context.authenticatedUser(); context.authenticatedUser();
Loan loan = this.loanRepository.findOne(loanId); LoanMapper rm = new LoanMapper();
if (loan == null) { String sql = "select " + rm.loanSchema() + " where l.id = ?";
selectedLoan = this.jdbcTemplate.queryForObject(sql, rm,
new Object[] { loanId });
} catch (EmptyResultDataAccessException e) {
throw new LoanNotFoundException(loanId); throw new LoanNotFoundException(loanId);
} }
FundData fundData = null; return selectedLoan;
if (loan.getFund() != null) { // return convertToData(loan, currencyData, fundData);
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);
} }
/*
* 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 @Override
public NewLoanData retrieveClientAndProductDetails(final Long clientId, final Long productId) { public NewLoanData retrieveClientAndProductDetails(final Long clientId,
final Long productId) {
context.authenticatedUser(); context.authenticatedUser();
NewLoanData workflowData = new NewLoanData(); NewLoanData workflowData = new NewLoanData();
Collection<LoanProductLookup> loanProducts = this.loanProductReadPlatformService.retrieveAllLoanProductsForLookup(); Collection<LoanProductLookup> loanProducts = this.loanProductReadPlatformService
workflowData.setAllowedProducts(new ArrayList<LoanProductLookup>(loanProducts)); .retrieveAllLoanProductsForLookup();
workflowData.setAllowedProducts(new ArrayList<LoanProductLookup>(
loanProducts));
if (loanProducts.size() == 1) { if (loanProducts.size() == 1) {
Long allowedProductId = workflowData.getAllowedProducts().get(0).getId(); Long allowedProductId = workflowData.getAllowedProducts().get(0)
LoanProductData selectedProduct = this.loanProductReadPlatformService.retrieveLoanProduct(allowedProductId); .getId();
LoanProductData selectedProduct = this.loanProductReadPlatformService
.retrieveLoanProduct(allowedProductId);
workflowData.setProductId(selectedProduct.getId()); workflowData.setProductId(selectedProduct.getId());
workflowData.setProductName(selectedProduct.getName()); workflowData.setProductName(selectedProduct.getName());
workflowData.setSelectedProduct(selectedProduct); workflowData.setSelectedProduct(selectedProduct);
} else { } else {
LoanProductData selectedProduct = findLoanProductById(loanProducts, productId); LoanProductData selectedProduct = findLoanProductById(loanProducts,
productId);
workflowData.setProductId(selectedProduct.getId()); workflowData.setProductId(selectedProduct.getId());
workflowData.setProductName(selectedProduct.getName()); workflowData.setProductName(selectedProduct.getName());
workflowData.setSelectedProduct(selectedProduct); workflowData.setSelectedProduct(selectedProduct);
} }
ClientData clientAccount = this.clientReadPlatformService.retrieveIndividualClient(clientId); ClientData clientAccount = this.clientReadPlatformService
.retrieveIndividualClient(clientId);
workflowData.setClientId(clientAccount.getId()); workflowData.setClientId(clientAccount.getId());
workflowData.setClientName(clientAccount.getDisplayName()); workflowData.setClientName(clientAccount.getDisplayName());
@@ -133,11 +141,14 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService {
return workflowData; return workflowData;
} }
private LoanProductData findLoanProductById(Collection<LoanProductLookup> loanProducts, Long productId) { private LoanProductData findLoanProductById(
LoanProductData match = this.loanProductReadPlatformService.retrieveNewLoanProductDetails(); Collection<LoanProductLookup> loanProducts, Long productId) {
LoanProductData match = this.loanProductReadPlatformService
.retrieveNewLoanProductDetails();
for (LoanProductLookup loanProductLookup : loanProducts) { for (LoanProductLookup loanProductLookup : loanProducts) {
if (loanProductLookup.getId().equals(productId)) { if (loanProductLookup.getId().equals(productId)) {
match = this.loanProductReadPlatformService.retrieveLoanProduct(loanProductLookup.getId()); match = this.loanProductReadPlatformService
.retrieveLoanProduct(loanProductLookup.getId());
break; break;
} }
} }
@@ -175,7 +186,8 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService {
possibleNextRepaymentAmount.getAmount()); possibleNextRepaymentAmount.getAmount());
LoanTransactionData newRepaymentDetails = new LoanTransactionData(); LoanTransactionData newRepaymentDetails = new LoanTransactionData();
newRepaymentDetails.setTransactionType(LoanEnumerations.transactionType(LoanTransactionType.REPAYMENT)); newRepaymentDetails.setTransactionType(LoanEnumerations
.transactionType(LoanTransactionType.REPAYMENT));
newRepaymentDetails.setDate(earliestUnpaidInstallmentDate); newRepaymentDetails.setDate(earliestUnpaidInstallmentDate);
newRepaymentDetails.setTotal(possibleNextRepayment); newRepaymentDetails.setTotal(possibleNextRepayment);
@@ -211,7 +223,8 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService {
totalOutstanding.getAmount()); totalOutstanding.getAmount());
LoanTransactionData newWaiverDetails = new LoanTransactionData(); LoanTransactionData newWaiverDetails = new LoanTransactionData();
newWaiverDetails.setTransactionType(LoanEnumerations.transactionType(LoanTransactionType.WAIVED)); newWaiverDetails.setTransactionType(LoanEnumerations
.transactionType(LoanTransactionType.WAIVED));
newWaiverDetails.setDate(new LocalDate()); newWaiverDetails.setDate(new LocalDate());
newWaiverDetails.setTotal(totalOutstandingData); newWaiverDetails.setTotal(totalOutstandingData);
@@ -219,7 +232,8 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService {
} }
@Override @Override
public LoanTransactionData retrieveLoanTransactionDetails(final Long loanId, final Long transactionId) { public LoanTransactionData retrieveLoanTransactionDetails(
final Long loanId, final Long transactionId) {
context.authenticatedUser(); context.authenticatedUser();
@@ -236,7 +250,8 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService {
throw new CurrencyNotFoundException(currencyCode); throw new CurrencyNotFoundException(currencyCode);
} }
LoanTransaction transaction = this.loanTransactionRepository.findOne(transactionId); LoanTransaction transaction = this.loanTransactionRepository
.findOne(transactionId);
if (transaction == null) { if (transaction == null) {
throw new LoanTransactionNotFoundException(transactionId); throw new LoanTransactionNotFoundException(transactionId);
} }
@@ -252,11 +267,136 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService {
LocalDate date = transaction.getTransactionDate(); LocalDate date = transaction.getTransactionDate();
LoanTransactionData loanRepaymentData = new LoanTransactionData(); LoanTransactionData loanRepaymentData = new LoanTransactionData();
loanRepaymentData.setTransactionType(LoanEnumerations.transactionType(transaction.getTypeOf())); loanRepaymentData.setTransactionType(LoanEnumerations
.transactionType(transaction.getTypeOf()));
loanRepaymentData.setId(transactionId); loanRepaymentData.setId(transactionId);
loanRepaymentData.setTotal(total); loanRepaymentData.setTotal(total);
loanRepaymentData.setDate(date); loanRepaymentData.setDate(date);
return loanRepaymentData; return loanRepaymentData;
} }
private static final class LoanMapper implements
RowMapper<LoanBasicDetailsData> {
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);
}
}
} }