Search in sources :

Example 6 with PatientAttachmentLog

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);
    }
}
Also used : PatientMigrationRequest(uk.nhs.adaptors.connector.model.PatientMigrationRequest) ValidationException(javax.xml.bind.ValidationException) PatientAttachmentLog(uk.nhs.adaptors.connector.model.PatientAttachmentLog) XmlUnmarshallUtil.unmarshallString(uk.nhs.adaptors.pss.translator.util.XmlUnmarshallUtil.unmarshallString) ParseException(java.text.ParseException) Document(org.w3c.dom.Document) COPCIN000001UK01Message(org.hl7.v3.COPCIN000001UK01Message) InlineAttachmentProcessingException(uk.nhs.adaptors.pss.translator.exception.InlineAttachmentProcessingException) SAXException(org.xml.sax.SAXException)

Example 7 with PatientAttachmentLog

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);
}
Also used : PatientAttachmentLog(uk.nhs.adaptors.connector.model.PatientAttachmentLog) XmlUnmarshallUtil.unmarshallString(uk.nhs.adaptors.pss.translator.util.XmlUnmarshallUtil.unmarshallString)

Example 8 with PatientAttachmentLog

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;
    }
}
Also used : AttachmentNotFoundException(uk.nhs.adaptors.pss.translator.exception.AttachmentNotFoundException) InboundMessage(uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage) XmlUnmarshallUtil.unmarshallString(uk.nhs.adaptors.pss.translator.util.XmlUnmarshallUtil.unmarshallString) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) PatientMigrationRequest(uk.nhs.adaptors.connector.model.PatientMigrationRequest) DataFormatException(ca.uhn.fhir.parser.DataFormatException) PatientAttachmentLog(uk.nhs.adaptors.connector.model.PatientAttachmentLog) MigrationStatusLog(uk.nhs.adaptors.connector.model.MigrationStatusLog) BundleMappingException(uk.nhs.adaptors.pss.translator.exception.BundleMappingException) ParseException(java.text.ParseException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) StorageException(uk.nhs.adaptors.pss.translator.storage.StorageException) RCMRIN030000UK06Message(org.hl7.v3.RCMRIN030000UK06Message) InlineAttachmentProcessingException(uk.nhs.adaptors.pss.translator.exception.InlineAttachmentProcessingException)

Example 9 with PatientAttachmentLog

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());
}
Also used : PatientAttachmentLog(uk.nhs.adaptors.connector.model.PatientAttachmentLog) ArrayList(java.util.ArrayList) InboundMessage(uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage) EbxmlReference(uk.nhs.adaptors.pss.translator.model.EbxmlReference) Test(org.junit.jupiter.api.Test)

Example 10 with PatientAttachmentLog

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());
}
Also used : PatientAttachmentLog(uk.nhs.adaptors.connector.model.PatientAttachmentLog) ArrayList(java.util.ArrayList) InboundMessage(uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage) EbxmlReference(uk.nhs.adaptors.pss.translator.model.EbxmlReference) Test(org.junit.jupiter.api.Test)

Aggregations

PatientAttachmentLog (uk.nhs.adaptors.connector.model.PatientAttachmentLog)11 InboundMessage (uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage)8 ArrayList (java.util.ArrayList)5 Test (org.junit.jupiter.api.Test)5 EbxmlReference (uk.nhs.adaptors.pss.translator.model.EbxmlReference)5 XmlUnmarshallUtil.unmarshallString (uk.nhs.adaptors.pss.translator.util.XmlUnmarshallUtil.unmarshallString)5 ParseException (java.text.ParseException)4 Document (org.w3c.dom.Document)4 SAXException (org.xml.sax.SAXException)4 PatientMigrationRequest (uk.nhs.adaptors.connector.model.PatientMigrationRequest)4 InlineAttachmentProcessingException (uk.nhs.adaptors.pss.translator.exception.InlineAttachmentProcessingException)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ValidationException (javax.xml.bind.ValidationException)3 COPCIN000001UK01Message (org.hl7.v3.COPCIN000001UK01Message)3 AttachmentNotFoundException (uk.nhs.adaptors.pss.translator.exception.AttachmentNotFoundException)3 BundleMappingException (uk.nhs.adaptors.pss.translator.exception.BundleMappingException)3 Arrays (java.util.Arrays)2 Comparator (java.util.Comparator)2 List (java.util.List)2 JAXBException (javax.xml.bind.JAXBException)2