Source:
<code>public CommandProcessingResult updateUser(final Long userId, final JsonCommand command) { try { this.context.authenticatedUser(new CommandWrapperBuilder().updateUser(null).build()); this.fromApiJsonDeserializer.validateForUpdate(command.json()); final AppUser userToUpdate = this.appUserRepository.findOne(userId); if (userToUpdate == null) { throw new UserNotFoundException(userId); } final AppUserPreviousPassword currentPasswordToSaveAsPreview = getCurrentPasswordToSaveAsPreview(userToUpdate, command); final Map<String, Object> changes = userToUpdate.update(command, this.platformPasswordEncoder); if (changes.containsKey("officeId")) { final Long officeId = (Long) changes.get("officeId"); final Office office = this.officeRepository.findOne(officeId); if (office == null) { throw new OfficeNotFoundException(officeId); } userToUpdate.changeOffice(office); } if (changes.containsKey("staffId")) { final Long staffId = (Long) changes.get("staffId"); final Staff linkedStaff = this.staffRepository.findOne(staffId); <START> if (linkedStaff == null) { throw new StaffNotFoundException(staffId); } <END> userToUpdate.changeStaff(linkedStaff); } if (changes.containsKey("roles")) { final String[] roleIds = (String[]) changes.get("roles"); final Set<Role> allRoles = assembleSetOfRoles(roleIds); userToUpdate.updateRoles(allRoles); } if (!changes.isEmpty()) { this.appUserRepository.saveAndFlush(userToUpdate); if (currentPasswordToSaveAsPreview != null) { this.appUserPreviewPasswordRepository.save(currentPasswordToSaveAsPreview); } } return new CommandProcessingResultBuilder() .withEntityId(userId) .withOfficeId(userToUpdate.getOffice().getId()) .with(changes) .build(); } catch (final DataIntegrityViolationException dve) { handleDataIntegrityIssues(command, dve); return CommandProcessingResult.empty(); } }</code><technical_language>method findOneWithNotFoundDetection StaffRepositoryWrapper.java</technical_language>

Target:
public CommandProcessingResult updateUser(final Long userId, final JsonCommand command) { try { this.context.authenticatedUser(new CommandWrapperBuilder().updateUser(null).build()); this.fromApiJsonDeserializer.validateForUpdate(command.json()); final AppUser userToUpdate = this.appUserRepository.findOne(userId); if (userToUpdate == null) { throw new UserNotFoundException(userId); } final AppUserPreviousPassword currentPasswordToSaveAsPreview = getCurrentPasswordToSaveAsPreview(userToUpdate, command); final Map<String, Object> changes = userToUpdate.update(command, this.platformPasswordEncoder); if (changes.containsKey("officeId")) { final Long officeId = (Long) changes.get("officeId"); final Office office = this.officeRepository.findOne(officeId); if (office == null) { throw new OfficeNotFoundException(officeId); } userToUpdate.changeOffice(office); } if (changes.containsKey("staffId")) { final Long staffId = (Long) changes.get("staffId"); final Staff linkedStaff; if(staffId!=null) { linkedStaff = this.staffRepositoryWrapper.findOneWithNotFoundDetection(staffId); } else { linkedStaff = null; } userToUpdate.changeStaff(linkedStaff); } if (changes.containsKey("roles")) { final String[] roleIds = (String[]) changes.get("roles"); final Set<Role> allRoles = assembleSetOfRoles(roleIds); userToUpdate.updateRoles(allRoles); } if (!changes.isEmpty()) { this.appUserRepository.saveAndFlush(userToUpdate); if (currentPasswordToSaveAsPreview != null) { this.appUserPreviewPasswordRepository.save(currentPasswordToSaveAsPreview); } } return new CommandProcessingResultBuilder() .withEntityId(userId) .withOfficeId(userToUpdate.getOffice().getId()) .with(changes) .build(); } catch (final DataIntegrityViolationException dve) { handleDataIntegrityIssues(command, dve); return CommandProcessingResult.empty(); } }