public CommandProcessingResult updateClient(final Long clientId, final JsonCommand command) {
try {
this.fromApiJsonDeserializer.validateForUpdate(command.json());
final Client clientForUpdate = this.clientRepository.findOneWithNotFoundDetection(clientId);
final String clientHierarchy = clientForUpdate.getOffice().getHierarchy();
this.context.validateAccessRights(clientHierarchy);
final Map<String, Object> changes = clientForUpdate.update(command);
if (changes.containsKey(ClientApiConstants.staffIdParamName)) {
final Long newValue = command.longValueOfParameterNamed(ClientApiConstants.staffIdParamName);
Staff newStaff = null;
if (newValue != null) {
newStaff = this.staffRepository.findByOfficeHierarchyWithNotFoundDetection(newValue, clientForUpdate.getOffice()
.getHierarchy());
}
clientForUpdate.updateStaff(newStaff);
}
if (changes.containsKey(ClientApiConstants.genderIdParamName)) {
final Long newValue = command.longValueOfParameterNamed(ClientApiConstants.genderIdParamName);
CodeValue gender = null;
if (newValue != null) {
gender = this.codeValueRepository.findOneByCodeNameAndIdWithNotFoundDetection(ClientApiConstants.GENDER, newValue);
}
clientForUpdate.updateGender(gender);
}
if (changes.containsKey(ClientApiConstants.savingsProductIdParamName)) {
if (clientForUpdate.isActive()) { throw new ClientActiveForUpdateException(clientId,
ClientApiConstants.savingsProductIdParamName); }
SavingsProduct savingsProduct = null;
final Long savingsProductId = command.longValueOfParameterNamed(ClientApiConstants.savingsProductIdParamName);
if (savingsProductId != null) {
savingsProduct = this.savingsProductRepository.findOne(savingsProductId);
if (savingsProduct == null) { throw new SavingsProductNotFoundException(savingsProductId); }
}
clientForUpdate.updateSavingsProduct(savingsProduct);
}
if (changes.containsKey(ClientApiConstants.genderIdParamName)) {
final Long newValue = command.longValueOfParameterNamed(ClientApiConstants.genderIdParamName);
CodeValue newCodeVal = null;
if (newValue != null) {
newCodeVal = this.codeValueRepository.findOneByCodeNameAndIdWithNotFoundDetection(ClientApiConstants.GENDER, newValue);
}
clientForUpdate.updateGender(newCodeVal);
}
if (changes.containsKey(ClientApiConstants.clientTypeIdParamName)) {
final Long newValue = command.longValueOfParameterNamed(ClientApiConstants.clientTypeIdParamName);
CodeValue newCodeVal = null;
if (newValue != null) {
newCodeVal = this.codeValueRepository.findOneByCodeNameAndIdWithNotFoundDetection(ClientApiConstants.CLIENT_TYPE,
newValue);
}
clientForUpdate.updateClientType(newCodeVal);
}
if (changes.containsKey(ClientApiConstants.clientClassificationIdParamName)) {
final Long newValue = command.longValueOfParameterNamed(ClientApiConstants.clientClassificationIdParamName);
CodeValue newCodeVal = null;
if (newValue != null) {
newCodeVal = this.codeValueRepository.findOneByCodeNameAndIdWithNotFoundDetection(
ClientApiConstants.CLIENT_CLASSIFICATION, newValue);
}
clientForUpdate.updateClientClassification(newCodeVal);
}
if (!changes.isEmpty()) {
this.clientRepository.saveAndFlush(clientForUpdate);
}
return new CommandProcessingResultBuilder() //
.withCommandId(command.commandId()) //
.withOfficeId(clientForUpdate.officeId()) //
.withClientId(clientId) //
.withEntityId(clientId) //
.with(changes) //
.build();
} catch (final DataIntegrityViolationException dve) {