示例#1
0
        internal static void FillFullCodes(this List <SettingNamespace> childs, SettingNamespace parent)
        {
            foreach (var child in childs)
            {
                child.FullCode = parent != null
                    ? $"{parent.FullCode}.{child.Code}"
                    : child.Code;

                if (!child.Namespaces.IsNullOrEmpty())
                {
                    FillFullCodes(child.Namespaces, child);
                }
                ;

                if (!child.Settings.IsNullOrEmpty())
                {
                    foreach (var setting in child.Settings)
                    {
                        setting.FullCode = child.FullCode + "." + setting.Code;
                        if (string.IsNullOrEmpty(setting.Value))
                        {
                            setting.Value = setting.DefaultValue;
                        }
                    }
                }
            }
        }
示例#2
0
        public SettingNamespace FindNamespace(List <SettingNamespace> namespaces, string namespaceCode)
        {
            string[]                codes        = namespaceCode.Split('.');
            SettingNamespace        result       = null;
            List <SettingNamespace> searchSpaces = namespaces;

            foreach (string code in codes)
            {
                if (searchSpaces == null)
                {
                    return(null);
                }

                result = searchSpaces.SingleOrDefault(nSpace => nSpace.Code.EqualsOrdinalIgnoreCase(code));

                if (result == null)
                {
                    return(null);
                }

                searchSpaces = result.Namespaces;
            }

            return(result);
        }
        internal static NamespaceModel MapToNamespaceModel(this SettingNamespace settingNamespace, string namespacesPath, GrViewModelBase viewModel)
        {
            var result = new NamespaceModel
            {
                NamespacePath = namespacesPath,
                Namespace     = settingNamespace,
                Settings      = new ObservableCollection <SettingModel>(),
            };

            viewModel.BindModel(result);
            return(result);
        }
示例#4
0
        public List <SettingNamespace> GetSettingNamespaces(IEnumerable <string> namespaceCodes)
        {
            Dictionary <string, bool> codes = namespaceCodes
                                              .Distinct()
                                              .ToDictionary(code => code, code => false);

            var result = new SettingNamespace[codes.Count];

            foreach (var pair in GetFacade <SettingContextFacade>().LoadSettingContext())
            {
                int            counter = -1;
                SettingContext context = pair.Value;

                foreach (string code in codes.Keys.ToList())
                {
                    counter++;
                    if (codes[code])
                    {
                        continue;
                    }

                    var nSpace = FindNamespace(context.Namespaces, code);

                    if (nSpace != null)
                    {
                        result[counter] = nSpace;
                        codes[code]     = true;
                    }
                }

                if (codes.All(p => p.Value))
                {
                    break;
                }
            }

            var notFoundCodes = codes.Where(p => !p.Value).Select(p => p.Key).ToList();

            if (!notFoundCodes.IsNullOrEmpty())
            {
                throw CdiHelper.CreateException(
                          notFoundCodes
                          .Select(code => new ErrorDetail
                {
                    Code   = SettingsErrorCode.SettingNamespaceNotFound,
                    Reason = $"Namespace '{code}' not found."
                })
                          .ToList());
            }

            return(result.ToList());
        }
