cleaning up collateral api's

This commit is contained in:
Vishwas Babu AJ
2013-03-28 09:44:42 +05:30
parent 69d228a502
commit c0464a571b
15 changed files with 201 additions and 48 deletions
@@ -9,5 +9,6 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface CodeValueRepository extends JpaRepository<CodeValue, Long>, JpaSpecificationExecutor<CodeValue> {
// no added behaviour
CodeValue findByCodeNameAndId(String codeName, Long id);
}
@@ -12,8 +12,9 @@ import org.springframework.stereotype.Service;
/**
* <p>
* Wrapper for {@link CodeValueRepository} that is responsible for checking if
* {@link CodeValue} is returned when using <code>findOne</code> repository
* method and throwing an appropriate not found exception.
* {@link CodeValue} is returned when using <code>findOne</code> and
* <code>findByCodeNameAndId</code> repository methods and throwing an
* appropriate not found exception.
* </p>
*
* <p>
@@ -36,4 +37,10 @@ public class CodeValueRepositoryWrapper {
if (codeValue == null) { throw new CodeValueNotFoundException(id); }
return codeValue;
}
public CodeValue findOneByCodeNameAndIdWithNotFoundDetection(final String codeName, final Long id) {
final CodeValue codeValue = this.repository.findByCodeNameAndId(codeName, id);
if (codeValue == null) { throw new CodeValueNotFoundException(codeName, id); }
return codeValue;
}
}
@@ -15,4 +15,9 @@ public class CodeValueNotFoundException extends AbstractPlatformResourceNotFound
public CodeValueNotFoundException(final Long id) {
super("error.msg.codevalue.id.invalid", "Code value with identifier " + id + " does not exist", id);
}
public CodeValueNotFoundException(final String codeName, final Long id) {
super("error.msg.codevalue.codename.id.combination.invalid", "Code value with identifier " + id
+ " does not exist for a code with name " + codeName, id, codeName);
}
}
@@ -10,6 +10,8 @@ import java.util.Set;
public class CollateralApiConstants {
public static final String COLLATERAL_CODE_NAME = "LoanCollateral";
/***
* Enum of all parameters passed in while creating/updating a collateral
***/
@@ -45,7 +45,7 @@ import org.springframework.stereotype.Component;
public class CollateralsApiResource {
private static final Set<String> RESPONSE_DATA_PARAMETERS = new HashSet<String>(Arrays.asList("id", "type", "value", "description",
"allowedCollateralTypes"));
"allowedCollateralTypes", "currency"));
private final String resourceNameForPermission = "COLLATERAL";
@@ -111,7 +111,8 @@ public class CollateralsApiResource {
final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters());
if (settings.isTemplate()) {
final Collection<CodeValueData> codeValues = this.codeValueReadPlatformService.retrieveCodeValuesByCode("LoanCollateral");
final Collection<CodeValueData> codeValues = this.codeValueReadPlatformService
.retrieveCodeValuesByCode(CollateralApiConstants.COLLATERAL_CODE_NAME);
CollateralData = CollateralData.template(CollateralData, codeValues);
}
@@ -12,6 +12,7 @@ import java.util.List;
import org.mifosplatform.infrastructure.core.data.ApiParameterError;
import org.mifosplatform.infrastructure.core.data.DataValidatorBuilder;
import org.mifosplatform.infrastructure.core.exception.PlatformApiDataValidationException;
import org.mifosplatform.portfolio.collateral.api.CollateralApiConstants.COLLATERAL_JSON_INPUT_PARAMS;
/**
* Immutable command for creating or updating details of a Collateral.
@@ -45,9 +46,12 @@ public class CollateralCommand {
DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("collateral");
baseDataValidator.reset().parameter("collateralTypeId").value(this.collateralTypeId).notNull().integerGreaterThanZero();
baseDataValidator.reset().parameter("value").value(this.value).ignoreIfNull().positiveAmount();
baseDataValidator.reset().parameter("description").value(this.description).ignoreIfNull().notExceedingLengthOf(500);
baseDataValidator.reset().parameter(COLLATERAL_JSON_INPUT_PARAMS.COLLATERAL_TYPE_ID.getValue()).value(this.collateralTypeId)
.notNull().integerGreaterThanZero();
baseDataValidator.reset().parameter(COLLATERAL_JSON_INPUT_PARAMS.VALUE.getValue()).value(this.value).ignoreIfNull()
.positiveAmount();
baseDataValidator.reset().parameter(COLLATERAL_JSON_INPUT_PARAMS.DESCRIPTION.getValue()).value(this.description).ignoreIfNull()
.notExceedingLengthOf(500);
if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
"Validation errors exist.", dataValidationErrors); }
@@ -58,9 +62,12 @@ public class CollateralCommand {
DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("collateral");
baseDataValidator.reset().parameter("collateralTypeId").value(this.collateralTypeId).ignoreIfNull().integerGreaterThanZero();
baseDataValidator.reset().parameter("value").value(this.value).ignoreIfNull().positiveAmount();
baseDataValidator.reset().parameter("description").value(this.description).ignoreIfNull().notExceedingLengthOf(500);
baseDataValidator.reset().parameter(COLLATERAL_JSON_INPUT_PARAMS.COLLATERAL_TYPE_ID.getValue()).value(this.collateralTypeId)
.ignoreIfNull().integerGreaterThanZero();
baseDataValidator.reset().parameter(COLLATERAL_JSON_INPUT_PARAMS.VALUE.getValue()).value(this.value).ignoreIfNull()
.positiveAmount();
baseDataValidator.reset().parameter(COLLATERAL_JSON_INPUT_PARAMS.DESCRIPTION.getValue()).value(this.description).ignoreIfNull()
.notExceedingLengthOf(500);
if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
"Validation errors exist.", dataValidationErrors); }
@@ -9,6 +9,7 @@ import java.math.BigDecimal;
import java.util.Collection;
import org.mifosplatform.infrastructure.codes.data.CodeValueData;
import org.mifosplatform.organisation.monetary.data.CurrencyData;
/**
* Immutable data object for Collateral data.
@@ -21,33 +22,39 @@ public class CollateralData {
private final String description;
@SuppressWarnings("unused")
private final Collection<CodeValueData> allowedCollateralTypes;
private final CurrencyData currency;
public static CollateralData instance(final Long id, final CodeValueData type, final BigDecimal value, final String description) {
return new CollateralData(id, type, value, description);
public static CollateralData instance(final Long id, final CodeValueData type, final BigDecimal value, final String description,
final CurrencyData currencyData) {
return new CollateralData(id, type, value, description, currencyData);
}
public static CollateralData template(final Collection<CodeValueData> codeValues) {
return new CollateralData(null, null, null, null, codeValues);
return new CollateralData(null, null, null, null, null, codeValues);
}
private CollateralData(final Long id, final CodeValueData type, final BigDecimal value, final String description) {
private CollateralData(final Long id, final CodeValueData type, final BigDecimal value, final String description,
final CurrencyData currencyData) {
this.id = id;
this.type = type;
this.value = value;
this.description = description;
this.currency = currencyData;
this.allowedCollateralTypes = null;
}
private CollateralData(final Long id, final CodeValueData type, final BigDecimal value, final String description,
Collection<CodeValueData> allowedCollateralTypes) {
final CurrencyData currencyData, Collection<CodeValueData> allowedCollateralTypes) {
this.id = id;
this.type = type;
this.value = value;
this.description = description;
this.currency = currencyData;
this.allowedCollateralTypes = allowedCollateralTypes;
}
public CollateralData template(CollateralData collateralData, Collection<CodeValueData> codeValues) {
return new CollateralData(collateralData.id, collateralData.type, collateralData.value, collateralData.description, codeValues);
return new CollateralData(collateralData.id, collateralData.type, collateralData.value, collateralData.description,
collateralData.currency, codeValues);
}
}
@@ -21,6 +21,7 @@ import org.apache.commons.lang.builder.HashCodeBuilder;
import org.mifosplatform.infrastructure.codes.data.CodeValueData;
import org.mifosplatform.infrastructure.codes.domain.CodeValue;
import org.mifosplatform.infrastructure.core.api.JsonCommand;
import org.mifosplatform.portfolio.collateral.api.CollateralApiConstants.COLLATERAL_JSON_INPUT_PARAMS;
import org.mifosplatform.portfolio.collateral.data.CollateralData;
import org.mifosplatform.portfolio.loanaccount.domain.Loan;
import org.springframework.data.jpa.domain.AbstractPersistable;
@@ -70,8 +71,8 @@ public class LoanCollateral extends AbstractPersistable<Long> {
}
public static LoanCollateral fromJson(final Loan loan, final CodeValue collateralType, final JsonCommand command) {
final String description = command.stringValueOfParameterNamed("description");
final BigDecimal value = command.bigDecimalValueOfParameterNamed("value");
final String description = command.stringValueOfParameterNamed(COLLATERAL_JSON_INPUT_PARAMS.DESCRIPTION.getValue());
final BigDecimal value = command.bigDecimalValueOfParameterNamed(COLLATERAL_JSON_INPUT_PARAMS.VALUE.getValue());
return new LoanCollateral(loan, collateralType, value, description);
}
@@ -79,20 +80,20 @@ public class LoanCollateral extends AbstractPersistable<Long> {
final Map<String, Object> actualChanges = new LinkedHashMap<String, Object>(7);
final String collateralTypeIdParamName = "type";
final String collateralTypeIdParamName = COLLATERAL_JSON_INPUT_PARAMS.COLLATERAL_TYPE_ID.getValue();
if (command.isChangeInLongParameterNamed(collateralTypeIdParamName, this.type.getId())) {
final Long newValue = command.longValueOfParameterNamed(collateralTypeIdParamName);
actualChanges.put(collateralTypeIdParamName, newValue);
}
final String descriptionParamName = "description";
final String descriptionParamName = COLLATERAL_JSON_INPUT_PARAMS.DESCRIPTION.getValue();
if (command.isChangeInStringParameterNamed(descriptionParamName, this.description)) {
final String newValue = command.stringValueOfParameterNamed(descriptionParamName);
actualChanges.put(descriptionParamName, newValue);
this.description = StringUtils.defaultIfEmpty(newValue, null);
}
final String valueParamName = "value";
final String valueParamName = COLLATERAL_JSON_INPUT_PARAMS.VALUE.getValue();
if (command.isChangeInBigDecimalParameterNamed(valueParamName, this.value)) {
final BigDecimal newValue = command.bigDecimalValueOfParameterNamed(valueParamName);
actualChanges.put(valueParamName, newValue);
@@ -104,7 +105,7 @@ public class LoanCollateral extends AbstractPersistable<Long> {
public CollateralData toData() {
final CodeValueData typeData = this.type.toData();
return CollateralData.instance(this.getId(), typeData, this.value, this.description);
return CollateralData.instance(this.getId(), typeData, this.value, this.description, null);
}
public void setCollateralType(CodeValue type) {
@@ -9,5 +9,6 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface LoanCollateralRepository extends JpaRepository<LoanCollateral, Long>, JpaSpecificationExecutor<LoanCollateral> {
// no added behaviour
LoanCollateral findByLoanIdAndId(Long loanId, Long id);
}
@@ -0,0 +1,30 @@
/**
* 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.collateral.exception;
import org.mifosplatform.infrastructure.core.exception.AbstractPlatformDomainRuleException;
public class CollateralCannotBeCreatedException extends AbstractPlatformDomainRuleException {
/*** enum of reasons of why Loan Charge cannot be waived **/
public static enum LOAN_COLLATERAL_CANNOT_BE_CREATED_REASON {
LOAN_NOT_IN_SUBMITTED_AND_PENDING_APPROVAL_STAGE;
public String errorMessage() {
if (name().toString().equalsIgnoreCase("LOAN_NOT_IN_SUBMITTED_AND_PENDING_APPROVAL_STAGE")) { return "This collateral cannot be created as the loan it is associated with is not in submitted and pending approval stage"; }
return name().toString();
}
public String errorCode() {
if (name().toString().equalsIgnoreCase("LOAN_NOT_IN_SUBMITTED_AND_PENDING_APPROVAL_STAGE")) { return "error.msg.loan.collateral.associated.loan.not.in.submitted.and.pending.approval.stage"; }
return name().toString();
}
}
public CollateralCannotBeCreatedException(final LOAN_COLLATERAL_CANNOT_BE_CREATED_REASON reason, final Long loanId) {
super(reason.errorCode(), reason.errorMessage(), loanId);
}
}
@@ -0,0 +1,31 @@
/**
* 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.collateral.exception;
import org.mifosplatform.infrastructure.core.exception.AbstractPlatformDomainRuleException;
public class CollateralCannotBeDeletedException extends AbstractPlatformDomainRuleException {
/*** enum of reasons of why Loan Charge cannot be waived **/
public static enum LOAN_COLLATERAL_CANNOT_BE_DELETED_REASON {
LOAN_NOT_IN_SUBMITTED_AND_PENDING_APPROVAL_STAGE;
public String errorMessage() {
if (name().toString().equalsIgnoreCase("LOAN_NOT_IN_SUBMITTED_AND_PENDING_APPROVAL_STAGE")) { return "This collateral cannot be updated as the loan it is associated with is not in submitted and pending approval stage"; }
return name().toString();
}
public String errorCode() {
if (name().toString().equalsIgnoreCase("LOAN_NOT_IN_SUBMITTED_AND_PENDING_APPROVAL_STAGE")) { return "error.msg.loan.collateral.associated.loan.not.in.submitted.and.pending.approval.stage"; }
return name().toString();
}
}
public CollateralCannotBeDeletedException(final LOAN_COLLATERAL_CANNOT_BE_DELETED_REASON reason, final Long loanId,
final Long loanCollateralId) {
super(reason.errorCode(), reason.errorMessage(), loanId, loanCollateralId);
}
}
@@ -0,0 +1,30 @@
/**
* 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.collateral.exception;
import org.mifosplatform.infrastructure.core.exception.AbstractPlatformDomainRuleException;
public class CollateralCannotBeUpdatedException extends AbstractPlatformDomainRuleException {
/*** enum of reasons of why Loan Charge cannot be waived **/
public static enum LOAN_COLLATERAL_CANNOT_BE_UPDATED_REASON {
LOAN_NOT_IN_SUBMITTED_AND_PENDING_APPROVAL_STAGE;
public String errorMessage() {
if (name().toString().equalsIgnoreCase("LOAN_NOT_IN_SUBMITTED_AND_PENDING_APPROVAL_STAGE")) { return "This collateral cannot be updated as the loan it is associated with is not in submitted and pending approval stage"; }
return name().toString();
}
public String errorCode() {
if (name().toString().equalsIgnoreCase("LOAN_NOT_IN_SUBMITTED_AND_PENDING_APPROVAL_STAGE")) { return "error.msg.loan.collateral.associated.loan.not.in.submitted.and.pending.approval.stage"; }
return name().toString();
}
}
public CollateralCannotBeUpdatedException(final LOAN_COLLATERAL_CANNOT_BE_UPDATED_REASON reason, final Long loanCollateralId) {
super(reason.errorCode(), reason.errorMessage(), loanCollateralId);
}
}
@@ -23,5 +23,4 @@ public interface CollateralReadPlatformService {
CollateralData retrieveCollateral(Long loanId, Long collateralId);
CollateralData retrieveNewCollateralDetails();
}
@@ -14,6 +14,7 @@ import org.mifosplatform.infrastructure.codes.data.CodeValueData;
import org.mifosplatform.infrastructure.core.domain.JdbcSupport;
import org.mifosplatform.infrastructure.core.service.TenantAwareRoutingDataSource;
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
import org.mifosplatform.organisation.monetary.data.CurrencyData;
import org.mifosplatform.portfolio.collateral.data.CollateralData;
import org.mifosplatform.portfolio.collateral.exception.CollateralNotFoundException;
import org.mifosplatform.portfolio.loanaccount.domain.Loan;
@@ -43,9 +44,12 @@ public class CollateralReadPlatformServiceImpl implements CollateralReadPlatform
private static final class CollateralMapper implements RowMapper<CollateralData> {
private StringBuilder sqlBuilder = new StringBuilder(
"lc.id as id, lc.description as description, lc.value as value, cv.id as typeId, cv.code_value as typeName") //
"lc.id as id, lc.description as description, lc.value as value, cv.id as typeId, cv.code_value as typeName, oc.code as currencyCode, ")
.append(" oc.name as currencyName,oc.decimal_places as currencyDecimalPlaces, oc.display_symbol as currencyDisplaySymbol, oc.internationalized_name_code as currencyNameCode")
.append(" FROM m_loan_collateral lc") //
.append(" JOIN m_code_value cv on lc.type_cv_id = cv.id");
.append(" JOIN m_code_value cv on lc.type_cv_id = cv.id")//
.append(" JOIN m_loan loan on lc.loan_id = loan.id")//
.append(" JOIN m_organisation_currency oc on loan.currency_code = oc.code");
public String schema() {
return sqlBuilder.toString();
@@ -55,14 +59,23 @@ public class CollateralReadPlatformServiceImpl implements CollateralReadPlatform
public CollateralData mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException {
final Long id = rs.getLong("id");
final String name = rs.getString("description");
final String description = rs.getString("description");
final Long typeId = rs.getLong("typeId");
final BigDecimal value = JdbcSupport.getBigDecimalDefaultToNullIfZero(rs, "value");
final String typeName = rs.getString("typeName");
final CodeValueData type = CodeValueData.instance(typeId, typeName);
return CollateralData.instance(id, type, value, name);
final String currencyCode = rs.getString("currencyCode");
final String currencyName = rs.getString("currencyName");
final String currencyNameCode = rs.getString("currencyNameCode");
final String currencyDisplaySymbol = rs.getString("currencyDisplaySymbol");
final Integer currencyDecimalPlaces = JdbcSupport.getInteger(rs, "currencyDecimalPlaces");
final CurrencyData currencyData = new CurrencyData(currencyCode, currencyName, currencyDecimalPlaces, currencyDisplaySymbol,
currencyNameCode);
return CollateralData.instance(id, type, value, description, currencyData);
}
}
@@ -97,10 +110,4 @@ public class CollateralReadPlatformServiceImpl implements CollateralReadPlatform
return retrieveCollaterals(loanId);
}
@Override
public CollateralData retrieveNewCollateralDetails() {
// TODO Auto-generated method stub
return null;
}
}
@@ -9,15 +9,22 @@ import java.util.Map;
import org.mifosplatform.infrastructure.codes.domain.CodeValue;
import org.mifosplatform.infrastructure.codes.domain.CodeValueRepositoryWrapper;
import org.mifosplatform.infrastructure.codes.exception.CodeValueNotFoundException;
import org.mifosplatform.infrastructure.core.api.JsonCommand;
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
import org.mifosplatform.infrastructure.core.data.CommandProcessingResultBuilder;
import org.mifosplatform.infrastructure.core.exception.PlatformDataIntegrityException;
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
import org.mifosplatform.portfolio.collateral.api.CollateralApiConstants;
import org.mifosplatform.portfolio.collateral.api.CollateralApiConstants.COLLATERAL_JSON_INPUT_PARAMS;
import org.mifosplatform.portfolio.collateral.command.CollateralCommand;
import org.mifosplatform.portfolio.collateral.domain.LoanCollateral;
import org.mifosplatform.portfolio.collateral.domain.LoanCollateralRepository;
import org.mifosplatform.portfolio.collateral.exception.CollateralCannotBeCreatedException;
import org.mifosplatform.portfolio.collateral.exception.CollateralCannotBeCreatedException.LOAN_COLLATERAL_CANNOT_BE_CREATED_REASON;
import org.mifosplatform.portfolio.collateral.exception.CollateralCannotBeDeletedException;
import org.mifosplatform.portfolio.collateral.exception.CollateralCannotBeDeletedException.LOAN_COLLATERAL_CANNOT_BE_DELETED_REASON;
import org.mifosplatform.portfolio.collateral.exception.CollateralCannotBeUpdatedException;
import org.mifosplatform.portfolio.collateral.exception.CollateralCannotBeUpdatedException.LOAN_COLLATERAL_CANNOT_BE_UPDATED_REASON;
import org.mifosplatform.portfolio.collateral.exception.CollateralNotFoundException;
import org.mifosplatform.portfolio.collateral.serialization.CollateralCommandFromApiJsonDeserializer;
import org.mifosplatform.portfolio.loanaccount.domain.Loan;
@@ -64,11 +71,17 @@ public class CollateralWritePlatformServiceJpaRepositoryImpl implements Collater
final Loan loan = this.loanRepository.findOne(loanId);
if (loan == null) { throw new LoanNotFoundException(loanId); }
// TODO Vishwas, need to check if the codevalue is of the right code
// (same fix needed in client Identifiers)
final CodeValue collateralType = this.codeValueRepository.findOneWithNotFoundDetection(collateralCommand.getCollateralTypeId());
final CodeValue collateralType = this.codeValueRepository.findOneByCodeNameAndIdWithNotFoundDetection(
CollateralApiConstants.COLLATERAL_CODE_NAME, collateralCommand.getCollateralTypeId());
final LoanCollateral collateral = LoanCollateral.fromJson(loan, collateralType, command);
/**
* Collaterals may be added only when the loan associated with them
* are yet to be approved
**/
if (!loan.status().isSubmittedAndPendingApproval()) { throw new CollateralCannotBeCreatedException(
LOAN_COLLATERAL_CANNOT_BE_CREATED_REASON.LOAN_NOT_IN_SUBMITTED_AND_PENDING_APPROVAL_STAGE, loan.getId()); }
this.collateralRepository.save(collateral);
return new CommandProcessingResultBuilder() //
@@ -102,16 +115,20 @@ public class CollateralWritePlatformServiceJpaRepositoryImpl implements Collater
final Map<String, Object> changes = collateralForUpdate.update(command);
if (changes.containsKey("collateralTypeId")) {
// TODO Vishwas, need to check if the codevalue is of the right
// code
// (same fix needed in client Identifiers)
collateralType = this.codeValueRepository.findOneWithNotFoundDetection(collateralTypeId);
if (collateralType == null) { throw new CodeValueNotFoundException(collateralTypeId); }
collateralTypeId = collateralType.getId();
if (changes.containsKey(COLLATERAL_JSON_INPUT_PARAMS.COLLATERAL_TYPE_ID.getValue())) {
collateralType = this.codeValueRepository.findOneByCodeNameAndIdWithNotFoundDetection(
CollateralApiConstants.COLLATERAL_CODE_NAME, collateralTypeId);
collateralForUpdate.setCollateralType(collateralType);
}
/**
* Collaterals may be updated only when the loan associated with
* them are yet to be approved
**/
if (!loan.status().isSubmittedAndPendingApproval()) { throw new CollateralCannotBeUpdatedException(
LOAN_COLLATERAL_CANNOT_BE_UPDATED_REASON.LOAN_NOT_IN_SUBMITTED_AND_PENDING_APPROVAL_STAGE, loan.getId()); }
if (!changes.isEmpty()) {
this.collateralRepository.saveAndFlush(collateralForUpdate);
}
@@ -131,11 +148,18 @@ public class CollateralWritePlatformServiceJpaRepositoryImpl implements Collater
@Transactional
@Override
public CommandProcessingResult deleteCollateral(final Long loanId, final Long collateralId, final Long commandId) {
// TODO vishwas fetch collateral by loan and collateral Id
final Loan loan = this.loanRepository.findOne(loanId);
if (loan == null) { throw new LoanNotFoundException(loanId); }
final LoanCollateral collateral = this.collateralRepository.findOne(collateralId);
final LoanCollateral collateral = this.collateralRepository.findByLoanIdAndId(loanId, collateralId);
if (collateral == null) { throw new CollateralNotFoundException(loanId, collateralId); }
/**
* Collaterals may be deleted only when the loan associated with them
* are yet to be approved
**/
if (!loan.status().isSubmittedAndPendingApproval()) { throw new CollateralCannotBeDeletedException(
LOAN_COLLATERAL_CANNOT_BE_DELETED_REASON.LOAN_NOT_IN_SUBMITTED_AND_PENDING_APPROVAL_STAGE, loanId, collateralId); }
loan.getCollateral().remove(collateral);
this.collateralRepository.delete(collateral);