Merge pull request #195 from vishwasbabu/mifosx-228

Update loan code to handle Collateral Value
This commit is contained in:
Vishwas Babu A J
2013-03-28 01:49:38 -07:00
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, super("error.msg.loan.collateral.", "Collateral with Id " + collateralId + " does not exist for loan with Id " + loanId, loanId,
collateralId); 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.CodeValue;
import org.mifosplatform.infrastructure.codes.domain.CodeValueRepositoryWrapper; import org.mifosplatform.infrastructure.codes.domain.CodeValueRepositoryWrapper;
import org.mifosplatform.infrastructure.core.serialization.FromJsonHelper; 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.LoanCollateral;
import org.mifosplatform.portfolio.collateral.domain.LoanCollateralRepository; import org.mifosplatform.portfolio.collateral.domain.LoanCollateralRepository;
import org.mifosplatform.portfolio.collateral.exception.CollateralNotFoundException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -62,7 +62,7 @@ public class CollateralAssembler {
collateralItems.add(LoanCollateral.from(collateralType, value, description)); collateralItems.add(LoanCollateral.from(collateralType, value, description));
} else { } else {
LoanCollateral loanCollateralItem = this.loanCollateralRepository.findOne(id); 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); loanCollateralItem.assembleFrom(collateralType, value, description);
@@ -250,10 +250,11 @@ public final class LoanApplicationCommandFromApiJsonHelper {
final String collateralParameterName = "collateral"; final String collateralParameterName = "collateral";
if (element.isJsonObject() && fromApiJsonHelper.parameterExists(collateralParameterName, element)) { if (element.isJsonObject() && fromApiJsonHelper.parameterExists(collateralParameterName, element)) {
final JsonObject topLevelJsonElement = element.getAsJsonObject(); final JsonObject topLevelJsonElement = element.getAsJsonObject();
final Locale locale = fromApiJsonHelper.extractLocaleParameter(topLevelJsonElement);
if (topLevelJsonElement.get("collateral").isJsonArray()) { if (topLevelJsonElement.get("collateral").isJsonArray()) {
final Type collateralParameterTypeOfMap = new TypeToken<Map<String, Object>>() {}.getType(); 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(); final JsonArray array = topLevelJsonElement.get("collateral").getAsJsonArray();
for (int i = 1; i <= array.size(); i++) { for (int i = 1; i <= array.size(); i++) {
final JsonObject collateralItemElement = array.get(i - 1).getAsJsonObject(); 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() baseDataValidator.reset().parameter("collateral").parameterAtIndexArray("type", i).value(collateralTypeId).notNull()
.integerGreaterThanZero(); .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); final String description = fromApiJsonHelper.extractStringNamed("description", collateralItemElement);
baseDataValidator.reset().parameter("collateral").parameterAtIndexArray("description", i).value(description).notBlank() baseDataValidator.reset().parameter("collateral").parameterAtIndexArray("description", i).value(description).notBlank()
.notExceedingLengthOf(500); .notExceedingLengthOf(500);
@@ -527,10 +532,11 @@ public final class LoanApplicationCommandFromApiJsonHelper {
final String collateralParameterName = "collateral"; final String collateralParameterName = "collateral";
if (element.isJsonObject() && fromApiJsonHelper.parameterExists(collateralParameterName, element)) { if (element.isJsonObject() && fromApiJsonHelper.parameterExists(collateralParameterName, element)) {
final JsonObject topLevelJsonElement = element.getAsJsonObject(); final JsonObject topLevelJsonElement = element.getAsJsonObject();
final Locale locale = fromApiJsonHelper.extractLocaleParameter(topLevelJsonElement);
if (topLevelJsonElement.get("collateral").isJsonArray()) { if (topLevelJsonElement.get("collateral").isJsonArray()) {
final Type collateralParameterTypeOfMap = new TypeToken<Map<String, Object>>() {}.getType(); 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(); final JsonArray array = topLevelJsonElement.get("collateral").getAsJsonArray();
for (int i = 1; i <= array.size(); i++) { for (int i = 1; i <= array.size(); i++) {
final JsonObject collateralItemElement = array.get(i - 1).getAsJsonObject(); 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() baseDataValidator.reset().parameter("collateral").parameterAtIndexArray("type", i).value(collateralTypeId).notNull()
.integerGreaterThanZero(); .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); final String description = fromApiJsonHelper.extractStringNamed("description", collateralItemElement);
baseDataValidator.reset().parameter("collateral").parameterAtIndexArray("description", i).value(description).notBlank() baseDataValidator.reset().parameter("collateral").parameterAtIndexArray("description", i).value(description).notBlank()
.notExceedingLengthOf(500); .notExceedingLengthOf(500);