Example #1
0
        public void TestEnumerators()
        {
            var segment = sut.GetSegment("MSH");

            Assert.AreEqual(3, Terser.numComponents(segment.GetField(9, 0)));

            segment.GetField(9, 0).ExtraComponents.GetComponent(0);
            Assert.AreEqual(4, Terser.numComponents(segment.GetField(9, 0)));

            segment.GetField(9, 0).ExtraComponents.GetComponent(1);
            Assert.AreEqual(5, Terser.numComponents(segment.GetField(9, 0)));

            segment.GetField(2, 0).ExtraComponents.GetComponent(0);
            Assert.AreEqual(2, Terser.numComponents(segment.GetField(2, 0)));

            ((IPrimitive)((IComposite)segment.GetField(9, 0)).Components[0].ExtraComponents.GetComponent(0).Data).Value = "xxx";
            Assert.AreEqual(2, Terser.numSubComponents(segment.GetField(9, 0), 1));
        }
Example #2
0
        /// <summary>
        /// Create an Ack message based on a received message
        /// </summary>
        /// <param name="inboundMessage">received message</param>
        /// <param name="ackResult">Send AA, AE or AR message.</param>
        /// <param name="errorMessage">The reason the message was rejected or an error. If "AA" was supplied as ackCode the errorMessage should be null.</param>
        /// <returns>Created ACK message</returns>
        public IMessage MakeACK(IMessage inboundMessage, AckTypes ackResult, string errorMessage)
        {
            //this should avoid an unhandled null reference exception in "inboundMessage.Version", because people tend to send the inboudMessage without a check
            if (inboundMessage == null)
            {
                throw new ArgumentNullException("Either process the valid message while retreiving the ack or handle invalid message differently");
            }

            IMessage ackMessage = null;
            // Get an object from the right ACK class
            string ackClassType = string.Format("NHapi.Model.V{0}.Message.ACK, NHapi.Model.V{0}", inboundMessage.Version.Replace(".", ""));
            Type   x            = Type.GetType(ackClassType);

            if (x != null)
            {
                ackMessage = (IMessage)Activator.CreateInstance(x);
            }
            else
            {
                // Fix for V2.2 and V2.1 Since tha ACK message class is missing there in NHapi
                if (inboundMessage.Version == "2.1")
                {
                    ackMessage = (IMessage) new NHapiTools.Base.CustomImplementation.V21.Messages.ACK();
                }
                if (inboundMessage.Version == "2.2")
                {
                    ackMessage = (IMessage) new NHapiTools.Base.CustomImplementation.V22.Messages.ACK();
                }
            }

            Terser   inboundTerser = new Terser(inboundMessage);
            ISegment inboundHeader = null;

            inboundHeader = inboundTerser.GetSegment("MSH");

            // Find the HL7 version of the inbound message:
            string version = null;

            try
            {
                version = Terser.Get(inboundHeader, 12, 0, 1, 1);
            }
            catch (NHapi.Base.HL7Exception)
            {
                // I'm not happy to proceed if we can't identify the inbound
                // message version.
                throw new NHapi.Base.HL7Exception("Failed to get valid HL7 version from inbound MSH-12-1");
            }

            // Create a Terser instance for the outbound message (the ACK).
            Terser terser = new Terser(ackMessage);

            // Populate outbound MSH fields using data from inbound message
            ISegment outHeader = (ISegment)terser.GetSegment("MSH");

            DeepCopy.Copy(inboundHeader, outHeader);

            // Now set the message type, HL7 version number, acknowledgement code
            // and message control ID fields:
            string sendingApp = terser.Get("/MSH-3");
            string sendingEnv = terser.Get("/MSH-4");

            terser.Set("/MSH-3", appCommunicationName);
            terser.Set("/MSH-4", environmentIdentifier);
            terser.Set("/MSH-5", sendingApp);
            terser.Set("/MSH-6", sendingEnv);
            terser.Set("/MSH-7", DateTime.Now.ToString("yyyyMMddHHmm"));
            terser.Set("/MSH-9", "ACK");
            terser.Set("/MSH-9-3", null);
            terser.Set("/MSH-12", version);
            terser.Set("/MSA-1", Enum.GetName(typeof(AckTypes), ackResult));
            terser.Set("/MSA-2", Terser.Get(inboundHeader, 10, 0, 1, 1));

            // Set error message
            if (errorMessage != null)
            {
                terser.Set("/ERR-1-1", errorMessage);
            }

            return(ackMessage);
        }