Search in sources :

Example 21 with ModuleException

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

the class UtkastModuleApiController method getDraft.

/**
 * Returns the draft certificate as JSON identified by the intygId.
 *
 * @param intygsId The id of the certificate
 * @return a JSON object
 */
@GET
@Path("/{intygsTyp}/{intygsId}")
@Produces(MediaType.APPLICATION_JSON + UTF_8_CHARSET)
public Response getDraft(@PathParam("intygsTyp") String intygsTyp, @PathParam("intygsId") String intygsId, @Context HttpServletRequest request) {
    LOG.debug("Retrieving Intyg with id {} and type {}", intygsId, intygsTyp);
    Utkast utkast = utkastService.getDraft(intygsId, intygsTyp);
    Patient resolvedPatient = patientDetailsResolver.resolvePatient(utkast.getPatientPersonnummer(), intygsTyp);
    if (resolvedPatient == null) {
        throw new WebCertServiceException(WebCertServiceErrorCodeEnum.PU_PROBLEM, "Could not resolve Patient in PU-service when opening draft.");
    }
    authoritiesValidator.given(getWebCertUserService().getUser(), intygsTyp).features(AuthoritiesConstants.FEATURE_HANTERA_INTYGSUTKAST).privilege(AuthoritiesConstants.PRIVILEGE_SKRIVA_INTYG).orThrow();
    verifySekretessmarkering(intygsTyp, utkast.getEnhetsId(), resolvedPatient);
    request.getSession(true).removeAttribute(LAST_SAVED_DRAFT);
    DraftHolder draftHolder = new DraftHolder();
    draftHolder.setVersion(utkast.getVersion());
    draftHolder.setVidarebefordrad(utkast.getVidarebefordrad());
    draftHolder.setStatus(utkast.getStatus());
    draftHolder.setEnhetsNamn(utkast.getEnhetsNamn());
    draftHolder.setVardgivareNamn(utkast.getVardgivarNamn());
    draftHolder.setLatestTextVersion(intygTextsService.getLatestVersion(utkast.getIntygsTyp()));
    Relations relations1 = certificateRelationService.getRelations(utkast.getIntygsId());
    draftHolder.setRelations(relations1);
    draftHolder.setKlartForSigneringDatum(utkast.getKlartForSigneringDatum());
    draftHolder.setCreated(utkast.getSkapad());
    // The patientResolved is unnecessary?
    draftHolder.setPatientResolved(true);
    draftHolder.setSekretessmarkering(resolvedPatient.isSekretessmarkering());
    draftHolder.setAvliden(resolvedPatient.isAvliden());
    // Businesss logic below should not be here inside a controller.. Should preferably be moved in the future.
    try {
        try {
            Utlatande utlatande = moduleRegistry.getModuleApi(intygsTyp).getUtlatandeFromJson(utkast.getModel());
            draftHolder.setPatientNameChangedInPU(patientDetailsResolver.isPatientNamedChanged(utlatande.getGrundData().getPatient(), resolvedPatient));
            draftHolder.setPatientAddressChangedInPU(patientDetailsResolver.isPatientAddressChanged(utlatande.getGrundData().getPatient(), resolvedPatient));
        } catch (IOException e) {
            LOG.error("Failed to getUtlatandeFromJson intygsId {} while checking for updated patient information", intygsId);
        }
        if (!completeAddressProvided(resolvedPatient)) {
            // Overwrite retrieved address data with saved one.
            Patient oldPatientData = null;
            try {
                oldPatientData = moduleRegistry.getModuleApi(intygsTyp).getUtlatandeFromJson(utkast.getModel()).getGrundData().getPatient();
            } catch (IOException e) {
                LOG.error("Error while using the module api to convert json to Utlatande for intygsId {}", intygsId);
            }
            copyOldAddressToNewPatientData(oldPatientData, resolvedPatient);
        }
        // Update the internal model with the resolved patient. This means the draft may be updated
        // with new patient info on the next auto-save!
        String updatedModel = moduleRegistry.getModuleApi(intygsTyp).updateBeforeSave(utkast.getModel(), resolvedPatient);
        utkast.setModel(updatedModel);
        draftHolder.setContent(utkast.getModel());
        return Response.ok(draftHolder).build();
    } catch (ModuleException | ModuleNotFoundException e) {
        throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, e.getMessage());
    }
}
Also used : ModuleNotFoundException(se.inera.intyg.common.support.modules.registry.ModuleNotFoundException) DraftHolder(se.inera.intyg.webcert.web.web.controller.moduleapi.dto.DraftHolder) Utkast(se.inera.intyg.webcert.persistence.utkast.model.Utkast) Utlatande(se.inera.intyg.common.support.model.common.internal.Utlatande) Patient(se.inera.intyg.common.support.model.common.internal.Patient) Relations(se.inera.intyg.webcert.web.web.controller.api.dto.Relations) IOException(java.io.IOException) ModuleException(se.inera.intyg.common.support.modules.support.api.exception.ModuleException) WebCertServiceException(se.inera.intyg.webcert.common.service.exception.WebCertServiceException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 22 with ModuleException

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

the class CopyUtkastServiceImpl method createUtkastFromTemplate.

@Override
public CreateUtkastFromTemplateResponse createUtkastFromTemplate(CreateUtkastFromTemplateRequest copyRequest) {
    String originalIntygId = copyRequest.getOriginalIntygId();
    LOG.debug("Creating utkast from template certificate '{}'", 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 create utkast from template certificate with id '{}', the certificate is revoked", originalIntygId);
            throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "Original certificate is revoked");
        }
        String intygsTyp = copyRequest.getTyp();
        if (authoritiesValidator.given(user, intygsTyp).features(AuthoritiesConstants.FEATURE_UNIKT_INTYG, AuthoritiesConstants.FEATURE_UNIKT_INTYG_INOM_VG, AuthoritiesConstants.FEATURE_UNIKT_UTKAST_INOM_VG).isVerified()) {
            Personnummer personnummer = copyRequest.containsNyttPatientPersonnummer() ? copyRequest.getNyttPatientPersonnummer() : copyRequest.getPatient().getPersonId();
            Map<String, Map<String, Boolean>> intygstypToStringToBoolean = utkastService.checkIfPersonHasExistingIntyg(personnummer, user);
            Boolean utkastExists = intygstypToStringToBoolean.get("utkast").get(intygsTyp);
            Boolean intygExists = intygstypToStringToBoolean.get("intyg").get(intygsTyp);
            if (utkastExists != null && utkastExists) {
                if (authoritiesValidator.given(user, intygsTyp).features(AuthoritiesConstants.FEATURE_UNIKT_UTKAST_INOM_VG).isVerified()) {
                    throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "Drafts of this type must be unique within this caregiver.");
                }
            }
            if (intygExists != null) {
                if (authoritiesValidator.given(user, intygsTyp).features(AuthoritiesConstants.FEATURE_UNIKT_INTYG).isVerified()) {
                    throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "Certificates of this type must be globally unique.");
                } else if (intygExists && authoritiesValidator.given(user, intygsTyp).features(AuthoritiesConstants.FEATURE_UNIKT_INTYG_INOM_VG).isVerified()) {
                    throw new WebCertServiceException(WebCertServiceErrorCodeEnum.INVALID_STATE, "Certificates of this type must be unique within this caregiver.");
                }
            }
        }
        verifyNotReplacedWithSigned(copyRequest.getOriginalIntygId(), "create utkast from template");
        CopyUtkastBuilderResponse builderResponse = buildUtkastFromTemplateBuilderResponse(copyRequest, originalIntygId, true, coherentJournaling);
        Utkast savedUtkast = saveAndNotify(builderResponse, user);
        if (copyRequest.isDjupintegrerad()) {
            checkIntegreradEnhet(builderResponse);
        }
        return new CreateUtkastFromTemplateResponse(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 : CreateUtkastFromTemplateResponse(se.inera.intyg.webcert.web.service.utkast.dto.CreateUtkastFromTemplateResponse) ModuleNotFoundException(se.inera.intyg.common.support.modules.registry.ModuleNotFoundException) CopyUtkastBuilderResponse(se.inera.intyg.webcert.web.service.utkast.dto.CopyUtkastBuilderResponse) WebCertServiceException(se.inera.intyg.webcert.common.service.exception.WebCertServiceException) Personnummer(se.inera.intyg.schemas.contract.Personnummer) Utkast(se.inera.intyg.webcert.persistence.utkast.model.Utkast) ModuleException(se.inera.intyg.common.support.modules.support.api.exception.ModuleException) Map(java.util.Map) WebCertUser(se.inera.intyg.webcert.web.service.user.dto.WebCertUser)

Example 23 with ModuleException

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

the class UtkastServiceImpl method getPopulatedModelFromIntygModule.

private String getPopulatedModelFromIntygModule(String intygType, CreateNewDraftHolder draftRequest) {
    LOG.debug("Calling module '{}' to get populated model", intygType);
    String modelAsJson;
    try {
        ModuleApi moduleApi = moduleRegistry.getModuleApi(intygType);
        modelAsJson = moduleApi.createNewInternal(draftRequest);
    } catch (ModuleException | ModuleNotFoundException me) {
        throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, me);
    }
    LOG.debug("Got populated model of {} chars from module '{}'", getSafeLength(modelAsJson), intygType);
    return modelAsJson;
}
Also used : ModuleApi(se.inera.intyg.common.support.modules.support.api.ModuleApi) ModuleNotFoundException(se.inera.intyg.common.support.modules.registry.ModuleNotFoundException) ModuleException(se.inera.intyg.common.support.modules.support.api.exception.ModuleException) WebCertServiceException(se.inera.intyg.webcert.common.service.exception.WebCertServiceException)

