public CommandProcessingResult updateCharge(final Long chargeId, final JsonCommand command) {
try {
this.fromApiJsonDeserializer.validateForUpdate(command.json());
final Charge chargeForUpdate = this.chargeRepository.findOne(chargeId);
if (chargeForUpdate == null) { throw new ChargeNotFoundException(chargeId); }
final Map<String, Object> changes = chargeForUpdate.update(command);
// MIFOSX-900: Check if the Charge has been active before and now is
// deactivated:
if (changes.containsKey("active")) {
// IF the key exists then it has changed (otherwise it would
// have been filtered), so check current state:
if (!chargeForUpdate.isActive()) {
// TODO: Change this function to only check the mappings!!!
final Boolean isChargeExistWithLoans = isAnyLoanProductsAssociateWithThisCharge(chargeId);
final Boolean isChargeExistWithSavings = isAnySavingsProductsAssociateWithThisCharge(chargeId);
if (isChargeExistWithLoans || isChargeExistWithSavings) { throw new ChargeCannotBeUpdatedException(
"error.msg.charge.cannot.be.updated.it.is.used.in.loan", "This charge cannot be updated, it is used in loan"); }
}
}else if((changes.containsKey("feeFrequency") || changes.containsKey("feeInterval")) && chargeForUpdate.isLoanCharge()){
final Boolean isChargeExistWithLoans = isAnyLoanProductsAssociateWithThisCharge(chargeId);
if (isChargeExistWithLoans) { throw new ChargeCannotBeUpdatedException(
"error.msg.charge.frequency.cannot.be.updated.it.is.used.in.loan", "This charge frequency cannot be updated, it is used in loan"); }
}