use of uk.nhs.adaptors.pss.translator.exception.BundleMappingException in project nia-patient-switching-standard-adaptor by NHSDigital.
the class EhrExtractMessageHandler method handleMessage.
public void handleMessage(InboundMessage inboundMessage, String conversationId) throws JAXBException, JsonProcessingException, InlineAttachmentProcessingException, BundleMappingException, AttachmentNotFoundException, ParseException, SAXException {
RCMRIN030000UK06Message payload = unmarshallString(inboundMessage.getPayload(), RCMRIN030000UK06Message.class);
PatientMigrationRequest migrationRequest = migrationRequestDao.getMigrationRequest(conversationId);
MigrationStatusLog migrationStatusLog = migrationStatusLogService.getLatestMigrationStatusLog(conversationId);
migrationStatusLogService.addMigrationStatusLog(EHR_EXTRACT_RECEIVED, conversationId);
try {
Document ebXmlDocument = getEbXmlDocument(inboundMessage);
String messageId = xPathService.getNodeValue(ebXmlDocument, MESSAGE_ID_PATH);
boolean hasExternalAttachment = !(inboundMessage.getExternalAttachments() == null || inboundMessage.getExternalAttachments().isEmpty());
// Manage attachments against the EHR message
var attachments = inboundMessage.getAttachments();
if (attachments != null) {
attachmentHandlerService.storeAttachments(attachments, conversationId);
for (var i = 0; i < attachments.size(); i++) {
var attachment = attachments.get(i);
PatientAttachmentLog newAttachmentLog = buildPatientAttachmentLogFromAttachment(messageId, migrationRequest, attachment);
patientAttachmentLogService.addAttachmentLog(newAttachmentLog);
}
}
if (!hasExternalAttachment) {
var fileUpdatedPayload = attachmentReferenceUpdaterService.updateReferenceToAttachment(inboundMessage.getAttachments(), conversationId, inboundMessage.getPayload());
inboundMessage.setPayload(fileUpdatedPayload);
payload = unmarshallString(inboundMessage.getPayload(), RCMRIN030000UK06Message.class);
var bundle = bundleMapperService.mapToBundle(payload, migrationRequest.getLosingPracticeOdsCode());
migrationStatusLogService.updatePatientMigrationRequestAndAddMigrationStatusLog(conversationId, fhirParser.encodeToJson(bundle), objectMapper.writeValueAsString(inboundMessage), EHR_EXTRACT_TRANSLATED);
nackAckPreparationService.sendAckMessage(payload, conversationId);
}
// sending continue message
if (hasExternalAttachment) {
String patientNhsNumber = XmlParseUtilService.parseNhsNumber(payload);
for (InboundMessage.ExternalAttachment externalAttachment : inboundMessage.getExternalAttachments()) {
PatientAttachmentLog patientAttachmentLog;
// save COPC_UK01 messages
patientAttachmentLog = buildPatientAttachmentLogFromExternalAttachment(migrationRequest, externalAttachment);
patientAttachmentLogService.addAttachmentLog(patientAttachmentLog);
}
migrationStatusLogService.updatePatientMigrationRequestAndAddMigrationStatusLog(conversationId, null, objectMapper.writeValueAsString(inboundMessage), EHR_EXTRACT_TRANSLATED);
sendContinueRequest(payload, conversationId, patientNhsNumber, migrationRequest.getWinningPracticeOdsCode(), migrationStatusLog.getDate().toInstant());
}
} catch (BundleMappingException | DataFormatException | JsonProcessingException | InlineAttachmentProcessingException | AttachmentNotFoundException | SAXException | StorageException ex) {
nackAckPreparationService.sendNackMessage(EHR_EXTRACT_CANNOT_BE_PROCESSED, payload, conversationId);
throw ex;
} catch (ParseException ex) {
throw ex;
}
}
use of uk.nhs.adaptors.pss.translator.exception.BundleMappingException in project nia-patient-switching-standard-adaptor by NHSDigital.
the class MhsQueueMessageHandler method handleMessage.
public boolean handleMessage(Message message) {
try {
InboundMessage inboundMessage = readMessage(message);
Document ebXmlDocument = xPathService.parseDocumentFromXml(inboundMessage.getEbXML());
String conversationId = xPathService.getNodeValue(ebXmlDocument, CONVERSATION_ID_PATH);
applyConversationId(conversationId);
String interactionId = xPathService.getNodeValue(ebXmlDocument, INTERACTION_ID_PATH);
if (ACKNOWLEDGEMENT_INTERACTION_ID.equals(interactionId)) {
acknowledgmentMessageHandler.handleMessage(inboundMessage, conversationId);
} else if (EHR_EXTRACT_INTERACTION_ID.equals(interactionId)) {
ehrExtractMessageHandler.handleMessage(inboundMessage, conversationId);
} else if (CONTINUE_ATTACHMENT_INTERACTION_ID.equals(interactionId)) {
continueMessageHandler.handleMessage(inboundMessage, conversationId);
} else {
LOGGER.info("Handling message with [{}] interaction id not implemented", interactionId);
}
return true;
} catch (JMSException | JAXBException | SAXException e) {
LOGGER.error("Unable to read the content of the inbound MHS message", e);
return false;
} catch (JsonProcessingException | DataFormatException e) {
LOGGER.error("Unable to parse messages", e);
return false;
} catch (InlineAttachmentProcessingException | AttachmentLogException e) {
LOGGER.error("Unable to process inline attachments", e);
return false;
} catch (AttachmentNotFoundException e) {
LOGGER.error("Unable to find attachment reference inbound message", e);
return false;
} catch (BundleMappingException e) {
LOGGER.error("Unable to map EHR Extract to FHIR bundle", e);
return false;
} catch (ParseException e) {
LOGGER.error("Unable to parse Ebxml References", e);
return false;
}
// catch (SkeletonEhrProcessingException e) {
// LOGGER.error("Unable to process EhrExtract", e);
// return false;
// }
}
use of uk.nhs.adaptors.pss.translator.exception.BundleMappingException in project nia-patient-switching-standard-adaptor by NHSDigital.
the class EhrExtractMessageHandlerTest method When_HandleMessage_WithMapToBundleThrows_Expect_BundleMappingException.
@Test
public void When_HandleMessage_WithMapToBundleThrows_Expect_BundleMappingException() throws BundleMappingException, AttachmentNotFoundException, ValidationException, InlineAttachmentProcessingException {
InboundMessage inboundMessage = new InboundMessage();
inboundMessage.setPayload(readInboundMessagePayloadFromFile());
inboundMessage.setExternalAttachments(new ArrayList<>());
PatientMigrationRequest migrationRequest = PatientMigrationRequest.builder().losingPracticeOdsCode(LOSING_ODE_CODE).winningPracticeOdsCode(WINNING_ODE_CODE).build();
when(migrationRequestDao.getMigrationRequest(CONVERSATION_ID)).thenReturn(migrationRequest);
when(attachmentReferenceUpdaterService.updateReferenceToAttachment(inboundMessage.getAttachments(), CONVERSATION_ID, inboundMessage.getPayload())).thenReturn(inboundMessage.getPayload());
doThrow(new BundleMappingException("Test Exception")).when(bundleMapperService).mapToBundle(any(RCMRIN030000UK06Message.class), any());
assertThrows(BundleMappingException.class, () -> ehrExtractMessageHandler.handleMessage(inboundMessage, CONVERSATION_ID));
}
use of uk.nhs.adaptors.pss.translator.exception.BundleMappingException in project nia-patient-switching-standard-adaptor by NHSDigital.
the class BundleMapperService method mapToBundle.
public Bundle mapToBundle(RCMRIN030000UK06Message xmlMessage, String losingPracticeOdsCode) throws BundleMappingException {
try {
Bundle bundle = generator.generateBundle();
final RCMRMT030101UK04EhrExtract ehrExtract = getEhrExtract(xmlMessage);
final RCMRMT030101UK04EhrFolder ehrFolder = getEhrFolder(xmlMessage);
var agents = mapAgentDirectories(ehrFolder);
var patient = mapPatient(getEhrExtract(xmlMessage), getPatientOrganization(agents));
addEntry(bundle, patient);
Organization authorOrg = organizationMapper.mapAuthorOrganization(losingPracticeOdsCode);
addEntry(bundle, authorOrg);
addEntries(bundle, agents);
var mappedEncounterEhrCompositions = mapEncounters(ehrExtract, patient, losingPracticeOdsCode);
var encounters = handleMappedEncounterResources(mappedEncounterEhrCompositions, bundle);
var locations = mapLocations(ehrFolder, losingPracticeOdsCode);
addEntries(bundle, locations);
var procedureRequests = procedureRequestMapper.mapResources(ehrExtract, patient, encounters, losingPracticeOdsCode);
addEntries(bundle, procedureRequests);
var referralRequests = referralRequestMapper.mapResources(ehrExtract, patient, encounters, losingPracticeOdsCode);
addEntries(bundle, referralRequests);
var medicationResources = medicationRequestMapper.mapResources(ehrExtract, patient, encounters, losingPracticeOdsCode);
addEntries(bundle, medicationResources);
var bloodPressures = bloodPressureMapper.mapResources(ehrExtract, patient, encounters, losingPracticeOdsCode);
addEntries(bundle, bloodPressures);
var observations = observationMapper.mapResources(ehrExtract, patient, encounters, losingPracticeOdsCode);
addEntries(bundle, observations);
var immunizations = immunizationMapper.mapResources(ehrExtract, patient, encounters, losingPracticeOdsCode);
addEntries(bundle, immunizations);
var conditions = conditionMapper.mapResources(ehrExtract, patient, encounters, losingPracticeOdsCode);
addEntries(bundle, conditions);
var observationComments = observationCommentMapper.mapResources(ehrExtract, patient, encounters, losingPracticeOdsCode);
addEntries(bundle, observationComments);
var documentReferences = documentReferenceMapper.mapResources(ehrExtract, patient, encounters, authorOrg);
addEntries(bundle, documentReferences);
var templates = templateMapper.mapResources(ehrExtract, patient, encounters, losingPracticeOdsCode);
addEntries(bundle, templates);
var allergyIntolerances = allergyIntoleranceMapper.mapResources(ehrExtract, patient, encounters, losingPracticeOdsCode);
addEntries(bundle, allergyIntolerances);
mapDiagnosticReports(bundle, ehrExtract, patient, encounters, observations, observationComments, losingPracticeOdsCode);
conditionMapper.addReferences(bundle, conditions, ehrExtract);
unknownPractitionerHandler.updateUnknownPractitionersRefs(bundle);
LOGGER.debug("Mapped Bundle with [{}] entries", bundle.getEntry().size());
return bundle;
} catch (Exception e) {
throw new BundleMappingException(e.getMessage());
}
}
use of uk.nhs.adaptors.pss.translator.exception.BundleMappingException in project nia-patient-switching-standard-adaptor by NHSDigital.
the class InboundMessageMergingService method mergeAndBundleMessage.
public void mergeAndBundleMessage(String conversationId) throws JAXBException, JsonProcessingException {
if (!StringUtils.hasText(conversationId)) {
throw new ValidationException(CONVERSATION_ID_HAS_NOT_BEEN_GIVEN);
}
PatientMigrationRequest migrationRequest = migrationRequestDao.getMigrationRequest(conversationId);
var inboundMessage = objectMapper.readValue(migrationRequest.getInboundMessage(), InboundMessage.class);
RCMRIN030000UK06Message payload = unmarshallString(inboundMessage.getPayload(), RCMRIN030000UK06Message.class);
try {
var attachmentLogs = getUndeletedLogsForConversation(conversationId);
var attachmentsContainSkeletonMessage = attachmentLogs.stream().anyMatch(log -> log.getSkeleton().equals(true));
if (attachmentsContainSkeletonMessage) {
findAndReplaceSkeleton(attachmentLogs, inboundMessage, conversationId);
}
// process attachments
var bypassPayloadLoadingArray = new String[attachmentLogs.size()];
Arrays.fill(bypassPayloadLoadingArray, "");
var messageAttachments = attachmentHandlerService.buildInboundAttachmentsFromAttachmentLogs(attachmentLogs, Arrays.asList(bypassPayloadLoadingArray), conversationId);
var newPayloadStr = attachmentReferenceUpdaterService.updateReferenceToAttachment(messageAttachments, conversationId, inboundMessage.getPayload());
// process bundle
inboundMessage.setPayload(newPayloadStr);
payload = unmarshallString(inboundMessage.getPayload(), RCMRIN030000UK06Message.class);
var bundle = bundleMapperService.mapToBundle(payload, migrationRequest.getLosingPracticeOdsCode());
migrationStatusLogService.updatePatientMigrationRequestAndAddMigrationStatusLog(conversationId, fhirParser.encodeToJson(bundle), objectMapper.writeValueAsString(inboundMessage), EHR_EXTRACT_TRANSLATED);
} catch (InlineAttachmentProcessingException | SAXException | TransformerException | BundleMappingException | JAXBException | AttachmentNotFoundException | JsonProcessingException e) {
LOGGER.error("failed to merge Large Message Parts", e);
nackAckPreparationService.sendNackMessage(EHR_EXTRACT_CANNOT_BE_PROCESSED, payload, conversationId);
}
}
Aggregations