Exemple #1
0
        private bool LoadLine(Tuple <long, string> pair, ref object rec)
        {
            try
            {
                if (!RaiseBeforeRecordLoad(rec, ref pair))
                {
                    ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Skipping...");
                    rec = null;
                    return(true);
                }

                if (pair.Item2 == null)
                {
                    rec = null;
                    return(true);
                }
                else if (pair.Item2 == String.Empty)
                {
                    return(true);
                }

                if (!pair.Item2.IsNullOrWhiteSpace())
                {
                    if (!FillRecord(rec, pair))
                    {
                        return(false);
                    }

                    //if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.ObjectLevel) == ChoObjectValidationMode.ObjectLevel)
                    //    rec.DoObjectLevelValidation(Configuration, Configuration.CSVRecordFieldConfigurations);
                }

                bool skip = false;
                if (!RaiseAfterRecordLoad(rec, pair, ref skip))
                {
                    return(false);
                }
                else if (skip)
                {
                    rec = null;
                    return(true);
                }
            }
            catch (ChoHL7Exception)
            {
                throw;
            }
            catch (ChoMissingRecordFieldException)
            {
                throw;
            }
            catch (Exception ex)
            {
                ChoETLFramework.HandleException(ref ex);
                if (Configuration.ErrorMode == ChoErrorMode.IgnoreAndContinue)
                {
                    rec = null;
                }
                else if (Configuration.ErrorMode == ChoErrorMode.ReportAndContinue)
                {
                    if (!RaiseRecordLoadError(rec, pair, ex))
                    {
                        throw;
                    }
                }
                else
                {
                    throw;
                }

                return(true);
            }

            return(true);
        }
Exemple #2
0
        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;
                        }
                    }
                }
            }
        }
Exemple #3
0
        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);
        }