public static void Initialize(this object target) { if (target == null) { return; } object defaultValue = null; foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties <DefaultValueAttribute>(target.GetType())) { try { defaultValue = ChoTypeDescriptor.GetPropetyAttribute <DefaultValueAttribute>(pd).Value; if (defaultValue != null) { ChoType.ConvertNSetMemberValue(target, pd.Name, defaultValue); } } catch (Exception ex) { ChoETLFramework.WriteLog(ChoETLFramework.TraceSwitch.TraceError, "Error while assigning default value '{0}' to '{1}' member. {2}".FormatString(defaultValue, ChoType.GetMemberName(pd), ex.Message)); } } ChoETLFramework.InitializeObject(target); if (target is IChoInitializable) { ((IChoInitializable)target).Initialize(); } }
public virtual void SetParams(string cmdParams) { foreach (Tuple <string, string> tuple in ChoStringEx.ToKeyValuePairs(cmdParams, ';', '=')) { MemberInfo memberInfo = ChoType.GetMemberInfo(this.GetType(), tuple.Item1); if (memberInfo != (MemberInfo)null && ChoType.GetAttribute <BrowsableAttribute>(memberInfo, false) == null) { ChoType.ConvertNSetMemberValue((object)this, tuple.Item1, (object)DenormalizeString(tuple.Item2)); } } }
public static void ConvertNSetMemberValue(this object rec, string fn, ChoRecordFieldConfiguration fieldConfig, ref object fieldValue, CultureInfo culture) { if (fieldConfig.Converters.IsNullOrEmpty()) { ChoType.ConvertNSetMemberValue(rec, fn, fieldValue, culture); fieldValue = ChoType.GetMemberValue(rec, fn); } else { fieldValue = ChoConvert.ConvertFrom(fieldValue, ChoType.GetMemberType(rec.GetType(), fn), null, fieldConfig.Converters.ToArray(), null, culture); ChoType.SetMemberValue(rec, fn, fieldValue); } }
private void LoadFixedLengthRecordFieldOptions(ChoIniFile iniFile1) { ChoIniFile iniFile = iniFile1.GetSection("FIXED_LENGTH_RECORD_FIELD"); var dict = new OrderedDictionary(); ChoFixedLengthRecordFieldAttribute value; Dictionary <string, string> iniKeyValuesDict = iniFile.KeyValuesDict; if (iniKeyValuesDict.Count == 0) { throw new ApplicationException("Missing fixed length field names in the '{0}' ini file.".FormatString(iniFile1.FilePath)); } foreach (string fieldName in iniKeyValuesDict.Keys) //GetFieldNames(iniFile1)) { if (fieldName.IsNullOrWhiteSpace()) { continue; } if (!iniKeyValuesDict.ContainsKey(fieldName)) { continue; } try { value = new ChoFixedLengthRecordFieldAttribute(); value.FieldValueTrimOption = ChoFieldValueTrimOption.Trim; value.FieldValueJustification = ChoFieldValueJustification.Left; foreach (KeyValuePair <string, string> kvp in iniKeyValuesDict[fieldName].ToKeyValuePairs()) { try { ChoType.ConvertNSetMemberValue(value, kvp.Key, kvp.Value); } catch (Exception ex) { ChoETLFramework.WriteLog(ChoETLFramework.Switch.TraceError, "Error while loading record field option '{0}' for '{1}' field. {2}".FormatString(iniKeyValuesDict[fieldName], fieldName, ex.Message)); } } value.Validate(); dict.Add(fieldName, value); } catch (Exception ex) { throw new ApplicationException("[FieldName: {0}] - {1}".FormatString(fieldName, ex.Message)); } } _flRecordFieldOptionCache.Add(iniFile1.Key, dict); }
private void LoadDbRecordFieldOptions(ChoIniFile iniFile1) { ChoIniFile iniFile = iniFile1.GetSection("Db_RECORD_FIELD"); var dict = new Dictionary <string, ChoDbRecordFieldAttribute>(); ChoDbRecordFieldAttribute value; Dictionary <string, string> iniKeyValuesDict = iniFile.KeyValuesDict; foreach (string fieldName in iniKeyValuesDict.Keys) { if (fieldName.IsNullOrWhiteSpace()) { continue; } if (!iniKeyValuesDict.ContainsKey(fieldName)) { continue; } value = new ChoDbRecordFieldAttribute(); foreach (KeyValuePair <string, string> kvp in iniKeyValuesDict[fieldName].ToKeyValuePairs()) { try { ChoType.ConvertNSetMemberValue(value, kvp.Key, kvp.Value); } catch (Exception ex) { ChoETLFramework.WriteLog(ChoETLFramework.Switch.TraceError, "Error while loading record field '{0}' for '{1}' field. {2}".FormatString(iniKeyValuesDict[fieldName], fieldName, ex.Message)); } } value.Validate(); dict.Add(fieldName, value); } _dbRecordFieldOptionCache.Add(iniFile1.Key, dict); }
private void Initialize() { try { if (!Monitor.TryEnter(_padLock, 1 * 1000)) { return; } IDictionary <string, object> kvpDict = null; if (_func != null) { kvpDict = _func(); } else { kvpDict = Seed(); } if (kvpDict == null) { return; } IDictionary <string, object> mkvpDict = _kvpDict; bool hasDiff = mkvpDict == null || kvpDict.Except(mkvpDict).Concat(mkvpDict.Except(kvpDict)).Any(); if (!hasDiff) { return; } _kvpDict = kvpDict; ChoPropertyAttribute attr = null; object memberValue = null; string propName = null; //scan through members and load them foreach (var prop in ChoType.GetMembers(GetType()).Where(m => m.GetCustomAttribute <ChoIgnoreMemberAttribute>() != null && !ChoType.IsReadOnlyMember(m))) { attr = ChoType.GetMemberAttribute <ChoPropertyAttribute>(prop); try { SetDefaultValue(prop, true); propName = attr != null && !attr.Name.IsNullOrWhiteSpace() ? attr.Name : prop.Name; if (kvpDict.ContainsKey(propName)) { memberValue = AfterKVPLoaded(prop.Name, kvpDict[propName]); } else { memberValue = AfterKVPLoaded(prop.Name, null); } if (memberValue != null && memberValue is string) { string mv = memberValue as string; if (attr != null) { switch (attr.TrimOption) { case ChoPropertyValueTrimOption.Trim: mv = mv.Trim(); break; case ChoPropertyValueTrimOption.TrimEnd: mv = mv.TrimEnd(); break; case ChoPropertyValueTrimOption.TrimStart: mv = mv.TrimStart(); break; } } memberValue = mv; } if (ChoType.GetMemberType(prop) == typeof(string)) { if (!((string)memberValue).IsNullOrEmpty()) { ChoType.ConvertNSetMemberValue(this, prop, memberValue); } } else { if (memberValue != null) { ChoType.ConvertNSetMemberValue(this, prop, memberValue); } } ChoValidator.ValidateFor(this, prop); } catch (Exception ex) { //ChoLog.Error("{0}: Error loading '{1}' property. {2}".FormatString(NName, prop.Name, ex.Message)); SetDefaultValue(prop, false); } } } catch (Exception outerEx) { //ChoLog.Error("{0}: Error loading options. {1}".FormatString(NName, outerEx.Message)); } finally { Monitor.Exit(_padLock); } }
private bool FillRecord(object rec, Tuple <int, string> pair) { int lineNo; string line; lineNo = pair.Item1; line = pair.Item2; object fieldValue = null; string[] fieldValues = (from x in line.Split(Configuration.Delimiter, Configuration.StringSplitOptions, Configuration.QuoteChar) select x).ToArray(); if (Configuration.ColumnCountStrict) { if (fieldValues.Length != Configuration.RecordFieldConfigurations.Count) { throw new ChoParserException("Incorrect number of field values found at line [{2}]. Expected [{0}] field values. Found [{1}] field values.".FormatString(Configuration.RecordFieldConfigurations.Count, fieldValues.Length, pair.Item1)); } } Dictionary <string, string> _fieldNameValues = ToFieldNameValues(fieldValues); ValidateLine(pair.Item1, fieldValues); ChoCSVRecordFieldConfiguration fieldConfig = null; foreach (KeyValuePair <string, ChoCSVRecordFieldConfiguration> kvp in Configuration.RecordFieldConfigurationsDict) { fieldConfig = kvp.Value; if (Configuration.CSVFileHeaderConfiguration.HasHeaderRecord) { if (_fieldNameValues.ContainsKey(fieldConfig.FieldName)) { fieldValue = _fieldNameValues[fieldConfig.FieldName]; } else if (Configuration.ColumnCountStrict) { throw new ChoParserException("No matching '{0}' field header found.".FormatString(fieldConfig.FieldName)); } } else { if (fieldConfig.FieldPosition - 1 < fieldValues.Length) { fieldValue = fieldValues[fieldConfig.FieldPosition - 1]; } else if (Configuration.ColumnCountStrict) { throw new ChoParserException("Missing field value for {0} [Position: {1}] field.".FormatString(fieldConfig.FieldName, fieldConfig.FieldPosition)); } } fieldValue = CleanFieldValue(fieldConfig, fieldValue as string); if (!RaiseBeforeRecordFieldLoad(rec, pair.Item1, kvp.Key, ref fieldValue)) { continue; } try { bool ignoreFieldValue = fieldConfig.IgnoreFieldValue(fieldValue); if (!ignoreFieldValue) { if (rec is ExpandoObject) { if (fieldConfig.FieldType != typeof(string)) { fieldValue = ChoConvert.ConvertTo(fieldValue, fieldConfig.FieldType, Configuration.Culture); } var x = rec as IDictionary <string, Object>; x.Add(kvp.Key, fieldValue); } else { if (ChoType.HasProperty(rec.GetType(), kvp.Key)) { ChoType.ConvertNSetMemberValue(rec, kvp.Key, fieldValue); fieldValue = ChoType.GetMemberValue(rec, kvp.Key); if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.MemberLevel) == ChoObjectValidationMode.MemberLevel) { ChoValidator.ValididateFor(rec, kvp.Key); } } else { throw new ChoMissingRecordFieldException("Missing '{0}' property in {1} type.".FormatString(kvp.Key, ChoType.GetTypeName(rec))); } } } if (!RaiseAfterRecordFieldLoad(rec, pair.Item1, kvp.Key, fieldValue)) { return(false); } } catch (ChoParserException) { throw; } catch (ChoMissingRecordFieldException) { if (Configuration.ThrowAndStopOnMissingField) { throw; } } catch (Exception ex) { ChoETLFramework.HandleException(ex); if (fieldConfig.ErrorMode == ChoErrorMode.ThrowAndStop) { throw; } try { ChoFallbackValueAttribute fbAttr = ChoTypeDescriptor.GetPropetyAttribute <ChoFallbackValueAttribute>(rec.GetType(), kvp.Key); if (fbAttr != null) { if (!fbAttr.Value.IsNullOrDbNull()) { ChoType.ConvertNSetMemberValue(rec, kvp.Key, fbAttr.Value); } } else { throw; } } catch { if (fieldConfig.ErrorMode == ChoErrorMode.IgnoreAndContinue) { continue; } else if (fieldConfig.ErrorMode == ChoErrorMode.ReportAndContinue) { if (!RaiseRecordFieldLoadError(rec, pair.Item1, kvp.Key, fieldValue, ex)) { throw; } } else { throw; } } } } return(true); }