private void Validate() { if (!UiServices.Contains <IUiNotification>()) { throw CdiHelper.CreateException(CdoWpfErrorCode.NotificationNotInstalled, "Notification service not installed."); } }
/// <inheritdoc/> public virtual PowerMode GetPowerModeSetting(string settingFullCode) { Setting setting = GetSetting(settingFullCode); if (!setting.Type.EqualsOrdinalIgnoreCase(SettingType.PowerMode.Code)) { throw CdiHelper.CreateException(SettingsErrorCode.InvalidSettingValue, $"The '{settingFullCode}' setting value is not a PowerMode."); } return((PowerMode)Enum.Parse(typeof(PowerMode), setting.Value, true)); }
/// <inheritdoc/> public virtual int GetIntSetting(string settingFullCode) { Setting setting = GetSetting(settingFullCode); if (!setting.Type.EqualsOrdinalIgnoreCase(SettingType.Int.Code)) { throw CdiHelper.CreateException(SettingsErrorCode.InvalidSettingValue, $"The '{settingFullCode}' setting value is not a int."); } return(int.Parse(setting.Value)); }
private static string GetPathFolder() { var config = GrConfigManager.Current.Sections[GrConfigKeys.Settings.Name].Configs[GrConfigKeys.Settings.SettingsFolder]; if (config == null) { throw CdiHelper.CreateException(SettingsErrorCode.InvalidConfig, $"The GetcuReone.config does not contain config '{GrConfigKeys.Settings.SettingsFolder}'."); } return(config.Value); }
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()); }
internal static SettingType SingleType(this IEnumerable <SettingType> types, string settingTypeCode) { var type = types.SingleOrDefault(t => t.Code.EqualsOrdinalIgnoreCase(settingTypeCode)); if (type == null) { throw CdiHelper.CreateException( SettingsErrorCode.SettingTypeNotFaound, $"Not found type setting with '{type.Code}' code."); } return(type); }
public IEnumerable <KeyValuePair <string, SettingContext> > LoadSettingContext(bool blockFiles = false) { var templateFileConfig = GrConfigManager.Current.Sections[GrConfigKeys.Settings.Name].Configs[GrConfigKeys.Settings.TemplateSettingFile]; if (templateFileConfig == null) { throw CdiHelper.CreateException(SettingsErrorCode.InvalidConfig, $"The GetcuReone.config does not contain config '{GrConfigKeys.Settings.TemplateSettingFile}'."); } var lockerFacade = GetFacade <LockerFacade>(); var fileAdapter = GetAdapter <FileAdapter>(); var files = GetAdapter <SettingsFolderAdapter>().GetFiles(templateFileConfig.Value); foreach (var file in files) { SettingContext context = null; lockerFacade.WaitUnblock(file); if (blockFiles) { lockerFacade.Block(file); } lock (lockerFacade.GetLocker(file)) { try { using (var fileStream = fileAdapter.OpenRead(file)) context = fileStream.DeserializeFromXml <SettingContext>(); } catch { lockerFacade.Unblock(file); throw; } } context.Namespaces.FillFullCodes(null); yield return(new KeyValuePair <string, SettingContext>(file, context)); } }
/// <summary> /// Check if the setting matches the type. /// </summary> /// <param name="setting"></param> /// <param name="settingType"></param> public void ValidateSetting(Setting setting, SettingType settingType) { if (!setting.Type.EqualsOrdinalIgnoreCase(settingType.Code)) { throw CdiHelper.CreateException( SettingsErrorCode.InvalidSettingType, $"Invalid setting type. Expected <{settingType.Code}>, actual <{setting.Type}>."); } switch (settingType.FormatValue) { case FormatValue.Enumerate: var expectedValue = settingType .PermissibleValues .FirstOrDefault(value => value.EqualsOrdinalIgnoreCase(setting.Value)); if (expectedValue == null) { throw CdiHelper.CreateException( SettingsErrorCode.InvalidSettingType, $"The setting contains an invalid value. Value <{setting.Value}>."); } setting.Value = expectedValue; break; case FormatValue.Text: if (!Regex.IsMatch(setting.Value, settingType.TextPattern)) { throw CdiHelper.CreateException( SettingsErrorCode.InvalidSettingValue, $"The setting value does not match the pattern. Pattern <{settingType.TextPattern}>, value <{setting.Value}>."); } break; default: throw new InvalidEnumArgumentException($"Unknown value <{settingType.FormatValue}>."); } }
public List <Setting> GetSettins(IEnumerable <string> settingFullCodes) { var data = new List <(string settingCode, string namespaceCode, string settingFullCode)>(); var result = new List <Setting>(); foreach (string settingFullCode in settingFullCodes) { string[] codes = settingFullCode.Split('.'); string settingCode = codes.Last(); string namespaceCode = string.Join(".", codes.Take(codes.Length - 1)); (string settingCode, string namespaceCode, string settingFullCode)item = (settingCode, namespaceCode, settingFullCode); data.Add(item); } List <SettingNamespace> namespaces = GetFacade <SettingNamespaceFacade>().GetSettingNamespaces(data.Select(d => d.namespaceCode)); var notFoundSettings = new List <string>(); var defaultTypes = GetFacade <SettingContextFacade>().GetDefaultSettingTypes(); IReadOnlyCollection <SettingType> allTypes = null; foreach (var item in data) { Setting setting = namespaces .First(nSpace => nSpace.FullCode.EqualsOrdinalIgnoreCase(item.namespaceCode)) .Settings .SingleOrDefault(s => s.Code.EqualsOrdinalIgnoreCase(item.settingCode)); if (setting == null) { notFoundSettings.Add(item.settingFullCode); continue; } else if (string.IsNullOrEmpty(setting.Value)) { setting.Value = setting.DefaultValue; } SettingType type = defaultTypes.SingleOrDefault(t => t.Code.EqualsOrdinalIgnoreCase(setting.Type)); if (type == null) { if (allTypes == null) { allTypes = GetFacade <SettingContextFacade>().GetSettingTypes(); } type = allTypes.SingleType(setting.Type); } ValidateSetting(setting, type); result.Add(setting); } if (!notFoundSettings.IsNullOrEmpty()) { throw CdiHelper.CreateException( notFoundSettings .Select(code => new ErrorDetail { Code = SettingsErrorCode.SettingNotFound, Reason = $"Setting '{code}' not found.", }) .ToList()); } 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; } } }