Search in sources :

Example 6 with ModuleApi

use of se.inera.intyg.common.support.modules.support.api.ModuleApi 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 7 with ModuleApi

use of se.inera.intyg.common.support.modules.support.api.ModuleApi in project webcert by sklintyg.

the class UtkastServiceImpl method updateUtkastModel.

private void updateUtkastModel(Utkast utkast, String modelJson) {
    WebCertUser user = webCertUserService.getUser();
    try {
        ModuleApi moduleApi = moduleRegistry.getModuleApi(utkast.getIntygsTyp());
        GrundData grundData = moduleApi.getUtlatandeFromJson(modelJson).getGrundData();
        Vardenhet vardenhetFromJson = grundData.getSkapadAv().getVardenhet();
        HoSPersonal hosPerson = IntygConverterUtil.buildHosPersonalFromWebCertUser(user, vardenhetFromJson);
        utkast.setSenastSparadAv(UpdateUserUtil.createVardpersonFromWebCertUser(user));
        utkast.setPatientPersonnummer(grundData.getPatient().getPersonId());
        String updatedInternal = moduleApi.updateBeforeSave(modelJson, hosPerson);
        // String updatedInternalWithResolvedPatient = moduleApi.updateBeforeSave(updatedInternal,
        // updatedPatientForSaving);
        utkast.setModel(updatedInternal);
        updatePatientNameFromModel(utkast, grundData.getPatient());
    } catch (ModuleException | ModuleNotFoundException | IOException e) {
        if (e.getCause() != null && e.getCause().getCause() != null) {
            // This error message is helpful when debugging save problems.
            LOG.debug(e.getCause().getCause().getMessage());
        }
        throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, "Could not update with HoS personal", e);
    }
}
Also used : ModuleApi(se.inera.intyg.common.support.modules.support.api.ModuleApi) ModuleNotFoundException(se.inera.intyg.common.support.modules.registry.ModuleNotFoundException) IOException(java.io.IOException) 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 8 with ModuleApi

use of se.inera.intyg.common.support.modules.support.api.ModuleApi in project webcert by sklintyg.

the class UtkastServiceImpl method validateDraft.

@Override
public DraftValidation validateDraft(String intygId, String intygType, String draftAsJson) {
    LOG.debug("Validating Intyg '{}' with type '{}'", intygId, intygType);
    try {
        ModuleApi moduleApi = moduleRegistry.getModuleApi(intygType);
        ValidateDraftResponse validateDraftResponse = moduleApi.validateDraft(draftAsJson);
        return convertToDraftValidation(validateDraftResponse);
    } catch (ModuleException | ModuleNotFoundException me) {
        throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, me);
    }
}
Also used : ModuleApi(se.inera.intyg.common.support.modules.support.api.ModuleApi) ModuleNotFoundException(se.inera.intyg.common.support.modules.registry.ModuleNotFoundException) ValidateDraftResponse(se.inera.intyg.common.support.modules.support.api.dto.ValidateDraftResponse) ModuleException(se.inera.intyg.common.support.modules.support.api.exception.ModuleException) WebCertServiceException(se.inera.intyg.webcert.common.service.exception.WebCertServiceException)

Example 9 with ModuleApi

use of se.inera.intyg.common.support.modules.support.api.ModuleApi in project webcert by sklintyg.

the class AbstractUtkastBuilder method populateCopyUtkastFromSignedIntyg.

/*
     * (non-Javadoc)
     *
     * @see
     * se.inera.intyg.webcert.web.service.utkast.CopyUtkastBuilder#populateCopyUtkastFromSignedIntyg(se.inera.intyg.
     * webcert.web.service.utkast.dto.CreateNewDraftCopyRequest, se.inera.intyg.webcert.integration.pu.model.Person)
     */
