Ejemplo n.º 1
0
        private void LoadFieldExprs(ChoIniFile iniFile1)
        {
            ChoIniFile iniFile = iniFile1.GetSection("EXPRESSION_FIELD");

            var    dict  = new Dictionary <string, string>();
            string value = null;
            Dictionary <string, string> iniKeyValuesDict = iniFile.KeyValuesDict;

            foreach (string fieldName in iniKeyValuesDict.Keys)
            {
                if (fieldName.IsNullOrWhiteSpace())
                {
                    continue;
                }
                if (!iniKeyValuesDict.ContainsKey(fieldName))
                {
                    continue;
                }

                try
                {
                    value = iniKeyValuesDict[fieldName].ExpandProperties();
                    dict.Add(fieldName, value);
                }
                catch (Exception ex)
                {
                    ChoETLFramework.WriteLog(ChoETLFramework.Switch.TraceError, "Error while loading field expression value '{0}' for '{1}' member. {2}".FormatString(value, fieldName, ex.Message));
                }
            }
            _fieldExprCache.Add(iniFile1.Key, dict);
        }
Ejemplo n.º 2
0
        public static void SetFixedLengthRecordField(this ChoIniFile @this, string fieldName, string value)
        {
            ChoGuard.ArgumentNotNullOrEmpty(fieldName, "FieldName");
            fieldName = fieldName.Trim();

            @this.OpenFixedLengthRecordFieldSection().SetValue(fieldName, value);
        }
Ejemplo n.º 3
0
        private void LoadDataTypes(ChoIniFile iniFile1)
        {
            ChoIniFile iniFile = iniFile1.GetSection("DATATYPE");

            var  dict  = new Dictionary <string, Type>();
            Type value = null;
            Dictionary <string, string> iniKeyValuesDict = iniFile.KeyValuesDict;

            foreach (string fieldName in iniKeyValuesDict.Keys) //GetFieldNames(iniFile1))
            {
                if (fieldName.IsNullOrWhiteSpace())
                {
                    continue;
                }
                if (!iniKeyValuesDict.ContainsKey(fieldName))
                {
                    continue;
                }

                try
                {
                    value = Type.GetType(iniKeyValuesDict[fieldName]);
                    if (value != null)
                    {
                        dict.Add(fieldName, value);
                    }
                }
                catch (Exception ex)
                {
                    ChoETLFramework.WriteLog(ChoETLFramework.Switch.TraceError, "Error while loading format value '{0}' for '{1}' member. {2}".FormatString(value, fieldName, ex.Message));
                }
            }
            _dataTypeCache.Add(iniFile1.Key, dict);
        }
Ejemplo n.º 4
0
        private static Dictionary <string, ChoRandomGenerator> GetRandomGenerators(ChoIniFile iniFile)
        {
            if (_randomGeneratorsCache.ContainsKey(iniFile.Key))
            {
                return(_randomGeneratorsCache[iniFile.Key]);
            }

            lock (_rgLock)
            {
                if (_randomGeneratorsCache.ContainsKey(iniFile.Key))
                {
                    return(_randomGeneratorsCache[iniFile.Key]);
                }

                Dictionary <string, ChoRandomGenerator> dict = new Dictionary <string, ChoRandomGenerator>();
                ChoIniFile                  randomIniFile    = iniFile.GetSection("RANDOM");
                ChoRandomGenerator          ra = null;
                Dictionary <string, string> iniKeyValuesDict = randomIniFile.KeyValuesDict;
                foreach (string fieldName in iniKeyValuesDict.Keys)
                {
                    ra = GetRandomObject(fieldName, randomIniFile.GetValue <string>(fieldName));
                    dict.Add(fieldName, ra);
                }

                _randomGeneratorsCache.Add(iniFile.Key, dict);
                return(_randomGeneratorsCache[iniFile.Key]);
            }
        }
Ejemplo n.º 5
0
        private void LoadDefaults(ChoIniFile iniFile1)
        {
            ChoIniFile iniFile = iniFile1.GetSection("DEFAULT_VALUE");

            var    dict         = new Dictionary <string, string>();
            string defaultValue = null;
            Dictionary <string, string> iniKeyValuesDict = iniFile.KeyValuesDict;

            foreach (string fieldName in iniKeyValuesDict.Keys)
            {
                if (fieldName.IsNullOrWhiteSpace())
                {
                    continue;
                }
                if (!iniKeyValuesDict.ContainsKey(fieldName))
                {
                    continue;
                }

                try
                {
                    defaultValue = iniKeyValuesDict[fieldName].ExpandProperties();
                    dict.Add(fieldName, defaultValue);
                }
                catch (Exception ex)
                {
                    ChoETLFramework.WriteLog(ChoETLFramework.Switch.TraceError, "Error while converting default value '{0}' for '{1}' member. {2}".FormatString(defaultValue, fieldName, ex.Message));
                }
            }
            _defaultsCache.Add(iniFile1.Key, dict);
        }