示例#5
0
        private List <NamespaceModel> GetNamespaceModels(SettingNamespace settingNamespace, string namespacesPath, IReadOnlyCollection <SettingType> types, List <SettingValueError> settingValueErrors, GrViewModelBase viewModel)
        {
            var resultSpace = settingNamespace.MapToNamespaceModel(namespacesPath, viewModel);
            var result      = new List <NamespaceModel>
            {
                resultSpace,
            };

            if (!settingNamespace.Namespaces.IsNullOrEmpty())
            {
                foreach (var nSpase in settingNamespace.Namespaces)
                {
                    result.AddRange(GetNamespaceModels(nSpase, namespacesPath + settingNamespace.Name + " > ", types, settingValueErrors, viewModel));
                }
            }

            if (!settingNamespace.Settings.IsNullOrEmpty())
            {
                resultSpace.Visibility = Visibility.Visible;

                foreach (var setting in settingNamespace.Settings)
                {
                    var type  = types.First(t => t.Code.EqualsOrdinalIgnoreCase(setting.Type));
                    var model = new SettingModel
                    {
                        Setting     = setting,
                        _value      = setting.Value,
                        SettingType = type,
                        ValueError  = settingValueErrors?.FirstOrDefault(pattern => pattern.SettingFullCode.EqualsOrdinalIgnoreCase(setting.FullCode)),
                    };
                    viewModel.BindModel(model);
                    resultSpace.Settings.Add(model);
                }
            }
            else
            {
                resultSpace.Visibility = Visibility.Collapsed;
            }

            return(result);
        }
        public void SetSettings(List <SetSettingsRequest> requests)
        {
            List <Setting>     settingFormFile = GetSettins(requests.ConvertAll(s => s.FullCode));
            var                defaultTypes    = GetFacade <SettingContextFacade>().GetDefaultSettingTypes();
            List <SettingType> allTypes        = null;

            foreach (var item in settingFormFile)
            {
                var request = requests.First(s => s.FullCode.EqualsOrdinalIgnoreCase(item.FullCode));

                if (request.NeedSetDefaultValue)
                {
                    item.Value = item.DefaultValue;
                }
                else
                {
                    item.Value = request.Value;
                }

                SettingType type = defaultTypes.SingleOrDefault(t => t.Code.EqualsOrdinalIgnoreCase(item.Type));

                if (type == null)
                {
                    if (allTypes == null)
                    {
                        allTypes = GetFacade <SettingContextFacade>().GetSettingTypes();
                    }
                    type = allTypes.SingleType(item.Type);
                }

                if (request.CheckSettingType && !request.SettingType.EqualsOrdinalIgnoreCase(item.Type))
                {
                    throw CdiHelper.CreateException(SettingsErrorCode.InvalidSettingType, $"The setting contains an unexpected type. Expected <{request.SettingType}>, Actual <{item.Type}>.");
                }

                ValidateSetting(item, type);
            }

            var lockerFacade = GetFacade <LockerFacade>();

            foreach (var pair in GetFacade <SettingContextFacade>().LoadSettingContext(true))
            {
                var context     = pair.Value;
                var namespaces  = new List <SettingNamespace>();
                var setSettings = new List <Setting>();

                foreach (var item in settingFormFile)
                {
                    string[] codes         = item.FullCode.Split('.');
                    string   namespaceCode = string.Join(".", codes.Take(codes.Length - 1));

                    SettingNamespace nSpace = namespaces.FirstOrDefault(n => n.FullCode.EqualsOrdinalIgnoreCase(namespaceCode));

                    if (nSpace == null)
                    {
                        nSpace = GetFacade <SettingNamespaceFacade>().FindNamespace(context.Namespaces, namespaceCode);

                        if (nSpace != null)
                        {
                            nSpace.FullCode = namespaceCode;
                            namespaces.Add(nSpace);
                        }
                    }

                    if (nSpace != null)
                    {
                        nSpace.Settings.First(s => s.Code.EqualsOrdinalIgnoreCase(item.Code)).Value = item.Value;
                        setSettings.Add(item);
                        continue;
                    }
                }

                if (setSettings.Count != 0)
                {
                    lock (lockerFacade.GetLocker(pair.Key))
                    {
                        try
                        {
                            using (var fileStrem = GetAdapter <FileAdapter>().Open(pair.Key, FileMode.Create))
                            {
                                using (var streamWriter = new StreamWriter(fileStrem, Encoding.UTF8))
                                    context.SerializeToTextWriter(streamWriter);
                            }
                        }
                        catch
                        {
                            lockerFacade.Unblock(pair.Key);
                            throw;
                        }
                    }

                    foreach (var item in setSettings)
                    {
                        settingFormFile.Remove(item);
                    }
                }

                lockerFacade.Unblock(pair.Key);

                if (settingFormFile.Count == 0)
                {
                    break;
                }
            }
        }