protected override void ProcessValue() { List <string> allSubComponents; if (this.isDelimiter) { allSubComponents = new List <string>(new [] { this.Value }); } else { allSubComponents = MessageHelper.SplitString(_value, this.Encoding.SubComponentDelimiter); } if (allSubComponents.Count > 1) { this.IsSubComponentized = true; } this.SubComponentList = new List <SubComponent>(); foreach (string strSubComponent in allSubComponents) { SubComponent subComponent = new SubComponent(this.Encoding.Decode(strSubComponent), this.Encoding); SubComponentList.Add(subComponent); } }
protected override void ProcessValue() { if (_value.Length > 0) { _value = _value.TrimEnd(this.Encoding.FieldDelimiter); List <string> allFields = MessageHelper.SplitString(_value, this.Encoding.FieldDelimiter); if (allFields.Count > 1) { allFields.RemoveAt(0); } for (int i = 0; i < allFields.Count; i++) { string strField = allFields[i]; Field field = new Field(this.Encoding); if (Name == "MSH" && i == 0) { field.IsDelimiters = true; // special case } field.Value = strField; FieldList.Add(field); } } }
protected override void ProcessValue() { List <string> allFields = MessageHelper.SplitString(_value, this.Encoding.FieldDelimiter); if (allFields.Count > 1) { allFields.RemoveAt(0); } for (int i = 0; i < allFields.Count; i++) { string strField = allFields[i]; Field field = new Field(this.Encoding); if (Name == "MSH" && i == 0) { field.IsDelimiters = true; // special case } field.Value = strField; this.FieldList.Add(field); } if (this.Name == "MSH") { var field1 = new Field(this.Encoding); field1.IsDelimiters = true; field1.Value = this.Encoding.FieldDelimiter.ToString(); this.FieldList.Insert(0, field1); } }
protected override void ProcessValue() { List <string> AllSubComponents = MessageHelper.SplitString(_value, this.Encoding.SubComponentDelimiter); if (AllSubComponents.Count > 1) { this.IsSubComponentized = true; } SubComponentList = new List <SubComponent>(); foreach (string strSubComponent in AllSubComponents) { SubComponent subComponent = new SubComponent(Encoding.Decode(strSubComponent)); SubComponentList.Add(subComponent); } }
public override bool Equals(object obj) { if (obj is Message) { return(this.Equals((obj as Message).HL7Message)); } if (obj is string) { var arr1 = MessageHelper.SplitString(this.HL7Message, this.Encoding.SegmentDelimiter, StringSplitOptions.RemoveEmptyEntries); var arr2 = MessageHelper.SplitString(obj as string, this.Encoding.SegmentDelimiter, StringSplitOptions.RemoveEmptyEntries); return(arr1.SequenceEqual(arr2)); } return(false); }
/// <summary> /// check if specified component has sub components /// </summary> /// <param name="strValueFormat">Field/Component position in format SEGMENTNAME.FieldIndex.ComponentIndex.SubComponentIndex example PID.5.2</param> /// <returns>boolean</returns> public bool IsSubComponentized(string strValueFormat) { bool isSubComponentized = false; bool isValid = false; string segmentName = string.Empty; int componentIndex = 0; int comCount = 0; List <string> allComponents = MessageHelper.SplitString(strValueFormat, new char[] { '.' }); comCount = allComponents.Count; isValid = validateValueFormat(allComponents); if (isValid) { segmentName = allComponents[0]; if (comCount >= 3) { try { var segment = SegmentList[segmentName].First(); var field = this.getField(segment, allComponents[1]); Int32.TryParse(allComponents[2], out componentIndex); isSubComponentized = field.ComponentList[componentIndex - 1].IsSubComponentized; } catch (Exception ex) { throw new HL7Exception("Component not available - " + strValueFormat + " Error: " + ex.Message); } } else { throw new HL7Exception("Component not identified in request"); } } else { throw new HL7Exception("Request format is not valid"); } return(isSubComponentized); }
protected override void ProcessValue() { if (this.IsDelimiters) // Special case for the delimiters field (MSH) { var subcomponent = new SubComponent(_value); this.ComponentList = new ComponentCollection(); Component component = new Component(this.Encoding); component.SubComponentList.Add(subcomponent); this.ComponentList.Add(component); return; } if (_value.Length > 0) { this.HasRepetitions = _value.Contains(this.Encoding.RepeatDelimiter); if (this.HasRepetitions) { _RepetitionList = new List <Field>(); List <string> individualFields = MessageHelper.SplitString(_value, this.Encoding.RepeatDelimiter); for (int index = 0; index < individualFields.Count; index++) { Field field = new Field(individualFields[index], this.Encoding); _RepetitionList.Add(field); } } else { List <string> allComponents = MessageHelper.SplitString(_value, this.Encoding.ComponentDelimiter); this.ComponentList = new ComponentCollection(); foreach (string strComponent in allComponents) { Component component = new Component(this.Encoding); component.Value = strComponent; this.ComponentList.Add(component); } this.IsComponentized = this.ComponentList.Count > 1; } } }
/// <summary> /// check if specified fields has repeatitions /// </summary> /// <param name="strValueFormat">Field/Component position in format SEGMENTNAME.FieldIndex.ComponentIndex.SubComponentIndex example PID.5.2</param> /// <returns>boolean</returns> public bool HasRepetitions(string strValueFormat) { bool hasRepetitions = false; bool isValid = false; string segmentName = string.Empty; int comCount = 0; List <string> allComponents = MessageHelper.SplitString(strValueFormat, new char[] { '.' }); comCount = allComponents.Count; isValid = validateValueFormat(allComponents); if (isValid) { segmentName = allComponents[0]; if (comCount >= 2) { try { var segment = SegmentList[segmentName].First(); var field = this.getField(segment, allComponents[1]); hasRepetitions = field.HasRepetitions; } catch (Exception ex) { throw new HL7Exception("Field not available - " + strValueFormat + " Error: " + ex.Message); } } else { throw new HL7Exception("Field not identified in request"); } } else { throw new HL7Exception("Request format is not valid"); } return(hasRepetitions); }
/// <summary> /// check if specified fields has repeatitions /// </summary> /// <param name="strValueFormat">Field/Component position in format SEGMENTNAME.FieldIndex.ComponentIndex.SubComponentIndex example PID.5.2</param> /// <returns>boolean</returns> public bool HasRepetitions(string strValueFormat) { bool hasRepetitions = false; bool isValid = false; string segmentName = string.Empty; int fieldIndex = 0; int comCount = 0; List <string> AllComponents = MessageHelper.SplitString(strValueFormat, new char[] { '.' }); comCount = AllComponents.Count; isValid = validateValueFormat(AllComponents); if (isValid) { segmentName = AllComponents[0]; if (comCount >= 2) { try { Int32.TryParse(AllComponents[1], out fieldIndex); hasRepetitions = SegmentList[segmentName].First().FieldList[fieldIndex - 1].HasRepetitions; } catch (Exception ex) { throw new HL7Exception("Field not available - " + strValueFormat + " Error: " + ex.Message); } } else { throw new HL7Exception("Field not identified in request"); } } else { throw new HL7Exception("Request format is not valid"); } return(hasRepetitions); }
public override bool Equals(object?obj) { if ((obj is null) == false) { if (obj is Message) { Message?msg = obj as Message; return(this.Equals(msg !.HL7Message)); } if (obj is string) { string?str = obj as string; str = (str == null) ? string.Empty : str; var arr1 = MessageHelper.SplitString(this.HL7Message, this.Encoding.SegmentDelimiter, StringSplitOptions.RemoveEmptyEntries); var arr2 = MessageHelper.SplitString(str !, this.Encoding.SegmentDelimiter, StringSplitOptions.RemoveEmptyEntries); return(arr1.SequenceEqual(arr2)); } } return(false); }
/// <summary> /// Validates the HL7 message for basic syntax /// </summary> /// <returns>A boolean indicating whether the whole message is valid or not</returns> private bool validateMessage() { try { if (!string.IsNullOrEmpty(HL7Message)) { //check message length - MSH+Delimeters+12Fields in MSH if (HL7Message.Length < 20) { throw new HL7Exception("Message Length too short: " + HL7Message.Length + " chars.", HL7Exception.BAD_MESSAGE); } //check if message starts with header segment if (!HL7Message.StartsWith("MSH")) { throw new HL7Exception("MSH segment not found at the beggining of the message", HL7Exception.BAD_MESSAGE); } this.Encoding.EvaluateSegmentDelimiter(this.HL7Message); this.HL7Message = string.Join(this.Encoding.SegmentDelimiter, MessageHelper.SplitMessage(this.HL7Message)) + this.Encoding.SegmentDelimiter; //check Segment Name & 4th character of each segment char fourthCharMSH = HL7Message[3]; this.allSegments = MessageHelper.SplitMessage(HL7Message); foreach (string strSegment in this.allSegments) { if (string.IsNullOrWhiteSpace(strSegment)) { continue; } bool isValidSegName = false; string segmentName = strSegment.Substring(0, 3); string segNameRegEx = "[A-Z][A-Z][A-Z1-9]"; isValidSegName = System.Text.RegularExpressions.Regex.IsMatch(segmentName, segNameRegEx); if (!isValidSegName) { throw new HL7Exception("Invalid segment name found: " + strSegment, HL7Exception.BAD_MESSAGE); } char fourthCharSEG = strSegment[3]; if (fourthCharMSH != fourthCharSEG) { throw new HL7Exception("Invalid segment found: " + strSegment, HL7Exception.BAD_MESSAGE); } } string _fieldDelimiters_Message = this.allSegments[0].Substring(3, 8 - 3); this.Encoding.EvaluateDelimiters(_fieldDelimiters_Message); // Count field separators, MSH.12 is required so there should be at least 11 field separators in MSH int countFieldSepInMSH = this.allSegments[0].Count(f => f == Encoding.FieldDelimiter); if (countFieldSepInMSH < 11) { throw new HL7Exception("MSH segment doesn't contain all the required fields", HL7Exception.BAD_MESSAGE); } // Find Message Version var MSHFields = MessageHelper.SplitString(this.allSegments[0], Encoding.FieldDelimiter); if (MSHFields.Count >= 12) { this.Version = MessageHelper.SplitString(MSHFields[11], Encoding.ComponentDelimiter)[0]; } else { throw new HL7Exception("HL7 version not found in the MSH segment", HL7Exception.REQUIRED_FIELD_MISSING); } //Find Message Type & Trigger Event try { string MSH_9 = MSHFields[8]; if (!string.IsNullOrEmpty(MSH_9)) { var MSH_9_comps = MessageHelper.SplitString(MSH_9, this.Encoding.ComponentDelimiter); if (MSH_9_comps.Count >= 3) { this.MessageStructure = MSH_9_comps[2]; } else if (MSH_9_comps.Count > 0 && MSH_9_comps[0] != null && MSH_9_comps[0].Equals("ACK")) { this.MessageStructure = "ACK"; } else if (MSH_9_comps.Count == 2) { this.MessageStructure = MSH_9_comps[0] + "_" + MSH_9_comps[1]; } else { throw new HL7Exception("Message Type & Trigger Event value not found in message", HL7Exception.UNSUPPORTED_MESSAGE_TYPE); } } else { throw new HL7Exception("MSH.10 not available", HL7Exception.UNSUPPORTED_MESSAGE_TYPE); } } catch (System.IndexOutOfRangeException e) { throw new HL7Exception("Can't find message structure (MSH.9.3) - " + e.Message, HL7Exception.UNSUPPORTED_MESSAGE_TYPE); } try { this.MessageControlID = MSHFields[9]; if (string.IsNullOrEmpty(this.MessageControlID)) { throw new HL7Exception("MSH.10 - Message Control ID not found", HL7Exception.REQUIRED_FIELD_MISSING); } } catch (Exception ex) { throw new HL7Exception("Error occured while accessing MSH.10 - " + ex.Message, HL7Exception.REQUIRED_FIELD_MISSING); } try { this.ProcessingID = MSHFields[10]; if (string.IsNullOrEmpty(this.ProcessingID)) { throw new HL7Exception("MSH.11 - Processing ID not found", HL7Exception.REQUIRED_FIELD_MISSING); } } catch (Exception ex) { throw new HL7Exception("Error occured while accessing MSH.11 - " + ex.Message, HL7Exception.REQUIRED_FIELD_MISSING); } } else { throw new HL7Exception("No Message Found", HL7Exception.BAD_MESSAGE); } } catch (Exception ex) { throw new HL7Exception("Failed to validate the message with error - " + ex.Message, HL7Exception.BAD_MESSAGE); } return(true); }
/// <summary> /// Sets the Value of specific Field/Component/SubCpomponent, throws error if field/component index is not valid /// </summary> /// <param name="strValueFormat">Field/Component position in format SEGMENTNAME.FieldIndex.ComponentIndex.SubComponentIndex example PID.5.2</param> /// <param name="strValue">Value for the specified field/component</param> /// <returns>boolean</returns> public bool SetValue(string strValueFormat, string strValue) { bool isValid = false; bool isSet = false; string segmentName = string.Empty; int fieldIndex = 0; int componentIndex = 0; int subComponentIndex = 0; int comCount = 0; List <string> AllComponents = MessageHelper.SplitString(strValueFormat, new char[] { '.' }); comCount = AllComponents.Count; isValid = validateValueFormat(AllComponents); if (isValid) { segmentName = AllComponents[0]; if (SegmentList.ContainsKey(segmentName)) { if (comCount == 4) { Int32.TryParse(AllComponents[1], out fieldIndex); Int32.TryParse(AllComponents[2], out componentIndex); Int32.TryParse(AllComponents[3], out subComponentIndex); try { SegmentList[segmentName].First().FieldList[fieldIndex - 1].ComponentList[componentIndex - 1].SubComponentList[subComponentIndex - 1].Value = strValue; isSet = true; } catch (Exception ex) { throw new HL7Exception("SubComponent not available - " + strValueFormat + " Error: " + ex.Message); } } else if (comCount == 3) { Int32.TryParse(AllComponents[1], out fieldIndex); Int32.TryParse(AllComponents[2], out componentIndex); try { SegmentList[segmentName].First().FieldList[fieldIndex - 1].ComponentList[componentIndex - 1].Value = strValue; isSet = true; } catch (Exception ex) { throw new HL7Exception("Component not available - " + strValueFormat + " Error: " + ex.Message); } } else if (comCount == 2) { Int32.TryParse(AllComponents[1], out fieldIndex); try { SegmentList[segmentName].First().FieldList[fieldIndex - 1].Value = strValue; isSet = true; } catch (Exception ex) { throw new HL7Exception("Field not available - " + strValueFormat + " Error: " + ex.Message); } } else { throw new HL7Exception("Cannot overwrite a segment value"); } } else { throw new HL7Exception("Segment name not available"); } } else { throw new HL7Exception("Request format is not valid"); } return(isSet); }
/// <summary> /// Get the Value of specific Field/Component/SubCpomponent, throws error if field/component index is not valid /// </summary> /// <param name="strValueFormat">Field/Component position in format SEGMENTNAME.FieldIndex.ComponentIndex.SubComponentIndex example PID.5.2</param> /// <returns>Value of specified field/component/subcomponent</returns> public string GetValue(string strValueFormat) { string segmentName = string.Empty; int componentIndex = 0; int subComponentIndex = 0; string strValue = string.Empty; List <string> allComponents = MessageHelper.SplitString(strValueFormat, new char[] { '.' }); int comCount = allComponents.Count; bool isValid = validateValueFormat(allComponents); if (isValid) { segmentName = allComponents[0]; if (SegmentList.ContainsKey(segmentName)) { var segment = SegmentList[segmentName].First(); if (comCount == 4) { Int32.TryParse(allComponents[2], out componentIndex); Int32.TryParse(allComponents[3], out subComponentIndex); try { var field = this.getField(segment, allComponents[1]); strValue = field.ComponentList[componentIndex - 1].SubComponentList[subComponentIndex - 1].Value; } catch (Exception ex) { throw new HL7Exception("SubComponent not available - " + strValueFormat + " Error: " + ex.Message); } } else if (comCount == 3) { Int32.TryParse(allComponents[2], out componentIndex); try { var field = this.getField(segment, allComponents[1]); strValue = field.ComponentList[componentIndex - 1].Value; } catch (Exception ex) { throw new HL7Exception("Component not available - " + strValueFormat + " Error: " + ex.Message); } } else if (comCount == 2) { try { var field = this.getField(segment, allComponents[1]); strValue = field.Value; } catch (Exception ex) { throw new HL7Exception("Field not available - " + strValueFormat + " Error: " + ex.Message); } } else { try { strValue = segment.Value; } catch (Exception ex) { throw new HL7Exception("Segment value not available - " + strValueFormat + " Error: " + ex.Message); } } } else { throw new HL7Exception("Segment name not available: " + strValueFormat); } } else { throw new HL7Exception("Request format is not valid: " + strValueFormat); } return(this.Encoding.Decode(strValue)); }
/// <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("Unhandled Exception in validation - " + ex.Message, HL7Exception.BAD_MESSAGE); } if (isValid) { try { if (allSegments == null || allSegments.Count <= 0) { allSegments = MessageHelper.SplitString(HL7Message, this.Encoding.SegmentDelimiter); } short SegSeqNo = 0; foreach (string strSegment in allSegments) { if (string.IsNullOrWhiteSpace(strSegment)) { continue; } Segment newSegment = new Segment(this.Encoding); string segmentName = strSegment.Substring(0, 3); newSegment.Name = segmentName; newSegment.Value = strSegment; newSegment.SequenceNo = SegSeqNo++; this.AddNewSegment(newSegment); } this.SegmentCount = SegSeqNo; string strSerializedMessage = string.Empty; try { strSerializedMessage = SerializeMessage(false); } catch (HL7Exception ex) { throw new HL7Exception("Failed to serialize parsed message with error - " + ex.Message, HL7Exception.PARSING_ERROR); } if (!string.IsNullOrEmpty(strSerializedMessage)) { if (this.Equals(strSerializedMessage)) { isParsed = true; } } else { throw new HL7Exception("Unable to serialize to original message - ", HL7Exception.PARSING_ERROR); } } catch (Exception ex) { throw new HL7Exception("Failed to parse the message with error - " + ex.Message, HL7Exception.PARSING_ERROR); } } return(isParsed); }
/// <summary> /// Sets the Value of specific Field/Component/SubComponent, throws error if field/component index is not valid /// </summary> /// <param name="strValueFormat">Field/Component position in format SEGMENTNAME.FieldIndex.ComponentIndex.SubComponentIndex example PID.5.2</param> /// <param name="strValue">Value for the specified field/component</param> /// <returns>boolean</returns> public bool SetValue(string strValueFormat, string strValue) { bool isSet = false; string segmentName = string.Empty; int componentIndex = 0; int subComponentIndex = 0; IList <string> allComponents = MessageHelper.SplitString(strValueFormat, new char[] { '.' }); int comCount = allComponents.Count; bool isValid = ValidateValueFormat(allComponents); if (isValid) { segmentName = allComponents[0]; if (this.SegmentList.ContainsKey(segmentName)) { var segment = this.SegmentList[segmentName].First(); if (comCount == 4) { try { componentIndex = int.Parse(allComponents[2], NumberStyles.Integer, NumberFormatInfo.CurrentInfo); subComponentIndex = int.Parse(allComponents[3], NumberStyles.Integer, NumberFormatInfo.CurrentInfo); var field = GetField(segment, allComponents[1]); field.ComponentList[componentIndex - 1].SubComponentList[subComponentIndex - 1].Value = strValue; isSet = true; } catch (Exception ex) { throw new HL7Exception("SubComponent not available - " + strValueFormat + " Error: " + ex.Message); } } else if (comCount == 3) { try { componentIndex = int.Parse(allComponents[2], NumberStyles.Integer, NumberFormatInfo.CurrentInfo); var field = GetField(segment, allComponents[1]); field.ComponentList[componentIndex - 1].Value = strValue; isSet = true; } catch (Exception ex) { throw new HL7Exception("Component not available - " + strValueFormat + " Error: " + ex.Message); } } else if (comCount == 2) { try { var field = GetField(segment, allComponents[1]); field.Value = strValue; isSet = true; } catch (Exception ex) { throw new HL7Exception("Field not available - " + strValueFormat + " Error: " + ex.Message); } } else { throw new HL7Exception("Cannot overwrite a segment value"); } } else { throw new HL7Exception("Segment name not available"); } } else { throw new HL7Exception("Request format is not valid"); } return(isSet); }