Ejemplo n.º 6
0
        private void LoadSequences(ChoIniFile iniFile1)
        {
            ChoIniFile iniFile = iniFile1.GetSection("SEQUENCE_VALUE_GENERATOR");

            var                         dict             = new Dictionary <string, Tuple <IValueConverter, string> >();
            IValueConverter             value            = null;
            Dictionary <string, string> iniKeyValuesDict = iniFile.KeyValuesDict;

            foreach (string fieldName in iniKeyValuesDict.Keys)
            {
                if (fieldName.IsNullOrWhiteSpace())
                {
                    continue;
                }
                if (!iniKeyValuesDict.ContainsKey(fieldName))
                {
                    continue;
                }
                if (dict.ContainsKey(fieldName))
                {
                    continue;
                }

                if (!iniKeyValuesDict[fieldName].IsNullOrWhiteSpace())
                {
                    string val = iniKeyValuesDict[fieldName];

                    string vcType  = null;
                    string vcParam = null;
                    foreach (KeyValuePair <string, string> convParams in val.ToKeyValuePairs())
                    {
                        if (convParams.Key == "Type")
                        {
                            vcType = convParams.Value;
                        }
                        else if (convParams.Key == "Parameter")
                        {
                            vcParam = convParams.Value.Replace('&', ';');
                        }
                    }

                    if (!vcType.IsNullOrWhiteSpace())
                    {
                        try
                        {
                            value = Activator.CreateInstance(ChoType.GetType(vcType)) as IValueConverter;
                            if (value != null)
                            {
                                dict.Add(fieldName, new Tuple <IValueConverter, string>(value, vcParam));
                            }
                        }
                        catch (Exception ex)
                        {
                            ChoETLFramework.WriteLog(ChoETLFramework.Switch.TraceError, "Error while loading format value '{0}' for '{1}' member. {2}".FormatString(value, fieldName, ex.Message));
                        }
                    }
                }
            }
            _seqGeneratorCache.Add(iniFile1.Key, dict);
        }
Ejemplo n.º 7
0
        public Type GetDynamicMemberType(ChoIniFile iniFile, string memberName)
        {
            Type fieldType = typeof(string);

            if (iniFile != null && ChoDynamicObjectMemberMetaDataCache.Default.GetDataTypes(iniFile).ContainsKey(memberName))
            {
                fieldType = ChoDynamicObjectMemberMetaDataCache.Default.GetDataTypes(iniFile)[memberName];
            }

            return(Nullable.GetUnderlyingType(fieldType) ?? fieldType);
        }
Ejemplo n.º 8
0
        public object ConvertBackValue(ChoIniFile iniFile, string fieldName, object fieldValue, Type fieldType = null)
        {
            if (fieldType == null)
            {
                if (iniFile != null && ChoDynamicObjectMemberMetaDataCache.Default.GetDataTypes(iniFile).ContainsKey(fieldName))
                {
                    fieldType = ChoDynamicObjectMemberMetaDataCache.Default.GetDataTypes(iniFile)[fieldName];
                }

                if (fieldType == null)
                {
                    if (fieldValue == null)
                    {
                        return(null);
                    }

                    fieldType = fieldValue.GetType();
                }
            }

            if (fieldType == null)
            {
                return(fieldValue);
            }

            //Look for converters and convert the value
            if (iniFile != null && ChoDynamicObjectMemberMetaDataCache.Default.GetValueConverters(iniFile).ContainsKey(fieldName))
            {
                foreach (Tuple <IValueConverter, string> vcPair in ChoDynamicObjectMemberMetaDataCache.Default.GetValueConverters(iniFile)[fieldName].AsEnumerable().Reverse())
                {
                    if (!vcPair.Item1.IsNull())
                    {
                        fieldValue = vcPair.Item1.ConvertBack(fieldValue, fieldType, vcPair.Item2, null);
                    }
                }
            }

            if (!fieldValue.IsNullOrDbNull())
            {
                fieldValue = Convert.ChangeType(fieldValue, fieldType);
            }

            if (iniFile != null && ChoDynamicObjectMemberMetaDataCache.Default.GetFormatters(iniFile).ContainsKey(fieldName))
            {
                string format = ChoDynamicObjectMemberMetaDataCache.Default.GetFormatters(iniFile)[fieldName];
                if (!format.IsNullOrWhiteSpace())
                {
                    fieldValue = String.Format("{0:" + format + "}", fieldValue);
                }
            }

            return(fieldValue);
        }
