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 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 IEnumerable <object> AsEnumerable(object source, TraceSwitch traceSwitch, Func <object, bool?> filterFunc = null) { TraceSwitch = traceSwitch; TextReader sr = source as TextReader; ChoGuard.ArgumentNotNull(sr, "TextReader"); if (sr is StreamReader) { ((StreamReader)sr).Seek(0, SeekOrigin.Begin); } if (!RaiseBeginLoad(sr)) { yield break; } string[] commentTokens = new string[] { }; bool? skip = false; bool abortRequested = false; long runningCount = 0; using (ChoPeekEnumerator <Tuple <long, string> > e = new ChoPeekEnumerator <Tuple <long, string> >( new ChoIndexedEnumerator <string>(sr.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 (TraceSwitch.TraceVerbose) { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, Environment.NewLine); if (!skip.Value) { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Loading line [{0}]...".FormatString(pair.Item1)); } else { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Skipping line [{0}]...".FormatString(pair.Item1)); } } if (skip.Value) { return(skip); } //if (!(sr.BaseStream is MemoryStream)) // ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, ChoETLFramework.Switch.TraceVerbose, "Loading line [{0}]...".FormatString(item.Item1)); //if (Task != null) // return !IsStateNOTExistsOrNOTMatch(item); if (pair.Item2.IsNullOrWhiteSpace()) { if (!Configuration.IgnoreEmptyLine) { throw new ChoHL7Exception("Empty line found at [{0}] location.".FormatString(pair.Item1)); } else { if (TraceSwitch.TraceVerbose) { ChoETLFramework.WriteLog(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 (TraceSwitch.TraceVerbose) { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Comment line found at [{0}]...".FormatString(pair.Item1)); } return(true); } } } if (!_configCheckDone) { //ChoHL7Version version; //ChoHL7MessageType mt; Configuration.Validate(pair.Item2); _configCheckDone = true; } return(false); })) { while (true) { Tuple <long, string> pair = e.Peek; if (pair == null) { if (!abortRequested) { RaisedRowsLoaded(runningCount); } RaiseEndLoad(sr); yield break; } runningCount = pair.Item1; object rec = null; // Configuration.IsDynamicObject ? new ExpandoObject() : Activator.CreateInstance(RecordType); if (!LoadLine(pair, ref rec)) { yield break; } //StoreState(e.Current, rec != null); e.MoveNext(); if (rec == null) { continue; } yield return(rec); if (Configuration.NotifyAfter > 0 && pair.Item1 % Configuration.NotifyAfter == 0) { if (RaisedRowsLoaded(pair.Item1)) { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Abort requested."); abortRequested = true; yield break; } } } } }
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); }