@Override
public CopyUtkastBuilderResponse populateCopyUtkastFromSignedIntyg(T copyRequest, Person patientDetails, boolean addRelation, boolean coherentJournaling, boolean enforceEnhet) throws ModuleNotFoundException, ModuleException {
    String orignalIntygsId = copyRequest.getOriginalIntygId();
    String originalIntygsTyp = copyRequest.getOriginalIntygTyp();
    String intygsTyp = copyRequest.getTyp();
    IntygContentHolder signedIntygHolder = intygService.fetchIntygData(orignalIntygsId, originalIntygsTyp, coherentJournaling);
    ModuleApi orgModuleApi = moduleRegistry.getModuleApi(originalIntygsTyp);
    Utlatande orgUtlatande;
    try {
        orgUtlatande = orgModuleApi.getUtlatandeFromJson(signedIntygHolder.getContents());
    } catch (IOException e) {
        throw new ModuleException("Could not convert orignal certificate to Utlatande", e);
    }
    GrundData grundData = signedIntygHolder.getUtlatande().getGrundData();
    se.inera.intyg.common.support.model.common.internal.Vardenhet vardenhet = grundData.getSkapadAv().getVardenhet();
    if (coherentJournaling && enforceEnhet) {
        verifyEnhetsAuth(vardenhet.getVardgivare().getVardgivarid(), vardenhet.getEnhetsid(), true);
    }
    LOG.debug("Populating copy with details from signed Intyg '{}'", orignalIntygsId);
    CopyUtkastBuilderResponse builderResponse = new CopyUtkastBuilderResponse();
    builderResponse.setOrginalEnhetsId(vardenhet.getEnhetsid());
    builderResponse.setOrginalEnhetsNamn(vardenhet.getEnhetsnamn());
    builderResponse.setOrginalVardgivarId(vardenhet.getVardgivare().getVardgivarid());
    builderResponse.setOrginalVardgivarNamn(vardenhet.getVardgivare().getVardgivarnamn());
    ModuleApi moduleApi = moduleRegistry.getModuleApi(intygsTyp);
    // Set relation to null if not applicable
    Relation relation = createRelation(copyRequest);
    String newDraftCopyId = intygsIdStrategy.createId();
    String draftCopyJson = getInternalModel(orgUtlatande, moduleApi, copyRequest, patientDetails, relation, newDraftCopyId);
    UtkastStatus utkastStatus = validateDraft(moduleApi, draftCopyJson);
    Utkast utkast = buildUtkastCopy(copyRequest, newDraftCopyId, intygsTyp, addRelation, relation, draftCopyJson, utkastStatus);
    if (patientDetails != null) {
        populatePatientDetailsFromPerson(utkast, patientDetails);
    } else {
        se.inera.intyg.common.support.model.common.internal.Patient patient = signedIntygHolder.getUtlatande().getGrundData().getPatient();
        populatePatientDetailsFromPatient(utkast, patient);
    }
    replacePatientPersonnummerWithNew(utkast, copyRequest);
    builderResponse.setUtkastCopy(utkast);
    return builderResponse;
}
Also used : ModuleApi(se.inera.intyg.common.support.modules.support.api.ModuleApi) UtkastStatus(se.inera.intyg.webcert.common.model.UtkastStatus) CopyUtkastBuilderResponse(se.inera.intyg.webcert.web.service.utkast.dto.CopyUtkastBuilderResponse) Vardenhet(se.inera.intyg.common.support.model.common.internal.Vardenhet) IOException(java.io.IOException) Relation(se.inera.intyg.common.support.model.common.internal.Relation) IntygContentHolder(se.inera.intyg.webcert.web.service.intyg.dto.IntygContentHolder) Utlatande(se.inera.intyg.common.support.model.common.internal.Utlatande) Utkast(se.inera.intyg.webcert.persistence.utkast.model.Utkast) GrundData(se.inera.intyg.common.support.model.common.internal.GrundData) ModuleException(se.inera.intyg.common.support.modules.support.api.exception.ModuleException) Patient(se.inera.intyg.common.support.model.common.internal.Patient)

Example 10 with ModuleApi

use of se.inera.intyg.common.support.modules.support.api.ModuleApi in project webcert by sklintyg.

the class AbstractUtkastBuilder method populateCopyUtkastFromOrignalUtkast.

/*
     * (non-Javadoc)
     *
     * @see
     * se.inera.intyg.webcert.web.service.utkast.CopyUtkastBuilder#populateCopyUtkastFromOrignalUtkast(se.inera.intyg.
     * webcert.web.service.utkast.dto.CreateNewDraftCopyRequest, se.inera.intyg.webcert.integration.pu.model.Person)
     */
