Search in sources :

Example 51 with IndexOf

use of org.hl7.elm.r1.IndexOf in project nia-patient-switching-standard-adaptor by NHSDigital.

the class COPCMessageHandler method extractFragmentsAndLog.

private void extractFragmentsAndLog(PatientMigrationRequest migrationRequest, PatientAttachmentLog parentAttachmentLog, String conversationId, InboundMessage message) throws ParseException, SAXException, ValidationException, InlineAttachmentProcessingException {
    List<EbxmlReference> attachmentReferenceDescription = new ArrayList<>();
    attachmentReferenceDescription.addAll(xmlParseUtilService.getEbxmlAttachmentsData(message));
    // first item is always the message payload reference so skip it
    for (var index = 1; index < attachmentReferenceDescription.size(); index++) {
        var payloadReference = attachmentReferenceDescription.get(index);
        var descriptionString = "";
        var messageId = "";
        var fileUpload = false;
        // in this instance there should only ever be one CID on a fragment index file
        if (payloadReference.getHref().contains("cid:")) {
            messageId = payloadReference.getHref().substring(payloadReference.getHref().indexOf("cid:") + "cid:".length());
            descriptionString = message.getAttachments().get(0).getDescription();
            // upload the file
            attachmentHandlerService.storeAttachments(message.getAttachments(), conversationId);
            fileUpload = true;
        } else {
            var localMessageId = payloadReference.getHref().substring(payloadReference.getHref().indexOf("mid:") + "mid:".length());
            messageId = localMessageId;
            var externalAttachmentResult = message.getExternalAttachments().stream().filter(attachment -> attachment.getMessageId().equals(localMessageId)).findFirst();
            if (externalAttachmentResult == null || externalAttachmentResult.stream().count() != 1) {
                throw new ValidationException("External Attachment in payload header does not match a received External Attachment ID");
            }
            var externalAttachment = externalAttachmentResult.get();
            descriptionString = externalAttachment.getDescription();
        }
        PatientAttachmentLog fragmentLog = patientAttachmentLogService.findAttachmentLog(messageId, conversationId);
        if (fragmentLog != null) {
            updateFragmentLog(fragmentLog, parentAttachmentLog, descriptionString, index - 1, parentAttachmentLog.getLargeAttachment());
            patientAttachmentLogService.updateAttachmentLog(fragmentLog, conversationId);
        } else {
            PatientAttachmentLog newFragmentLog = buildPatientAttachmentLog(messageId, descriptionString, parentAttachmentLog.getMid(), migrationRequest.getId(), index - 1, fileUpload, parentAttachmentLog.getLargeAttachment());
            patientAttachmentLogService.addAttachmentLog(newFragmentLog);
        }
    }
}
Also used : Arrays(java.util.Arrays) AttachmentNotFoundException(uk.nhs.adaptors.pss.translator.exception.AttachmentNotFoundException) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Autowired(org.springframework.beans.factory.annotation.Autowired) NackAckPreparationService(uk.nhs.adaptors.pss.translator.service.NackAckPreparationService) XmlUnmarshallUtil.unmarshallString(uk.nhs.adaptors.pss.translator.util.XmlUnmarshallUtil.unmarshallString) PatientAttachmentLog(uk.nhs.adaptors.connector.model.PatientAttachmentLog) COPCIN000001UK01Message(org.hl7.v3.COPCIN000001UK01Message) ArrayList(java.util.ArrayList) BundleMappingException(uk.nhs.adaptors.pss.translator.exception.BundleMappingException) Document(org.w3c.dom.Document) ParseException(java.text.ParseException) PatientMigrationRequest(uk.nhs.adaptors.connector.model.PatientMigrationRequest) InboundMessageMergingService(uk.nhs.adaptors.pss.translator.service.InboundMessageMergingService) PatientAttachmentLogService(uk.nhs.adaptors.connector.service.PatientAttachmentLogService) EbxmlReference(uk.nhs.adaptors.pss.translator.model.EbxmlReference) AttachmentHandlerService(uk.nhs.adaptors.pss.translator.service.AttachmentHandlerService) XPathService(uk.nhs.adaptors.pss.translator.service.XPathService) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) AttachmentLogException(uk.nhs.adaptors.pss.translator.exception.AttachmentLogException) InboundMessage(uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage) JAXBException(javax.xml.bind.JAXBException) List(java.util.List) Component(org.springframework.stereotype.Component) Slf4j(lombok.extern.slf4j.Slf4j) SAXException(org.xml.sax.SAXException) InlineAttachmentProcessingException(uk.nhs.adaptors.pss.translator.exception.InlineAttachmentProcessingException) EHR_EXTRACT_CANNOT_BE_PROCESSED(uk.nhs.adaptors.pss.translator.model.NACKReason.EHR_EXTRACT_CANNOT_BE_PROCESSED) ValidationException(javax.xml.bind.ValidationException) PatientMigrationRequestDao(uk.nhs.adaptors.connector.dao.PatientMigrationRequestDao) Comparator(java.util.Comparator) XmlParseUtilService(uk.nhs.adaptors.pss.translator.util.XmlParseUtilService) ValidationException(javax.xml.bind.ValidationException) PatientAttachmentLog(uk.nhs.adaptors.connector.model.PatientAttachmentLog) ArrayList(java.util.ArrayList) EbxmlReference(uk.nhs.adaptors.pss.translator.model.EbxmlReference)

Aggregations

DefinitionException (org.hl7.fhir.exceptions.DefinitionException)12 ArrayList (java.util.ArrayList)11 FHIRException (org.hl7.fhir.exceptions.FHIRException)11 ElementDefinition (org.hl7.fhir.r5.model.ElementDefinition)9 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)8 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)7 FileNotFoundException (java.io.FileNotFoundException)6 IOException (java.io.IOException)6 ContactPoint (org.hl7.fhir.r5.model.ContactPoint)6 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)6 List (java.util.List)5 ElementDefinition (org.hl7.fhir.r4.model.ElementDefinition)5 ElementDefinition (org.hl7.fhir.r4b.model.ElementDefinition)5 File (java.io.File)4 NotImplementedException (org.apache.commons.lang3.NotImplementedException)4 ElementDefinition (org.hl7.fhir.dstu3.model.ElementDefinition)4 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)4 URISyntaxException (java.net.URISyntaxException)3 ParseException (java.text.ParseException)3 IndexOf (io.atlasmap.v2.IndexOf)2