Search in sources :

Example 36 with Utkast

use of se.inera.intyg.webcert.persistence.utkast.model.Utkast in project webcert by sklintyg.

the class CopyUtkastServiceImpl method saveAndNotify.

private Utkast saveAndNotify(CopyUtkastBuilderResponse builderResponse, WebCertUser user) {
    builderResponse.getUtkastCopy().setSkapad(LocalDateTime.now());
    Utkast savedUtkast = utkastRepository.save(builderResponse.getUtkastCopy());
    if (user.getParameters() != null && !Strings.isNullOrEmpty(user.getParameters().getReference())) {
        referensService.saveReferens(savedUtkast.getIntygsId(), user.getParameters().getReference());
    }
    notificationService.sendNotificationForDraftCreated(savedUtkast);
    LOG.debug("Notification sent: utkast with id '{}' was created as a copy.", savedUtkast.getIntygsId());
    LogRequest logRequest = LogRequestFactory.createLogRequestFromUtkast(savedUtkast);
    logService.logCreateIntyg(logRequest);
    return savedUtkast;
}
Also used : LogRequest(se.inera.intyg.webcert.web.service.log.dto.LogRequest) Utkast(se.inera.intyg.webcert.persistence.utkast.model.Utkast)

Example 37 with Utkast

use of se.inera.intyg.webcert.persistence.utkast.model.Utkast in project webcert by sklintyg.

the class CopyUtkastServiceImpl method createRenewalCopy.

/*
     * (non-Javadoc)
     *
     * @see
     * se.inera.intyg.webcert.web.service.utkast.CopyUtkastService#createCopy(se.inera.intyg.webcert.web.service.utkast.
     * dto.
     * CreateRenewalCopyRequest)
     */
@Override
public CreateRenewalCopyResponse createRenewalCopy(CreateRenewalCopyRequest copyRequest) {
    String originalIntygId = copyRequest.getOriginalIntygId();
    LOG.debug("Creating renewal for intyg '{}'", originalIntygId);
    WebCertUser user = userService.getUser();
    boolean coherentJournaling = user != null && user.getParameters() != null && user.getParameters().isSjf();
    try {
        if (intygService.isRevoked(copyRequest.getOriginalIntygId(), copyRequest.getOriginalIntygTyp(), coherentJournaling)) {
            LOG.debug("Cannot renew certificate with id '{}', the certificate is revoked", originalIntygId);
            throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "Original certificate is revoked");
        }
        verifyNotReplacedWithSigned(copyRequest.getOriginalIntygId(), "create renewal");
        verifyNotComplementedWithSigned(copyRequest.getOriginalIntygId(), "create renewal");
        CopyUtkastBuilderResponse builderResponse = buildRenewalUtkastBuilderResponse(copyRequest, originalIntygId, coherentJournaling);
        if (copyRequest.isDjupintegrerad()) {
            checkIntegreradEnhet(builderResponse);
        }
        Utkast savedUtkast = saveAndNotify(builderResponse, user);
        monitoringService.logIntygCopiedRenewal(savedUtkast.getIntygsId(), originalIntygId);
        return new CreateRenewalCopyResponse(savedUtkast.getIntygsTyp(), savedUtkast.getIntygsId(), originalIntygId);
    } catch (ModuleException | ModuleNotFoundException me) {
        LOG.error("Module exception occured when trying to make a copy of " + originalIntygId);
        throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, me);
    }
}
Also used : ModuleNotFoundException(se.inera.intyg.common.support.modules.registry.ModuleNotFoundException) CopyUtkastBuilderResponse(se.inera.intyg.webcert.web.service.utkast.dto.CopyUtkastBuilderResponse) Utkast(se.inera.intyg.webcert.persistence.utkast.model.Utkast) CreateRenewalCopyResponse(se.inera.intyg.webcert.web.service.utkast.dto.CreateRenewalCopyResponse) ModuleException(se.inera.intyg.common.support.modules.support.api.exception.ModuleException) WebCertServiceException(se.inera.intyg.webcert.common.service.exception.WebCertServiceException) WebCertUser(se.inera.intyg.webcert.web.service.user.dto.WebCertUser)

Example 38 with Utkast

