public static IQueryable <T> StageOnSQLite <T>(this IEnumerable <T> items, ChoETLSqliteSettings sqliteSettings = null) where T : class { Dictionary <string, PropertyInfo> PIDict = null; if (!typeof(T).IsDynamicType() && typeof(T) != typeof(object)) { PIDict = ChoType.GetProperties(typeof(T)).ToDictionary(p => p.Name); } sqliteSettings = ValidateSettings <T>(sqliteSettings); LoadDataToDb(items, sqliteSettings, PIDict); if (!typeof(T).IsDynamicType() && typeof(T) != typeof(object)) { var ctx = new ChoETLSQLiteDbContext <T>(sqliteSettings.GetConnectionString()); ctx.Log = sqliteSettings.Log; var dbSet = ctx.Set <T>(); return(dbSet); } else { return(Enumerable.Empty <T>().AsQueryable()); } }
public static object ToJSONObject(this IDictionary <string, object> dict, Type type) { object target = ChoActivator.CreateInstance(type); string key = null; foreach (var p in ChoType.GetProperties(type)) { if (p.GetCustomAttribute <JsonIgnoreAttribute>() != null) { continue; } key = p.Name; var attr = p.GetCustomAttribute <JsonPropertyAttribute>(); if (attr != null && !attr.PropertyName.IsNullOrWhiteSpace()) { key = attr.PropertyName.NTrim(); } if (!dict.ContainsKey(key)) { continue; } p.SetValue(target, dict[key].CastObjectTo(p.PropertyType)); } return(target); }
private string[] GetFields(object record) { string[] fieldNames = null; Type recordType = ElementType == null?record.GetType() : ElementType; Configuration.RecordType = recordType.ResolveType(); Configuration.IsDynamicObject = recordType.IsDynamicType(); if (!Configuration.IsDynamicObject) { if (Configuration.FixedLengthRecordFieldConfigurations.Count == 0) { Configuration.MapRecordFields(Configuration.RecordType); } } if (Configuration.IsDynamicObject) { var dictKeys = new List <string>(); var dict = record.ToDynamicObject() as IDictionary <string, Object>; fieldNames = dict.Flatten().ToDictionary().Keys.ToArray(); } else { fieldNames = ChoTypeDescriptor.GetProperties <ChoCSVRecordFieldAttribute>(Configuration.RecordType).Select(pd => pd.Name).ToArray(); if (fieldNames.Length == 0) { fieldNames = ChoType.GetProperties(Configuration.RecordType).Select(p => p.Name).ToArray(); } } return(fieldNames); }
public virtual void Validate() { string propName = null; object propValue = null; string propValueText = null; var results = new List <ValidationResult>(); foreach (PropertyInfo pi in ChoType.GetProperties(GetType())) { propName = pi.Name; results.Clear(); try { propValue = ChoType.GetPropertyValue(this, pi); } catch (Exception ex) { throw new ChoHL7Exception("Failed to validate '{0}' member.".FormatString(pi.FullName()), ex); } try { propValueText = propValue.ToNString(); } catch (Exception ex) { throw new ChoHL7Exception("Failed to validate '{0}' member.".FormatString(pi.FullName()), ex); } pi.CheckRequired(propValue); if (Field != null && Field.Configuration != null && !Field.Configuration.DisableEnumChecks) { pi.CheckForValidEnumValue(propValueText); } var context = new ValidationContext(propValueText, null, null); context.MemberName = pi.Name; Validator.TryValidateValue(propValueText, context, results, ChoTypeDescriptor.GetPropetyAttributes <ValidationAttribute>(ChoTypeDescriptor.GetProperty <ValidationAttribute>(GetType(), pi.Name))); if (results.Count > 0) { throw new ChoHL7Exception("Failed to validate '{0}' member. {2}{1}".FormatString(pi.FullName(), ChoHL7Helper.ToString(results), Environment.NewLine)); } try { if (propValue is ChoHL7AbstractField) { ((ChoHL7AbstractField)propValue).Validate(); } } catch (Exception ex) { throw new ChoHL7Exception("Failed to validate '{0}' member.".FormatString(pi.FullName()), ex); } } }
private static PropertyInfo[] GetPropertyInfos <T>() { PropertyInfo[] props = null; if (!_propInfoCache.TryGetValue(typeof(T), out props)) { props = ChoType.GetProperties(typeof(T)).OrderBy(pi => ChoType.GetAttribute <ColumnAttribute>(pi) == null ? 0 : ChoType.GetAttribute <ColumnAttribute>(pi).Order).ToArray(); _propInfoCache.AddOrUpdate(typeof(T), props); } return(props); }
public virtual void Validate(object state) { if (!IsDynamicObject) { PIDict = ChoType.GetProperties(RecordType).ToDictionary(p => p.Name); PDDict = new Dictionary <string, PropertyDescriptor>(); foreach (var fn in PIDict.Keys) { PDDict.Add(fn, ChoTypeDescriptor.GetProperty(RecordType, fn)); } } }
public static object ConvertToObject(this object source, Type type) { if (source == null) { return(source); } if (source is IDictionary <string, object> ) { return(((IDictionary <string, object>)source).ToObject(type)); } else { if (source is IDictionary) { var dict = ((IDictionary)source).ToDictionary(); return(dict.ToObject(type)); } else { Type sourceType = source.GetType(); object target = ChoActivator.CreateInstance(type); string key = null; object value = null; foreach (var p in ChoType.GetProperties(type)) { if (p.GetCustomAttribute <ChoIgnoreMemberAttribute>() != null) { continue; } key = p.Name; var attr = p.GetCustomAttribute <ChoPropertyAttribute>(); if (attr != null && !attr.Name.IsNullOrWhiteSpace()) { key = attr.Name.NTrim(); } if (!ChoType.HasProperty(sourceType, key)) { continue; } value = ChoType.GetPropertyValue(source, key); p.SetValue(target, value.CastObjectTo(p.PropertyType)); } return(target); } } }
public static IQueryable <T> StageOnSqlServer <T>(this IEnumerable <T> items, ChoETLSqlServerSettings sqlServerSettings = null) where T : class { if (typeof(T) == typeof(ExpandoObject) || typeof(T) == typeof(object)) { throw new NotSupportedException(); } Dictionary <string, PropertyInfo> PIDict = ChoType.GetProperties(typeof(T)).ToDictionary(p => p.Name); sqlServerSettings = ValidateSettings <T>(sqlServerSettings); LoadDataToDb(items, sqlServerSettings, PIDict); var ctx = new ChoETLSqlServerDbContext <T>(sqlServerSettings.ConnectionString); var dbSet = ctx.Set <T>(); return(dbSet); }
public static T MemberwiseClone <T>(this T target) where T : new() { T obj = Activator.CreateInstance <T>(); if (target != null) { foreach (PropertyInfo info in ChoType.GetProperties(typeof(T))) { if (ChoType.GetAttribute <ChoIgnorePropertyAttribute>(info, false) == null) { ChoType.SetPropertyValue(obj, info, ChoType.GetPropertyValue(target, info)); } } } return(obj); }
public static IQueryable <T> StageOnSQLite <T>(this IEnumerable <T> items, ChoETLSqliteSettings sqliteSettings = null) where T : class { if (typeof(T).IsDynamicType() || typeof(T) == typeof(object)) { throw new NotSupportedException(); } Dictionary <string, PropertyInfo> PIDict = ChoType.GetProperties(typeof(T)).ToDictionary(p => p.Name); sqliteSettings = ValidateSettings <T>(sqliteSettings); LoadDataToDb(items, sqliteSettings, PIDict); var ctx = new ChoETLSQLiteDbContext <T>(sqliteSettings.DatabaseFilePath); var dbSet = ctx.Set <T>(); return(dbSet); }
private string[] GetFields(List <object> records) { string[] fieldNames = null; Type recordType = ElementType == null?records.First().GetType() : ElementType; Configuration.RecordType = recordType.ResolveType(); Configuration.IsDynamicObject = recordType.IsDynamicType(); if (!Configuration.IsDynamicObject) { if (Configuration.FixedLengthRecordFieldConfigurations.Count == 0) { Configuration.MapRecordFields(Configuration.RecordType); } } if (Configuration.IsDynamicObject) { var record = new Dictionary <string, object>(); foreach (var r in records.Select(r => (IDictionary <string, Object>)r.ToDynamicObject())) { record.Merge(r); } if (Configuration.UseNestedKeyFormat) { fieldNames = record.Flatten(Configuration.NestedColumnSeparator).ToDictionary().Keys.ToArray(); } else { fieldNames = record.Keys.ToArray(); } } else { fieldNames = ChoTypeDescriptor.GetProperties <ChoCSVRecordFieldAttribute>(Configuration.RecordType).Select(pd => pd.Name).ToArray(); if (fieldNames.Length == 0) { fieldNames = ChoType.GetProperties(Configuration.RecordType).Select(p => p.Name).ToArray(); } } return(fieldNames); }
public static object MemberwiseClone <T>(this object target) where T : new() { if (target == null) { object obj = Activator.CreateInstance(target.GetType()); foreach (PropertyInfo info in ChoType.GetProperties(target.GetType())) { if (ChoType.GetAttribute <ChoIgnorePropertyAttribute>(info, false) == null) { ChoType.SetPropertyValue(obj, info, ChoType.GetPropertyValue(target, info)); } } return(obj); } else { return(null); } }
public override IEnumerable <object> WriteTo(object writer, IEnumerable <object> records, Func <object, bool> predicate = null) { TextWriter sw = writer as TextWriter; ChoGuard.ArgumentNotNull(sw, "TextWriter"); if (records == null) { yield break; } CultureInfo prevCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture; System.Threading.Thread.CurrentThread.CurrentCulture = Configuration.Culture; _se = new Lazy <XmlSerializer>(() => Configuration.XmlSerializer == null ? null : Configuration.XmlSerializer); string recText = String.Empty; try { foreach (object record in records) { _index++; if (TraceSwitch.TraceVerbose) { if (record is IChoETLNameableObject) { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Writing [{0}] object...".FormatString(((IChoETLNameableObject)record).Name)); } else { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Writing [{0}] object...".FormatString(_index)); } } recText = String.Empty; if (predicate == null || predicate(record)) { //Discover and load Xml columns from first record if (!_configCheckDone) { if (record == null) { continue; } string[] fieldNames = null; Type recordType = ElementType == null?record.GetType() : ElementType; if (typeof(ICollection).IsAssignableFrom(recordType)) { recordType = recordType.GetEnumerableItemType().GetUnderlyingType(); } else { recordType = recordType.GetUnderlyingType(); } Configuration.IsDynamicObject = recordType.IsDynamicType(); if (!Configuration.IsDynamicObject) { if (recordType.IsSimple()) { Configuration.RecordType = typeof(ChoScalarObject <>).MakeGenericType(recordType); } else { Configuration.RecordType = recordType; } } if (Configuration.IsDynamicObject) { var dict = record.ToDynamicObject() as IDictionary <string, Object>; fieldNames = dict.Keys.ToArray(); } else { fieldNames = ChoTypeDescriptor.GetProperties <ChoXmlNodeRecordFieldAttribute>(Configuration.RecordType).Select(pd => pd.Name).ToArray(); if (fieldNames.Length == 0) { fieldNames = ChoType.GetProperties(Configuration.RecordType).Select(p => p.Name).ToArray(); } } Configuration.Validate(fieldNames); _configCheckDone = true; if (!RaiseBeginWrite(sw)) { yield break; } sw.Write("<{0}{1}>".FormatString(Configuration.RootName, GetNamespaceText())); } if (!RaiseBeforeRecordWrite(record, _index, ref recText)) { yield break; } if (recText == null) { continue; } try { if (!Configuration.UseXmlSerialization) { if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.ObjectLevel) == ChoObjectValidationMode.ObjectLevel) { record.DoObjectLevelValidation(Configuration, Configuration.XmlRecordFieldConfigurations); } if (ToText(_index, record, out recText)) { if (!recText.IsNullOrEmpty()) { sw.Write("{1}{0}", recText, Configuration.EOLDelimiter); } if (!RaiseAfterRecordWrite(record, _index, recText)) { yield break; } } } else { if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.Off) != ChoObjectValidationMode.Off) { record.DoObjectLevelValidation(Configuration, Configuration.XmlRecordFieldConfigurations); } if (record != null) { if (_se.Value != null) { _se.Value.Serialize(sw, record); } else { sw.Write("{1}{0}", ChoUtility.XmlSerialize(record).Indent(2, Configuration.IndentChar.ToString()), Configuration.EOLDelimiter); } if (!RaiseAfterRecordWrite(record, _index, null)) { yield break; } } } } //catch (ChoParserException) //{ // throw; //} catch (Exception ex) { ChoETLFramework.HandleException(ex); if (Configuration.ErrorMode == ChoErrorMode.IgnoreAndContinue) { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Error [{0}] found. Ignoring record...".FormatString(ex.Message)); } else if (Configuration.ErrorMode == ChoErrorMode.ReportAndContinue) { if (!RaiseRecordWriteError(record, _index, recText, ex)) { throw; } else { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Error [{0}] found. Ignoring record...".FormatString(ex.Message)); } } else { throw; } } } yield return(record); if (Configuration.NotifyAfter > 0 && _index % Configuration.NotifyAfter == 0) { if (RaisedRowsWritten(_index)) { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Abort requested."); yield break; } } } } finally { System.Threading.Thread.CurrentThread.CurrentCulture = prevCultureInfo; } }
public override IEnumerable <object> WriteTo(object writer, IEnumerable <object> records, Func <object, bool> predicate = null) { TextWriter sw = writer as TextWriter; ChoGuard.ArgumentNotNull(sw, "TextWriter"); if (records == null) { yield break; } if (!RaiseBeginWrite(sw)) { yield break; } CultureInfo prevCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture; System.Threading.Thread.CurrentThread.CurrentCulture = Configuration.Culture; string recText = String.Empty; try { foreach (object record in records) { _index++; if (TraceSwitch.TraceVerbose) { if (record is IChoETLNameableObject) { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Writing [{0}] object...".FormatString(((IChoETLNameableObject)record).Name)); } else { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Writing [{0}] object...".FormatString(_index)); } } recText = String.Empty; if (record != null) { if (predicate == null || predicate(record)) { //Discover and load Xml columns from first record if (!_configCheckDone) { string[] fieldNames = null; if (Configuration.IsDynamicObject) { var dict = record.ToDynamicObject() as IDictionary <string, Object>; fieldNames = dict.Keys.ToArray(); } else { fieldNames = ChoTypeDescriptor.GetProperties <ChoXmlNodeRecordFieldAttribute>(record.GetType()).Select(pd => pd.Name).ToArray(); if (fieldNames.Length == 0) { fieldNames = ChoType.GetProperties(record.GetType()).Select(p => p.Name).ToArray(); } } Configuration.Validate(fieldNames); _configCheckDone = true; sw.Write("<{0}{1}>".FormatString(Configuration.RootName, GetNamespaceText())); } if (!RaiseBeforeRecordWrite(record, _index, ref recText)) { yield break; } if (recText == null) { continue; } try { if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.ObjectLevel) == ChoObjectValidationMode.ObjectLevel) { record.DoObjectLevelValidation(Configuration, Configuration.XmlRecordFieldConfigurations); } if (ToText(_index, record, out recText)) { sw.Write("{1}{0}", recText, Configuration.EOLDelimiter); if (!RaiseAfterRecordWrite(record, _index, recText)) { yield break; } } } catch (ChoParserException) { throw; } catch (Exception ex) { ChoETLFramework.HandleException(ex); if (Configuration.ErrorMode == ChoErrorMode.IgnoreAndContinue) { } else if (Configuration.ErrorMode == ChoErrorMode.ReportAndContinue) { if (!RaiseRecordWriteError(record, _index, recText, ex)) { throw; } } else { throw; } } } } yield return(record); if (Configuration.NotifyAfter > 0 && _index % Configuration.NotifyAfter == 0) { if (RaisedRowsWritten(_index)) { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Abort requested."); yield break; } } } if (_configCheckDone) { sw.Write("{1}</{0}>".FormatString(Configuration.RootName, Configuration.EOLDelimiter)); } } finally { System.Threading.Thread.CurrentThread.CurrentCulture = prevCultureInfo; } RaiseEndWrite(sw); }
public static IQueryable <T> StageOnSqlServerUsingBcp <T>(this IEnumerable <T> items, ChoETLSqlServerSettings sqlServerSettings = null) where T : class { if (typeof(T) == typeof(ExpandoObject) || typeof(T) == typeof(object)) { throw new NotSupportedException(); } Dictionary <string, PropertyInfo> PIDict = ChoType.GetProperties(typeof(T)).ToDictionary(p => p.Name); sqlServerSettings = ValidateSettings <T>(sqlServerSettings); CreateDatabaseIfLocalDb(sqlServerSettings); var firstItem = items.FirstOrDefault(); if (firstItem != null) { using (var conn1 = new SqlConnection(sqlServerSettings.ConnectionString)) { conn1.Open(); try { SqlCommand command = new SqlCommand(firstItem.CreateTableScript(sqlServerSettings.ColumnDataMapper, sqlServerSettings.TableName), conn1); command.ExecuteNonQuery(); } catch { } //Truncate table try { SqlCommand command = new SqlCommand("TRUNCATE TABLE [{0}]".FormatString(sqlServerSettings.TableName), conn1); command.ExecuteNonQuery(); } catch { try { SqlCommand command = new SqlCommand("DELETE FROM [{0}]".FormatString(sqlServerSettings.TableName), conn1); command.ExecuteNonQuery(); } catch { } } } using (SqlBulkCopy bcp = new SqlBulkCopy(sqlServerSettings.ConnectionString)) { bcp.DestinationTableName = sqlServerSettings.TableName; bcp.EnableStreaming = true; //bcp.NotifyAfter = 10; //bcp.SqlRowsCopied += delegate (object sender, SqlRowsCopiedEventArgs e) //{ // Console.WriteLine(e.RowsCopied.ToString("#,##0") + " rows copied."); //}; bcp.WriteToServer(new ChoEnumerableDataReader(items)); } var ctx = new ChoETLSqlServerDbContext <T>(sqlServerSettings.ConnectionString); var dbSet = ctx.Set <T>(); return(dbSet); } else { return(items.AsQueryable()); } }
public override IEnumerable <object> WriteTo(object writer, IEnumerable <object> records, Func <object, bool> predicate = null) { StreamWriter sw = writer as StreamWriter; ChoGuard.ArgumentNotNull(sw, "StreamWriter"); if (records == null) { yield break; } if (!RaiseBeginWrite(sw)) { yield break; } CultureInfo prevCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture; System.Threading.Thread.CurrentThread.CurrentCulture = Configuration.Culture; string recText = String.Empty; try { int index = 0; foreach (object record in records) { if (record is IChoETLNameableObject) { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Writing [{0}] object...".FormatString(((IChoETLNameableObject)record).Name)); } else { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Writing [{0}] object...".FormatString(++index)); } recText = String.Empty; _index++; if (record != null) { if (predicate == null || predicate(record)) { //Discover and load CSV columns from first record if (!_configCheckDone) { string[] fieldNames = null; if (record is ExpandoObject) { var dict = record as IDictionary <string, Object>; fieldNames = dict.Keys.ToArray(); } else { fieldNames = ChoTypeDescriptor.GetProperties <ChoCSVRecordFieldAttribute>(record.GetType()).Select(pd => pd.Name).ToArray(); if (fieldNames.Length == 0) { fieldNames = ChoType.GetProperties(record.GetType()).Select(p => p.Name).ToArray(); } } Configuration.Validate(fieldNames); WriteHeaderLine(sw); _configCheckDone = true; } if (!RaiseBeforeRecordWrite(record, _index, ref recText)) { yield break; } if (recText == null) { continue; } else if (recText.Length > 0) { sw.Write("{1}{0}", recText, Configuration.FileHeaderConfiguration.HasHeaderRecord || HasExcelSeparator ? Configuration.EOLDelimiter : ""); continue; } try { if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.ObjectLevel) == ChoObjectValidationMode.ObjectLevel) { record.DoObjectLevelValidatation(Configuration.CSVRecordFieldConfigurations.Cast <ChoRecordFieldConfiguration>().ToArray()); } if (ToText(_index, record, out recText)) { if (_index == 1) { sw.Write("{1}{0}", recText, Configuration.FileHeaderConfiguration.HasHeaderRecord || HasExcelSeparator ? Configuration.EOLDelimiter : ""); } else { sw.Write("{1}{0}", recText, Configuration.EOLDelimiter); } if (!RaiseAfterRecordWrite(record, _index, recText)) { yield break; } } } catch (ChoParserException) { throw; } catch (Exception ex) { ChoETLFramework.HandleException(ex); if (Configuration.ErrorMode == ChoErrorMode.IgnoreAndContinue) { } else if (Configuration.ErrorMode == ChoErrorMode.ReportAndContinue) { if (!RaiseRecordWriteError(record, _index, recText, ex)) { throw; } } else { throw; } } } } yield return(record); } } finally { System.Threading.Thread.CurrentThread.CurrentCulture = prevCultureInfo; } RaiseEndWrite(sw); }
public override IEnumerable <object> WriteTo(object writer, IEnumerable <object> records, Func <object, bool> predicate = null) { _sw = writer; TextWriter sw = writer as TextWriter; ChoGuard.ArgumentNotNull(sw, "TextWriter"); if (Configuration.JsonSerializerSettings.ContractResolver is ChoPropertyRenameAndIgnoreSerializerContractResolver) { ChoPropertyRenameAndIgnoreSerializerContractResolver cr = Configuration.JsonSerializerSettings.ContractResolver as ChoPropertyRenameAndIgnoreSerializerContractResolver; cr.CallbackRecordFieldWrite = _callbackRecordFieldWrite; cr.Writer = Writer; } if (records == null) { yield break; } if (Configuration.SingleDocument == null) { Configuration.SingleDocument = false; } CultureInfo prevCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture; System.Threading.Thread.CurrentThread.CurrentCulture = Configuration.Culture; string recText = String.Empty; bool recordIgnored = false; try { foreach (object record in records) { _index++; //if (!isFirstRec) //{ // if (!recordIgnored) // sw.Write(","); // else // recordIgnored = false; //} if (TraceSwitch.TraceVerbose) { if (record is IChoETLNameableObject) { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Writing [{0}] object...".FormatString(((IChoETLNameableObject)record).Name)); } else { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Writing [{0}] object...".FormatString(_index)); } } recText = String.Empty; if (predicate == null || predicate(record)) { //Discover and load Xml columns from first record if (!Configuration.IsInitialized) { if (record == null) { continue; } string[] fieldNames = null; if (Configuration.RecordType == typeof(object)) { Type recordType = ElementType == null?record.GetType() : ElementType; RecordType = Configuration.RecordType = recordType.GetUnderlyingType(); //.ResolveType(); Configuration.IsDynamicObject = recordType.IsDynamicType(); } if (typeof(IDictionary).IsAssignableFrom(Configuration.RecordType) || typeof(IList).IsAssignableFrom(Configuration.RecordType)) { Configuration.UseYamlSerialization = true; } if (!Configuration.IsDynamicObject) { if (Configuration.YamlRecordFieldConfigurations.Count == 0) { Configuration.MapRecordFields(Configuration.RecordType); } } if (Configuration.IsDynamicObject) { var dict = record.ToDynamicObject() as IDictionary <string, Object>; fieldNames = dict.Keys.ToArray(); } else { fieldNames = ChoTypeDescriptor.GetProperties <ChoYamlRecordFieldAttribute>(Configuration.RecordType).Select(pd => pd.Name).ToArray(); if (fieldNames.Length == 0) { fieldNames = ChoType.GetProperties(Configuration.RecordType).Select(p => p.Name).ToArray(); } } Configuration.Validate(fieldNames); Configuration.IsInitialized = true; if (!BeginWrite.Value) { yield break; } } if (!RaiseBeforeRecordWrite(record, _index, ref recText)) { yield break; } if (recText == null) { continue; } try { if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.ObjectLevel) == ChoObjectValidationMode.ObjectLevel) { record.DoObjectLevelValidation(Configuration, Configuration.YamlRecordFieldConfigurations); } if (ToText(_index, record, out recText)) { if (recText.EndsWith("...")) { recText = recText.Left(recText.Length - 3); } if (recText.EndsWith($"...{Environment.NewLine}")) { recText = recText.Left(recText.Length - $"...{Environment.NewLine}".Length); } if (!Configuration.SingleDocument.Value) { sw.Write($"---{EOLDelimiter}"); } sw.Write("{0}", recText); if (!Configuration.SingleDocument.Value) { sw.Write($"...{EOLDelimiter}"); } if (!RaiseAfterRecordWrite(record, _index, recText)) { yield break; } } } //catch (ChoParserException) //{ // throw; //} catch (Exception ex) { ChoETLFramework.HandleException(ref ex); if (Configuration.ErrorMode == ChoErrorMode.IgnoreAndContinue) { recordIgnored = true; ChoETLFramework.WriteLog(TraceSwitch.TraceError, "Error [{0}] found. Ignoring record...".FormatString(ex.Message)); } else if (Configuration.ErrorMode == ChoErrorMode.ReportAndContinue) { if (!RaiseRecordWriteError(record, _index, recText, ex)) { throw; } else { recordIgnored = true; //ChoETLFramework.WriteLog(TraceSwitch.TraceError, "Error [{0}] found. Ignoring record...".FormatString(ex.Message)); } } else { throw; } } } yield return(record); if (Configuration.NotifyAfter > 0 && _index % Configuration.NotifyAfter == 0) { if (RaisedRowsWritten(_index)) { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Abort requested."); yield break; } } } } finally { System.Threading.Thread.CurrentThread.CurrentCulture = prevCultureInfo; } }
public virtual void Validate() { string propName = null; object propValue = null; string propValueText = null; var results = new List <ValidationResult>(); foreach (PropertyInfo pi in ChoType.GetProperties(GetType())) { propName = pi.Name; results.Clear(); try { propValue = ChoType.GetPropertyValue(this, pi); } catch (Exception ex) { throw new ChoHL7Exception("Failed to validate '{0}' member.".FormatString(pi.FullName()), ex); } if (propValue is Array) { Array arr = (Array)propValue; if (arr.Length == 0 && ChoTypeDescriptor.GetPropetyAttributes <RequiredAttribute>(ChoTypeDescriptor.GetProperty <RequiredAttribute>(GetType(), pi.Name)).Any()) { throw new ChoHL7Exception("'{0}' member is required.".FormatString(pi.FullName())); } var maxCountAttr = ChoTypeDescriptor.GetPropetyAttributes <ChoHL7MaxCountAttribute>(ChoTypeDescriptor.GetProperty <ChoHL7MaxCountAttribute>(GetType(), pi.Name)).FirstOrDefault(); if (maxCountAttr != null) { if (arr.Length > maxCountAttr.Count) { throw new ChoHL7Exception("Incorrect number of elements found in '{0}' member. [Expected: {1}, Found: {2}]".FormatString(pi.FullName(), maxCountAttr.Count, arr.Length)); } } foreach (ChoHL7AbstractField arrValue in arr) { try { propValueText = arrValue.ToNString(); } catch (Exception ex) { throw new ChoHL7Exception("Failed to validate '{0}' member.".FormatString(pi.FullName()), ex); } pi.CheckRequired(propValueText); if (Configuration != null && !Configuration.DisableEnumChecks) { pi.CheckForValidEnumValue(propValueText); } var context = new ValidationContext(propValueText, null, null); context.MemberName = pi.Name; Validator.TryValidateValue(propValueText, context, results, ChoTypeDescriptor.GetPropetyAttributes <ValidationAttribute>(ChoTypeDescriptor.GetProperty <ValidationAttribute>(GetType(), pi.Name))); if (results.Count > 0) { throw new ChoHL7Exception("Failed to validate '{0}' member. {2}{1}".FormatString(pi.FullName(), ChoHL7Helper.ToString(results), Environment.NewLine)); } try { if (arrValue is ChoHL7AbstractField) { ((ChoHL7AbstractField)arrValue).Validate(); } } catch (Exception ex) { throw new ChoHL7Exception("Failed to validate '{0}' member.".FormatString(pi.FullName()), ex); } } } else { try { propValueText = propValue.ToNString(); } catch (Exception ex) { throw new ChoHL7Exception("Failed to validate '{0}' member.".FormatString(pi.FullName()), ex); } pi.CheckRequired(propValueText); if (Configuration != null && !Configuration.DisableEnumChecks) { pi.CheckForValidEnumValue(propValueText); } var context = new ValidationContext(propValueText, null, null); context.MemberName = pi.Name; Validator.TryValidateValue(propValueText, context, results, ChoTypeDescriptor.GetPropetyAttributes <ValidationAttribute>(ChoTypeDescriptor.GetProperty <ValidationAttribute>(GetType(), pi.Name))); if (results.Count > 0) { if (Configuration.DisableValueInErrorMessage) { throw new ChoHL7Exception("Failed to validate '{0}' member. {2}{1}".FormatString(pi.FullName(), ChoHL7Helper.ToString(results), Environment.NewLine)); } else { throw new ChoHL7Exception("Failed to validate '{0}' member [Value: {3}]. {2}{1}".FormatString(pi.FullName(), ChoHL7Helper.ToString(results), Environment.NewLine, propValueText)); } } try { if (propValue is ChoHL7AbstractField) { ((ChoHL7AbstractField)propValue).Validate(); } } catch (Exception ex) { throw new ChoHL7Exception("Failed to validate '{0}' member.".FormatString(pi.FullName()), ex); } } } }
private void MapToDictionaryInternal(IDictionary <string, object> dictionary, object source) { var isKVPAttrDefined = source.GetType().GetCustomAttribute <ChoKeyValueTypeAttribute>() != null; //check if object is KeyValuePair Type valueType = source.GetType(); if (valueType.IsGenericType) { Type baseType = valueType.GetGenericTypeDefinition(); if (baseType == typeof(KeyValuePair <,>)) { object kvpKey = valueType.GetProperty("Key").GetValue(source, null); object kvpValue = valueType.GetProperty("Value").GetValue(source, null); if (kvpValue is IEnumerable) { dictionary[kvpKey.ToNString()] = MapToDictionary(kvpValue as IEnumerable).ToArray(); } else if (kvpValue != null) { dictionary[kvpKey.ToNString()] = MapToDictionary(kvpValue); } } } if (isKVPAttrDefined) { var kP = source.GetType().GetProperties().Where(p => p.GetCustomAttribute <ChoKeyAttribute>() != null).FirstOrDefault(); var vP = source.GetType().GetProperties().Where(p => p.GetCustomAttribute <ChoValueAttribute>() != null).FirstOrDefault(); if (kP != null && vP != null) { object value = vP.GetValue(source); if (value is IEnumerable) { dictionary[kP.GetValue(source).ToNString()] = MapToDictionary(value as IEnumerable).ToArray(); } else if (value != null) { dictionary[kP.GetValue(source).ToNString()] = MapToDictionary(value); } return; } } if (typeof(IChoKeyValueType).IsAssignableFrom(source.GetType())) { IChoKeyValueType kvp = source as IChoKeyValueType; object value = kvp.Value; if (value.GetType().IsDynamicType()) { dictionary[kvp.Key.ToNString()] = value; } else if (value is IEnumerable && !(value is IDictionary)) { dictionary[kvp.Key.ToNString()] = MapToDictionary(value as IEnumerable).ToArray(); } else if (value != null) { dictionary[kvp.Key.ToNString()] = MapToDictionary(value); } return; } var properties = ChoType.GetProperties(source.GetType()); // source.GetType().GetProperties(); foreach (var p in properties) { var key = p.Name; var attr = p.GetCustomAttribute <JsonPropertyAttribute>(); if (attr != null && !attr.PropertyName.IsNullOrWhiteSpace()) { key = attr.PropertyName.NTrim(); } object value = p.GetValue(source, null); if (value == null) { if (attr != null && attr.NullValueHandling == NullValueHandling.Ignore) { } else { dictionary[key] = null; } continue; } valueType = value.GetType(); if (valueType.IsSimple()) { dictionary[key] = Marshal(value); } else if (value.GetType().IsDynamicType()) { dictionary[key] = value; } else if (value is IDictionary) { IDictionary dict = ((IDictionary)value); foreach (var key1 in dict.Keys) { var val = dict[key]; dictionary[key1.ToNString()] = Marshal(value); } dictionary[key] = dict; } else if (value is IEnumerable) { dictionary[key] = MapToDictionary((IEnumerable)value).ToArray(); } else { dictionary[key] = Marshal(value); } } }