Ejemplo n.º 9
0
        private void LoadFallbacks(ChoIniFile iniFile1)
        {
            ChoIniFile iniFile = iniFile1.GetSection("FALLBACK_VALUE");

            var    dict          = new Dictionary <string, object>();
            object fallbackValue = null;
            Dictionary <string, string> iniKeyValuesDict = iniFile.KeyValuesDict;
            Type fieldType = null;

            foreach (string fieldName in iniKeyValuesDict.Keys)
            {
                if (fieldName.IsNullOrWhiteSpace())
                {
                    continue;
                }
                if (!iniKeyValuesDict.ContainsKey(fieldName))
                {
                    continue;
                }

                try
                {
                    fallbackValue = iniKeyValuesDict[fieldName].ExpandProperties();
                    fieldType     = typeof(string);
                    if (ChoDynamicObjectMemberMetaDataCache.Default.GetDataTypes(iniFile1).ContainsKey(fieldName))
                    {
                        fieldType = ChoDynamicObjectMemberMetaDataCache.Default.GetDataTypes(iniFile1)[fieldName];
                    }

                    //Look for converters and convert the value
                    if (ChoDynamicObjectMemberMetaDataCache.Default.GetValueConverters(iniFile1).ContainsKey(fieldName))
                    {
                        foreach (Tuple <IValueConverter, string> vcPair in ChoDynamicObjectMemberMetaDataCache.Default.GetValueConverters(iniFile1)[fieldName])
                        {
                            if (!vcPair.Item1.IsNull())
                            {
                                fallbackValue = vcPair.Item1.Convert(fallbackValue, fieldType, vcPair.Item2, null);
                            }
                        }
                    }

                    fallbackValue = Convert.ChangeType(fallbackValue, fieldType);

                    dict.Add(fieldName, fallbackValue);
                }
                catch (Exception ex)
                {
                    ChoETLFramework.WriteLog(ChoETLFramework.Switch.TraceError, "Error while converting fallback value '{0}' for '{1}' member. {2}".FormatString(fallbackValue, fieldName, ex.Message));
                }
            }
            _fallbackCache.Add(iniFile1.Key, dict);
        }
Ejemplo n.º 10
0
        //public IEnumerable<string> GetFieldNames(ChoIniFile iniFile1)
        //{
        //    ChoGuard.ArgumentNotNull(iniFile1, "iniFile");

        //    ChoIniFile iniFile = iniFile1.OpenRecordParamsSection();
        //    string fieldNames = iniFile1.GetFieldNames();

        //    if (fieldNames.IsNullOrWhiteSpace())
        //        throw new ApplicationException("Missing field names in the '{0}' ini file.".FormatString(iniFile.FilePath));

        //    return fieldNames.SplitNTrim();
        //}

        public Dictionary <string, string> GetDefaultValues(ChoIniFile iniFile)
        {
            ChoGuard.ArgumentNotNull(iniFile, "iniFile");

            InitDefaults(iniFile);

            if (!_defaultsCache.ContainsKey(iniFile.Key))
            {
                return(new Dictionary <string, string>());
            }

            return(_defaultsCache[iniFile.Key]);
        }
Ejemplo n.º 11
0
        public OrderedDictionary GetFixedLengthRecordFieldOptions(ChoIniFile iniFile)
        {
            ChoGuard.ArgumentNotNull(iniFile, "iniFile");

            InitFixedLengthRecordFieldOptions(iniFile);

            if (!_flRecordFieldOptionCache.ContainsKey(iniFile.Key))
            {
                return(new OrderedDictionary());
            }

            return(_flRecordFieldOptionCache[iniFile.Key]);
        }
Ejemplo n.º 12
0
        public Dictionary <string, Type> GetDataTypes(ChoIniFile iniFile)
        {
            ChoGuard.ArgumentNotNull(iniFile, "iniFile");

            InitDataTypes(iniFile);

            if (!_dataTypeCache.ContainsKey(iniFile.Key))
            {
                return(new Dictionary <string, Type>());
            }

            return(_dataTypeCache[iniFile.Key]);
        }
Ejemplo n.º 13
0
        public Dictionary <string, string> GetFieldExprs(ChoIniFile iniFile)
        {
            ChoGuard.ArgumentNotNull(iniFile, "iniFile");

            InitFieldExprs(iniFile);

            if (!_fieldExprCache.ContainsKey(iniFile.Key))
            {
                return(new Dictionary <string, string>());
            }

            return(_fieldExprCache[iniFile.Key]);
        }