use of se.inera.intyg.webcert.persistence.utkast.model.Utkast in project webcert by sklintyg.

the class UtkastServiceImpl method saveDraft.

@Override
@Transactional("jpaTransactionManager")
public SaveDraftResponse saveDraft(String intygId, long version, String draftAsJson, boolean createPdlLogEvent) {
    LOG.debug("Saving and validating utkast '{}'", intygId);
    Utkast utkast = utkastRepository.findOne(intygId);
    if (utkast == null) {
        LOG.warn("Utkast '{}' was not found", intygId);
        throw new WebCertServiceException(WebCertServiceErrorCodeEnum.DATA_NOT_FOUND, "The utkast could not be found");
    }
    // check that the draft hasn't been modified concurrently
    if (utkast.getVersion() != version) {
        LOG.debug("Utkast '{}' was concurrently modified", intygId);
        throw new OptimisticLockException(utkast.getSenastSparadAv().getNamn());
    }
    // check that the draft is still a draft
    if (!isTheDraftStillADraft(utkast.getStatus())) {
        LOG.error("Utkast '{}' can not be updated since it is no longer in draft mode", intygId);
        throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "This utkast can not be updated since it is no longer in draft mode");
    }
    String intygType = utkast.getIntygsTyp();
    // Keep persisted json for comparsion
    String persistedJson = utkast.getModel();
    // Update draft with user information
    updateUtkastModel(utkast, draftAsJson);
    // Is draft valid?
    DraftValidation draftValidation = validateDraft(intygId, intygType, draftAsJson);
    UtkastStatus utkastStatus = draftValidation.isDraftValid() ? UtkastStatus.DRAFT_COMPLETE : UtkastStatus.DRAFT_INCOMPLETE;
    utkast.setStatus(utkastStatus);
    // Save the updated draft
    utkast = saveDraft(utkast);
    LOG.debug("Utkast '{}' updated", utkast.getIntygsId());
    if (createPdlLogEvent) {
        LogRequest logRequest = LogRequestFactory.createLogRequestFromUtkast(utkast);
        logService.logUpdateIntyg(logRequest);
        monitoringService.logUtkastEdited(utkast.getIntygsId(), utkast.getIntygsTyp());
    }
    // Notify stakeholders when a draft has been changed/updated
    try {
        ModuleApi moduleApi = moduleRegistry.getModuleApi(intygType);
        if (moduleApi.shouldNotify(persistedJson, draftAsJson)) {
            LOG.debug("*** Detected changes in model, sending notification! ***");
            sendNotification(utkast, Event.CHANGED);
        }
    } catch (ModuleException | ModuleNotFoundException e) {
        throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, e);
    }
    // Flush JPA changes, to make sure the version attribute is updated
    utkastRepository.flush();
    return new SaveDraftResponse(utkast.getVersion(), utkastStatus);
}
Also used : ModuleApi(se.inera.intyg.common.support.modules.support.api.ModuleApi) UtkastStatus(se.inera.intyg.webcert.common.model.UtkastStatus) LogRequest(se.inera.intyg.webcert.web.service.log.dto.LogRequest) ModuleNotFoundException(se.inera.intyg.common.support.modules.registry.ModuleNotFoundException) Utkast(se.inera.intyg.webcert.persistence.utkast.model.Utkast) OptimisticLockException(javax.persistence.OptimisticLockException) ModuleException(se.inera.intyg.common.support.modules.support.api.exception.ModuleException) WebCertServiceException(se.inera.intyg.webcert.common.service.exception.WebCertServiceException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 39 with Utkast

use of se.inera.intyg.webcert.persistence.utkast.model.Utkast in project webcert by sklintyg.

the class UtkastServiceImpl method updatePatientOnDraft.

@Override
public void updatePatientOnDraft(UpdatePatientOnDraftRequest request) {
    // diff draftPatient and request patient: if no changes, do nothing
    String draftId = request.getDraftId();
    LOG.debug("Checking that Patient is up-to-date on Utkast '{}'", draftId);
    Utkast utkast = utkastRepository.findOne(draftId);
    if (utkast == null) {
        LOG.warn("Utkast '{}' was not found", draftId);
        throw new WebCertServiceException(WebCertServiceErrorCodeEnum.DATA_NOT_FOUND, "The utkast 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());
    }
    // check that the draft hasn't been modified concurrently
    if (utkast.getVersion() != request.getVersion()) {
        LOG.debug("Utkast '{}' was concurrently modified", draftId);
        throw new OptimisticLockException(utkast.getSenastSparadAv().getNamn());
    }
    // check that the draft is still a draft
    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());
    // INTYG-4086
    Patient draftPatient = getPatientFromCurrentDraft(moduleApi, utkast.getModel());
    Optional<Personnummer> optionalPnr = Optional.ofNullable(request.getPersonnummer());
    Optional<Personnummer> optionalDraftPnr = Optional.ofNullable(draftPatient.getPersonId());
    if (optionalDraftPnr.isPresent()) {
        // Spara undan det gamla personnummret temporärt
        String oldPersonId = optionalDraftPnr.get().getPersonnummer();
        webCertUserService.getUser().getParameters().setBeforeAlternateSsn(oldPersonId);
    }
    if ((optionalPnr.isPresent() || SamordningsnummerValidator.isSamordningsNummer(optionalPnr)) && !isHashEqual(optionalPnr, optionalDraftPnr)) {
        // INTYG-4086: Ta reda på om man skall kunna uppdatera annat än personnumret? Och om man uppdaterar
        // personnumret -
        // vilka regler gäller då för namn och adress? Samma regler som i PatientDetailsResolverImpl?
        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);
    }
}
Also used : ModuleApi(se.inera.intyg.common.support.modules.support.api.ModuleApi) Personnummer(se.inera.intyg.schemas.contract.Personnummer) Utkast(se.inera.intyg.webcert.persistence.utkast.model.Utkast) OptimisticLockException(javax.persistence.OptimisticLockException) ModuleException(se.inera.intyg.common.support.modules.support.api.exception.ModuleException) WebCertServiceException(se.inera.intyg.webcert.common.service.exception.WebCertServiceException)