Example 24 with ModuleException

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

the class CertificateRevokeProcessor method process.

public void process(@Body String xmlBody, @Header(Constants.INTYGS_ID) String intygsId, @Header(Constants.LOGICAL_ADDRESS) String logicalAddress, @Header(Constants.INTYGS_TYP) String intygsTyp) throws TemporaryException, PermanentException {
    checkArgument(!Strings.isNullOrEmpty(intygsId), "Message of type %s does not have a %s header.", Constants.REVOKE_MESSAGE, Constants.INTYGS_ID);
    checkArgument(!Strings.isNullOrEmpty(logicalAddress), "Message of type %s does not have a %s header.", Constants.REVOKE_MESSAGE, Constants.LOGICAL_ADDRESS);
    checkArgument(!Strings.isNullOrEmpty(intygsTyp), "Message of type %s does not have a %s header.", Constants.REVOKE_MESSAGE, Constants.INTYGS_TYP);
    try {
        ModuleApi moduleApi = registry.getModuleApi(intygsTyp);
        moduleApi.revokeCertificate(xmlBody, logicalAddress);
    } catch (ExternalServiceCallException e) {
        switch(e.getErroIdEnum()) {
            case TECHNICAL_ERROR:
            case APPLICATION_ERROR:
                throw new TemporaryException(e.getMessage());
            default:
                throw new PermanentException(e.getMessage());
        }
    } catch (ModuleException e) {
        throw new PermanentException(e.getMessage());
    } catch (WebServiceException e) {
        throw new TemporaryException(e.getMessage());
    } catch (Exception e) {
        throw new PermanentException(e.getMessage());
    }
}
Also used : ModuleApi(se.inera.intyg.common.support.modules.support.api.ModuleApi) TemporaryException(se.inera.intyg.webcert.common.sender.exception.TemporaryException) WebServiceException(javax.xml.ws.WebServiceException) PermanentException(se.inera.intyg.webcert.common.sender.exception.PermanentException) ModuleException(se.inera.intyg.common.support.modules.support.api.exception.ModuleException) ExternalServiceCallException(se.inera.intyg.common.support.modules.support.api.exception.ExternalServiceCallException) ModuleException(se.inera.intyg.common.support.modules.support.api.exception.ModuleException) TemporaryException(se.inera.intyg.webcert.common.sender.exception.TemporaryException) PermanentException(se.inera.intyg.webcert.common.sender.exception.PermanentException) WebServiceException(javax.xml.ws.WebServiceException) ExternalServiceCallException(se.inera.intyg.common.support.modules.support.api.exception.ExternalServiceCallException)