Ejemplo n.º 14
0
        public static ChoIniFile OpenIniSection(string sectionName = null)
        {
            CheckInitCalled();

            if (sectionName == null)
            {
                return(IniFile);
            }
            else
            {
                return(ChoIniFile.New(IniFile.FilePath, sectionName));
            }
        }
Ejemplo n.º 15
0
        public Dictionary <string, object> GetFallbackValues(ChoIniFile iniFile)
        {
            ChoGuard.ArgumentNotNull(iniFile, "iniFile");

            InitFallbacks(iniFile);

            if (!_fallbackCache.ContainsKey(iniFile.Key))
            {
                return(new Dictionary <string, object>());
            }

            return(_fallbackCache[iniFile.Key]);
        }
Ejemplo n.º 16
0
        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);
        }
Ejemplo n.º 17
0
        private void InitValueConverters(ChoIniFile iniFile)
        {
            if (_valueConverterCache.ContainsKey(iniFile.Key))
            {
                return;
            }

            lock (_padlock)
            {
                if (_valueConverterCache.ContainsKey(iniFile.Key))
                {
                    return;
                }

                LoadValueConverters(iniFile);
            }
        }
Ejemplo n.º 18
0
        private void InitSequences(ChoIniFile iniFile)
        {
            if (_seqGeneratorCache.ContainsKey(iniFile.Key))
            {
                return;
            }

            lock (_padlock)
            {
                if (_seqGeneratorCache.ContainsKey(iniFile.Key))
                {
                    return;
                }

                LoadSequences(iniFile);
            }
        }
Ejemplo n.º 19
0
        private void InitFormats(ChoIniFile iniFile)
        {
            if (_formatsCache.ContainsKey(iniFile.Key))
            {
                return;
            }

            lock (_padlock)
            {
                if (_formatsCache.ContainsKey(iniFile.Key))
                {
                    return;
                }

                LoadFormats(iniFile);
            }
        }
Ejemplo n.º 20
0
        private void InitDbRecordFieldOptions(ChoIniFile iniFile)
        {
            if (_dbRecordFieldOptionCache.ContainsKey(iniFile.Key))
            {
                return;
            }

            lock (_padlock)
            {
                if (_dbRecordFieldOptionCache.ContainsKey(iniFile.Key))
                {
                    return;
                }

                LoadDbRecordFieldOptions(iniFile);
            }
        }
Ejemplo n.º 21
0
        private void InitDataTypes(ChoIniFile iniFile)
        {
            if (_dataTypeCache.ContainsKey(iniFile.Key))
            {
                return;
            }

            lock (_padlock)
            {
                if (_dataTypeCache.ContainsKey(iniFile.Key))
                {
                    return;
                }

                LoadDataTypes(iniFile);
            }
        }
Ejemplo n.º 22
0
        private void InitFixedLengthRecordFieldOptions(ChoIniFile iniFile)
        {
            if (_flRecordFieldOptionCache.ContainsKey(iniFile.Key))
            {
                return;
            }

            lock (_padlock)
            {
                if (_flRecordFieldOptionCache.ContainsKey(iniFile.Key))
                {
                    return;
                }

                LoadFixedLengthRecordFieldOptions(iniFile);
            }
        }
Ejemplo n.º 23
0
        private void InitFieldExprs(ChoIniFile iniFile)
        {
            if (_fieldExprCache.ContainsKey(iniFile.Key))
            {
                return;
            }

            lock (_padlock)
            {
                if (_fieldExprCache.ContainsKey(iniFile.Key))
                {
                    return;
                }

                LoadFieldExprs(iniFile);
            }
        }
Ejemplo n.º 24
0
        private void InitDefaults(ChoIniFile iniFile)
        {
            if (_defaultsCache.ContainsKey(iniFile.Key))
            {
                return;
            }

            lock (_padlock)
            {
                if (_defaultsCache.ContainsKey(iniFile.Key))
                {
                    return;
                }

                LoadDefaults(iniFile);
            }
        }
Ejemplo n.º 25
0
        private void InitFallbacks(ChoIniFile iniFile)
        {
            if (_fallbackCache.ContainsKey(iniFile.Key))
            {
                return;
            }

            lock (_padlock)
            {
                if (_fallbackCache.ContainsKey(iniFile.Key))
                {
                    return;
                }

                LoadFallbacks(iniFile);
            }
        }
