public bool AddNewSegment(Segment newSeg) { try { newSeg.SequenceNo = SegmentCount++; if (SegmentList.ContainsKey(newSeg.Name)) { SegmentList[newSeg.Name].List.Add(newSeg); } else { SegmentList.Add(newSeg.Name, newSeg); SegmentList[newSeg.Name].List.Add(newSeg); } return true; } catch (Exception ex) { SegmentCount--; throw new HL7Exception("Unable to add new segment Error - " + ex.Message); } }
/// <summary> /// Parse the HL7 message in text format, throws HL7Exception if error occurs /// </summary> /// <returns>boolean</returns> public bool ParseMessage() { bool isValid = false; bool isParsed = false; try { isValid = ValidateMessage(); } catch (HL7Exception ex) { throw ex; } catch (Exception ex) { throw new HL7Exception("Unhandeled Exceiption in validation - " + ex.Message, HL7Exception.BAD_MESSAGE); } if (isValid) { try { if (AllSegments == null || AllSegments.Count <= 0) { AllSegments = MessageHelper.SplitString(HL7Message, new Char[1] { DefaultSegmentSeparatorString[msgSegmentSeparatorIndex] }); } short SegSeqNo = 0; foreach (String strSegment in AllSegments) { Segment segNew = new Segment(); String segmentName = strSegment.Substring(0, 3); segNew.FieldDelimiters = new Char[] { FieldDelimiters[0], FieldDelimiters[1], FieldDelimiters[2], FieldDelimiters[4] }; segNew.Name = segmentName; segNew.Value = strSegment; segNew.SequenceNo = SegSeqNo++; if (SegmentList.ContainsKey(segmentName)) { SegmentList[segmentName].List.Add(segNew); } else { SegmentList.Add(segmentName, segNew); SegmentList[segmentName].List.Add(segNew); } } _SegmentCount = SegSeqNo; String strSerializedMessage = String.Empty; try { strSerializedMessage = SerializeMessageWithoutValidate(); //validation not required here as we haven't changed anything in message } catch (HL7Exception ex) { throw new HL7Exception("Failed to serialize parsed message with error -" + ex.Message, HL7Exception.PARSING_ERROR); } if (!String.IsNullOrEmpty(strSerializedMessage)) { if (HL7Message.Equals(strSerializedMessage)) isParsed = true; } else { throw new HL7Exception("Unable to serialize to origional message -", HL7Exception.PARSING_ERROR); } } catch (Exception ex) { throw new HL7Exception("Failed to parse the message with error -" + ex.Message, HL7Exception.PARSING_ERROR); } } return isParsed; }