try {
this.context.authenticatedUser();
this.fromApiJsonDataValidator.validateForUpdate(command.json());
final SavingsProduct product = this.savingProductRepository.findOne(productId);
if (product == null) { throw new SavingsProductNotFoundException(productId); }
final Map<String, Object> changes = product.update(command);
if (changes.containsKey(chargesParamName)) {
final Set<Charge> savingsProductCharges = this.savingsProductAssembler.assembleListOfSavingsProductCharges(command, product
.currency().getCode());
final boolean updated = product.update(savingsProductCharges);
if (!updated) {
changes.remove(chargesParamName);
}
}
// accounting related changes
final boolean accountingTypeChanged = changes.containsKey(accountingRuleParamName);
final Map<String, Object> accountingMappingChanges = this.accountMappingWritePlatformService
.updateSavingsProductToGLAccountMapping(product.getId(), command, accountingTypeChanged, product.getAccountingType(),
DepositAccountType.SAVINGS_DEPOSIT);
changes.putAll(accountingMappingChanges);
if (!changes.isEmpty()) {
this.savingProductRepository.saveAndFlush(product);
}
return new CommandProcessingResultBuilder() //
.withEntityId(product.getId()) //
.with(changes).build();
} catch (final DataAccessException e) {
handleDataIntegrityIssues(command, e);
return CommandProcessingResult.empty();
}