Ejemplo n.º 26
0
        public static object CreateMoqInstance(Type objType, ChoIniFile iniFile = null)
        {
            //if (typeof(ChoRecord).IsAssignableFrom(objType))
            //    return CreateDynamicMoqInstance(objType, iniFile);

            object obj = ChoActivator.CreateInstanceAndInit(objType);

            foreach (KeyValuePair <MemberInfo, Attribute> kv in ChoUtility.DiscoverMembers(objType, typeof(ChoRandomAttribute)))
            {
                if (!(kv.Value is ChoRandomAttribute))
                {
                    continue;
                }

                ChoType.SetMemberValue(obj, kv.Key, ((ChoRandomAttribute)kv.Value).NextValue());
            }

            return(obj);
        }
        private void InitIsLockedCache(Type declaringType)
        {
            if (!_turnOnMetaDataCache)
            {
                return;
            }

            ChoIniFile iniFile = ChoIniFile.New(declaringType.FullName);

            if (!_isLockedCache.ContainsKey(declaringType))
            {
                lock (_padlock)
                {
                    if (!_isLockedCache.ContainsKey(declaringType))
                    {
                        _isLockedCache.Add(declaringType, iniFile.GetValue <bool>("IsLocked"));
                    }
                }
            }
        }
Ejemplo n.º 28
0
        private void Save()
        {
            lock (_padLock)
            {
                try
                {
                    if (ChoIniFile.New(this.IniFilePath).GetValue <bool>("IsLocked", false))
                    {
                        return;
                    }

                    Directory.CreateDirectory(Path.GetDirectoryName(_iniFilePath));

                    string iniContents = String.Empty;
                    if (File.Exists(_iniFilePath))
                    {
                        iniContents = File.ReadAllText(_iniFilePath);
                    }

                    if (_sectionName.IsNullOrWhiteSpace())
                    {
                        iniContents = Regex.Replace(iniContents, @"^(?<ini>[^\[]*)", Matcher, RegexOptions.IgnoreCase);
                        File.WriteAllText(_iniFilePath, iniContents);
                    }
                    else
                    {
                        if (!Regex.IsMatch(iniContents, @"(?<ini>\[{0}\][^\[]*)".FormatString(_sectionName)))
                        {
                            iniContents += "{0}[{1}]{0}".FormatString(Environment.NewLine, _sectionName);
                        }

                        iniContents = Regex.Replace(iniContents, @"(?<ini>\[{0}\][^\[]*)".FormatString(_sectionName), Matcher, RegexOptions.IgnoreCase);
                        File.WriteAllText(_iniFilePath, iniContents);
                    }
                }
                catch (Exception ex)
                {
                    ChoETLLog.Error(ex.ToString());
                }
            }
        }
        private void LoadConverterParams(Type declaringType)
        {
            ChoIniFile iniFile = GetIniSection(declaringType, "FORMATTER");

            var                dict            = new Dictionary <string, object[]>();
            string             parameters      = null;
            ChoMemberAttribute memberAttribute = null;

            foreach (MemberInfo memberInfo in ChoType.GetMembers(declaringType))
            {
                memberAttribute = ChoType.GetAttribute <ChoMemberAttribute>(memberInfo);
                if (memberAttribute == null)
                {
                    continue;
                }

                try
                {
                    if (_turnOnMetaDataCache)
                    {
                        parameters = iniFile.GetValue(memberInfo.Name);
                    }

                    List <object> p = new List <object>();
                    if (!parameters.IsNullOrWhiteSpace())
                    {
                        foreach (string kv in parameters.SplitNTrim(';'))
                        {
                            p.Add(kv.SplitNTrim(','));
                        }
                    }

                    dict.Add(memberInfo.Name, parameters.IsNullOrWhiteSpace() ? null : p.ToArray());
                }
                catch (Exception ex)
                {
                    ChoETLFramework.WriteLog(ChoETLFramework.Switch.TraceError, "Failed to retrieve converter params for '{0}' member type from INI file. {1}".FormatString(ChoType.GetMemberName(memberInfo), ex.Message));
                }
            }
            _converterParams.Add(declaringType, dict);
        }
Ejemplo n.º 30
0
        public static ChoIniFile New(string iniFilePath, string sectionName = null)
        {
            ChoGuard.ArgumentNotNullOrEmpty(iniFilePath, "IniFilePath");

            string key = IniFileKey(iniFilePath, sectionName);

            if (_iniFiles.ContainsKey(key))
            {
                return(_iniFiles[key]);
            }

            lock (_iniFileLockObjDictLock)
            {
                if (_iniFiles.ContainsKey(key))
                {
                    return(_iniFiles[key]);
                }

                ChoIniFile iniFile = new ChoIniFile(iniFilePath, sectionName);
                _iniFiles.Add(key, iniFile);
                return(_iniFiles[key]);
            }
        }