SettingsService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using System.Security.Principal;
  2. using Microsoft.Extensions.Logging;
  3. using Quadarax.Application.QLiberace.Base.Dtos;
  4. using Quadarax.Application.QLiberace.Base.Entities;
  5. using Quadarax.Application.QLiberace.Base.Repositories;
  6. using Quadarax.Application.QLiberace.Common;
  7. using Quadarax.Application.QLiberace.Common.Attributes;
  8. using Quadarax.Application.QLiberace.Common.Settings;
  9. using Quadarax.Foundation.Core.Business;
  10. using Quadarax.Foundation.Core.Data;
  11. using Quadarax.Foundation.Core.Data.Interface;
  12. using Quadarax.Foundation.Core.Data.Interface.Entity.Dto;
  13. using Quadarax.Foundation.Core.Reflection;
  14. namespace Quadarax.Application.QLiberace.Base.Services
  15. {
  16. public class SettingsService : AbstractRepositoryService<RepoSetting>
  17. {
  18. #region *** Constructor ***
  19. public SettingsService(RepoSetting repository, IPrincipal currentPrincipal, ILoggerFactory logger) : base(repository, currentPrincipal, logger)
  20. {
  21. }
  22. #endregion
  23. #region *** Public Operations ***
  24. public ResultDto CreateSettings<TSettings>(ModuleSetting settings) where TSettings : ModuleSetting
  25. {
  26. if (settings == null)
  27. throw new ArgumentNullException(nameof(settings));
  28. return TryCatchBlock(() =>
  29. {
  30. var attrs = AttributeQuery<ModuleAttribute>.FindAll(typeof(TSettings), true);
  31. if (attrs.Length == 0)
  32. throw new InvalidOperationException(
  33. $"ModuleAttribute is not defined on class '{typeof(TSettings).Name}'.");
  34. if (attrs.Length > 1)
  35. throw new InvalidOperationException(
  36. "ModuleAttribute is defined on more than one ModuleSetting class.");
  37. var settingsFlat = settings.GetData();
  38. foreach (var flat in settingsFlat)
  39. {
  40. var setting = Repository.New();
  41. setting.Code = flat.Item1;
  42. setting.ModuleCode = attrs[0].ModuleCode;
  43. setting.IsEnabled = true;
  44. setting.Description = string.Empty;
  45. setting.ValueTypeQualified = flat.Item2;
  46. setting.Value = flat.Item3?.ToString();
  47. Repository.Set(setting);
  48. }
  49. return new ResultPlain();
  50. });
  51. }
  52. public ResultValueDto<TSettings> GetSettings<TSettings>(string moduleCode) where TSettings : ModuleSetting, new()
  53. {
  54. if (string.IsNullOrEmpty(moduleCode)) throw new ArgumentNullException(nameof(moduleCode));
  55. return TryCatchBlock(() =>
  56. {
  57. if (!Repository.QueryModule(moduleCode, Paging.NoPaging()).Any())
  58. throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.SettingsNotFoundForModuleCode, moduleCode);
  59. var settingDto = default(TSettings);
  60. if (settingDto == null) throw new ArgumentNullException(nameof(settingDto));
  61. var settings = Repository.QueryModule(moduleCode,Paging.NoPaging());
  62. settingDto.SetData(settings.Select(x=>new Tuple<string, string, object?>(x.Code,x.ValueTypeQualified,x.GetTypedValue())));
  63. return new ResultValueDto<TSettings>(settingDto);
  64. });
  65. }
  66. public ResultValueDto GetSettingsValue<TValue>(string moduleCode, string valueCode)
  67. {
  68. if (string.IsNullOrEmpty(moduleCode)) throw new ArgumentNullException(nameof(moduleCode));
  69. if (string.IsNullOrEmpty(valueCode)) throw new ArgumentNullException(nameof(valueCode));
  70. return TryCatchBlock(() =>
  71. {
  72. var settings = GetSettings(moduleCode, valueCode);
  73. if (settings == null)
  74. throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.SettingsNotFoundCode, valueCode, moduleCode);
  75. return new ResultValueDto(settings.GetTypedValue<TValue>());
  76. });
  77. }
  78. public ResultBoolDto ExistsSettings(string moduleCode)
  79. {
  80. if (string.IsNullOrEmpty(moduleCode)) throw new ArgumentNullException(nameof(moduleCode));
  81. return TryCatchBlock(() =>
  82. {
  83. var result = Repository.QueryModule(moduleCode, Paging.NoPaging()).Any();
  84. return new ResultBoolDto(result);
  85. });
  86. }
  87. public ResultBoolDto ExistsSettingsValue(string moduleCode, string valueCode)
  88. {
  89. if (string.IsNullOrEmpty(moduleCode)) throw new ArgumentNullException(nameof(moduleCode));
  90. if (string.IsNullOrEmpty(valueCode)) throw new ArgumentNullException(nameof(valueCode));
  91. return TryCatchBlock(() =>
  92. {
  93. var result = Repository.QueryModule(moduleCode, Paging.NoPaging()).Any(x => x.Code == valueCode);
  94. return new ResultBoolDto(result);
  95. });
  96. }
  97. public ResultDto AddSettingsValue(string moduleCode, string valueCode, string valueDescription, object value)
  98. {
  99. if (string.IsNullOrEmpty(moduleCode)) throw new ArgumentNullException(nameof(moduleCode));
  100. if (string.IsNullOrEmpty(valueCode)) throw new ArgumentNullException(nameof(valueCode));
  101. return TryCatchBlock(() =>
  102. {
  103. var exists = ExistsSettingsValue(moduleCode, valueCode);
  104. if (!exists.IsSuccess) return (ResultDto)exists;
  105. if (exists.Value)
  106. throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.SettingsAlreadyExistsCode, valueCode, moduleCode);
  107. var settings = Repository.New();
  108. settings.ModuleCode = moduleCode;
  109. settings.Code = valueCode;
  110. settings.Description = valueDescription;
  111. settings.Value = value?.ToString();
  112. settings.IsEnabled = true;
  113. //Repository.Commit();
  114. return new ResultPlain();
  115. });
  116. }
  117. public ResultDto UpdateSettings<TSettings>(ModuleSetting settings) where TSettings : ModuleSetting
  118. {
  119. if (settings == null)
  120. throw new ArgumentNullException(nameof(settings));
  121. return TryCatchBlock(() =>
  122. {
  123. var attrs = AttributeQuery<ModuleAttribute>.FindAll(typeof(TSettings), true);
  124. if (attrs.Length == 0)
  125. throw new InvalidOperationException(
  126. $"ModuleAttribute is not defined on class '{typeof(TSettings).Name}'.");
  127. if (attrs.Length > 1)
  128. throw new InvalidOperationException(
  129. "ModuleAttribute is defined on more than one ModuleSetting class.");
  130. var settingsFlat = settings.GetData();
  131. foreach (var flat in settingsFlat)
  132. {
  133. var setting = GetSettings(attrs[0].ModuleCode, flat.Item1);
  134. if (setting == null)
  135. throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.SettingsNotFoundCode, flat.Item1, attrs[0].ModuleCode);
  136. Repository.Set(setting);
  137. }
  138. //Repository.Commit();
  139. return new ResultPlain();
  140. });
  141. }
  142. public ResultDto UpdateSettingsValue(string moduleCode, string valueCode, object value)
  143. {
  144. if (string.IsNullOrEmpty(moduleCode)) throw new ArgumentNullException(nameof(moduleCode));
  145. if (string.IsNullOrEmpty(valueCode)) throw new ArgumentNullException(nameof(valueCode));
  146. return TryCatchBlock(() =>
  147. {
  148. var exists = ExistsSettingsValue(moduleCode, valueCode);
  149. if (!exists.IsSuccess) return (ResultDto)exists;
  150. if (!exists.Value)
  151. throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.SettingsNotFoundCode, valueCode, moduleCode);
  152. var settings = Repository.Get(moduleCode, valueCode);
  153. if (settings==null)
  154. throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.SettingsNotFoundCode, valueCode, moduleCode);
  155. settings.Value = value?.ToString();
  156. //Repository.Commit();
  157. return new ResultPlain();
  158. });
  159. }
  160. public ResultDto AddOrUpdateSettingsValue(string moduleCode, string valueCode, object value)
  161. {
  162. return AddOrUpdateSettingsValue(moduleCode, valueCode, value, string.Empty);
  163. }
  164. public ResultDto AddOrUpdateSettingsValue(string moduleCode, string valueCode, object value, string valueDescription)
  165. {
  166. if (string.IsNullOrEmpty(moduleCode)) throw new ArgumentNullException(nameof(moduleCode));
  167. if (string.IsNullOrEmpty(valueCode)) throw new ArgumentNullException(nameof(valueCode));
  168. return TryCatchBlock(() =>
  169. {
  170. var exists = ExistsSettingsValue(moduleCode, valueCode);
  171. if (!exists.IsSuccess) return (ResultDto)exists;
  172. if (exists.Value)
  173. {
  174. return UpdateSettingsValue(moduleCode, valueCode, value);
  175. }
  176. else
  177. {
  178. return AddSettingsValue(moduleCode, valueCode, valueDescription, value);
  179. }
  180. });
  181. }
  182. public ResultSettingsValuesListDto GetSettingsValues(string moduleCode, IPaging paging)
  183. {
  184. if (string.IsNullOrEmpty(moduleCode)) throw new ArgumentNullException(nameof(moduleCode));
  185. return TryCatchBlock(() =>
  186. {
  187. var result = Repository.QueryModule(moduleCode, paging)
  188. .ToDictionary(x => x.Code, y => y.GetTypedValue<object>());
  189. return new ResultSettingsValuesListDto(result);
  190. });
  191. }
  192. #endregion
  193. #region ** Private Operations ***
  194. private Setting? GetSettings(string moduleCode, string valueCode)
  195. {
  196. var settings = Repository.Get(moduleCode, valueCode);
  197. return settings;
  198. }
  199. #endregion
  200. }
  201. }