Example 25 with ModuleException

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

the class IntygServiceImpl method buildIntygContentHolderForUtkast.

// NOTE! INTYG-4086. This method is used when fetching Intyg/Utkast from WC locally. The question is, should we
// replace the patient on the existing model with a freshly fetched one here or not? In case we're storing patient
// info entered manually for non FK-types here, we may end up overwriting a manually stored name etc...
private IntygContentHolder buildIntygContentHolderForUtkast(Utkast utkast, boolean relations) {
    try {
        // INTYG-4086: Patient object populated according to ruleset for the intygstyp at hand.
        Patient newPatientData = patientDetailsResolver.resolvePatient(utkast.getPatientPersonnummer(), utkast.getIntygsTyp());
        // create an "empty" Patient with personnummer only.
        if (newPatientData == null) {
            newPatientData = new Patient();
            newPatientData.setPersonId(utkast.getPatientPersonnummer());
        }
        // INTYG-5354, INTYG-5380: Don't use incomplete address from external data sources (PU/js).
        Utlatande utlatande = modelFacade.getUtlatandeFromInternalModel(utkast.getIntygsTyp(), utkast.getModel());
        if (!completeAddressProvided(newPatientData)) {
            // Use the old address data.
            Patient oldPatientData = utlatande.getGrundData().getPatient();
            copyOldAddressToNewPatientData(oldPatientData, newPatientData);
        }
        String updatedModel = moduleRegistry.getModuleApi(utkast.getIntygsTyp()).updateBeforeSave(utkast.getModel(), newPatientData);
        utlatande = modelFacade.getUtlatandeFromInternalModel(utkast.getIntygsTyp(), updatedModel);
        List<Status> statuses = IntygConverterUtil.buildStatusesFromUtkast(utkast);
        Relations certificateRelations = certificateRelationService.getRelations(utkast.getIntygsId());
        final SekretessStatus sekretessStatus = patientDetailsResolver.getSekretessStatus(newPatientData.getPersonId());
        if (SekretessStatus.UNDEFINED.equals(sekretessStatus)) {
            throw new WebCertServiceException(WebCertServiceErrorCodeEnum.PU_PROBLEM, "Sekretesstatus could not be fetched from the PU service");
        }
        final boolean sekretessmarkerad = SekretessStatus.TRUE.equals(sekretessStatus);
        boolean patientNameChanged = patientDetailsResolver.isPatientNamedChanged(utlatande.getGrundData().getPatient(), newPatientData);
        boolean patientAddressChanged = patientDetailsResolver.isPatientAddressChanged(utlatande.getGrundData().getPatient(), newPatientData);
        return IntygContentHolder.builder().setContents(updatedModel).setUtlatande(utlatande).setStatuses(statuses).setRevoked(utkast.getAterkalladDatum() != null).setRelations(certificateRelations).setCreated(utkast.getSkapad()).setDeceased(isDeceased(utkast.getPatientPersonnummer())).setSekretessmarkering(sekretessmarkerad).setPatientNameChangedInPU(patientNameChanged).setPatientAddressChangedInPU(patientAddressChanged).build();
    } catch (ModuleException | ModuleNotFoundException e) {
        throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, e);
    }
}
Also used : UtkastStatus(se.inera.intyg.webcert.common.model.UtkastStatus) SekretessStatus(se.inera.intyg.webcert.common.model.SekretessStatus) Status(se.inera.intyg.common.support.model.Status) ModuleNotFoundException(se.inera.intyg.common.support.modules.registry.ModuleNotFoundException) SekretessStatus(se.inera.intyg.webcert.common.model.SekretessStatus) Utlatande(se.inera.intyg.common.support.model.common.internal.Utlatande) Patient(se.inera.intyg.common.support.model.common.internal.Patient) Relations(se.inera.intyg.webcert.web.web.controller.api.dto.Relations) ModuleException(se.inera.intyg.common.support.modules.support.api.exception.ModuleException) WebCertServiceException(se.inera.intyg.webcert.common.service.exception.WebCertServiceException)

