use of uk.nhs.adaptors.connector.model.PatientAttachmentLog in project nia-patient-switching-standard-adaptor by NHSDigital.
the class COPCMessageHandler method handleMessage.
public void handleMessage(InboundMessage inboundMessage, String conversationId) throws JAXBException, InlineAttachmentProcessingException, SAXException, AttachmentLogException, AttachmentNotFoundException, BundleMappingException, JsonProcessingException {
COPCIN000001UK01Message payload = unmarshallString(inboundMessage.getPayload(), COPCIN000001UK01Message.class);
PatientMigrationRequest migrationRequest = migrationRequestDao.getMigrationRequest(conversationId);
try {
Document ebXmlDocument = getEbXmlDocument(inboundMessage);
String messageId = xPathService.getNodeValue(ebXmlDocument, MESSAGE_ID_PATH);
PatientAttachmentLog patientAttachmentLog = patientAttachmentLogService.findAttachmentLog(messageId, conversationId);
// If there is no PatientAttachmentLog for this message then we have receieved a message out of order
if (patientAttachmentLog == null) {
addLogForEarlyFragmentAndStore(inboundMessage, conversationId, payload, ebXmlDocument, migrationRequest.getId());
} else {
if (isManifestMessage(ebXmlDocument)) {
extractFragmentsAndLog(migrationRequest, patientAttachmentLog, conversationId, inboundMessage);
} else {
storeCOPCAttachment(patientAttachmentLog, inboundMessage, conversationId);
patientAttachmentLog.setUploaded(true);
patientAttachmentLogService.updateAttachmentLog(patientAttachmentLog, conversationId);
}
}
nackAckPreparationService.sendAckMessage(payload, conversationId, migrationRequest.getLosingPracticeOdsCode());
checkAndMergeFileParts(inboundMessage, conversationId);
// merge and uncompress large EHR message
if (inboundMessageMergingService.canMergeCompleteBundle(conversationId)) {
inboundMessageMergingService.mergeAndBundleMessage(conversationId);
}
} catch (ParseException | InlineAttachmentProcessingException | ValidationException | SAXException e) {
LOGGER.error("failed to parse COPC_IN000001UK01 ebxml: " + "failed to extract \"mid:\" from xlink:href, before sending the continue message", e);
nackAckPreparationService.sendNackMessage(EHR_EXTRACT_CANNOT_BE_PROCESSED, payload, conversationId);
}
}
use of uk.nhs.adaptors.connector.model.PatientAttachmentLog in project nia-patient-switching-standard-adaptor by NHSDigital.
the class COPCMessageHandler method addLogForEarlyFragmentAndStore.
private void addLogForEarlyFragmentAndStore(InboundMessage inboundMessage, String conversationId, COPCIN000001UK01Message payload, Document ebXmlDocument, int patientId) throws ValidationException, InlineAttachmentProcessingException {
String fragmentMid = getFragmentMidId(ebXmlDocument);
String fileName = getFileNameForFragment(inboundMessage, payload);
PatientAttachmentLog fragmentAttachmentLog = buildFragmentAttachmentLog(fragmentMid, fileName, inboundMessage.getAttachments().get(0).getContentType(), patientId);
storeCOPCAttachment(fragmentAttachmentLog, inboundMessage, conversationId);
fragmentAttachmentLog.setUploaded(true);
patientAttachmentLogService.addAttachmentLog(fragmentAttachmentLog);
}
use of uk.nhs.adaptors.connector.model.PatientAttachmentLog 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.connector.model.PatientAttachmentLog in project nia-patient-switching-standard-adaptor by NHSDigital.
the class COPCMessageHandlerTest method When_MIDFragmentRecordDoesNotHaveAnAttachmentLog_Expect_AttachmentLogToBeCreated.
@Test
public void When_MIDFragmentRecordDoesNotHaveAnAttachmentLog_Expect_AttachmentLogToBeCreated() throws JAXBException, InlineAttachmentProcessingException, SAXException, AttachmentLogException, AttachmentNotFoundException, BundleMappingException, JsonProcessingException {
var childMid = "28B31-4245-4AFC-8DA2-8A40623A5101";
InboundMessage message = new InboundMessage();
prepareFragmentIndexMocks(message);
when(patientAttachmentLogService.findAttachmentLog(MESSAGE_ID, CONVERSATION_ID)).thenReturn(buildPatientAttachmentLog("CBBAE92D-C7E8-4A9C-8887-F5AEBA1F8CE1", null, true));
when(patientAttachmentLogService.findAttachmentLog(childMid, CONVERSATION_ID)).thenReturn(null);
when(patientAttachmentLogService.findAttachmentLog("CBBAE92D-C7E8-4A9C-8887-F5AEBA1F8CE1", CONVERSATION_ID)).thenReturn(buildPatientAttachmentLog("047C22B4-613F-47D3-9A72-44A1758464FB", "CBBAE92D-C7E8-4A9C-8887-F5AEBA1F8CE1", true));
EbxmlReference reference = new EbxmlReference("First instance is always a payload", "mid:xxxx-xxxx-xxxx-xxxx", "docId");
EbxmlReference reference2 = new EbxmlReference("desc", "mid:28B31-4245-4AFC-8DA2-8A40623A5101", "docId");
List<EbxmlReference> attachmentReferenceDescription = new ArrayList<>();
attachmentReferenceDescription.add(reference);
attachmentReferenceDescription.add(reference2);
when(xmlParseUtilService.getEbxmlAttachmentsData(any())).thenReturn(attachmentReferenceDescription);
copcMessageHandler.handleMessage(message, CONVERSATION_ID);
verify(patientAttachmentLogService).addAttachmentLog(patientLogCaptor.capture());
PatientAttachmentLog actual = patientLogCaptor.getValue();
assertEquals(childMid, actual.getMid());
assertEquals("E39E79A2-FA96-48FF-9373-7BBCB9D036E7_0.messageattachment", actual.getFilename());
assertEquals("047C22B4-613F-47D3-9A72-44A1758464FB", actual.getParentMid());
assertEquals(1, actual.getPatientMigrationReqId());
assertEquals("text/plain", actual.getContentType());
assertThat(actual.getCompressed()).isFalse();
assertThat(actual.getLargeAttachment()).isTrue();
assertThat(actual.getBase64()).isTrue();
assertEquals(0, actual.getOrderNum());
}
use of uk.nhs.adaptors.connector.model.PatientAttachmentLog in project nia-patient-switching-standard-adaptor by NHSDigital.
the class COPCMessageHandlerTest method When_FragmentIndexIsRecievedWithCIDAndMIDParts_Expect_CIDMessageToBeProcessed.
@Test
public void When_FragmentIndexIsRecievedWithCIDAndMIDParts_Expect_CIDMessageToBeProcessed() throws JAXBException, InlineAttachmentProcessingException, SAXException, AttachmentLogException, AttachmentNotFoundException, BundleMappingException, JsonProcessingException {
var childMid = "28B31-4245-4AFC-8DA2-8A40623A5101";
var childCid = "435B1171-31F6-4EF2-AD7F-C7E64EEFF357";
InboundMessage message = new InboundMessage();
prepareFragmentIndexWithCidMocks(message);
when(patientAttachmentLogService.findAttachmentLog(MESSAGE_ID, CONVERSATION_ID)).thenReturn(buildPatientAttachmentLog("CBBAE92D-C7E8-4A9C-8887-F5AEBA1F8CE1", null, true));
when(patientAttachmentLogService.findAttachmentLog(childMid, CONVERSATION_ID)).thenReturn(null);
when(patientAttachmentLogService.findAttachmentLog(childCid, CONVERSATION_ID)).thenReturn(null);
when(patientAttachmentLogService.findAttachmentLog("CBBAE92D-C7E8-4A9C-8887-F5AEBA1F8CE1", CONVERSATION_ID)).thenReturn(buildPatientAttachmentLog("047C22B4-613F-47D3-9A72-44A1758464FB", "CBBAE92D-C7E8-4A9C-8887-F5AEBA1F8CE1", true));
EbxmlReference reference = new EbxmlReference("First instance is always a payload", "mid:xxxx-xxxx-xxxx-xxxx", "docId");
EbxmlReference reference1 = new EbxmlReference("desc", "cid:435B1171-31F6-4EF2-AD7F-C7E64EEFF357", "doc1Id");
EbxmlReference reference2 = new EbxmlReference("desc", "mid:28B31-4245-4AFC-8DA2-8A40623A5101", "doc2Id");
List<EbxmlReference> attachmentReferenceDescription = new ArrayList<>();
attachmentReferenceDescription.add(reference);
attachmentReferenceDescription.add(reference1);
attachmentReferenceDescription.add(reference2);
when(xmlParseUtilService.getEbxmlAttachmentsData(any())).thenReturn(attachmentReferenceDescription);
copcMessageHandler.handleMessage(message, CONVERSATION_ID);
verify(attachmentHandlerService).storeAttachments(message.getAttachments(), CONVERSATION_ID);
verify(patientAttachmentLogService, times(2)).addAttachmentLog(patientLogCaptor.capture());
PatientAttachmentLog actualCidAttachmentLog = patientLogCaptor.getAllValues().get(0);
PatientAttachmentLog actualMidAttachmentLog = patientLogCaptor.getAllValues().get(1);
assertEquals(childCid, actualCidAttachmentLog.getMid());
assertEquals(childMid, actualMidAttachmentLog.getMid());
}
Aggregations