@Test(expected = WebCertServiceException.class) public void testUpdatePatientOnDraftNoMedarbetaruppdragThrowsException() { utkast.setEnhetsId("<unknownenhet>"); Patient newPatient = getUpdatedPatient(); UpdatePatientOnDraftRequest request = new UpdatePatientOnDraftRequest(newPatient.getPersonId(), utkast.getIntygsId(), utkast.getVersion()); WebCertUser user = createUser(); Utlatande utlatande = mock(Utlatande.class); GrundData grunddata = new GrundData(); grunddata.setPatient(defaultPatient); grunddata.setSkapadAv(new HoSPersonal()); utkast = spy(utkast); when(utkastRepository.findById(INTYG_ID)).thenReturn(Optional.ofNullable(utkast)); when(userService.getUser()).thenReturn(user); utkastService.updatePatientOnDraft(request); verifyNoMoreInteractions(utkastRepository, notificationService); }
@Override public void updatePatientOnDraft(UpdatePatientOnDraftRequest request) { String draftId = request.getDraftId(); LOG.debug("Checking that Patient is up-to-date on Utkast '{}'", draftId); Utkast utkast = utkastRepository.findById(draftId).orElse(null); verifyUtkastExists(utkast, draftId, null, "The draft could not be updated with patient details since it could not be found"); if (webCertUserService.getUser().getIdsOfAllVardenheter().stream() .noneMatch(enhet -> enhet.equalsIgnoreCase(utkast.getEnhetsId()))) { LOG.error("User did not have any medarbetaruppdrag for enhet '{}'", utkast.getEnhetsId()); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.AUTHORIZATION_PROBLEM, "User did not have any medarbetaruppdrag for enhet " + utkast.getEnhetsId()); } if (utkast.getVersion() != request.getVersion()) { LOG.debug("Utkast '{}' was concurrently modified", draftId); throw new OptimisticLockException(utkast.getSenastSparadAv().getNamn()); } if (!isTheDraftStillADraft(utkast.getStatus())) { LOG.error("Utkast '{}' can not be updated since it is no longer in draft mode", draftId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "This utkast can not be updated since it is no longer in draft mode"); } final ModuleApi moduleApi = getModuleApi(utkast.getIntygsTyp(), utkast.getIntygTypeVersion()); Patient draftPatient = getPatientFromCDraft(moduleApi, utkast.getModel()); Optional<Personnummer> optionalPnr = Optional.ofNullable(request.getPersonnummer()); Optional<Personnummer> optionalDraftPnr = Optional.ofNullable(draftPatient.getPersonId()); if (optionalDraftPnr.isPresent()) { String oldPersonId; if (request.getOldPersonnummer() != null) { oldPersonId = request.getOldPersonnummer().getPersonnummer(); } else { oldPersonId = optionalDraftPnr.get().getPersonnummer(); } webCertUserService.getUser().getParameters().setBeforeAlternateSsn(oldPersonId); } if ((optionalPnr.isPresent() || SamordningsnummerValidator.isSamordningsNummer(optionalPnr)) && !isHashEqual(optionalPnr, optionalDraftPnr)) { draftPatient.setPersonId(optionalPnr.get()); try { String updatedModel = moduleApi.updateBeforeSave(utkast.getModel(), draftPatient); updateUtkastModel(utkast, updatedModel); saveDraft(utkast); monitoringService.logUtkastPatientDetailsUpdated(utkast.getIntygsId(), utkast.getIntygsTyp()); sendNotification(utkast, Event.CHANGED); } catch (ModuleException e) { throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, "Patient details on Utkast " + draftId + " could not be updated", e); } } else { LOG.debug("Utkast '{}' patient details were already up-to-date: no update needed", draftId); } }
UtkastServiceImpl implements UtkastService { @Override public void updatePatientOnDraft(UpdatePatientOnDraftRequest request) { String draftId = request.getDraftId(); LOG.debug("Checking that Patient is up-to-date on Utkast '{}'", draftId); Utkast utkast = utkastRepository.findById(draftId).orElse(null); verifyUtkastExists(utkast, draftId, null, "The draft could not be updated with patient details since it could not be found"); if (webCertUserService.getUser().getIdsOfAllVardenheter().stream() .noneMatch(enhet -> enhet.equalsIgnoreCase(utkast.getEnhetsId()))) { LOG.error("User did not have any medarbetaruppdrag for enhet '{}'", utkast.getEnhetsId()); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.AUTHORIZATION_PROBLEM, "User did not have any medarbetaruppdrag for enhet " + utkast.getEnhetsId()); } if (utkast.getVersion() != request.getVersion()) { LOG.debug("Utkast '{}' was concurrently modified", draftId); throw new OptimisticLockException(utkast.getSenastSparadAv().getNamn()); } if (!isTheDraftStillADraft(utkast.getStatus())) { LOG.error("Utkast '{}' can not be updated since it is no longer in draft mode", draftId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "This utkast can not be updated since it is no longer in draft mode"); } final ModuleApi moduleApi = getModuleApi(utkast.getIntygsTyp(), utkast.getIntygTypeVersion()); Patient draftPatient = getPatientFromCDraft(moduleApi, utkast.getModel()); Optional<Personnummer> optionalPnr = Optional.ofNullable(request.getPersonnummer()); Optional<Personnummer> optionalDraftPnr = Optional.ofNullable(draftPatient.getPersonId()); if (optionalDraftPnr.isPresent()) { String oldPersonId; if (request.getOldPersonnummer() != null) { oldPersonId = request.getOldPersonnummer().getPersonnummer(); } else { oldPersonId = optionalDraftPnr.get().getPersonnummer(); } webCertUserService.getUser().getParameters().setBeforeAlternateSsn(oldPersonId); } if ((optionalPnr.isPresent() || SamordningsnummerValidator.isSamordningsNummer(optionalPnr)) && !isHashEqual(optionalPnr, optionalDraftPnr)) { draftPatient.setPersonId(optionalPnr.get()); try { String updatedModel = moduleApi.updateBeforeSave(utkast.getModel(), draftPatient); updateUtkastModel(utkast, updatedModel); saveDraft(utkast); monitoringService.logUtkastPatientDetailsUpdated(utkast.getIntygsId(), utkast.getIntygsTyp()); sendNotification(utkast, Event.CHANGED); } catch (ModuleException e) { throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, "Patient details on Utkast " + draftId + " could not be updated", e); } } else { LOG.debug("Utkast '{}' patient details were already up-to-date: no update needed", draftId); } } }
UtkastServiceImpl implements UtkastService { @Override public void updatePatientOnDraft(UpdatePatientOnDraftRequest request) { String draftId = request.getDraftId(); LOG.debug("Checking that Patient is up-to-date on Utkast '{}'", draftId); Utkast utkast = utkastRepository.findById(draftId).orElse(null); verifyUtkastExists(utkast, draftId, null, "The draft could not be updated with patient details since it could not be found"); if (webCertUserService.getUser().getIdsOfAllVardenheter().stream() .noneMatch(enhet -> enhet.equalsIgnoreCase(utkast.getEnhetsId()))) { LOG.error("User did not have any medarbetaruppdrag for enhet '{}'", utkast.getEnhetsId()); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.AUTHORIZATION_PROBLEM, "User did not have any medarbetaruppdrag for enhet " + utkast.getEnhetsId()); } if (utkast.getVersion() != request.getVersion()) { LOG.debug("Utkast '{}' was concurrently modified", draftId); throw new OptimisticLockException(utkast.getSenastSparadAv().getNamn()); } if (!isTheDraftStillADraft(utkast.getStatus())) { LOG.error("Utkast '{}' can not be updated since it is no longer in draft mode", draftId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "This utkast can not be updated since it is no longer in draft mode"); } final ModuleApi moduleApi = getModuleApi(utkast.getIntygsTyp(), utkast.getIntygTypeVersion()); Patient draftPatient = getPatientFromCDraft(moduleApi, utkast.getModel()); Optional<Personnummer> optionalPnr = Optional.ofNullable(request.getPersonnummer()); Optional<Personnummer> optionalDraftPnr = Optional.ofNullable(draftPatient.getPersonId()); if (optionalDraftPnr.isPresent()) { String oldPersonId; if (request.getOldPersonnummer() != null) { oldPersonId = request.getOldPersonnummer().getPersonnummer(); } else { oldPersonId = optionalDraftPnr.get().getPersonnummer(); } webCertUserService.getUser().getParameters().setBeforeAlternateSsn(oldPersonId); } if ((optionalPnr.isPresent() || SamordningsnummerValidator.isSamordningsNummer(optionalPnr)) && !isHashEqual(optionalPnr, optionalDraftPnr)) { draftPatient.setPersonId(optionalPnr.get()); try { String updatedModel = moduleApi.updateBeforeSave(utkast.getModel(), draftPatient); updateUtkastModel(utkast, updatedModel); saveDraft(utkast); monitoringService.logUtkastPatientDetailsUpdated(utkast.getIntygsId(), utkast.getIntygsTyp()); sendNotification(utkast, Event.CHANGED); } catch (ModuleException e) { throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, "Patient details on Utkast " + draftId + " could not be updated", e); } } else { LOG.debug("Utkast '{}' patient details were already up-to-date: no update needed", draftId); } }  }
UtkastServiceImpl implements UtkastService { @Override public void updatePatientOnDraft(UpdatePatientOnDraftRequest request) { String draftId = request.getDraftId(); LOG.debug("Checking that Patient is up-to-date on Utkast '{}'", draftId); Utkast utkast = utkastRepository.findById(draftId).orElse(null); verifyUtkastExists(utkast, draftId, null, "The draft could not be updated with patient details since it could not be found"); if (webCertUserService.getUser().getIdsOfAllVardenheter().stream() .noneMatch(enhet -> enhet.equalsIgnoreCase(utkast.getEnhetsId()))) { LOG.error("User did not have any medarbetaruppdrag for enhet '{}'", utkast.getEnhetsId()); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.AUTHORIZATION_PROBLEM, "User did not have any medarbetaruppdrag for enhet " + utkast.getEnhetsId()); } if (utkast.getVersion() != request.getVersion()) { LOG.debug("Utkast '{}' was concurrently modified", draftId); throw new OptimisticLockException(utkast.getSenastSparadAv().getNamn()); } if (!isTheDraftStillADraft(utkast.getStatus())) { LOG.error("Utkast '{}' can not be updated since it is no longer in draft mode", draftId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "This utkast can not be updated since it is no longer in draft mode"); } final ModuleApi moduleApi = getModuleApi(utkast.getIntygsTyp(), utkast.getIntygTypeVersion()); Patient draftPatient = getPatientFromCDraft(moduleApi, utkast.getModel()); Optional<Personnummer> optionalPnr = Optional.ofNullable(request.getPersonnummer()); Optional<Personnummer> optionalDraftPnr = Optional.ofNullable(draftPatient.getPersonId()); if (optionalDraftPnr.isPresent()) { String oldPersonId; if (request.getOldPersonnummer() != null) { oldPersonId = request.getOldPersonnummer().getPersonnummer(); } else { oldPersonId = optionalDraftPnr.get().getPersonnummer(); } webCertUserService.getUser().getParameters().setBeforeAlternateSsn(oldPersonId); } if ((optionalPnr.isPresent() || SamordningsnummerValidator.isSamordningsNummer(optionalPnr)) && !isHashEqual(optionalPnr, optionalDraftPnr)) { draftPatient.setPersonId(optionalPnr.get()); try { String updatedModel = moduleApi.updateBeforeSave(utkast.getModel(), draftPatient); updateUtkastModel(utkast, updatedModel); saveDraft(utkast); monitoringService.logUtkastPatientDetailsUpdated(utkast.getIntygsId(), utkast.getIntygsTyp()); sendNotification(utkast, Event.CHANGED); } catch (ModuleException e) { throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, "Patient details on Utkast " + draftId + " could not be updated", e); } } else { LOG.debug("Utkast '{}' patient details were already up-to-date: no update needed", draftId); } }  static boolean isUtkast(Utkast utkast); static boolean isEditableUtkast(Utkast utkast); @Override @Transactional() // , readOnly=true int countFilterIntyg(UtkastFilter filter); @Override Utkast createNewDraft(CreateNewDraftRequest request); @Override SaveDraftResponse updateDraftFromCandidate(String fromIntygId, String fromIntygType, String toUtkastId, String toUtkastType); @Override SaveDraftResponse updateDraftFromCandidate(String fromIntygId, String fromIntygType, Utkast toUtkast); @Override String getQuestions(String intygsTyp, String version); @Override @Transactional void setKlarForSigneraAndSendStatusMessage(String intygsId, String intygType); @Override Map<String, Map<String, PreviousIntyg>> checkIfPersonHasExistingIntyg(final Personnummer personnummer,
                                                                                 final IntygUser user,
                                                                                 final String currentDraftId); @Override @Transactional void deleteUnsignedDraft(String intygId, long version); @Override @Transactional(readOnly = true) List<Utkast> filterIntyg(UtkastFilter filter); @Override @Transactional(readOnly = true) Utkast getDraft(String intygId, String intygType); @Override @Transactional(readOnly = true) Utkast getDraft(String intygId, String intygType, boolean createPdlLogEvent); @Override @Transactional(readOnly = true) List<Lakare> getLakareWithDraftsByEnhet(String enhetsId); @Override @Transactional(readOnly = true) Map<String, Long> getNbrOfUnsignedDraftsByCareUnits(List<String> careUnitIds); @Override @Transactional SaveDraftResponse saveDraft(String intygId, long version, String draftAsJson, boolean createPdlLogEvent); @Override void updatePatientOnDraft(UpdatePatientOnDraftRequest request); @Override @Transactional Utkast setNotifiedOnDraft(String intygsId, long version, Boolean notified); @Override DraftValidation validateDraft(String intygId, String intygType, String draftAsJson); @Override @Transactional int lockOldDrafts(int lockedAfterDay, LocalDate today); @Override void revokeLockedDraft(String intygId, String intygTyp, String revokeMessage, String reason); @Override boolean isDraftCreatedFromReplacement(String certificateId); }
