public CommandProcessingResult updateStaff(final Long staffId, final JsonCommand command) {
try {
this.fromApiJsonDeserializer.validateForUpdate(command.json());
final Staff staffForUpdate = this.staffRepository.findOne(staffId);
if (staffForUpdate == null) { throw new StaffNotFoundException(staffId); }
final Map<String, Object> changesOnly = staffForUpdate.update(command);
if (changesOnly.containsKey("officeId")) {
final Long officeId = (Long) changesOnly.get("officeId");
final Office newOffice = this.officeRepository.findOne(officeId);
if (newOffice == null) { throw new OfficeNotFoundException(officeId); }
staffForUpdate.changeOffice(newOffice);
}
if (!changesOnly.isEmpty()) {
this.staffRepository.saveAndFlush(staffForUpdate);
}
return new CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(staffId)
.withOfficeId(staffForUpdate.officeId()).with(changesOnly).build();
} catch (final DataIntegrityViolationException dve) {
handleStaffDataIntegrityIssues(command, dve);
return CommandProcessingResult.empty();
}
}