Example #1
0
        /// <summary>
        /// Handle the HL7 message update from the Image Manager Actor.
        /// </summary>
        /// <param name="message">HL7 message from Image Manager Actor.</param>
        public void HandleUpdateFromImageManagerActor(Hl7Message message)
        {
            switch(message.MessageType)
            {
                case "ADT" :
                    {
                        switch(message.MessageSubType)
                        {
                            case "A08" :
                                HandlePatientUpdate(message);
                                break;
                            case "A40" :
                                HandlePatientMerge(message);
                                break;
                        }
                    }
                    break;

                case "ORM" :
                    {
                        switch(message.MessageSubType)
                        {
                            case "O01" :
                                HandleProcedureScheduled(message);
                                HandleProcedureUpdated(message);
                                break;
                        }
                    }
                    break;

                default:
                    break;
            }
        }
Example #2
0
        /// <summary>
        /// Query message handler for the received Hl7 message - send a query response.
        /// </summary>
        /// <param name="hl7RequestMessage">Received HL7 message.</param>
        /// <returns>Hl7Message response.</returns>
        public override Hl7Message MessageHandler(Hl7Message hl7RequestMessage)
        {
            AdrMessage hl7ResponseMessage = new AdrMessage("A19");

            if (hl7RequestMessage is QryMessage)
            {
                QryMessage qryMessage = (QryMessage)hl7RequestMessage;

                // Try to get the ADR message from those stored
                hl7ResponseMessage = GetMatchingResponse(qryMessage.QRD[8]);
                if (hl7ResponseMessage == null)
                {
                    // return empty message
                    hl7ResponseMessage = new AdrMessage("A19");
                }

                // copy the QRD segment
                for (int i = 0; i < qryMessage.QRD.Count; i++)
                {
                    hl7ResponseMessage.QRD[i] = qryMessage.QRD[i];
                }
            }

            // fill in the MSA segment
            hl7ResponseMessage.MSA[1] = "AA";
            hl7ResponseMessage.MSA[2] = hl7RequestMessage.MessageControlId;

            return hl7ResponseMessage;
        }
        public static DvtkData.Dimse.DataSet Convert(Hl7Message message)
        {
            DvtkData.Dimse.DataSet dataset = new DvtkData.Dimse.DataSet("Transient");

            try
            {
                if (message != null)
                {
                    // iterate over all the segments in the HL7 message
                    ICollection segments = message.Segments.Values;
                    foreach (Hl7Segment hl7Segment in segments)
                    {
                        // iterate over all the fields in the HL7 segments
                        for (int i = 1; i < hl7Segment.Count; i++)
                        {
                            System.String hl7Value = hl7Segment[i];
                            if (hl7Value != System.String.Empty)
                            {
                                // check if there is an Hl7 Tag corresponding to the value in the DicomHl7Template
                                Hl7Tag hl7Tag = new Hl7Tag(hl7Segment[0], i);
                                DicomHl7TagMap dicomHl7TagMap = DicomHl7TagMapTemplate.FindTagMap(hl7Tag);
                                if (dicomHl7TagMap != null)
                                {
                                    System.String dicomValue = hl7Value;
                                    if (dicomHl7TagMap.ValueConvertor != null)
                                    {
                                        dicomValue = dicomHl7TagMap.ValueConvertor.FromHl7ToDicom(hl7Value, dicomHl7TagMap.Hl7ComponentIndex);
                                    }
                                    AddDicomAttribute(dataset, dicomHl7TagMap.DicomTagPath, dicomValue);
                                }

                                for (int componentIndex = 2; componentIndex < 7; componentIndex++)
                                {
                                    dicomHl7TagMap = DicomHl7TagMapTemplate.FindTagMap(hl7Tag, componentIndex);
                                    if (dicomHl7TagMap != null)
                                    {
                                        System.String dicomValue = hl7Value;
                                        if (dicomHl7TagMap.ValueConvertor != null)
                                        {
                                            dicomValue = dicomHl7TagMap.ValueConvertor.FromHl7ToDicom(hl7Value, dicomHl7TagMap.Hl7ComponentIndex);
                                        }
                                        AddDicomAttribute(dataset, dicomHl7TagMap.DicomTagPath, dicomValue);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (System.Exception e)
            {
                Console.WriteLine("HL7 to DICOM conversion exception: {0} - {1}", e.Message, e.StackTrace);
            }

            return dataset;
        }
Example #4
0
        /// <summary>
        /// Get the next HL7 message read from the HL7 message directory.
        /// </summary>
        /// <returns>Next HL7 message read from message directory files.</returns>
        public Hl7Message GetNextHl7Message()
        {
            Hl7Message hl7Message = GetHl7Message(_iterator);

            if (hl7Message != null)
            {
                _iterator++;
            }

            return(hl7Message);
        }
Example #5
0
        //
        // - Methods -
        //

        /// <summary>
        /// Create a HL7 message from the content of a file.
        /// </summary>
        /// <param name="fullFileName">The full file name.</param>
        /// <returns>The HL7 messages created from the content of the file.</returns>
        public Hl7Message In(String fullFileName)
        {
            // set up the file stream for reading
            FileStream fileStream = new FileStream(fullFileName, FileMode.Open);

            // Decode the file stream into an HL7 message
            Hl7Message hl7Message = Decode(fileStream);

            // Return the HL7 message
            return(hl7Message);
        }
Example #6
0
        /// <summary>
        /// Initializes the encapsulated Dvtk.Comparator.Hl7Comparator class with the supplied HL7 message.
        /// </summary>
        /// <param name="hl7Message">The HL7 message.</param>
        /// <returns>The encapsulated Dvtk.Comparator.Hl7Comparator instance.</returns>
        public Hl7Comparator InitializeHl7Comparator(Hl7Message hl7Message)
        {
            _dicomComparator = null;

            bool initialized = _hl7Comparator.Initialize(hl7Message);
            if (initialized == false)
            {
                _hl7Comparator = null;
            }

            return _hl7Comparator;
        }
Example #7
0
        /// <summary>
        /// Get the HL7 message read from the indexed message file.
        /// </summary>
        /// <param name="index">Zero based index.</param>
        /// <returns>HL7 message read from indexed file.</returns>
        public Hl7Message GetHl7Message(int index)
        {
            Hl7Message hl7Message = null;

            if ((_fileInfo != null) &&
                (index < _fileInfo.Length))
            {
                String        filename      = _fileInfo[index].FullName;
                Hl7FileStream hl7FileStream = new Hl7FileStream();
                hl7Message = hl7FileStream.In(filename);
            }

            return(hl7Message);
        }
Example #8
0
        private void HandleProcedureUpdated(Hl7Message message)
        {
            // affects the information model maintained by the Dicom Query Retrive Server
            // - try to get the Dicom Query Retrive Server
            String actorId = GetFirstActorIdFromDicomServer(ActorTypeEnum.ImageDisplay);
            DicomServer dicomServer = GetDicomServer(new ActorName(ActorTypeEnum.ImageDisplay, actorId));
            if (dicomServer == null) return;

            // merge the patient with the Dicom Query Retrive Server
            DicomQueryRetrieveServer dicomQueryRetrieveServer = (DicomQueryRetrieveServer) dicomServer;
            Console.WriteLine("dicomQueryRetrieveServer.ProcedureUpdated(Hl7ToDicom(message));");
        }
Example #9
0
        /// <summary>
        /// Receive an HL7 message over the connected network stream.
        /// </summary>
        /// <param name="messageDelimiters">Initial HL7 message delimiters - updated to actual delimters during method.</param>
        /// <returns>Correctly instantiated HL7 message.</returns>
        public Hl7Message ReceiveMessage(out Hl7MessageDelimiters messageDelimiters)
        {
            // initialize the message delimiters to the default values
            messageDelimiters = new Hl7MessageDelimiters();

            Hl7Message hl7Message = new Hl7Message();
            if (_networkStream == null) return null;

            // set the read / write timeouts for this stream - zero means no timeout.
            if (_readTimeout != 0)
            {
                _networkStream.ReadTimeout = _readTimeout;
            }
            if (_writeTimeout != 0)
            {
                _networkStream.WriteTimeout = _writeTimeout;
            }

            // initialise the segment content
            System.String rxSegment = System.String.Empty;

            // initialise the MLLP state
            MllpStateEnum mllpState = MllpStateEnum.WaitingForStartOfMessageChar;

            // loop waiting for the end of message character
            while (mllpState != MllpStateEnum.DoneWaiting)
            {
                // check if data is available on network
                try
                {
                    // get the next character from the stream
                    int rxCode = _networkStream.ReadByte();
                    if (rxCode < 0)
                    {
                        return null;
                    }

                    char rxChar = (char) rxCode;

                    // switch on MLLP state
                    switch(mllpState)
                    {
                        case MllpStateEnum.WaitingForStartOfMessageChar:
                            // check if we have got the SOM
                            if (rxChar == _StartOfMessageChar)
                            {
                                // reset the segment
                                rxSegment = System.String.Empty;

                                // change state to waiting for end of segment
                                mllpState = MllpStateEnum.WaitingForEndOfSegmentChar;
                            }
                            else
                            {
                                Console.WriteLine("HL7 - MLLP: Waiting for SOM - got {0}...", rxChar);
                            }
                            break;
                        case MllpStateEnum.WaitingForEndOfSegmentChar:
                            // check if we have got the end of segment
                            if (rxChar == _EndOfSegmentChar)
                            {
                                // check if we have the MSH segment
                                // - we need to get the message delimiters
                                if (rxSegment.StartsWith("MSH") == true)
                                {
                                    // set the message delimiters to the values received
                                    // - we assume that the MSH segment is formatted properly at least in the first few bytes
                                    messageDelimiters = new Hl7MessageDelimiters(rxSegment.Substring(3,5));
                                }

                                // segment is complete
                                Hl7Segment segment = new Hl7Segment();
                                segment.Decode(rxSegment, messageDelimiters);

                                // add the segment to the HL7 message
                                hl7Message.AddSegment(segment);

                                // reset the segment
                                rxSegment = System.String.Empty;
                            }
                            else if (rxChar == _EndOfMessageChar1)
                            {
                                // we have the first end of message - that's OK
                                // check if any characters have been received since the last end of segment
                                if (rxSegment == System.String.Empty)
                                {
                                    // change state to waiting for second end of message
                                    mllpState = MllpStateEnum.WaitingForEndOfMessageChar;
                                }
                                else
                                {
                                    Console.WriteLine("HL7 - MLLP: First EOM does not immediately follow an EOS");
                                    return null;
                                }
                            }
                            else
                            {
                                // save the received character in the current segment
                                rxSegment += rxChar;
                            }
                            break;
                        case MllpStateEnum.WaitingForEndOfMessageChar:
                            // check if we have got the second end of message
                            if (rxChar == _EndOfMessageChar2)
                            {
                                // message is complete
                                mllpState = MllpStateEnum.DoneWaiting;
                            }
                            else
                            {
                                Console.WriteLine("HL7 - MLLP: Second EOM does not immediately follow first EOM");
                                return null;
                            }
                            break;
                        default:
                            break;
                    }
                }
                catch (System.Exception e)
                {
                    Console.WriteLine("HL7 - MLLP: ReceiveMessage() Exception: {0}", e.Message);
                    return null;
                }
            }

            // return the correct instantiation of the received HL7 message
            return Hl7MessageFactory.CreateHl7Message(hl7Message, messageDelimiters);
        }
Example #10
0
        /// <summary>
        /// Send an HL7 message over the connected network stream.
        /// </summary>
        /// <param name="hl7Message">HL7 message to encode.</param>
        /// <param name="messageDelimiters">HL7 message delimiters.</param>
        /// <returns>bool - successful sent = true else false.</returns>
        public bool SendMessage(Hl7Message hl7Message, Hl7MessageDelimiters messageDelimiters)
        {
            if (_networkStream == null) return false;

            // set the read / write timeouts for this stream - zero means no timeout.
            if (_readTimeout != 0)
            {
                _networkStream.ReadTimeout = _readTimeout;
            }
            if (_writeTimeout != 0)
            {
                _networkStream.WriteTimeout = _writeTimeout;
            }

            // write the start of message character
            _networkStream.WriteByte((byte)_StartOfMessageChar);

             			// stream all the segments - ordered by sequence number and then segment index
            int sequenceNumber = 0;
            int segmentIndex = 1;
            bool streaming = true;
            while (streaming == true)
            {
                bool segmentStreamed = false;
                ICollection segments = hl7Message.Segments.Values;
                foreach (Hl7Segment hl7Segment in segments)
                {
                    if (hl7Segment.SequenceNumber == sequenceNumber)
                    {
                        if (hl7Segment.SegmentId.SegmentIndex == segmentIndex)
                        {
                            System.String encodedStream = hl7Segment.Encode(messageDelimiters);
                            if (encodedStream != System.String.Empty)
                            {
                                byte[] buffer = new byte[encodedStream.Length];
                                for (int j = 0; j < encodedStream.Length; j++)
                                {
                                    buffer[j] = (byte) encodedStream[j];
                                }

                                // write the segment
                                _networkStream.Write(buffer, 0, encodedStream.Length);

                                // write the end of segment character
                                _networkStream.WriteByte((byte)_EndOfSegmentChar);
                            }

                            segmentIndex++;
                            segmentStreamed = true;
                            break;
                        }
                    }
                }

                if (segmentStreamed == false)
                {
                    if (sequenceNumber < hl7Message.Segments.Count)
                    {
                        sequenceNumber++;
                        segmentIndex = 1;
                    }
                    else
                    {
                        streaming = false;
                    }
                }
            }

            // write the end of message characters
            _networkStream.WriteByte((byte)_EndOfMessageChar1);
            _networkStream.WriteByte((byte)_EndOfMessageChar2);
            _networkStream.Flush();

            return true;
        }
        public void Add_hl7Message_flags()
        {
            FlagsHl7Attribute flags = new FlagsHl7Attribute();
            Hl7Message hl7Message = new Hl7Message();
            int count = attributeCollections.Count;

            attributeCollections.Add(hl7Message, flags);
            Assert.That(count + 1, Is.EqualTo(attributeCollections.Count));
        }
        public void Add_hl7Message()
        {
            Hl7Message hl7Message = new Hl7Message();
            int count = attributeCollections.Count;

            attributeCollections.Add(hl7Message);
            Assert.That(count + 1, Is.EqualTo(attributeCollections.Count));
        }
Example #13
0
        /// <summary>
        /// Add an HL7 message with specified flags to this collection.
        /// </summary>
        /// <param name="hl7Message">The HL7 message.</param>
        /// <param name="flags">The flags.</param>
        public void Add(Hl7Message hl7Message, FlagsHl7Attribute flags)
        {
            Hl7AttributeCollection hl7AttributeCollection = new Hl7AttributeCollection(hl7Message, flags);

            Add(hl7AttributeCollection);
        }
Example #14
0
        protected DvtkData.Dimse.DataSet Hl7ToDicom(Hl7Message message)
        {
            DvtkData.Dimse.DataSet dataset = new DvtkData.Dimse.DataSet("Transient");

            if (message != null)
            {
                // try to get the patient id
                CommonIdFormat patientId = new CommonIdFormat();
                patientId.FromHl7Format(message.Value(new Hl7Tag("PID", 3)));
                if (patientId.ToDicomFormat() != System.String.Empty)
                {
                   dataset.AddAttribute(DvtkData.Dimse.Tag.PATIENT_ID.GroupNumber, DvtkData.Dimse.Tag.PATIENT_ID.ElementNumber, DvtkData.Dimse.VR.LO, patientId.ToDicomFormat());
                }

                // try to get the patient's name
                CommonNameFormat patientName = new CommonNameFormat();
                patientName.FromHl7Format(message.Value(new Hl7Tag("PID", 5)));
                if (patientName.ToDicomFormat() != System.String.Empty)
                {
                    dataset.AddAttribute(DvtkData.Dimse.Tag.PATIENTS_NAME.GroupNumber, DvtkData.Dimse.Tag.PATIENTS_NAME.ElementNumber, DvtkData.Dimse.VR.PN, patientName.ToDicomFormat());
                }

                // try to get the patient's birth date
                CommonDateFormat patientBirthDate = new CommonDateFormat();
                patientBirthDate.FromHl7Format(message.Value(new Hl7Tag("PID", 7)));
                if (patientBirthDate.ToDicomFormat() != System.String.Empty)
                {
                    dataset.AddAttribute(DvtkData.Dimse.Tag.PATIENTS_BIRTH_DATE.GroupNumber, DvtkData.Dimse.Tag.PATIENTS_BIRTH_DATE.ElementNumber, DvtkData.Dimse.VR.DA, patientBirthDate.ToDicomFormat());
                }

                // try to get the patient's sex
                CommonStringFormat patientSex = new CommonStringFormat();
                patientSex.FromHl7Format(message.Value(new Hl7Tag("PID", 8)));
                if (patientSex.ToDicomFormat() != System.String.Empty)
                {
                    dataset.AddAttribute(DvtkData.Dimse.Tag.PATIENTS_SEX.GroupNumber, DvtkData.Dimse.Tag.PATIENTS_SEX.ElementNumber, DvtkData.Dimse.VR.CS, patientSex.ToDicomFormat());
                }

                // try to get the merge patient id
                CommonIdFormat mergePatientId = new CommonIdFormat();
                mergePatientId.FromHl7Format(message.Value(new Hl7Tag("MRG", 1)));
                if (mergePatientId.ToDicomFormat() != System.String.Empty)
                {
                    dataset.AddAttribute(DvtkData.Dimse.Tag.OTHER_PATIENT_IDS.GroupNumber, DvtkData.Dimse.Tag.OTHER_PATIENT_IDS.ElementNumber, DvtkData.Dimse.VR.LO, mergePatientId.ToDicomFormat());
                }
            }

            return dataset;
        }
Example #15
0
        /// <summary>
        /// Default message handler for the received Hl7 message - just send an ACK.
        /// </summary>
        /// <param name="hl7RequestMessage">Received HL7 message.</param>
        /// <returns>Hl7Message response.</returns>
        public virtual Hl7Message MessageHandler(Hl7Message hl7RequestMessage)
        {
            // Set up an ACK message to return
            AckMessage hl7ResponseMessage = new AckMessage(hl7RequestMessage.MessageSubType);

            // fill in the MSA segment
            hl7ResponseMessage.MSA[1] = "AA";
            hl7ResponseMessage.MSA[2] = hl7RequestMessage.MessageControlId;

            return hl7ResponseMessage;
        }
Example #16
0
        //
        // - Methods -
        //

        /// <summary>
        /// Decode a Hl7Message object from a given stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <returns>The decoded Hl7Message.</returns>
        internal Hl7Message Decode(System.IO.Stream stream)
        {
            Hl7Message hl7Message = new Hl7Message();

            // initialize the message delimiters to the default values
            _messageDelimiters = new Hl7MessageDelimiters();

            // initialise the segment content
            System.String rxSegment = System.String.Empty;

            // read the first character from the stream to determine if the MLLP is used
            int rxCode = stream.ReadByte();

            if (rxCode < 0)
            {
                throw new System.Exception("Incomplete HL7 stream.");
            }
            stream.Seek(0, System.IO.SeekOrigin.Begin);

            // initialise the MLLP state
            MllpStateEnum mllpState = MllpStateEnum.MllpNotUsed;

            if ((char)rxCode == _StartOfMessageChar)
            {
                // SOM read - use the MLLP protocol
                mllpState = MllpStateEnum.WaitingForStartOfMessageChar;
            }

            // loop waiting for the end of message character
            while (_continueReading)
            {
                // get the next character from the stream
                rxCode = stream.ReadByte();
                if (rxCode < 0)
                {
                    // end of stream reached when not using the MLLP
                    // - check if there is any data left in the rxSegment
                    if ((rxSegment != System.String.Empty) &&
                        (mllpState == MllpStateEnum.MllpNotUsed))
                    {
                        // segment is complete
                        Hl7Segment segment = new Hl7Segment();
                        segment.Decode(rxSegment, _messageDelimiters);

                        // add the segment to the HL7 message
                        hl7Message.AddSegment(segment);
                    }

                    // message is complete
                    _continueReading = false;
                    break;
                }

                char rxChar = (char)rxCode;

                // switch on MLLP state
                switch (mllpState)
                {
                case MllpStateEnum.MllpNotUsed:
                    // check if we have got the end of segment
                    if (rxChar == _EndOfSegmentChar)
                    {
                        // check if we have the MSH segment
                        // - we need to get the message delimiters
                        if (rxSegment.StartsWith("MSH") == true)
                        {
                            // set the message delimiters to the values received
                            // - we assume that the MSH segment is formatted properly at least in the first few bytes
                            _messageDelimiters = new Hl7MessageDelimiters(rxSegment.Substring(3, 5));
                        }

                        // segment is complete
                        Hl7Segment segment = new Hl7Segment();
                        segment.Decode(rxSegment, _messageDelimiters);

                        // add the segment to the HL7 message
                        hl7Message.AddSegment(segment);

                        // reset the segment
                        rxSegment = System.String.Empty;
                    }
                    else if (rxChar == _NewLineChar)
                    {
                        // ignore the line feed character
                    }
                    else
                    {
                        // save the received character in the current segment
                        rxSegment += rxChar;
                    }
                    break;

                case MllpStateEnum.WaitingForStartOfMessageChar:
                    // check if we have got the SOM
                    if (rxChar == _StartOfMessageChar)
                    {
                        // reset the segment
                        rxSegment = System.String.Empty;

                        // change state to waiting for end of segment
                        mllpState = MllpStateEnum.WaitingForEndOfSegmentChar;
                    }
                    else
                    {
                        Console.WriteLine("HL7 - MLLP: Waiting for SOM - got {0}...", rxChar);
                    }
                    break;

                case MllpStateEnum.WaitingForEndOfSegmentChar:
                    // check if we have got the end of segment
                    if (rxChar == _EndOfSegmentChar)
                    {
                        // check if we have the MSH segment
                        // - we need to get the message delimiters
                        if (rxSegment.StartsWith("MSH") == true)
                        {
                            // set the message delimiters to the values received
                            // - we assume that the MSH segment is formatted properly at least in the first few bytes
                            _messageDelimiters = new Hl7MessageDelimiters(rxSegment.Substring(3, 5));
                        }

                        // segment is complete
                        Hl7Segment segment = new Hl7Segment();
                        segment.Decode(rxSegment, _messageDelimiters);

                        // add the segment to the HL7 message
                        hl7Message.AddSegment(segment);

                        // reset the segment
                        rxSegment = System.String.Empty;
                    }
                    else if (rxChar == _EndOfMessageChar1)
                    {
                        // we have the first end of message - that's OK
                        // check if any characters have been received since the last end of segment
                        if (rxSegment == System.String.Empty)
                        {
                            // change state to waiting for second end of message
                            mllpState = MllpStateEnum.WaitingForEndOfMessageChar;
                        }
                        else
                        {
                            Console.WriteLine("HL7 - MLLP: First EOM does not immediately follow an EOS");
                            return(null);
                        }
                    }
                    else
                    {
                        // save the received character in the current segment
                        rxSegment += rxChar;
                    }
                    break;

                case MllpStateEnum.WaitingForEndOfMessageChar:
                    // check if we have got the second end of message
                    if (rxChar == _EndOfMessageChar2)
                    {
                        // message is complete
                        _continueReading = false;
                    }
                    else
                    {
                        Console.WriteLine("HL7 - MLLP: Second EOM does not immediately follow first EOM");
                        return(null);
                    }
                    break;

                default:
                    break;
                }
            }

            // return the correct instantiation of the received HL7 message
            return(Hl7MessageFactory.CreateHl7Message(hl7Message, _messageDelimiters));
        }
Example #17
0
        /// <summary>
        /// Create a correctly typed HL7 message instance based on the incoming Hl7 message.
        /// </summary>
        /// <param name="inHl7Message">Incoming HL7 message.</param>
        /// <param name="messageDelimiters">HL7 message delimiters.</param>
        /// <returns>Correctly typed HL7 message instance.</returns>
        public static Hl7Message CreateHl7Message(Hl7Message inHl7Message, Hl7MessageDelimiters messageDelimiters)
        {
            Hl7SegmentId segmentId = new Hl7SegmentId(Hl7SegmentEnum.MSH);
            Hl7Segment mshSegment = (Hl7Segment)inHl7Message.Segments[segmentId.Id];

            // can not determine what kind of message we have - so return the inHl7Message
            if (mshSegment == null)
            {
                return inHl7Message;
            }
            System.String messageType = mshSegment[9];

            Hl7Message hl7Message = null;

            // check for ACK message
            if (messageType == "ACK")
            {
                // now try to get the ORC segment
                segmentId = new Hl7SegmentId(Hl7SegmentEnum.ORC);
                Hl7Segment orcSegment = (Hl7Segment)inHl7Message.Segments[segmentId.Id];
                if (orcSegment != null)
                {
                    hl7Message = new OrrMessage();
                }
                else
                {
                    hl7Message = new AckMessage();
                }
            }
            else
            {
                System.String []messageTypeComponent = new System.String[3];
                messageTypeComponent = messageType.Split(messageDelimiters.ComponentDelimiter[0]);
                System.String messageMainType = System.String.Empty;
                if (messageTypeComponent.Length > 0)
                {
                    messageMainType = messageTypeComponent[0];
                }

                switch (messageMainType)
                {
                    case "ADR" :
                        // ADR message
                        hl7Message = new AdrMessage();
                        break;
                    case "ADT" :
                        // ADT message
                        hl7Message = new AdtMessage();
                        break;
                    case "ORM" :
                        // ORM message
                        hl7Message = new OrmMessage();
                        break;
                    case "ORU" :
                        // ORU message
                        hl7Message = new OruMessage();
                        break;
                    case "QRY" :
                        // QRY message
                        hl7Message = new QryMessage();
                        break;
                    default:
                        // do not know what kind of HL7 message this is - simply return it
                        return inHl7Message;
                }
            }

            // add the segments from the inMessage to the new hl7Message
            ICollection segments = inHl7Message.Segments.Values;
            foreach (Hl7Segment segment in segments)
            {
                hl7Message.CopySegment(segment);
            }

            return hl7Message;
        }
Example #18
0
        /// <summary>
        /// See the Dvtk.Comparator.Hl7Comparator.PopulateMessage method.
        /// </summary>
        /// <param name="hl7Message">See the Dvtk.Comparator.Hl7Comparator.PopulateMessage method.</param>
        /// <param name="hl7SourceComparator">See the Dvtk.Comparator.Hl7Comparator.PopulateMessage method.</param>
        /// <returns>See the Dvtk.Comparator.Hl7Comparator.PopulateMessage method.</returns>
        public bool PopulateHl7Message(Hl7Message hl7Message, Hl7Comparator hl7SourceComparator)
        {
            bool messagePopulated = false;

            if (_hl7Comparator != null)
            {
                messagePopulated = _hl7Comparator.PopulateMessage(hl7Message, hl7SourceComparator);
            }

            return messagePopulated;
        }
Example #19
0
        private bool LoadTemplate(Hl7Message hl7Message)
        {
            if (hl7Message == null) return false;

            // try to find the template tag in the dataset
            foreach (Hl7ComparisonTag comparisonTag in this.Template.ComparisonTags)
            {
                Hl7Tag tag = comparisonTag.Tag;
                System.String attributeValue = hl7Message.Value(tag.Segment, tag.FieldIndex);

                if (attributeValue != System.String.Empty)
                {
                    comparisonTag.DataFormat.FromHl7Format(attributeValue);
                }
            }

            return true;
        }
Example #20
0
        private bool CopyToHl7Message(Hl7Message hl7Message, Hl7Comparator sourceComparator)
        {
            bool messagePopulated = true;

            // Check if both templates have been initialized correctly
            if ((this.Template == null) ||
                (sourceComparator.Template == null))
            {
                return false;
            }

            // Iterate over this comparator
            foreach (Hl7ComparisonTag thisComparisonTag in this.Template.ComparisonTags)
            {
                // try to get the equivalent tag in the sourceComparator
                Hl7ComparisonTag sourceComparisonTag = sourceComparator.Template.ComparisonTags.Find(thisComparisonTag.Tag);
                if (sourceComparisonTag != null)
                {
                    System.String stringValue = sourceComparisonTag.DataFormat.ToHl7Format();
                    if (hl7Message != null)
                    {
                        // add the value
                        hl7Message.AddValue(sourceComparisonTag.Tag.Segment,
                                            sourceComparisonTag.Tag.FieldIndex,
                                            stringValue);
                    }
                }
            }

            return messagePopulated;
        }
Example #21
0
        public bool SetUp(Hl7Message hl7Message)
        {
            System.String messageType = hl7Message.MessageType;
            System.String messageSubType = hl7Message.MessageSubType;

            // Try to initialise a template
            _template = new Hl7ComparisonTemplate();
            bool setUpDone = _template.Initialize(messageType, messageSubType);

            return setUpDone;
        }
Example #22
0
        public bool PopulateMessage(Hl7Message hl7Message, Hl7Comparator sourceComparator)
        {
            // Check for comparator equality
            if (this == sourceComparator)
            {
                return false;
            }

            // Set up the comparator using the templates
            bool setUpOk = SetUp(hl7Message);
            if (setUpOk == true)
            {
                // Copy the source comparator values into the HL7 message if the tags are in this comparator
                setUpOk = CopyToHl7Message(hl7Message, sourceComparator);
            }

            return setUpOk;
        }
Example #23
0
        /// <summary>
        /// Initialize the Hl7Comparator
        /// </summary>
        /// <param name="hl7Message"></param>
        /// <returns>bool - true = template initialized, false template not initialized</returns>
        public bool Initialize(Hl7Message hl7Message)
        {
            // Set up the comparator using the templates
            bool setUpOk = SetUp(hl7Message);
            if (setUpOk == true)
            {
                // Load the template with the corresponding attribute values
                setUpOk = LoadTemplate(hl7Message);
            }

            return setUpOk;
        }
Example #24
0
        /// <summary>
        /// Create a correctly typed HL7 message instance based on the incoming Hl7 message.
        /// </summary>
        /// <param name="inHl7Message">Incoming HL7 message.</param>
        /// <param name="messageDelimiters">HL7 message delimiters.</param>
        /// <returns>Correctly typed HL7 message instance.</returns>
        public static Hl7Message CreateHl7Message(Hl7Message inHl7Message, Hl7MessageDelimiters messageDelimiters)
        {
            Hl7SegmentId segmentId  = new Hl7SegmentId(Hl7SegmentEnum.MSH);
            Hl7Segment   mshSegment = (Hl7Segment)inHl7Message.Segments[segmentId.Id];

            // can not determine what kind of message we have - so return the inHl7Message
            if (mshSegment == null)
            {
                return(inHl7Message);
            }
            System.String messageType = mshSegment[9];

            Hl7Message hl7Message = null;

            // check for ACK message
            if (messageType == "ACK")
            {
                // now try to get the ORC segment
                segmentId = new Hl7SegmentId(Hl7SegmentEnum.ORC);
                Hl7Segment orcSegment = (Hl7Segment)inHl7Message.Segments[segmentId.Id];
                if (orcSegment != null)
                {
                    hl7Message = new OrrMessage();
                }
                else
                {
                    hl7Message = new AckMessage();
                }
            }
            else
            {
                System.String [] messageTypeComponent = new System.String[3];
                messageTypeComponent = messageType.Split(messageDelimiters.ComponentDelimiter[0]);
                System.String messageMainType = System.String.Empty;
                if (messageTypeComponent.Length > 0)
                {
                    messageMainType = messageTypeComponent[0];
                }

                switch (messageMainType)
                {
                case "ADR":
                    // ADR message
                    hl7Message = new AdrMessage();
                    break;

                case "ADT":
                    // ADT message
                    hl7Message = new AdtMessage();
                    break;

                case "ORM":
                    // ORM message
                    hl7Message = new OrmMessage();
                    break;

                case "ORU":
                    // ORU message
                    hl7Message = new OruMessage();
                    break;

                case "QRY":
                    // QRY message
                    hl7Message = new QryMessage();
                    break;

                default:
                    // do not know what kind of HL7 message this is - simply return it
                    return(inHl7Message);
                }
            }

            // add the segments from the inMessage to the new hl7Message
            ICollection segments = inHl7Message.Segments.Values;

            foreach (Hl7Segment segment in segments)
            {
                hl7Message.CopySegment(segment);
            }

            return(hl7Message);
        }
Example #25
0
        private void HandlePlacerOrderManagement(Hl7Message message)
        {
            // affects the information model maintained by the Dicom Worklist Server
            // - try to get the Dicom Worklist Server
            String actorId = GetFirstActorIdFromDicomServer(ActorTypeEnum.AcquisitionModality);
            DicomServer dicomServer = GetDicomServer(new ActorName(ActorTypeEnum.AcquisitionModality, actorId));
            if (dicomServer == null) return;

            // placer order management with the Dicom Worklist Server
            DicomWorklistServer dicomWorklistServer = (DicomWorklistServer) dicomServer;
            dicomWorklistServer.PlacerOrderManagement(Hl7MessageToDicomMessageConvertor.Convert(message));
        }
Example #26
0
        private void ValidateMessage(Hl7Message hl7ResponseMessage, Hl7MessageDelimiters messageDelimiters)
        {
            try
            {
                // validate the HL7 message
                System.String facility = hl7ResponseMessage.SendingFacility;
                System.String version = hl7ResponseMessage.VersionId;
                if (_hl7ProfileStore != null)
                {
                    // get the validation profile - keyed off the facility, version and messageType
                    System.String messageType = hl7ResponseMessage.MessageType;
                    if (hl7ResponseMessage.MessageSubType != System.String.Empty)
                    {
                        messageType += ("^" + hl7ResponseMessage.MessageSubType);
                    }

                    System.String xmlProfile = _hl7ProfileStore.GetXmlHl7Profile(new Hl7ProfileId(facility, version, messageType));

                    if (_hl7ValidationContext != null)
                    {
                        // get the validation context
                        System.String xmlValidationContext = _hl7ValidationContext.XmlValidationContext;

                        if ((xmlProfile != System.String.Empty) &&
                            (xmlValidationContext != System.String.Empty) &&
                            (_nistWebServiceClient != null))
                        {
                            System.String errorDescription = System.String.Empty;
                            System.String xmlValidationResult = _nistWebServiceClient.Validate(xmlProfile, xmlValidationContext, hl7ResponseMessage.ToString(messageDelimiters), false, out errorDescription);
                            if (errorDescription != System.String.Empty)
                            {
                                _hl7ThreadForHl7Client.LogError(errorDescription);
                            }
                            NistXmlResultsParser xmlParser = new NistXmlResultsParser(xmlValidationResult);
                            _hl7ThreadForHl7Client.UpdateValidationErrorCount(xmlParser.ErrorCount);
                            _hl7ThreadForHl7Client.UpdateValidationWarningCount(xmlParser.WarningCount);
                            _hl7ThreadForHl7Client.WriteXmlStringToResults(xmlParser.RemoveHeader(xmlValidationResult));
                        }
                    }
                }
            }
            catch (System.Exception e)
            {
                System.String message = System.String.Format("NIST Validation Exception: {0} - {1}",
                    e.Message, e.StackTrace);
                Console.WriteLine(message);
                _hl7ThreadForHl7Client.LogInformation(message);
            }
        }
Example #27
0
        //
        // - Methods -
        //
        /// <summary>
        /// Decode a Hl7Message object from a given stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <returns>The decoded Hl7Message.</returns>
        internal Hl7Message Decode(System.IO.Stream stream)
        {
            Hl7Message hl7Message = new Hl7Message();

            // initialize the message delimiters to the default values
            _messageDelimiters = new Hl7MessageDelimiters();

            // initialise the segment content
            System.String rxSegment = System.String.Empty;

            // read the first character from the stream to determine if the MLLP is used
            int rxCode = stream.ReadByte();
            if (rxCode < 0)
            {
                throw new System.Exception("Incomplete HL7 stream.");
            }
            stream.Seek(0, System.IO.SeekOrigin.Begin);

            // initialise the MLLP state
            MllpStateEnum mllpState = MllpStateEnum.MllpNotUsed;
            if ((char)rxCode == _StartOfMessageChar)
            {
                // SOM read - use the MLLP protocol
                mllpState = MllpStateEnum.WaitingForStartOfMessageChar;
            }

            // loop waiting for the end of message character
            while (_continueReading)
            {
                // get the next character from the stream
                rxCode = stream.ReadByte();
                if (rxCode < 0)
                {
                    // end of stream reached when not using the MLLP
                    // - check if there is any data left in the rxSegment
                    if ((rxSegment != System.String.Empty) &&
                        (mllpState == MllpStateEnum.MllpNotUsed))
                    {
                        // segment is complete
                        Hl7Segment segment = new Hl7Segment();
                        segment.Decode(rxSegment, _messageDelimiters);

                        // add the segment to the HL7 message
                        hl7Message.AddSegment(segment);
                    }

                    // message is complete
                    _continueReading = false;
                    break;
                }

                char rxChar = (char) rxCode;

                // switch on MLLP state
                switch(mllpState)
                {
                    case MllpStateEnum.MllpNotUsed:
                        // check if we have got the end of segment
                        if (rxChar == _EndOfSegmentChar)
                        {
                            // check if we have the MSH segment
                            // - we need to get the message delimiters
                            if (rxSegment.StartsWith("MSH") == true)
                            {
                                // set the message delimiters to the values received
                                // - we assume that the MSH segment is formatted properly at least in the first few bytes
                                _messageDelimiters = new Hl7MessageDelimiters(rxSegment.Substring(3,5));
                            }

                            // segment is complete
                            Hl7Segment segment = new Hl7Segment();
                            segment.Decode(rxSegment, _messageDelimiters);

                            // add the segment to the HL7 message
                            hl7Message.AddSegment(segment);

                            // reset the segment
                            rxSegment = System.String.Empty;
                        }
                        else if (rxChar == _NewLineChar)
                        {
                            // ignore the line feed character
                        }
                        else
                        {
                            // save the received character in the current segment
                            rxSegment += rxChar;
                        }
                        break;
                    case MllpStateEnum.WaitingForStartOfMessageChar:
                        // check if we have got the SOM
                        if (rxChar == _StartOfMessageChar)
                        {
                            // reset the segment
                            rxSegment = System.String.Empty;

                            // change state to waiting for end of segment
                            mllpState = MllpStateEnum.WaitingForEndOfSegmentChar;
                        }
                        else
                        {
                            Console.WriteLine("HL7 - MLLP: Waiting for SOM - got {0}...", rxChar);
                        }
                        break;
                    case MllpStateEnum.WaitingForEndOfSegmentChar:
                        // check if we have got the end of segment
                        if (rxChar == _EndOfSegmentChar)
                        {
                            // check if we have the MSH segment
                            // - we need to get the message delimiters
                            if (rxSegment.StartsWith("MSH") == true)
                            {
                                // set the message delimiters to the values received
                                // - we assume that the MSH segment is formatted properly at least in the first few bytes
                                _messageDelimiters = new Hl7MessageDelimiters(rxSegment.Substring(3,5));
                            }

                            // segment is complete
                            Hl7Segment segment = new Hl7Segment();
                            segment.Decode(rxSegment, _messageDelimiters);

                            // add the segment to the HL7 message
                            hl7Message.AddSegment(segment);

                            // reset the segment
                            rxSegment = System.String.Empty;
                        }
                        else if (rxChar == _EndOfMessageChar1)
                        {
                            // we have the first end of message - that's OK
                            // check if any characters have been received since the last end of segment
                            if (rxSegment == System.String.Empty)
                            {
                                // change state to waiting for second end of message
                                mllpState = MllpStateEnum.WaitingForEndOfMessageChar;
                            }
                            else
                            {
                                Console.WriteLine("HL7 - MLLP: First EOM does not immediately follow an EOS");
                                return null;
                            }
                        }
                        else
                        {
                            // save the received character in the current segment
                            rxSegment += rxChar;
                        }
                        break;
                    case MllpStateEnum.WaitingForEndOfMessageChar:
                        // check if we have got the second end of message
                        if (rxChar == _EndOfMessageChar2)
                        {
                            // message is complete
                            _continueReading = false;
                        }
                        else
                        {
                            Console.WriteLine("HL7 - MLLP: Second EOM does not immediately follow first EOM");
                            return null;
                        }
                        break;
                    default:
                        break;
                }
            }

            // return the correct instantiation of the received HL7 message
            return Hl7MessageFactory.CreateHl7Message(hl7Message, _messageDelimiters);
        }
Example #28
0
 private void UpdateImageArchiveActor(Hl7Message message)
 {
     String actorId = GetFirstActorIdFromDicomServer(ActorTypeEnum.ImageArchive);
     ImageArchiveActor imageArchiveActor = (ImageArchiveActor)IheFramework.GetActor(new ActorName(ActorTypeEnum.ImageArchive, actorId));
     if (imageArchiveActor != null)
     {
         imageArchiveActor.HandleUpdateFromImageManagerActor(message);
     }
 }
Example #29
0
        /// <summary>
        /// Add a HL7 message to this collection.
        /// </summary>
        /// <param name="hl7Message">The HL7 message.</param>
        public void Add(Hl7Message hl7Message)
        {
            Hl7AttributeCollection hl7AttributeCollection = new Hl7AttributeCollection(hl7Message);

            Add(hl7AttributeCollection);
        }