ConfigurationService.cs 9.1 KB

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