@Override
@Transactional(value = "jpaTransactionManager", readOnly = true)
public CopyUtkastBuilderResponse populateCopyUtkastFromOrignalUtkast(T copyRequest, Person patientDetails, boolean addRelation, boolean coherentJournaling, boolean enforceEnhet) throws ModuleNotFoundException, ModuleException {
    String orignalIntygsId = copyRequest.getOriginalIntygId();
    Utkast orgUtkast = utkastRepository.findOne(orignalIntygsId);
    ModuleApi orgModuleApi = moduleRegistry.getModuleApi(copyRequest.getOriginalIntygTyp());
    Utlatande orgUtlatande;
    try {
        orgUtlatande = orgModuleApi.getUtlatandeFromJson(orgUtkast.getModel());
    } catch (IOException e) {
        throw new ModuleException("Could not convert original certificate to Utlatande", e);
    }
    // Perform enhets auth if coherent journaling is not active.
    if (!coherentJournaling || enforceEnhet) {
        verifyEnhetsAuth(orgUtkast.getVardgivarId(), orgUtkast.getEnhetsId(), true);
    } else {
        LogRequest logRequest = LogRequestFactory.createLogRequestFromUtkast(orgUtkast, coherentJournaling);
        logService.logReadIntyg(logRequest);
    }
    CopyUtkastBuilderResponse builderResponse = new CopyUtkastBuilderResponse();
    builderResponse.setOrginalEnhetsId(orgUtkast.getEnhetsId());
    builderResponse.setOrginalEnhetsNamn(orgUtkast.getEnhetsNamn());
    builderResponse.setOrginalVardgivarId(orgUtkast.getVardgivarId());
    builderResponse.setOrginalVardgivarNamn(orgUtkast.getVardgivarNamn());
    LOG.debug("Populating copy with details from Utkast '{}'", orignalIntygsId);
    ModuleApi moduleApi = moduleRegistry.getModuleApi(copyRequest.getTyp());
    // Set relation to null if not applicable
    Relation relation = createRelation(copyRequest);
    String newDraftCopyId = intygsIdStrategy.createId();
    String draftCopyJson = getInternalModel(orgUtlatande, moduleApi, copyRequest, patientDetails, relation, newDraftCopyId);
    UtkastStatus utkastStatus = validateDraft(moduleApi, draftCopyJson);
    Utkast utkast = buildUtkastCopy(copyRequest, newDraftCopyId, copyRequest.getTyp(), addRelation, relation, draftCopyJson, utkastStatus);
    if (patientDetails != null) {
        populatePatientDetailsFromPerson(utkast, patientDetails);
    } else {
        populatePatientDetailsFromUtkast(utkast, orgUtkast);
    }
    replacePatientPersonnummerWithNew(utkast, copyRequest);
    builderResponse.setUtkastCopy(utkast);
    return builderResponse;
}
Also used : ModuleApi(se.inera.intyg.common.support.modules.support.api.ModuleApi) LogRequest(se.inera.intyg.webcert.web.service.log.dto.LogRequest) UtkastStatus(se.inera.intyg.webcert.common.model.UtkastStatus) Relation(se.inera.intyg.common.support.model.common.internal.Relation) CopyUtkastBuilderResponse(se.inera.intyg.webcert.web.service.utkast.dto.CopyUtkastBuilderResponse) Utkast(se.inera.intyg.webcert.persistence.utkast.model.Utkast) Utlatande(se.inera.intyg.common.support.model.common.internal.Utlatande) IOException(java.io.IOException) ModuleException(se.inera.intyg.common.support.modules.support.api.exception.ModuleException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ModuleApi (se.inera.intyg.common.support.modules.support.api.ModuleApi)27 ModuleException (se.inera.intyg.common.support.modules.support.api.exception.ModuleException)13 ModuleNotFoundException (se.inera.intyg.common.support.modules.registry.ModuleNotFoundException)12 Utkast (se.inera.intyg.webcert.persistence.utkast.model.Utkast)11 Test (org.junit.Test)10 IOException (java.io.IOException)8 Utlatande (se.inera.intyg.common.support.model.common.internal.Utlatande)7 WebCertServiceException (se.inera.intyg.webcert.common.service.exception.WebCertServiceException)7 Patient (se.inera.intyg.common.support.model.common.internal.Patient)6 ExternalServiceCallException (se.inera.intyg.common.support.modules.support.api.exception.ExternalServiceCallException)5 Personnummer (se.inera.intyg.schemas.contract.Personnummer)5 ArrayList (java.util.ArrayList)3 WebServiceException (javax.xml.ws.WebServiceException)3 Transactional (org.springframework.transaction.annotation.Transactional)3 UtkastStatus (se.inera.intyg.webcert.common.model.UtkastStatus)3 Intyg (se.riv.clinicalprocess.healthcond.certificate.v3.Intyg)3 LocalDateTime (java.time.LocalDateTime)2 OptimisticLockException (javax.persistence.OptimisticLockException)2 Relation (se.inera.intyg.common.support.model.common.internal.Relation)2 Vardenhet (se.inera.intyg.common.support.model.common.internal.Vardenhet)2