/// <summary> /// Parse HL7 message using the local DB for Segments and trigger events /// </summary> /// <param name="message">Raw HL7 formated string. Multiple message file should be formated with \r\n after each message.</param> /// <returns>HL7 object with segments and events</returns> public HL7Message Parse(string message) { string[] tempMessage = message.Split(new string[] { "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i <= tempMessage.Length - 1; i++) { string segment = tempMessage[i].Substring(0, 3); if (segment == "MSH") { tempMessage[i] = string.Format("{0}", tempMessage[i]); } tempMessage[i].Trim(); } this._hl7 = new HL7Message(tempMessage); base.LogInfoMessage(string.Format("Creating HL7 message object from {0}", message)); //The Fields in the message should drive this, to handle multiple similar segments. TriggerEvent[] triggerEvents = this._repo.GetTriggerEventsBy(this._hl7.MessageToken.MessageVersion, this._hl7.MessageToken.MessageType, this._hl7.MessageToken.EventType).AsParallel().ToArray <TriggerEvent>(); base.LogInfoMessage(string.Format("Fetching TriggerEvent configuration for verions {0} {1}_{2}", this._hl7.MessageToken.MessageVersion , this._hl7.MessageToken.MessageType , this._hl7.MessageToken.EventType)); if (triggerEvents.Length == 0) { throw new ParserException(string.Format("No trigger events found for version {0}, message type {1}_{2}", this._hl7.MessageToken.MessageVersion, this._hl7.MessageToken.MessageType, this._hl7.MessageToken.EventType)); } else { foreach (string s in this._hl7.MessageToken.Segments) { string segId = s.Substring(0, 3); base.LogInfoMessage(string.Format("Searching TriggerEvent configuration for segment {0}", segId)); TriggerEvent tr = triggerEvents.Where(x => x.Segment == segId).FirstOrDefault(); if (tr != null) { HL7Segment newEvent = this.CreateFromSegmentString(s, tr); base.LogInfoMessage(string.Format("Successfully created HL7 segment {0} for TriggerEvent {1}_{2}", newEvent.Name, tr.MessageType, tr.EventType)); if (newEvent != null) { _hl7.AddEventSegment(newEvent); } } } return(_hl7); } }