@Transactional
@Override
public CommandProcessingResult addCollateral(final Long loanId, final JsonCommand command) {
this.context.authenticatedUser();
final CollateralCommand collateralCommand = this.collateralCommandFromApiJsonDeserializer.commandFromApiJson(command.json());
collateralCommand.validateForCreate();
try {
final Loan loan = this.loanRepository.findOne(loanId);
if (loan == null) { throw new LoanNotFoundException(loanId); }
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