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("");
}
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());
}
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);
}
Aggregations