Aggregations

ModuleException (se.inera.intyg.common.support.modules.support.api.exception.ModuleException)27 ModuleNotFoundException (se.inera.intyg.common.support.modules.registry.ModuleNotFoundException)16 WebCertServiceException (se.inera.intyg.webcert.common.service.exception.WebCertServiceException)14 ModuleApi (se.inera.intyg.common.support.modules.support.api.ModuleApi)13 Utkast (se.inera.intyg.webcert.persistence.utkast.model.Utkast)12 IOException (java.io.IOException)9 Utlatande (se.inera.intyg.common.support.model.common.internal.Utlatande)7 Test (org.junit.Test)6 WebCertUser (se.inera.intyg.webcert.web.service.user.dto.WebCertUser)6 CopyUtkastBuilderResponse (se.inera.intyg.webcert.web.service.utkast.dto.CopyUtkastBuilderResponse)6 Transactional (org.springframework.transaction.annotation.Transactional)4 Patient (se.inera.intyg.common.support.model.common.internal.Patient)4 UtkastStatus (se.inera.intyg.webcert.common.model.UtkastStatus)4 Personnummer (se.inera.intyg.schemas.contract.Personnummer)3 Relations (se.inera.intyg.webcert.web.web.controller.api.dto.Relations)3 LocalDateTime (java.time.LocalDateTime)2 OptimisticLockException (javax.persistence.OptimisticLockException)2 WebServiceException (javax.xml.ws.WebServiceException)2 Status (se.inera.intyg.common.support.model.Status)2 Relation (se.inera.intyg.common.support.model.common.internal.Relation)2