Update loan code to handle Collateral Value

This commit is contained in:
Vishwas Babu AJ
2013-03-28 14:18:02 +05:30
parent c0464a571b
commit 06a59b95f2
4 changed files with 19 additions and 30 deletions
@@ -1,25 +0,0 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.mifosplatform.portfolio.charge.exception;
import org.mifosplatform.infrastructure.core.exception.AbstractPlatformDomainRuleException;
import org.mifosplatform.infrastructure.core.exception.AbstractPlatformResourceNotFoundException;
/**
* {@link AbstractPlatformDomainRuleException} thrown when loan collateral does
* not exist.
*/
public class LoanCollateralNotFoundException extends AbstractPlatformResourceNotFoundException {
public LoanCollateralNotFoundException(final Long id) {
super("error.msg.loanCollateral.id.invalid", "Loan collateral with identifier " + id + " does not exist", id);
}
public LoanCollateralNotFoundException(final Long id, final Long loanId) {
super("error.msg.loanCollateral.id.invalid.for.given.loan", "Loan collateral with identifier " + id + " does not exist for loan "
+ loanId, id, loanId);
}
}
@@ -16,4 +16,8 @@ public class CollateralNotFoundException extends AbstractPlatformResourceNotFoun
super("error.msg.loan.collateral.", "Collateral with Id " + collateralId + " does not exist for loan with Id " + loanId, loanId,
collateralId);
}
public CollateralNotFoundException(final Long id) {
super("error.msg.loan.collateral.id.invalid", "Loan collateral with identifier " + id + " does not exist", id);
}
}
@@ -13,9 +13,9 @@ import java.util.Set;
import org.mifosplatform.infrastructure.codes.domain.CodeValue;
import org.mifosplatform.infrastructure.codes.domain.CodeValueRepositoryWrapper;
import org.mifosplatform.infrastructure.core.serialization.FromJsonHelper;
import org.mifosplatform.portfolio.charge.exception.LoanCollateralNotFoundException;
import org.mifosplatform.portfolio.collateral.domain.LoanCollateral;
import org.mifosplatform.portfolio.collateral.domain.LoanCollateralRepository;
import org.mifosplatform.portfolio.collateral.exception.CollateralNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -62,7 +62,7 @@ public class CollateralAssembler {
collateralItems.add(LoanCollateral.from(collateralType, value, description));
} else {
LoanCollateral loanCollateralItem = this.loanCollateralRepository.findOne(id);
if (loanCollateralItem == null) { throw new LoanCollateralNotFoundException(id); }
if (loanCollateralItem == null) { throw new CollateralNotFoundException(id); }
loanCollateralItem.assembleFrom(collateralType, value, description);
@@ -213,7 +213,7 @@ public final class LoanApplicationCommandFromApiJsonHelper {
final Long calendarId = fromApiJsonHelper.extractLongNamed(calendarIdParameterName, element);
baseDataValidator.reset().parameter(calendarIdParameterName).value(calendarId).ignoreIfNull().integerGreaterThanZero();
}
// charges
final String chargesParameterName = "charges";
if (element.isJsonObject() && fromApiJsonHelper.parameterExists(chargesParameterName, element)) {
@@ -250,10 +250,11 @@ public final class LoanApplicationCommandFromApiJsonHelper {
final String collateralParameterName = "collateral";
if (element.isJsonObject() && fromApiJsonHelper.parameterExists(collateralParameterName, element)) {
final JsonObject topLevelJsonElement = element.getAsJsonObject();
final Locale locale = fromApiJsonHelper.extractLocaleParameter(topLevelJsonElement);
if (topLevelJsonElement.get("collateral").isJsonArray()) {
final Type collateralParameterTypeOfMap = new TypeToken<Map<String, Object>>() {}.getType();
final Set<String> supportedParameters = new HashSet<String>(Arrays.asList("id", "type", "description"));
final Set<String> supportedParameters = new HashSet<String>(Arrays.asList("id", "type", "value", "description"));
final JsonArray array = topLevelJsonElement.get("collateral").getAsJsonArray();
for (int i = 1; i <= array.size(); i++) {
final JsonObject collateralItemElement = array.get(i - 1).getAsJsonObject();
@@ -265,6 +266,10 @@ public final class LoanApplicationCommandFromApiJsonHelper {
baseDataValidator.reset().parameter("collateral").parameterAtIndexArray("type", i).value(collateralTypeId).notNull()
.integerGreaterThanZero();
final BigDecimal collateralValue = fromApiJsonHelper.extractBigDecimalNamed("value", collateralItemElement, locale);
baseDataValidator.reset().parameter("collateral").parameterAtIndexArray("value", i).value(collateralValue)
.ignoreIfNull().positiveAmount();
final String description = fromApiJsonHelper.extractStringNamed("description", collateralItemElement);
baseDataValidator.reset().parameter("collateral").parameterAtIndexArray("description", i).value(description).notBlank()
.notExceedingLengthOf(500);
@@ -527,10 +532,11 @@ public final class LoanApplicationCommandFromApiJsonHelper {
final String collateralParameterName = "collateral";
if (element.isJsonObject() && fromApiJsonHelper.parameterExists(collateralParameterName, element)) {
final JsonObject topLevelJsonElement = element.getAsJsonObject();
final Locale locale = fromApiJsonHelper.extractLocaleParameter(topLevelJsonElement);
if (topLevelJsonElement.get("collateral").isJsonArray()) {
final Type collateralParameterTypeOfMap = new TypeToken<Map<String, Object>>() {}.getType();
final Set<String> supportedParameters = new HashSet<String>(Arrays.asList("id", "type", "description"));
final Set<String> supportedParameters = new HashSet<String>(Arrays.asList("id", "type", "value", "description"));
final JsonArray array = topLevelJsonElement.get("collateral").getAsJsonArray();
for (int i = 1; i <= array.size(); i++) {
final JsonObject collateralItemElement = array.get(i - 1).getAsJsonObject();
@@ -542,6 +548,10 @@ public final class LoanApplicationCommandFromApiJsonHelper {
baseDataValidator.reset().parameter("collateral").parameterAtIndexArray("type", i).value(collateralTypeId).notNull()
.integerGreaterThanZero();
final BigDecimal collateralValue = fromApiJsonHelper.extractBigDecimalNamed("value", collateralItemElement, locale);
baseDataValidator.reset().parameter("collateral").parameterAtIndexArray("value", i).value(collateralValue)
.ignoreIfNull().positiveAmount();
final String description = fromApiJsonHelper.extractStringNamed("description", collateralItemElement);
baseDataValidator.reset().parameter("collateral").parameterAtIndexArray("description", i).value(description).notBlank()
.notExceedingLengthOf(500);