UtkastServiceImpl implements UtkastService { @Override public void updatePatientOnDraft(UpdatePatientOnDraftRequest request) { String draftId = request.getDraftId(); LOG.debug("Checking that Patient is up-to-date on Utkast '{}'", draftId); Utkast utkast = utkastRepository.findById(draftId).orElse(null); verifyUtkastExists(utkast, draftId, null, "The draft could not be updated with patient details since it could not be found"); if (webCertUserService.getUser().getIdsOfAllVardenheter().stream() .noneMatch(enhet -> enhet.equalsIgnoreCase(utkast.getEnhetsId()))) { LOG.error("User did not have any medarbetaruppdrag for enhet '{}'", utkast.getEnhetsId()); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.AUTHORIZATION_PROBLEM, "User did not have any medarbetaruppdrag for enhet " + utkast.getEnhetsId()); } if (utkast.getVersion() != request.getVersion()) { LOG.debug("Utkast '{}' was concurrently modified", draftId); throw new OptimisticLockException(utkast.getSenastSparadAv().getNamn()); } if (!isTheDraftStillADraft(utkast.getStatus())) { LOG.error("Utkast '{}' can not be updated since it is no longer in draft mode", draftId); throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "This utkast can not be updated since it is no longer in draft mode"); } final ModuleApi moduleApi = getModuleApi(utkast.getIntygsTyp(), utkast.getIntygTypeVersion()); Patient draftPatient = getPatientFromCDraft(moduleApi, utkast.getModel()); Optional<Personnummer> optionalPnr = Optional.ofNullable(request.getPersonnummer()); Optional<Personnummer> optionalDraftPnr = Optional.ofNullable(draftPatient.getPersonId()); if (optionalDraftPnr.isPresent()) { String oldPersonId; if (request.getOldPersonnummer() != null) { oldPersonId = request.getOldPersonnummer().getPersonnummer(); } else { oldPersonId = optionalDraftPnr.get().getPersonnummer(); } webCertUserService.getUser().getParameters().setBeforeAlternateSsn(oldPersonId); } if ((optionalPnr.isPresent() || SamordningsnummerValidator.isSamordningsNummer(optionalPnr)) && !isHashEqual(optionalPnr, optionalDraftPnr)) { draftPatient.setPersonId(optionalPnr.get()); try { String updatedModel = moduleApi.updateBeforeSave(utkast.getModel(), draftPatient); updateUtkastModel(utkast, updatedModel); saveDraft(utkast); monitoringService.logUtkastPatientDetailsUpdated(utkast.getIntygsId(), utkast.getIntygsTyp()); sendNotification(utkast, Event.CHANGED); } catch (ModuleException e) { throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, "Patient details on Utkast " + draftId + " could not be updated", e); } } else { LOG.debug("Utkast '{}' patient details were already up-to-date: no update needed", draftId); } }  static boolean isUtkast(Utkast utkast); static boolean isEditableUtkast(Utkast utkast); @Override @Transactional() // , readOnly=true int countFilterIntyg(UtkastFilter filter); @Override Utkast createNewDraft(CreateNewDraftRequest request); @Override SaveDraftResponse updateDraftFromCandidate(String fromIntygId, String fromIntygType, String toUtkastId, String toUtkastType); @Override SaveDraftResponse updateDraftFromCandidate(String fromIntygId, String fromIntygType, Utkast toUtkast); @Override String getQuestions(String intygsTyp, String version); @Override @Transactional void setKlarForSigneraAndSendStatusMessage(String intygsId, String intygType); @Override Map<String, Map<String, PreviousIntyg>> checkIfPersonHasExistingIntyg(final Personnummer personnummer,
                                                                                 final IntygUser user,
                                                                                 final String currentDraftId); @Override @Transactional void deleteUnsignedDraft(String intygId, long version); @Override @Transactional(readOnly = true) List<Utkast> filterIntyg(UtkastFilter filter); @Override @Transactional(readOnly = true) Utkast getDraft(String intygId, String intygType); @Override @Transactional(readOnly = true) Utkast getDraft(String intygId, String intygType, boolean createPdlLogEvent); @Override @Transactional(readOnly = true) List<Lakare> getLakareWithDraftsByEnhet(String enhetsId); @Override @Transactional(readOnly = true) Map<String, Long> getNbrOfUnsignedDraftsByCareUnits(List<String> careUnitIds); @Override @Transactional SaveDraftResponse saveDraft(String intygId, long version, String draftAsJson, boolean createPdlLogEvent); @Override void updatePatientOnDraft(UpdatePatientOnDraftRequest request); @Override @Transactional Utkast setNotifiedOnDraft(String intygsId, long version, Boolean notified); @Override DraftValidation validateDraft(String intygId, String intygType, String draftAsJson); @Override @Transactional int lockOldDrafts(int lockedAfterDay, LocalDate today); @Override void revokeLockedDraft(String intygId, String intygTyp, String revokeMessage, String reason); @Override boolean isDraftCreatedFromReplacement(String certificateId);  }