Search in sources :

Example 1 with StorageException

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

the class AttachmentHandlerService method storeAttachments.

public void storeAttachments(List<InboundMessage.Attachment> attachments, String conversationId) throws ValidationException, InlineAttachmentProcessingException {
    if (!StringUtils.hasText(conversationId)) {
        throw new ValidationException("ConversationId cannot be null or empty");
    }
    if (attachments != null) {
        for (InboundMessage.Attachment attachment : attachments) {
            try {
                InlineAttachment inlineAttachment = new InlineAttachment(attachment);
                byte[] decodedPayload = Base64.getMimeDecoder().decode(inlineAttachment.getPayload());
                byte[] payload;
                if (inlineAttachment.isCompressed()) {
                    GZIPInputStream inputStream = new GZIPInputStream(new ByteArrayInputStream(decodedPayload));
                    payload = inputStream.readAllBytes();
                } else {
                    payload = decodedPayload;
                }
                StorageDataUploadWrapper dataWrapper = new StorageDataUploadWrapper(attachment.getContentType(), conversationId, payload);
                String filename = inlineAttachment.getOriginalFilename();
                storageManagerService.uploadFile(filename, dataWrapper, conversationId);
            } catch (StorageException ex) {
                throw new InlineAttachmentProcessingException("Unable to upload inline attachment to storage: " + ex.getMessage());
            } catch (IOException ex) {
                throw new InlineAttachmentProcessingException("Unable to decompress attachment: " + ex.getMessage());
            } catch (ParseException ex) {
                throw new InlineAttachmentProcessingException("Unable to parse inline attachment description: " + ex.getMessage());
            }
        }
    }
}
Also used : ValidationException(javax.xml.bind.ValidationException) InboundMessage(uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage) IOException(java.io.IOException) GZIPInputStream(java.util.zip.GZIPInputStream) StorageDataUploadWrapper(uk.nhs.adaptors.pss.translator.storage.StorageDataUploadWrapper) ByteArrayInputStream(java.io.ByteArrayInputStream) InlineAttachment(uk.nhs.adaptors.pss.translator.model.InlineAttachment) ParseException(java.text.ParseException) StorageException(uk.nhs.adaptors.pss.translator.storage.StorageException) InlineAttachmentProcessingException(uk.nhs.adaptors.pss.translator.exception.InlineAttachmentProcessingException)

Example 2 with StorageException

use of uk.nhs.adaptors.pss.translator.storage.StorageException 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)

Aggregations

ParseException (java.text.ParseException)2 InlineAttachmentProcessingException (uk.nhs.adaptors.pss.translator.exception.InlineAttachmentProcessingException)2 InboundMessage (uk.nhs.adaptors.pss.translator.mhs.model.InboundMessage)2 StorageException (uk.nhs.adaptors.pss.translator.storage.StorageException)2 DataFormatException (ca.uhn.fhir.parser.DataFormatException)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 ValidationException (javax.xml.bind.ValidationException)1 RCMRIN030000UK06Message (org.hl7.v3.RCMRIN030000UK06Message)1 Document (org.w3c.dom.Document)1 SAXException (org.xml.sax.SAXException)1 MigrationStatusLog (uk.nhs.adaptors.connector.model.MigrationStatusLog)1 PatientAttachmentLog (uk.nhs.adaptors.connector.model.PatientAttachmentLog)1 PatientMigrationRequest (uk.nhs.adaptors.connector.model.PatientMigrationRequest)1 AttachmentNotFoundException (uk.nhs.adaptors.pss.translator.exception.AttachmentNotFoundException)1 BundleMappingException (uk.nhs.adaptors.pss.translator.exception.BundleMappingException)1 InlineAttachment (uk.nhs.adaptors.pss.translator.model.InlineAttachment)1 StorageDataUploadWrapper (uk.nhs.adaptors.pss.translator.storage.StorageDataUploadWrapper)1