/// <summary> /// Parses the received HL7 message. /// </summary> /// <param name="messageForm">The raw message encoded in HL7 'pipes and hats' format.</param> /// <returns>The parsed HL7 message object</returns> /// <exception cref="HL7ParseException">Thrown when parsing fails</exception> public static HL7Message Parse(string messageForm) { // Determine the HL7 separators dynamically from the incoming message (usually "|^~\&") HL7Separators sep = new HL7Separators(messageForm); // Change CRLF or LF into the standard CR separators if (messageForm.Contains("\r\n")) { messageForm = messageForm.Replace("\r\n", "\r"); } if (messageForm.Contains("\n")) { messageForm = messageForm.Replace("\n", "\r"); } // Remove consecutive segment separators while (messageForm.Contains("\r\r")) { messageForm = messageForm.Replace("\r\r", "\r"); } // Parse all the segments messageForm = messageForm.TrimEnd(new char[] { sep.SegmentSeparator }); string[] segmentForm = messageForm.Split(sep.SegmentSeparator); for (int count = 0; count < segmentForm.Count(); count++) { segmentForm[count] = segmentForm[count].Replace("\n", string.Empty); } HL7Segment[] segments = new HL7Segment[segmentForm.Length]; for (int i = 0; i < segmentForm.Length; i++) { segments[i] = HL7Segment.Parse(segmentForm[i], sep); } // Grab the MSH segment in order to determine which message structure to use MSH msh = segments[0] as MSH; if (msh == null) { throw new HL7ParseException(ConstantsResource.HL7NoMshSegment); } if (msh.MessageType == null) { throw new HL7ParseException(ConstantsResource.NoMessageType); } // Determine the structure for the indicated message identifier Type messageStructure = GetMessageStructure(msh.MessageType); // Create the message and populate all the matching segments into its structure int segmentIndex = 0; HL7Message message = BuildSegmentGroup(messageStructure, segments, ref segmentIndex) as HL7Message; return(message); }
/// <summary> /// Creates an HL7 acknowledgement for a received message. If there is an /// error condition, the acknowledgement will be a negative acknowledgement /// (code AE), otherwise it will be a positive acknowedgement (code AA). /// </summary> /// <param HL7Name="message">The received message</param> /// <param HL7Name="facility">The facility that is returning the acknowledgement</param> /// <param HL7Name="application">The application that is returning the acknowledgement</param> /// <param HL7Name="errorCondition">Null if no error, otherwise a code and description of the error</param> /// <returns>The HL7 acknowledgement</returns> public static HL7Acknowledgement Acknowledge(HL7Message message, string facility, string application, CE errorCondition) { HL7Acknowledgement ack = new HL7Acknowledgement(); ack.MessageHeader = new MSH(); ack.MessageHeader.FieldSeparator = message.MessageHeader.FieldSeparator; ack.MessageHeader.EncodingCharacters = message.MessageHeader.EncodingCharacters; ack.MessageHeader.SendingApplication = new HD() { namespaceID = application }; ack.MessageHeader.SendingFacility = new HD() { namespaceID = facility }; ack.MessageHeader.ReceivingApplication = message.MessageHeader.SendingApplication; ack.MessageHeader.ReceivingFacility = message.MessageHeader.SendingFacility; ack.MessageHeader.DateTimeOfMessage = new TS() { TimestampValue = DateTime.Now }; ack.MessageHeader.Security = message.MessageHeader.Security; ack.MessageHeader.MessageType = message.MessageHeader.MessageType; ack.MessageHeader.MessageControlID = NewMessageControlID(); ack.MessageHeader.ProcessingID = message.MessageHeader.ProcessingID; ack.MessageHeader.VersionID = message.MessageHeader.VersionID; ack.MessageHeader.SequenceNumber = message.MessageHeader.SequenceNumber; ack.MessageHeader.ContinuationPointer = message.MessageHeader.ContinuationPointer; ack.MessageHeader.AcceptAcknowledgmentType = message.MessageHeader.AcceptAcknowledgmentType; ack.MessageHeader.ApplicationAcknowledgmentType = message.MessageHeader.ApplicationAcknowledgmentType; ack.MessageHeader.CountryCode = message.MessageHeader.CountryCode; ack.MessageHeader.CharacterSet = message.MessageHeader.CharacterSet; ack.MessageHeader.PrincipalLanguageOfMessage = message.MessageHeader.PrincipalLanguageOfMessage; ack.MessageHeader.AltCharsetHandlingScheme = message.MessageHeader.AltCharsetHandlingScheme; ack.MessageAcknowledgement = new MSA(); if (errorCondition == null) { ack.MessageAcknowledgement.AcknowledgmentCode = "AA"; } else { ack.MessageAcknowledgement.AcknowledgmentCode = "AE"; ack.MessageAcknowledgement.ErrorCondition = errorCondition; } ack.MessageAcknowledgement.MessageControlID = message.MessageHeader.MessageControlID; return(ack); }