From 43a40ab14ce6bc8e912e4ea75a54d7dd3449f5c1 Mon Sep 17 00:00:00 2001 From: Keith Woodlock Date: Fri, 10 Aug 2012 12:43:45 +0100 Subject: [PATCH] integrate deposit account with deposit product concept. --- .../api/DepositAccountsApiResource.java | 6 +++--- .../api/commands/DepositAccountCommand.java | 10 +++++----- .../platform/api/data/DepositAccountData.java | 12 ++++++------ .../ApiDataConversionServiceImpl.java | 6 +++--- .../platform/deposit/domain/DepositProduct.java | 6 +++++- .../domain/DepositProductRelatedDetail.java | 17 ++++++++++++++++- .../InterestRateOutsideRangeException.java | 14 ++++++++++++++ .../platform/saving/domain/DepositAccount.java | 15 ++++++++++----- .../service/DepositAccountAssembler.java | 14 ++++++++++++-- .../service/DepositAccountCommandValidator.java | 8 ++++---- 10 files changed, 78 insertions(+), 30 deletions(-) create mode 100644 mifosng-provider/src/main/java/org/mifosng/platform/exceptions/InterestRateOutsideRangeException.java diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/DepositAccountsApiResource.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/DepositAccountsApiResource.java index 5dcf6f4f5..56191ac11 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/DepositAccountsApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/DepositAccountsApiResource.java @@ -82,7 +82,7 @@ public class DepositAccountsApiResource { Set typicalResponseParameters = new HashSet( Arrays.asList("createdOn", "lastModifedOn", "id", "clientId", "clientName", "productId", "productName", - "currency", "deposit", "interestRate") + "currency", "deposit", "maturityInterestRate") ); Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); @@ -105,7 +105,7 @@ public class DepositAccountsApiResource { Set typicalResponseParameters = new HashSet( Arrays.asList("createdOn", "lastModifedOn", "id", "clientId", "clientName", "productId", "productName", - "currency", "deposit", "interestRate") + "currency", "deposit", "maturityInterestRate") ); Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); @@ -135,7 +135,7 @@ public class DepositAccountsApiResource { Set typicalResponseParameters = new HashSet( Arrays.asList("createdOn", "lastModifedOn", "id", "clientId", "clientName", "productId", "productName", - "currency", "deposit", "interestRate", "currencyOptions") + "currency", "deposit", "maturityInterestRate", "currencyOptions") ); Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/DepositAccountCommand.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/DepositAccountCommand.java index 5de7aa136..abcc747a6 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/DepositAccountCommand.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/commands/DepositAccountCommand.java @@ -16,7 +16,7 @@ public class DepositAccountCommand { private final String currencyCode; private final Integer digitsAfterDecimal; private final BigDecimal depositAmount; - private final BigDecimal interestRate; + private final BigDecimal maturityInterestRate; private final Integer termInMonths; private final Set modifiedParameters; @@ -39,7 +39,7 @@ public class DepositAccountCommand { this.currencyCode = currencyCode; this.digitsAfterDecimal = digitsAfterDecimal; this.depositAmount = depositAmount; - this.interestRate = interestRate; + this.maturityInterestRate = interestRate; this.termInMonths = termInMonths; this.modifiedParameters = modifiedParameters; @@ -72,9 +72,9 @@ public class DepositAccountCommand { public BigDecimal getDepositAmount() { return depositAmount; } - - public BigDecimal getInterestRate() { - return interestRate; + + public BigDecimal getMaturityInterestRate() { + return maturityInterestRate; } public Integer getTermInMonths() { diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/DepositAccountData.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/DepositAccountData.java index 151e4a193..d65c89713 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/DepositAccountData.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/DepositAccountData.java @@ -21,7 +21,7 @@ public class DepositAccountData { private final CurrencyData currency; private final BigDecimal deposit; - private final BigDecimal interestRate; + private final BigDecimal maturityInterestRate; private final DateTime createdOn; private final DateTime lastModifedOn; @@ -38,7 +38,7 @@ public class DepositAccountData { this.productName = null; this.currency = null; this.deposit = null; - this.interestRate = null; + this.maturityInterestRate = null; this.currencyOptions = new ArrayList(); } @@ -52,7 +52,7 @@ public class DepositAccountData { this.productName = account.getProductName(); this.currency = account.getCurrency(); this.deposit = account.getDeposit(); - this.interestRate = account.getInterestRate(); + this.maturityInterestRate = account.getMaturityInterestRate(); this.currencyOptions = currencies; } @@ -75,7 +75,7 @@ public class DepositAccountData { this.productName = productName; this.currency = currency; this.deposit = deposit; - this.interestRate=interestRate; + this.maturityInterestRate=interestRate; this.currencyOptions = new ArrayList(); } @@ -107,8 +107,8 @@ public class DepositAccountData { return deposit; } - public BigDecimal getInterestRate() { - return interestRate; + public BigDecimal getMaturityInterestRate() { + return maturityInterestRate; } public DateTime getCreatedOn() { diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java index 82c359e5d..59ee2be60 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java @@ -1493,7 +1493,7 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService { Map requestMap = gsonConverter.fromJson(json, typeOfMap); Set supportedParams = new HashSet( - Arrays.asList("clientId", "productId", "externalId", "currencyCode", "digitsAfterDecimal", "depositAmount", "interestRate", "termInMonths", "locale") + Arrays.asList("clientId", "productId", "externalId", "currencyCode", "digitsAfterDecimal", "depositAmount", "maturityInterestRate", "termInMonths", "locale") ); checkForUnsupportedParameters(requestMap, supportedParams); Set modifiedParameters = new HashSet(); @@ -1504,7 +1504,7 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService { String currencyCode=extractStringParameter("currencyCode", requestMap,modifiedParameters); Integer digitsAfterDecimalValue = extractIntegerParameter("digitsAfterDecimal", requestMap, modifiedParameters); BigDecimal depositAmount=extractBigDecimalParameter("depositAmount", requestMap, modifiedParameters); - BigDecimal interestRate = extractBigDecimalParameter("interestRate", requestMap, modifiedParameters); + BigDecimal interestRate = extractBigDecimalParameter("maturityInterestRate", requestMap, modifiedParameters); Integer termInMonths = extractIntegerParameter("termInMonths", requestMap, modifiedParameters); // isRenewable, isPreClosureAllowed, expectedDepositDate, pojectMaturityDate, projectedInterestAccrued @@ -1636,7 +1636,7 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService { Set supportedParameters = new HashSet( Arrays.asList("createdOn", "lastModifedOn", "id", "clientId", "clientName", "productId", "productName", - "currency", "deposit", "interestRate", "currencyOptions") + "currency", "deposit", "maturityInterestRate", "currencyOptions") ); final Set parameterNamesToSkip = new HashSet(); diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/deposit/domain/DepositProduct.java b/mifosng-provider/src/main/java/org/mifosng/platform/deposit/domain/DepositProduct.java index aa60f2b1d..60f70a044 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/deposit/domain/DepositProduct.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/deposit/domain/DepositProduct.java @@ -27,7 +27,7 @@ public class DepositProduct extends AbstractAuditableCustom { private boolean deleted = false; @Embedded - DepositProductRelatedDetail depositProductRelatedDetail; + private DepositProductRelatedDetail depositProductRelatedDetail; protected DepositProduct(){ this.name = null; @@ -115,4 +115,8 @@ public class DepositProduct extends AbstractAuditableCustom { this.depositProductRelatedDetail.update(command); } + + public void validateInterestRateInRange(final BigDecimal interestRate) { + this.depositProductRelatedDetail.validateInterestRateInRange(interestRate); + } } diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/deposit/domain/DepositProductRelatedDetail.java b/mifosng-provider/src/main/java/org/mifosng/platform/deposit/domain/DepositProductRelatedDetail.java index ed52e8b50..1b76fc9fc 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/deposit/domain/DepositProductRelatedDetail.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/deposit/domain/DepositProductRelatedDetail.java @@ -8,6 +8,7 @@ import javax.persistence.Embedded; import org.mifosng.platform.api.commands.DepositProductCommand; import org.mifosng.platform.currency.domain.MonetaryCurrency; +import org.mifosng.platform.exceptions.InterestRateOutsideRangeException; @Embeddable public class DepositProductRelatedDetail { @@ -165,4 +166,18 @@ import org.mifosng.platform.currency.domain.MonetaryCurrency; } -} + public void validateInterestRateInRange(final BigDecimal interestRate) { + boolean inRange = true; + if (interestRate.compareTo(this.maturityMinInterestRate) < 0) { + inRange = false; + } + + if (this.maturityMaxInterestRate.compareTo(interestRate) < 0) { + inRange = false; + } + + if (!inRange) { + throw new InterestRateOutsideRangeException(interestRate, this.maturityMinInterestRate, this.maturityMaxInterestRate); + } + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/exceptions/InterestRateOutsideRangeException.java b/mifosng-provider/src/main/java/org/mifosng/platform/exceptions/InterestRateOutsideRangeException.java new file mode 100644 index 000000000..ca7d06b57 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/exceptions/InterestRateOutsideRangeException.java @@ -0,0 +1,14 @@ +package org.mifosng.platform.exceptions; + +import java.math.BigDecimal; + +/** + * Exception thrown when an attempt is made update the parent of a root office. + */ +public class InterestRateOutsideRangeException extends AbstractPlatformDomainRuleException { + + public InterestRateOutsideRangeException(final BigDecimal interestRate, final BigDecimal rangeMin, final BigDecimal rangeMax) { + super("error.msg.deposit.account.maturityInterestRate.outside.of.allowed.range", "Interest rate of " + interestRate.toPlainString() + " is outside of allowed range of [" + + rangeMin.toPlainString() + ", " + rangeMax.toPlainString() + "].", interestRate, rangeMin, rangeMax); + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/saving/domain/DepositAccount.java b/mifosng-provider/src/main/java/org/mifosng/platform/saving/domain/DepositAccount.java index 20069917f..b2cf5484c 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/saving/domain/DepositAccount.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/saving/domain/DepositAccount.java @@ -14,6 +14,7 @@ import org.mifosng.platform.api.commands.DepositAccountCommand; import org.mifosng.platform.client.domain.Client; import org.mifosng.platform.currency.domain.MonetaryCurrency; import org.mifosng.platform.currency.domain.Money; +import org.mifosng.platform.deposit.domain.DepositProduct; import org.mifosng.platform.infrastructure.AbstractAuditableCustom; import org.mifosng.platform.user.domain.AppUser; @@ -25,9 +26,9 @@ public class DepositAccount extends AbstractAuditableCustom { @JoinColumn(name = "client_id", nullable = false) private Client client; -// @ManyToOne -// @JoinColumn(name = "product_id") -// private final DepositAccountProduct depositAccountProduct; + @ManyToOne + @JoinColumn(name = "product_id") + private final DepositProduct product; @Column(name = "external_id") private String externalId; @@ -57,15 +58,19 @@ public class DepositAccount extends AbstractAuditableCustom { private boolean deleted = false; protected DepositAccount() { - // + this.product = null; } - public DepositAccount(final Client client, final String externalId, final Money deposit, final BigDecimal interestRate, final Integer termInMonths) { + public DepositAccount( + final Client client, final DepositProduct product, + final String externalId, final Money deposit, final BigDecimal interestRate, final Integer termInMonths) { this.client = client; + this.product = product; this.externalId = externalId; this.currency = deposit.getCurrency(); this.depositAmount = deposit.getAmount(); this.interestRate = interestRate; + product.validateInterestRateInRange(interestRate); this.tenureInMonths = termInMonths; this.preClosureInterestRate = BigDecimal.ZERO; } diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountAssembler.java b/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountAssembler.java index fc84e0cfb..62651d5bb 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountAssembler.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountAssembler.java @@ -5,7 +5,10 @@ import org.mifosng.platform.client.domain.Client; import org.mifosng.platform.client.domain.ClientRepository; import org.mifosng.platform.currency.domain.MonetaryCurrency; import org.mifosng.platform.currency.domain.Money; +import org.mifosng.platform.deposit.domain.DepositProduct; +import org.mifosng.platform.deposit.domain.DepositProductRepository; import org.mifosng.platform.exceptions.ClientNotFoundException; +import org.mifosng.platform.exceptions.DepositProductNotFoundException; import org.mifosng.platform.saving.domain.DepositAccount; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -17,10 +20,12 @@ import org.springframework.stereotype.Service; public class DepositAccountAssembler { private final ClientRepository clientRepository; + private final DepositProductRepository depositProductRepository; @Autowired - public DepositAccountAssembler(final ClientRepository clientRepository) { + public DepositAccountAssembler(final ClientRepository clientRepository, final DepositProductRepository depositProductRepository) { this.clientRepository = clientRepository; + this.depositProductRepository = depositProductRepository; } public DepositAccount assembleFrom(final DepositAccountCommand command) { @@ -28,11 +33,16 @@ public class DepositAccountAssembler { Client client = this.clientRepository.findOne(command.getClientId()); if (client == null || client.isDeleted()) { throw new ClientNotFoundException(command.getClientId()); + } + + DepositProduct product = this.depositProductRepository.findOne(command.getProductId()); + if (product == null || product.isDeleted()) { + throw new DepositProductNotFoundException(command.getProductId()); } MonetaryCurrency currency = new MonetaryCurrency(command.getCurrencyCode(), command.getDigitsAfterDecimal()); Money deposit = Money.of(currency, command.getDepositAmount()); - return new DepositAccount(client, command.getExternalId(), deposit, command.getInterestRate(), command.getTermInMonths()); + return new DepositAccount(client, product, command.getExternalId(), deposit, command.getMaturityInterestRate(), command.getTermInMonths()); } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountCommandValidator.java b/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountCommandValidator.java index 8ac9dc8bd..dfc2a8b98 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountCommandValidator.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/savingproduct/service/DepositAccountCommandValidator.java @@ -30,13 +30,13 @@ public class DepositAccountCommandValidator { baseDataValidator.reset().parameter("currencyCode").value(command.getCurrencyCode()).ignoreIfNull().notBlank(); baseDataValidator.reset().parameter("digitsAfterDecimal").value(command.getDigitsAfterDecimal()).ignoreIfNull().notNull().inMinMaxRange(0, 6); baseDataValidator.reset().parameter("depositAmount").value(command.getDepositAmount()).ignoreIfNull().notNull().integerGreaterThanZero(); - baseDataValidator.reset().parameter("interestRate").value(command.getInterestRate()).ignoreIfNull().notNull().zeroOrPositiveAmount(); + baseDataValidator.reset().parameter("maturityInterestRate").value(command.getMaturityInterestRate()).ignoreIfNull().notNull().zeroOrPositiveAmount(); baseDataValidator.reset().parameter("termInMonths").value(command.getTermInMonths()).ignoreIfNull().notNull().integerGreaterThanZero(); baseDataValidator.reset().anyOfNotNull( command.getClientId(), command.getProductId(), command.getExternalId(), - command.getCurrencyCode(),command.getDigitsAfterDecimal(), - command.getDepositAmount(), command.getInterestRate(),command.getTermInMonths()); + command.getCurrencyCode(), command.getDigitsAfterDecimal(), + command.getDepositAmount(), command.getMaturityInterestRate(), command.getTermInMonths()); if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors); @@ -56,7 +56,7 @@ public class DepositAccountCommandValidator { baseDataValidator.reset().parameter("currencyCode").value(command.getCurrencyCode()).notBlank(); baseDataValidator.reset().parameter("digitsAfterDecimal").value(command.getDigitsAfterDecimal()).notNull().inMinMaxRange(0, 6); baseDataValidator.reset().parameter("depositAmount").value(command.getDepositAmount()).notNull().zeroOrPositiveAmount(); - baseDataValidator.reset().parameter("interestRate").value(command.getInterestRate()).notNull().zeroOrPositiveAmount(); + baseDataValidator.reset().parameter("maturityInterestRate").value(command.getMaturityInterestRate()).notNull().zeroOrPositiveAmount(); baseDataValidator.reset().parameter("termInMonths").value(command.getTermInMonths()).notNull().integerGreaterThanZero(); if (!dataValidationErrors.isEmpty()) {