Search in sources :

Example 1 with SdsRetrievalException

use of uk.nhs.adaptors.pss.translator.exception.SdsRetrievalException in project nia-patient-switching-standard-adaptor by NHSDigital.

the class EHRTimeoutHandler method handleMigrationTimeout.

private void handleMigrationTimeout(PatientMigrationRequest migrationRequest) {
    String conversationId = migrationRequest.getConversationId();
    mdcService.applyConversationId(conversationId);
    try {
        long timeout;
        Duration ehrPersistDuration = persistDurationService.getPersistDurationFor(migrationRequest, EHR_EXTRACT_MESSAGE_NAME);
        InboundMessage message = inboundMessageUtil.readMessage(migrationRequest.getInboundMessage());
        ZonedDateTime messageTimestamp = inboundMessageUtil.parseMessageTimestamp(message.getEbXML());
        ZonedDateTime currentTime = ZonedDateTime.now(messageTimestamp.getZone());
        long numberCOPCMessages = patientAttachmentLogService.countAttachmentsForMigrationRequest(migrationRequest.getId());
        if (numberCOPCMessages > 0) {
            Duration copcPersistDuration = persistDurationService.getPersistDurationFor(migrationRequest, COPC_MESSAGE_NAME);
            timeout = (timeoutProperties.getEhrExtractWeighting() * ehrPersistDuration.getSeconds()) + (timeoutProperties.getCopcWeighting() * numberCOPCMessages * copcPersistDuration.getSeconds());
            LOGGER.debug("Large message timeout calculated as [{}] seconds", timeout);
        } else {
            timeout = timeoutProperties.getEhrExtractWeighting() * ehrPersistDuration.getSeconds();
            LOGGER.debug("Non large message timeout calculated as [{}] seconds", timeout);
        }
        ZonedDateTime timeoutDateTime = messageTimestamp.plusSeconds(timeout);
        LOGGER.debug("Timeout datetime calculated as [{}]", timeoutDateTime);
        if (timeoutDateTime.isBefore(currentTime)) {
            LOGGER.info("Migration timed out at [{}]", timeoutDateTime);
            sendNackMessage(message, conversationId);
        }
    } catch (SdsRetrievalException e) {
        LOGGER.error("Error retrieving persist duration: [{}]", e.getMessage());
    } catch (JsonProcessingException | SAXException | DateTimeParseException | JAXBException e) {
        LOGGER.error("Error parsing inbound message from database");
        migrationStatusLogService.addMigrationStatusLog(EHR_GENERAL_PROCESSING_ERROR, conversationId);
    }
    mdcService.applyConversationId("");
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) ZonedDateTime(java.time.ZonedDateTime) JAXBException(javax.xml.bind.JAXBException) InboundMessage(uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage) Duration(java.time.Duration) XmlUnmarshallUtil.unmarshallString(uk.nhs.adaptors.pss.translator.util.XmlUnmarshallUtil.unmarshallString) SdsRetrievalException(uk.nhs.adaptors.pss.translator.exception.SdsRetrievalException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SAXException(org.xml.sax.SAXException)

Example 2 with SdsRetrievalException

use of uk.nhs.adaptors.pss.translator.exception.SdsRetrievalException in project nia-patient-switching-standard-adaptor by NHSDigital.

the class EHRTimeoutHandlerTest method When_CheckForTimeouts_WithSdsRetrievalException_Expect_MigrationLogNotUpdated.

@Test
public void When_CheckForTimeouts_WithSdsRetrievalException_Expect_MigrationLogNotUpdated() {
    List<PatientMigrationRequest> requests = List.of(mockRequest);
    when(migrationRequestService.getMigrationRequestByCurrentMigrationStatus(EHR_EXTRACT_TRANSLATED)).thenReturn(requests);
    when(persistDurationService.getPersistDurationFor(any(), eq(EHR_EXTRACT_MESSAGE_NAME))).thenThrow(new SdsRetrievalException("Test exception"));
    ehrTimeoutHandler.checkForTimeouts();
    verify(migrationStatusLogService, times(0)).addMigrationStatusLog(any(), any());
}
Also used : PatientMigrationRequest(uk.nhs.adaptors.connector.model.PatientMigrationRequest) SdsRetrievalException(uk.nhs.adaptors.pss.translator.exception.SdsRetrievalException) Test(org.junit.jupiter.api.Test)

Example 3 with SdsRetrievalException

use of uk.nhs.adaptors.pss.translator.exception.SdsRetrievalException in project nia-patient-switching-standard-adaptor by NHSDigital.

the class SDSService method parsePersistDuration.

private Duration parsePersistDuration(String sdsResponse) throws SdsRetrievalException {
    Bundle bundle;
    try {
        bundle = fhirParser.parseResource(sdsResponse, Bundle.class);
    } catch (FhirValidationException e) {
        throw new SdsRetrievalException(e.getMessage());
    }
    List<Bundle.BundleEntryComponent> entries = bundle.getEntry();
    if (entries.isEmpty()) {
        throw new SdsRetrievalException("sds response doesn't contain any results");
    }
    Resource resource = entries.get(0).getResource();
    Property matchingChildren = resource.getChildByName(EXTENSION_KEY_VALUE);
    Optional<Extension> extensions = matchingChildren.getValues().stream().map(child -> (Extension) child).findFirst();
    Optional<Extension> persistDuration = extensions.orElseThrow(() -> new SdsRetrievalException("Error parsing persist duration extension")).getExtensionsByUrl(PERSIST_DURATION_URL).stream().findFirst();
    String isoDuration = persistDuration.orElseThrow(() -> new SdsRetrievalException("Error parsing persist duration value")).getValue().toString();
    return Duration.parse(isoDuration);
}
Also used : Extension(org.hl7.fhir.dstu3.model.Extension) Bundle(org.hl7.fhir.dstu3.model.Bundle) Resource(org.hl7.fhir.dstu3.model.Resource) Autowired(org.springframework.beans.factory.annotation.Autowired) Extension(org.hl7.fhir.dstu3.model.Extension) FhirParser(uk.nhs.adaptors.common.util.fhir.FhirParser) SdsRequestBuilder(uk.nhs.adaptors.pss.translator.sds.SdsRequestBuilder) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Service(org.springframework.stereotype.Service) Duration(java.time.Duration) FhirValidationException(uk.nhs.adaptors.common.exception.FhirValidationException) Optional(java.util.Optional) WebClientResponseException(org.springframework.web.reactive.function.client.WebClientResponseException) AllArgsConstructor(lombok.AllArgsConstructor) Property(org.hl7.fhir.dstu3.model.Property) SdsRetrievalException(uk.nhs.adaptors.pss.translator.exception.SdsRetrievalException) Bundle(org.hl7.fhir.dstu3.model.Bundle) Resource(org.hl7.fhir.dstu3.model.Resource) SdsRetrievalException(uk.nhs.adaptors.pss.translator.exception.SdsRetrievalException) FhirValidationException(uk.nhs.adaptors.common.exception.FhirValidationException) Property(org.hl7.fhir.dstu3.model.Property)

Aggregations

SdsRetrievalException (uk.nhs.adaptors.pss.translator.exception.SdsRetrievalException)3 Duration (java.time.Duration)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ZonedDateTime (java.time.ZonedDateTime)1 DateTimeParseException (java.time.format.DateTimeParseException)1 List (java.util.List)1 Optional (java.util.Optional)1 JAXBException (javax.xml.bind.JAXBException)1 AllArgsConstructor (lombok.AllArgsConstructor)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Bundle (org.hl7.fhir.dstu3.model.Bundle)1 Extension (org.hl7.fhir.dstu3.model.Extension)1 Property (org.hl7.fhir.dstu3.model.Property)1 Resource (org.hl7.fhir.dstu3.model.Resource)1 Test (org.junit.jupiter.api.Test)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Service (org.springframework.stereotype.Service)1 WebClientResponseException (org.springframework.web.reactive.function.client.WebClientResponseException)1 SAXException (org.xml.sax.SAXException)1 FhirValidationException (uk.nhs.adaptors.common.exception.FhirValidationException)1