Search in sources :

Example 1 with AttachmentNotFoundException

use of uk.nhs.adaptors.pss.translator.exception.AttachmentNotFoundException 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 2 with AttachmentNotFoundException

use of uk.nhs.adaptors.pss.translator.exception.AttachmentNotFoundException 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;
// }
}
Also used : AttachmentNotFoundException(uk.nhs.adaptors.pss.translator.exception.AttachmentNotFoundException) JAXBException(javax.xml.bind.JAXBException) InboundMessage(uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage) JMSException(javax.jms.JMSException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) AttachmentLogException(uk.nhs.adaptors.pss.translator.exception.AttachmentLogException) DataFormatException(ca.uhn.fhir.parser.DataFormatException) BundleMappingException(uk.nhs.adaptors.pss.translator.exception.BundleMappingException) ParseException(java.text.ParseException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) InlineAttachmentProcessingException(uk.nhs.adaptors.pss.translator.exception.InlineAttachmentProcessingException)

Example 3 with AttachmentNotFoundException

use of uk.nhs.adaptors.pss.translator.exception.AttachmentNotFoundException in project nia-patient-switching-standard-adaptor by NHSDigital.

the class AttachmentReferenceUpdaterService method updateReferenceToAttachment.

public String updateReferenceToAttachment(List<InboundMessage.Attachment> attachments, String conversationId, String payloadStr) throws ValidationException, AttachmentNotFoundException, InlineAttachmentProcessingException {
    if (conversationId == null || conversationId.isEmpty()) {
        throw new ValidationException("ConversationId cannot be null or empty");
    }
    String resultPayload = payloadStr;
    if (attachments != null) {
        for (InboundMessage.Attachment attachment : attachments) {
            try {
                if (!xmlParseUtilService.parseIsSkeleton(attachment.getDescription())) {
                    InlineAttachment inlineAttachment = new InlineAttachment(attachment);
                    String filename = inlineAttachment.getOriginalFilename();
                    // find "local" reference by finding the following:
                    // "<reference value=\"file://localhost/${filename}\" />"
                    var patternStr = String.format("<reference value=\"file://localhost/%s\" \\/>", filename);
                    Pattern pattern = Pattern.compile(patternStr);
                    Matcher matcher = pattern.matcher(resultPayload);
                    var matchFound = matcher.find();
                    if (matchFound) {
                        // update local ref with external reference
                        String fileLocation = storageManagerService.getFileLocation(filename, conversationId);
                        var replaceStr = String.format("<reference value=\"%s\" />", xmlEscape(fileLocation));
                        resultPayload = matcher.replaceAll(replaceStr);
                    } else {
                        var message = String.format("Could not find file %s in payload", filename);
                        throw new AttachmentNotFoundException(message);
                    }
                }
            } catch (ParseException ex) {
                throw new InlineAttachmentProcessingException("Unable to parse inline attachment description: " + ex.getMessage());
            } catch (AttachmentNotFoundException e) {
                throw new AttachmentNotFoundException(e.getMessage());
            }
        }
    }
    return resultPayload;
}
Also used : Pattern(java.util.regex.Pattern) AttachmentNotFoundException(uk.nhs.adaptors.pss.translator.exception.AttachmentNotFoundException) ValidationException(javax.xml.bind.ValidationException) Matcher(java.util.regex.Matcher) InboundMessage(uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage) InlineAttachment(uk.nhs.adaptors.pss.translator.model.InlineAttachment) ParseException(java.text.ParseException) InlineAttachmentProcessingException(uk.nhs.adaptors.pss.translator.exception.InlineAttachmentProcessingException)

Example 4 with AttachmentNotFoundException

use of uk.nhs.adaptors.pss.translator.exception.AttachmentNotFoundException 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);
    }
}
Also used : AttachmentNotFoundException(uk.nhs.adaptors.pss.translator.exception.AttachmentNotFoundException) ValidationException(javax.xml.bind.ValidationException) JAXBException(javax.xml.bind.JAXBException) XmlUnmarshallUtil.unmarshallString(uk.nhs.adaptors.pss.translator.util.XmlUnmarshallUtil.unmarshallString) SAXException(org.xml.sax.SAXException) PatientMigrationRequest(uk.nhs.adaptors.connector.model.PatientMigrationRequest) BundleMappingException(uk.nhs.adaptors.pss.translator.exception.BundleMappingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) TransformerException(javax.xml.transform.TransformerException) RCMRIN030000UK06Message(org.hl7.v3.RCMRIN030000UK06Message) InlineAttachmentProcessingException(uk.nhs.adaptors.pss.translator.exception.InlineAttachmentProcessingException)

Aggregations

AttachmentNotFoundException (uk.nhs.adaptors.pss.translator.exception.AttachmentNotFoundException)4 InlineAttachmentProcessingException (uk.nhs.adaptors.pss.translator.exception.InlineAttachmentProcessingException)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ParseException (java.text.ParseException)3 SAXException (org.xml.sax.SAXException)3 BundleMappingException (uk.nhs.adaptors.pss.translator.exception.BundleMappingException)3 InboundMessage (uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage)3 DataFormatException (ca.uhn.fhir.parser.DataFormatException)2 JAXBException (javax.xml.bind.JAXBException)2 ValidationException (javax.xml.bind.ValidationException)2 RCMRIN030000UK06Message (org.hl7.v3.RCMRIN030000UK06Message)2 Document (org.w3c.dom.Document)2 PatientMigrationRequest (uk.nhs.adaptors.connector.model.PatientMigrationRequest)2 XmlUnmarshallUtil.unmarshallString (uk.nhs.adaptors.pss.translator.util.XmlUnmarshallUtil.unmarshallString)2 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 JMSException (javax.jms.JMSException)1 TransformerException (javax.xml.transform.TransformerException)1 MigrationStatusLog (uk.nhs.adaptors.connector.model.MigrationStatusLog)1 PatientAttachmentLog (uk.nhs.adaptors.connector.model.PatientAttachmentLog)1