Example 40 with Utkast

use of se.inera.intyg.webcert.persistence.utkast.model.Utkast in project webcert by sklintyg.

the class UtkastServiceImpl method saveDraft.

private Utkast saveDraft(Utkast utkast) {
    Utkast savedUtkast = utkastRepository.save(utkast);
    LOG.debug("ArendeDraft '{}' saved", savedUtkast.getIntygsId());
    return savedUtkast;
}
Also used : Utkast(se.inera.intyg.webcert.persistence.utkast.model.Utkast)

Aggregations

Utkast (se.inera.intyg.webcert.persistence.utkast.model.Utkast)171 Test (org.junit.Test)88 WebCertServiceException (se.inera.intyg.webcert.common.service.exception.WebCertServiceException)34 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)24 Personnummer (se.inera.intyg.schemas.contract.Personnummer)21 Signatur (se.inera.intyg.webcert.persistence.utkast.model.Signatur)21 VardpersonReferens (se.inera.intyg.webcert.persistence.utkast.model.VardpersonReferens)21 LogRequest (se.inera.intyg.webcert.web.service.log.dto.LogRequest)18 WebCertUser (se.inera.intyg.webcert.web.service.user.dto.WebCertUser)17 Arende (se.inera.intyg.webcert.persistence.arende.model.Arende)16 Utlatande (se.inera.intyg.common.support.model.common.internal.Utlatande)14 CreateNewDraftRequest (se.inera.intyg.webcert.web.service.utkast.dto.CreateNewDraftRequest)13 Patient (se.inera.intyg.common.support.model.common.internal.Patient)12 ModuleException (se.inera.intyg.common.support.modules.support.api.exception.ModuleException)12 ListIntygEntry (se.inera.intyg.webcert.web.web.controller.api.dto.ListIntygEntry)12 Transactional (org.springframework.transaction.annotation.Transactional)11 ModuleNotFoundException (se.inera.intyg.common.support.modules.registry.ModuleNotFoundException)11 MedicinsktArende (se.inera.intyg.webcert.persistence.arende.model.MedicinsktArende)11 CopyUtkastBuilderResponse (se.inera.intyg.webcert.web.service.utkast.dto.CopyUtkastBuilderResponse)10 ArrayList (java.util.ArrayList)9