Long documentTypeId = clientIdentifierCommand.getDocumentTypeId();
try {
CodeValue documentType = null;
final Client client = this.clientRepository.findOneWithNotFoundDetection(clientId);
final ClientIdentifier clientIdentifierForUpdate = this.clientIdentifierRepository.findOne(identifierId);
if (clientIdentifierForUpdate == null) { throw new ClientIdentifierNotFoundException(identifierId); }
final Map<String, Object> changes = clientIdentifierForUpdate.update(command);
if (changes.containsKey("documentTypeId")) {
documentType = this.codeValueRepository.findOneWithNotFoundDetection(documentTypeId);
if (documentType == null) { throw new CodeValueNotFoundException(documentTypeId); }
documentTypeId = documentType.getId();
documentTypeLabel = documentType.label();
clientIdentifierForUpdate.update(documentType);
}
if (changes.containsKey("documentTypeId") && changes.containsKey("documentKey")) {
documentTypeId = clientIdentifierCommand.getDocumentTypeId();
documentKey = clientIdentifierCommand.getDocumentKey();
} else if (changes.containsKey("documentTypeId") && !changes.containsKey("documentKey")) {
documentTypeId = clientIdentifierCommand.getDocumentTypeId();
documentKey = clientIdentifierForUpdate.documentKey();
} else if (!changes.containsKey("documentTypeId") && changes.containsKey("documentKey")) {
documentTypeId = clientIdentifierForUpdate.documentTypeId();
documentKey = clientIdentifierForUpdate.documentKey();
}
if (!changes.isEmpty()) {
this.clientIdentifierRepository.saveAndFlush(clientIdentifierForUpdate);
}