public bool IsGroupStart1(ChoPeekEnumerator <ChoHL7Segment> segments) { var attr = GetType().GetCustomAttributes(true).OfType <ChoHL7ObjectAttribute>().OrderBy(a => a.Order).FirstOrDefault(); if (attr == null) { return(false); } if (attr.IsGroup) { IChoHL7Group groupObj = attr.GetGroupObject(); return(groupObj.IsGroupStart(segments)); } else { if (segments.Peek == null) { return(false); } ChoHL7Segment segment1 = segments.Peek; return(segment1.TargetType == attr.Name); } }
public ChoHL7Segment(string type, ChoHL7Segment segment = null) { ChoGuard.ArgumentNotNullOrEmpty(type, nameof(type)); TargetType = type; if (segment != null && segment._fields != null) { _fields = new List <ChoHL7Field[]>(segment._fields); } }
internal static ChoHL7Segment Parse(string line, long?lineNo = null, ChoHL7Configuration configuration = null) { configuration = configuration ?? ChoHL7Configuration.Instance; if (line.IsNullOrWhiteSpace()) { throw new ChoHL7Exception("Empty line is found."); } string[] fields = line.Split(configuration.FieldSeparator); if (fields.Length == 0) { throw new ChoHL7Exception("No Segment type found."); } string segmentType = fields[0]; if (segmentType.IsNullOrWhiteSpace() || segmentType.Length != 3) { throw new ChoHL7Exception("Invalid '{0}' Segment type found.".FormatString(segmentType)); } int index = 0; ChoHL7Segment rec = CreateInstance(configuration, segmentType); //new ChoHL7Segment(segmentType); rec.Configuration = configuration; rec.Line = line; rec.LineNo = lineNo; foreach (var field in fields.Skip(1)) { if (segmentType == "MSH" && (index == 0 /* || index == 7*/)) { //if (index == 0) // rec.Fields.Add(new ChoHL7Field[] { new ChoHL7Field(configuration.FieldSeparator.ToString()) }); rec.Fields.Add(new ChoHL7Field[] { new ChoHL7Field(field) }); } else { List <ChoHL7Field> fieldRepeats = new List <ChoHL7Field>(); foreach (var fieldRepeat in field.Split(configuration.RepeatSeparator)) { fieldRepeats.Add(ChoHL7Field.Parse(fieldRepeat, configuration)); } rec.Fields.Add(fieldRepeats.ToArray()); } index++; } rec.Validate(); return(rec); }
public static bool TryParse(string line, ChoHL7Configuration configuration, out ChoHL7Segment rec, out string errMsg) { rec = null; errMsg = null; try { rec = Parse(line, null, configuration); return(true); } catch (Exception ex) { errMsg = ex.Message; return(false); } }
private static ChoHL7Segment CreateInstance(ChoHL7Configuration configuration, string segmentType) { ChoHL7Version version = configuration.Version; ChoHL7Segment msg = null; string segmentTypeName = "ChoETL.HL7.Model.{0}.{1}".FormatString(version.ToString(), segmentType); if (!ChoHL7MessageCache.Instance.HL7SegmentDict.ContainsKey(segmentTypeName)) { throw new ChoHL7Exception("Can't find '{0}' HL7 Segment type.".FormatString(segmentTypeName)); } msg = Activator.CreateInstance(ChoHL7MessageCache.Instance.HL7SegmentDict[segmentTypeName]) as ChoHL7Segment; return(msg); }
private string GetSegmentTypeName() { Type groupOrSegmentType = GroupOrSegmentType; if (!segmentTypeCache.ContainsKey(groupOrSegmentType)) { ChoHL7Segment segment = Activator.CreateInstance(groupOrSegmentType) as ChoHL7Segment; if (segment == null) { throw new ChoHL7Exception("'{0}' is not a segment object.".FormatString(groupOrSegmentType.FullName)); } segmentTypeCache.Add(groupOrSegmentType, segment.TargetType); } return(segmentTypeCache[groupOrSegmentType]); }
public bool IsGroupStart(ChoPeekEnumerator <ChoHL7Segment> segments) { foreach (var attr in GetType().GetCustomAttributes(true).OfType <ChoHL7ObjectAttribute>().OrderBy(a => a.Order)) { if (attr.IsGroup) { IChoHL7Group groupObj = attr.GetGroupObject(); if (groupObj.IsGroupStart(segments)) { return(true); } } else { if (segments.Peek == null) { return(false); } ChoHL7Segment segment1 = segments.Peek; if (segment1.TargetType == attr.Name) { return(true); } else { if (attr.Optional) { continue; } else { return(false); } } } } return(false); }
private void AddOrUpdateSegmentCache(ChoHL7Segment segment, bool isRepeatable) { Console.WriteLine("Group: " + GetType().Name + " Segment: " + segment.TargetType); segment.Validate(); if (isRepeatable) { if (!_repeatableSegmentCache.ContainsKey(segment.TargetType)) { _repeatableSegmentCache.Add(segment.TargetType, new List <ChoHL7Segment>()); } _repeatableSegmentCache[segment.TargetType].Add(segment); } else { if (!_segmentCache.ContainsKey(segment.TargetType)) { _segmentCache.Add(segment.TargetType, segment); } } }
public static ChoHL7Message Parse(TextReader textReader, ChoHL7Configuration configuration = null) { ChoHL7Message msg = null; configuration = configuration ?? ChoHL7Configuration.Instance; bool _configCheckDone = false; bool?skip = false; string[] commentTokens = configuration.Comments; using (ChoPeekEnumerator <Tuple <long, string> > e = new ChoPeekEnumerator <Tuple <long, string> >( new ChoIndexedEnumerator <string>(textReader.ReadLines(configuration.SegmentSeperator.ToString(), ChoCharEx.NUL, false)).ToEnumerable(), (pair) => { //bool isStateAvail = IsStateAvail(); skip = false; //if (isStateAvail) //{ // if (!IsStateMatches(item)) // { // skip = filterFunc != null ? filterFunc(item) : false; // } // else // skip = true; //} //else // skip = filterFunc != null ? filterFunc(item) : false; if (skip == null) { return(null); } if (configuration.TraceSwitch.TraceVerbose) { //ChoETLFramework.WriteLog(configuration.TraceSwitch.TraceVerbose, Environment.NewLine); if (!skip.Value) { ChoETLFramework.WriteLog(configuration.TraceSwitch.TraceVerbose, "Loading line [{0}]...".FormatString(pair.Item1)); } else { ChoETLFramework.WriteLog(configuration.TraceSwitch.TraceVerbose, "Skipping line [{0}]...".FormatString(pair.Item1)); } } if (skip.Value) { return(skip); } if (pair.Item2.IsNullOrWhiteSpace()) { if (!configuration.IgnoreEmptyLine) { throw new ChoHL7Exception("Empty line found at [{0}] location.".FormatString(pair.Item1)); } else { if (configuration.TraceSwitch.TraceVerbose) { ChoETLFramework.WriteLog(configuration.TraceSwitch.TraceVerbose, "Ignoring empty line found at [{0}].".FormatString(pair.Item1)); } return(true); } } if (commentTokens != null && commentTokens.Length > 0) { foreach (string comment in commentTokens) { if (!pair.Item2.IsNull() && pair.Item2.StartsWith(comment, StringComparison.Ordinal)) //, true, Configuration.Culture)) { if (configuration.TraceSwitch.TraceVerbose) { ChoETLFramework.WriteLog(configuration.TraceSwitch.TraceVerbose, "Comment line found at [{0}]...".FormatString(pair.Item1)); } return(true); } } } if (!_configCheckDone) { configuration.Validate(pair.Item2); msg = CreateInstance(configuration); _configCheckDone = true; } return(false); })) { while (true) { Tuple <long, string> pair = e.Peek; if (pair == null) { break; } try { var segment = ChoHL7Segment.Parse(pair.Item2, pair.Item1, configuration); msg.Segments.Add(segment); } catch (Exception ex) { msg.SetError(ex, pair.Item1, pair.Item2); break; } e.MoveNext(); } } var iter = new ChoPeekEnumerator <ChoHL7Segment>(msg.Segments); try { if (msg.IsValid) { msg.Construct(iter); msg.IsValid = iter.Peek == null; if (iter.Peek != null) { msg.SetError("[Line: {1}]: Unrecognized '{0}' segment found in message.".FormatString(iter.Peek.TargetType, iter.Peek.LineNo)); } } } catch (Exception ex) { msg.SetError(ex); } return(msg); }
protected virtual bool Construct(ChoPeekEnumerator <ChoHL7Segment> segments) { //Verify first element is a required segment //var attr = GetType().GetCustomAttributes(true).OfType<ChoHL7ObjectAttribute>().OrderBy(a => a.Order).FirstOrDefault(); var hasRequiredSegment = GetType().GetCustomAttributes(true).OfType <ChoHL7ObjectAttribute>().Where(s => !s.Optional).Any(); if (!hasRequiredSegment) { throw new ChoHL7Exception("Group must have at least one required segment."); } //Check for any duplicate orders var dupDict = GetType().GetCustomAttributes(true).OfType <ChoHL7ObjectAttribute>().GroupBy(i => i.Order).Where(g => g.Count() > 1).ToDictionary(x => x.Key, y => y.Count()); if (dupDict.Count > 0) { throw new ChoHL7Exception("Found segments/group defined with [{1}] duplicate order(s) in '{0}'.".FormatString(GetType().Name, String.Join(",", dupDict.Keys.ToArray()))); } List <Attribute> attrs = new List <Attribute>(); LoadAttributes(GetType(), attrs); var outAttrs = attrs.OfType <ChoHL7ObjectAttribute>().OrderBy(a => a.Order).ToArray(); foreach (var hl7Attr in GetType().GetCustomAttributes(true).OfType <ChoHL7ObjectAttribute>().OrderBy(a => a.Order)) { if (hl7Attr.IsGroup) { while (true) { ChoHL7AbstractMessage groupObj = Activator.CreateInstance(hl7Attr.GroupOrSegmentType) as ChoHL7AbstractMessage; if (groupObj == null) { throw new ChoHL7Exception("'{0}' is not a group object.".FormatString(hl7Attr.GroupOrSegmentType.FullName)); } IChoHL7Group group = groupObj as IChoHL7Group; if (group.IsGroupStart(segments)) { if (groupObj.Construct(segments)) { AddOrUpdateGroupCache(group); //repeatable if (!hl7Attr.Repeatable) { break; } } else { } } else { break; } } } else { string segmmentType = hl7Attr.Name; ChoHL7Segment segment1 = segments.Peek; if (segment1 == null) { return(true); } if (!hl7Attr.Optional && segment1.TargetType != segmmentType) { throw new ChoHL7Exception("Missing '{0}' segment in '{1}' message.".FormatString(segmmentType, GetType().Name)); } else { if (segment1.TargetType == segmmentType) { AddOrUpdateSegmentCache(segment1, hl7Attr.Repeatable); segments.MoveNext(); //repeatable if (hl7Attr.Repeatable) { bool canContinue = false; while (segments.Peek != null) { segment1 = segments.Peek; if (segment1.TargetType == segmmentType) { AddOrUpdateSegmentCache(segment1, hl7Attr.Repeatable); segments.MoveNext(); } else { canContinue = true; break; } } if (!canContinue) { return(true); } } } else { } } } } return(true); }