static void Main(string[] args) { ChoETLFrxBootstrap.TraceLevel = System.Diagnostics.TraceLevel.Error; ChoETLFramework.Initialize(); dynamic hl7Message = ChoHL7Message.Parse("Sample1.csv", new ChoHL7Configuration() { Comments = new string[] { ";" } }); Console.WriteLine($"Version: {hl7Message.Version}"); Console.WriteLine($"Name: {hl7Message.GetType().FullName}"); Console.WriteLine($"MessageType: {hl7Message.MessageType}"); Console.WriteLine($"IsValid: {hl7Message.IsValid}"); Console.WriteLine($"Error Line No: {hl7Message.ErrorLineNo}"); Console.WriteLine($"Error Line: {hl7Message.ErrorLine}"); Console.WriteLine($"Err: {hl7Message.ErrorMsg}"); Console.WriteLine($"Err Detail: {hl7Message.ErrorDetail}"); return; MSH msh = hl7Message.MSH; Console.WriteLine("*** Encoding Chars: " + msh.MessageType); //Console.WriteLine("*** PatientID: " + hl7Message.ORU_R01_RESULTS_GROUP[0]); //foreach (var s in hl7Message.MSH) Console.WriteLine(hl7Message.MSH); Console.WriteLine(hl7Message.EVN); return; foreach (var g in hl7Message.ORU_R01_RESULTS_GROUP) { foreach (var g1 in g.ORU_R01_PATIENT_GROUP) { Console.WriteLine(g1.PID.PatientID); Console.WriteLine(g1.PID.PatientExternalID); foreach (var pi in g1.PID.PatientInternalID) { Console.WriteLine(pi); } foreach (var pi in g1.PID.PatientName) { Console.WriteLine(pi); } Console.WriteLine(g1.PID.DateOfBirth); Console.WriteLine(g1.PID.Sex); } //Console.WriteLine(g1.PID.PatientID); } //Console.WriteLine(hl7Message.ToString()); //var hl7Message = new ChoHL7Message(ChoHL7Version.v2_3); //var hl7MSHSegment = new ChoHL7Segment("MSH"); //hl7MSHSegment.Fields.Add(new ChoHL7Field("1")); //hl7Message.Segments.Add(hl7MSHSegment); //Console.WriteLine(hl7Message.ToString()); //using (var p = new ChoHL7Reader("sample1.csv")) //{ // foreach (var r in p) // Console.WriteLine(r); //} }
static void Main(string[] args) { ChoETLFramework.Initialize(); SortByCity(); }
private static void OldTest() { //var t = ChoTypeDescriptor.GetPropetyAttributes<ChoTypeConverterAttribute>(ChoTypeDescriptor.GetProperty<ChoTypeConverterAttribute>(typeof(EmployeeRecMeta), "Name")).ToArray(); //return; //ChoMetadataObjectCache.Default.Attach(typeof(EmployeeRec), new EmployeeRecMeta()); //string v = @"4,'123\r\n4,abc'"; //foreach (var ss in v.SplitNTrim(",", ChoStringSplitOptions.None, '\'')) // Console.WriteLine(ss + "-"); //return; ChoCSVRecordConfiguration config = new ChoCSVRecordConfiguration(); //config.AutoDiscoverColumns = false; config.FileHeaderConfiguration.HasHeaderRecord = true; //config.CSVFileHeaderConfiguration.FillChar = '$'; config.ThrowAndStopOnMissingField = false; //config.HasExcelSeparator = true; config.ColumnCountStrict = false; //config.MapRecordFields<EmployeeRec>(); ChoCSVRecordFieldConfiguration idConfig = new ChoCSVRecordFieldConfiguration("Id", 1); idConfig.AddConverter(new IntConverter()); config.CSVRecordFieldConfigurations.Add(idConfig); config.CSVRecordFieldConfigurations.Add(new ChoCSVRecordFieldConfiguration("Name", 2)); config.CSVRecordFieldConfigurations.Add(new ChoCSVRecordFieldConfiguration("Name1", 2)); dynamic rec = new ExpandoObject(); rec.Id = 1; rec.Name = "Raj"; //using (var wr = new ChoCSVWriter("EmpOut.csv", config)) //{ // wr.Write(new List<ExpandoObject>() { rec }); //} //List<EmployeeRec> recs = new List<EmployeeRec>(); //recs.Add(new EmployeeRec() { Id = 1, Name = "Raj" }); //recs.Add(new EmployeeRec() { Id = 2, Name = "Mark" }); //using (var stream = new MemoryStream()) //using (var reader = new StreamReader(stream)) //using (var writer = new StreamWriter(stream)) //using (var parser = new ChoCSVWriter<EmployeeRec>(writer, config)) //{ // parser.Write(recs); // writer.Flush(); // stream.Position = 0; // Console.WriteLine(reader.ReadToEnd()); //} //return; //string txt = "Id, Name\r\n1, Mark"; //foreach (var e in ChoCSVReader.LoadText(txt)) // Console.WriteLine(e.ToStringEx()); //return; //dynamic row; //using (var stream = new MemoryStream()) //using (var reader = new StreamReader(stream)) //using (var writer = new StreamWriter(stream)) //using (var parser = new ChoCSVReader(reader, config)) //{ // //writer.WriteLine("Id,Name"); // writer.WriteLine("1,Carl"); // writer.WriteLine("2,Mark"); // writer.Flush(); // stream.Position = 0; // while ((row = parser.Read()) != null) // { // Console.WriteLine(row.Id); // } //} //return; //DataTable dt = new ChoCSVReader<EmployeeRec>("Emp.csv").AsDataTable(); //var z = dt.Rows.Count; //return; foreach (var item in new ChoCSVReader <EmployeeRec>("Emp.csv")) { Console.WriteLine(item.ToStringEx()); } return; //var reader = new ChoCSVReader<EmployeeRec>("Emp.csv"); //var rec = (object)null; //while ((rec = reader.Read()) != null) // Console.WriteLine(rec.ToStringEx()); //var config = new ChoCSVRecordConfiguration(typeof(EmployeeRec)); //var e = new ChoCSVReader("Emp.csv", config); //dynamic i; //while ((i = e.Read()) != null) // Console.WriteLine(i.Id); ChoETLFramework.Initialize(); using (var stream = new MemoryStream()) using (var reader = new StreamReader(stream)) using (var writer = new StreamWriter(stream)) using (var parser = new ChoCSVReader <EmployeeRec>(reader)) { writer.WriteLine("Id,Name"); writer.WriteLine("1,Carl"); writer.WriteLine("2,Mark"); writer.Flush(); stream.Position = 0; //var dr = parser.AsDataReader(); //while (dr.Read()) //{ // Console.WriteLine(dr[0]); //} object row1 = null; //parser.Configuration.ColumnCountStrict = true; while ((row1 = parser.Read()) != null) { Console.WriteLine(row1.ToStringEx()); } } }
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; } } } } }
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); }
static void Main(string[] args) { //string v = @"4,'123\r\n4,abc'"; //foreach (var ss in v.SplitNTrim(",", ChoStringSplitOptions.None, '\'')) // Console.WriteLine(ss + "-"); //return; dynamic rec = new ExpandoObject(); rec.Id = 1; rec.Name = "Raj"; using (var wr = new ChoCSVWriter("EmpOut.csv")) { wr.Write(new List <ExpandoObject>() { rec }); } return; ChoCSVRecordConfiguration config = new ChoCSVRecordConfiguration(); //config.AutoDiscoverColumns = false; config.CSVFileHeaderConfiguration.HasHeaderRecord = true; config.ThrowAndStopOnMissingField = true; //config.MapRecordFields<EmployeeRec>(); //config.RecordFieldConfigurations.Add(new ChoCSVRecordFieldConfiguration("Id", 1)); //config.RecordFieldConfigurations.Add(new ChoCSVRecordFieldConfiguration("Name", 2)); //dynamic row; //using (var stream = new MemoryStream()) //using (var reader = new StreamReader(stream)) //using (var writer = new StreamWriter(stream)) //using (var parser = new ChoCSVReader(reader, config)) //{ // writer.WriteLine("Id,Name"); // writer.WriteLine("1,Carl"); // writer.WriteLine("2,Mark"); // writer.Flush(); // stream.Position = 0; // while ((row = parser.Read()) != null) // { // Console.WriteLine(row.Id); // } //} //return; //DataTable dt = new ChoCSVReader<EmployeeRec>("Emp.csv").AsDataTable(); //var z = dt.Rows.Count; //return; foreach (var e in new ChoCSVReader <EmployeeRec>("Emp.csv")) { Console.WriteLine(e.ToStringEx()); } return; //var reader = new ChoCSVReader<EmployeeRec>("Emp.csv"); //var rec = (object)null; //while ((rec = reader.Read()) != null) // Console.WriteLine(rec.ToStringEx()); //var config = new ChoCSVRecordConfiguration(typeof(EmployeeRec)); //var e = new ChoCSVReader("Emp.csv", config); //dynamic i; //while ((i = e.Read()) != null) // Console.WriteLine(i.Id); ChoETLFramework.Initialize(); using (var stream = new MemoryStream()) using (var reader = new StreamReader(stream)) using (var writer = new StreamWriter(stream)) using (var parser = new ChoCSVReader <EmployeeRec>(reader)) { writer.WriteLine("Id,Name"); writer.WriteLine("1,Carl"); writer.WriteLine("2,Mark"); writer.Flush(); stream.Position = 0; //var dr = parser.AsDataReader(); //while (dr.Read()) //{ // Console.WriteLine(dr[0]); //} object row = null; //parser.Configuration.ColumnCountStrict = true; while ((row = parser.Read()) != null) { Console.WriteLine(row.ToStringEx()); } } }
static void Main(string[] args) { ChoCSVRecordConfiguration config = new ChoCSVRecordConfiguration(); config.RecordFieldConfigurations.Add(new ChoCSVRecordFieldConfiguration("Id", 1)); config.RecordFieldConfigurations.Add(new ChoCSVRecordFieldConfiguration("Name", 2)); dynamic row; using (var stream = new MemoryStream()) using (var reader = new StreamReader(stream)) using (var writer = new StreamWriter(stream)) using (var parser = new ChoCSVReader(reader, config)) { writer.WriteLine("1,Carl"); writer.WriteLine("2,Mark"); writer.Flush(); stream.Position = 0; while ((row = parser.Read()) != null) { Console.WriteLine(row.Name); } } return; //DataTable dt = new ChoCSVReader<EmployeeRec>("Emp.csv").AsDataTable(); //var z = dt.Rows.Count; //return; //foreach (var e in new ChoCSVReader<EmployeeRec>("Emp.csv")) // Console.WriteLine(e.ToStringEx()); //var reader = new ChoCSVReader<EmployeeRec>("Emp.csv"); //var rec = (object)null; //while ((rec = reader.Read()) != null) // Console.WriteLine(rec.ToStringEx()); //var config = new ChoCSVRecordConfiguration(typeof(EmployeeRec)); //var e = new ChoCSVReader("Emp.csv", config); //dynamic i; //while ((i = e.Read()) != null) // Console.WriteLine(i.Id); ChoETLFramework.Initialize(); using (var stream = new MemoryStream()) using (var reader = new StreamReader(stream)) using (var writer = new StreamWriter(stream)) using (var parser = new ChoCSVReader <EmployeeRec>(reader)) { //writer.WriteLine("Id,Name"); writer.WriteLine("1,Carl"); writer.WriteLine("2,Mark"); writer.Flush(); stream.Position = 0; var dr = parser.AsDataReader(); while (dr.Read()) { Console.WriteLine(dr[0]); } //object row = null; //parser.Configuration.ColumnCountStrict = true; //while ((row = parser.Read()) != null) //{ // Console.WriteLine(row.ToStringEx()); //} } }
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); }