ConfigurationService.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Web.Configuration;
  8. using GreenTree.Nachtragsmanagement.Core.Configuration;
  9. using GreenTree.Nachtragsmanagement.Core.Domain.Config;
  10. using GreenTree.Nachtragsmanagement.Core.Data;
  11. namespace GreenTree.Nachtragsmanagement.Services.Configuration
  12. {
  13. public class ConfigurationService : IConfigurationService
  14. {
  15. #region Fields
  16. private readonly IRepository<ConfigItem> _configItemRepository;
  17. private readonly IRepository<UserConfigItem> _userConfigItemRepository;
  18. private readonly Dictionary<string, ConfigItem> _configCache = new Dictionary<string, ConfigItem>();
  19. #endregion
  20. #region Ctor
  21. /// <summary>
  22. /// Initializes a new instance of the ConfigurationService class
  23. /// </summary>
  24. public ConfigurationService(
  25. IRepository<ConfigItem> configItemRepository,
  26. IRepository<UserConfigItem> userConfigItemRepository)
  27. {
  28. _configItemRepository = configItemRepository;
  29. _userConfigItemRepository = userConfigItemRepository;
  30. }
  31. #endregion
  32. /// <summary>
  33. /// Reads the current configuration from the global config
  34. /// </summary>
  35. public AppendixConfigurationSection GetCurrentConfiguration()
  36. {
  37. var config = WebConfigurationManager.OpenWebConfiguration("~/");
  38. var sectionGroup = config.GetSectionGroup("appendixSectionGroup");
  39. var section = sectionGroup.Sections["appendixConfigSection"];
  40. return (AppendixConfigurationSection)section;
  41. }
  42. #region ConfigItem
  43. /// <summary>
  44. /// Gets all configItems
  45. /// </summary>
  46. public IList<ConfigItem> GetAllConfigItems()
  47. {
  48. return _configItemRepository.Table.ToList();
  49. }
  50. /// <summary>
  51. /// Gets a configItem by specified Id
  52. /// </summary>
  53. /// <param name="id">ConfigItem identifier.</param>
  54. public ConfigItem GetConfigItemById(int id)
  55. {
  56. return _configItemRepository.GetById(id);
  57. }
  58. /// <summary>
  59. /// Gets a configItem by specified name
  60. /// </summary>
  61. /// <param name="name">ConfigItem name.</param>
  62. public ConfigItem GetConfigItemByName(string name)
  63. {
  64. return _configItemRepository.Table
  65. .FirstOrDefault(r => r.Name == name);
  66. }
  67. /// <summary>
  68. /// Gets all configItems to the specified ids
  69. /// </summary>
  70. public IList<ConfigItem> GetConfigItemsByIds(int[] ids)
  71. {
  72. return _configItemRepository.Table
  73. .Where(r => ids.Contains(r.Id))
  74. .ToList();
  75. }
  76. /// <summary>
  77. /// Insert a configItem
  78. /// </summary>
  79. /// <param name="configItem">ConfigItem.</param>
  80. public void InsertConfigItem(ConfigItem configItem)
  81. {
  82. _configItemRepository.Insert(configItem);
  83. if (_configCache.ContainsKey(configItem.Name))
  84. _configCache[configItem.Name] = configItem;
  85. }
  86. /// <summary>
  87. /// Update a configItem
  88. /// </summary>
  89. /// <param name="configItem">ConfigItem.</param>
  90. public void UpdateConfigItem(ConfigItem configItem)
  91. {
  92. _configItemRepository.Update(configItem);
  93. if (_configCache.ContainsKey(configItem.Name))
  94. _configCache[configItem.Name] = configItem;
  95. }
  96. /// <summary>
  97. /// Delete a configItem
  98. /// </summary>
  99. /// <param name="configItem">ConfigItem.</param>
  100. public void DeleteConfigItem(ConfigItem configItem)
  101. {
  102. _configItemRepository.Delete(configItem);
  103. if (_configCache.ContainsKey(configItem.Name))
  104. _configCache.Remove(configItem.Name);
  105. }
  106. #endregion
  107. #region UserConfigItem
  108. /// <summary>
  109. /// Gets all userConfigItems
  110. /// </summary>
  111. public IList<UserConfigItem> GetAllUserConfigItems()
  112. {
  113. return _userConfigItemRepository.Table.ToList();
  114. }
  115. /// <summary>
  116. /// Gets all userConfigItems of corresponding user
  117. /// </summary>
  118. /// <param name="userId">User id.</param>
  119. public IList<UserConfigItem> GetUserConfigItemsByUserId(int userId)
  120. {
  121. return _userConfigItemRepository.Table
  122. .Where(u => u.UserId == userId)
  123. .ToList();
  124. }
  125. /// <summary>
  126. /// Gets a userConfigItem by specified Id
  127. /// </summary>
  128. /// <param name="id">UserConfigItem identifier.</param>
  129. public UserConfigItem GetUserConfigItemById(int id)
  130. {
  131. return _userConfigItemRepository.GetById(id);
  132. }
  133. /// <summary>
  134. /// Gets a userConfigItem by specified name and user id
  135. /// </summary>
  136. /// <param name="name">UserConfigItem name.</param>
  137. /// <param name="userId">User Id.</param>
  138. public UserConfigItem GetUserConfigItemByNameAndUserId(string name, int userId)
  139. {
  140. return _userConfigItemRepository.Table
  141. .FirstOrDefault(u => u.UserId == userId && u.Name == name);
  142. }
  143. /// <summary>
  144. /// Insert or update a userConfigItem
  145. /// </summary>
  146. /// <param name="userConfigItem">UserConfigItem.</param>
  147. public void InsertOrUpdateUserConfigItem(UserConfigItem userConfigItem)
  148. {
  149. if (userConfigItem == null)
  150. return;
  151. var existingConfig = GetUserConfigItemByNameAndUserId(userConfigItem.Name, userConfigItem.UserId);
  152. if (existingConfig != null)
  153. {
  154. existingConfig.Value = userConfigItem.Value;
  155. _userConfigItemRepository.Update(existingConfig);
  156. }
  157. else
  158. _userConfigItemRepository.Insert(userConfigItem);
  159. }
  160. /// <summary>
  161. /// Insert or update a userConfigItem
  162. /// </summary>
  163. /// <param name="name">UserConfigItem name.</param>
  164. /// <param name="userId">User Id.</param>
  165. /// <param name="value">New value.</param>
  166. public void InsertOrUpdateUserConfigItem(string name, int userId, string value)
  167. {
  168. var existingConfig = GetUserConfigItemByNameAndUserId(name, userId);
  169. if (existingConfig != null)
  170. existingConfig.Value = value;
  171. if (existingConfig != null)
  172. {
  173. existingConfig.Value = value;
  174. _userConfigItemRepository.Update(existingConfig);
  175. }
  176. else
  177. _userConfigItemRepository.Insert(new UserConfigItem
  178. {
  179. Name = name,
  180. UserId = userId,
  181. Value = value
  182. });
  183. }
  184. /// <summary>
  185. /// Delete a userConfigItem
  186. /// </summary>
  187. /// <param name="userConfigItem">UserConfigItem.</param>
  188. public void DeleteUserConfigItem(UserConfigItem userConfigItem)
  189. {
  190. _userConfigItemRepository.Delete(userConfigItem);
  191. }
  192. #endregion
  193. #region Casting
  194. /// <summary>
  195. /// Trys to convert the config items value type and returns its actual value, otherwise NULL
  196. /// </summary>
  197. /// <typeparam name="T">The actual type.</typeparam>
  198. /// <param name="configItem">ConfigItem.</param>
  199. /// <param name="defaultValue">Value, when conversion fails.</param>
  200. public T TryGetConfigItemValue<T>(ConfigItem configItem, T defaultValue)
  201. {
  202. if (configItem == null)
  203. return defaultValue;
  204. object result = null;
  205. if (!_configCache.ContainsKey(configItem.Name))
  206. _configCache.Add(configItem.Name, configItem);
  207. try
  208. {
  209. if (configItem.TypeFullName.StartsWith("ConfigurationReference"))
  210. {
  211. var configurationReference = ConfigurationHelper.GetConfigurationReferenceByName(configItem.TypeFullName);
  212. if (configurationReference != null)
  213. {
  214. if (configurationReference.IsMultipleSelection)
  215. {
  216. var vals = configurationReference.TransformValueToValueCollection(configItem.Value);
  217. result = configurationReference.GetValues<T>(vals);
  218. }
  219. else
  220. result = configurationReference.GetValue<T>(configItem.Value);
  221. }
  222. }
  223. else
  224. {
  225. result = Convert.ChangeType(configItem.Value, typeof(T));
  226. }
  227. }
  228. catch
  229. {
  230. result = defaultValue;
  231. }
  232. return (T)result;
  233. }
  234. /// <summary>
  235. /// Trys to convert the config items value type and returns its actual value, otherwise NULL
  236. /// </summary>
  237. /// <typeparam name="T">The actual type.</typeparam>
  238. /// <param name="configItemName">ConfigItem name.</param>
  239. /// <param name="defaultValue">Value, when conversion fails.</param>
  240. public T TryGetConfigItemValue<T>(string configItemName, T defaultValue)
  241. {
  242. if (String.IsNullOrEmpty(configItemName))
  243. return defaultValue;
  244. if (_configCache.ContainsKey(configItemName))
  245. return TryGetConfigItemValue(_configCache[configItemName], defaultValue);
  246. var configItem = GetConfigItemByName(configItemName);
  247. return TryGetConfigItemValue(configItem, defaultValue);
  248. }
  249. #endregion
  250. }
  251. }