using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web.Configuration; using GreenTree.Nachtragsmanagement.Core.Configuration; using GreenTree.Nachtragsmanagement.Core.Domain.Config; using GreenTree.Nachtragsmanagement.Core.Data; namespace GreenTree.Nachtragsmanagement.Services.Configuration { public class ConfigurationService : IConfigurationService { #region Fields private readonly IRepository _configItemRepository; private readonly IRepository _userConfigItemRepository; private readonly Dictionary _configCache = new Dictionary(); #endregion #region Ctor /// /// Initializes a new instance of the ConfigurationService class /// public ConfigurationService( IRepository configItemRepository, IRepository userConfigItemRepository) { _configItemRepository = configItemRepository; _userConfigItemRepository = userConfigItemRepository; } #endregion /// /// Reads the current configuration from the global config /// public AppendixConfigurationSection GetCurrentConfiguration() { var config = WebConfigurationManager.OpenWebConfiguration("~/"); var sectionGroup = config.GetSectionGroup("appendixSectionGroup"); var section = sectionGroup.Sections["appendixConfigSection"]; return (AppendixConfigurationSection)section; } #region ConfigItem /// /// Gets all configItems /// public IList GetAllConfigItems() { return _configItemRepository.Table.ToList(); } /// /// Gets a configItem by specified Id /// /// ConfigItem identifier. public ConfigItem GetConfigItemById(int id) { return _configItemRepository.GetById(id); } /// /// Gets a configItem by specified name /// /// ConfigItem name. public ConfigItem GetConfigItemByName(string name) { return _configItemRepository.Table .FirstOrDefault(r => r.Name == name); } /// /// Gets all configItems to the specified ids /// public IList GetConfigItemsByIds(int[] ids) { return _configItemRepository.Table .Where(r => ids.Contains(r.Id)) .ToList(); } /// /// Insert a configItem /// /// ConfigItem. public void InsertConfigItem(ConfigItem configItem) { _configItemRepository.Insert(configItem); if (_configCache.ContainsKey(configItem.Name)) _configCache[configItem.Name] = configItem; } /// /// Update a configItem /// /// ConfigItem. public void UpdateConfigItem(ConfigItem configItem) { _configItemRepository.Update(configItem); if (_configCache.ContainsKey(configItem.Name)) _configCache[configItem.Name] = configItem; } /// /// Delete a configItem /// /// ConfigItem. public void DeleteConfigItem(ConfigItem configItem) { _configItemRepository.Delete(configItem); if (_configCache.ContainsKey(configItem.Name)) _configCache.Remove(configItem.Name); } #endregion #region UserConfigItem /// /// Gets all userConfigItems /// public IList GetAllUserConfigItems() { return _userConfigItemRepository.Table.ToList(); } /// /// Gets all userConfigItems of corresponding user /// /// User id. public IList GetUserConfigItemsByUserId(int userId) { return _userConfigItemRepository.Table .Where(u => u.UserId == userId) .ToList(); } /// /// Gets a userConfigItem by specified Id /// /// UserConfigItem identifier. public UserConfigItem GetUserConfigItemById(int id) { return _userConfigItemRepository.GetById(id); } /// /// Gets a userConfigItem by specified name and user id /// /// UserConfigItem name. /// User Id. public UserConfigItem GetUserConfigItemByNameAndUserId(string name, int userId) { return _userConfigItemRepository.Table .FirstOrDefault(u => u.UserId == userId && u.Name == name); } /// /// Insert or update a userConfigItem /// /// UserConfigItem. public void InsertOrUpdateUserConfigItem(UserConfigItem userConfigItem) { if (userConfigItem == null) return; var existingConfig = GetUserConfigItemByNameAndUserId(userConfigItem.Name, userConfigItem.UserId); if (existingConfig != null) { existingConfig.Value = userConfigItem.Value; _userConfigItemRepository.Update(existingConfig); } else _userConfigItemRepository.Insert(userConfigItem); } /// /// Insert or update a userConfigItem /// /// UserConfigItem name. /// User Id. /// New value. public void InsertOrUpdateUserConfigItem(string name, int userId, string value) { var existingConfig = GetUserConfigItemByNameAndUserId(name, userId); if (existingConfig != null) existingConfig.Value = value; if (existingConfig != null) { existingConfig.Value = value; _userConfigItemRepository.Update(existingConfig); } else _userConfigItemRepository.Insert(new UserConfigItem { Name = name, UserId = userId, Value = value }); } /// /// Delete a userConfigItem /// /// UserConfigItem. public void DeleteUserConfigItem(UserConfigItem userConfigItem) { _userConfigItemRepository.Delete(userConfigItem); } #endregion #region Casting /// /// Trys to convert the config items value type and returns its actual value, otherwise NULL /// /// The actual type. /// ConfigItem. /// Value, when conversion fails. public T TryGetConfigItemValue(ConfigItem configItem, T defaultValue) { if (configItem == null) return defaultValue; object result = null; if (!_configCache.ContainsKey(configItem.Name)) _configCache.Add(configItem.Name, configItem); try { if (configItem.TypeFullName.StartsWith("ConfigurationReference")) { var configurationReference = ConfigurationHelper.GetConfigurationReferenceByName(configItem.TypeFullName); if (configurationReference != null) { if (configurationReference.IsMultipleSelection) { var vals = configurationReference.TransformValueToValueCollection(configItem.Value); result = configurationReference.GetValues(vals); } else result = configurationReference.GetValue(configItem.Value); } } else { result = Convert.ChangeType(configItem.Value, typeof(T)); } } catch { result = defaultValue; } return (T)result; } /// /// Trys to convert the config items value type and returns its actual value, otherwise NULL /// /// The actual type. /// ConfigItem name. /// Value, when conversion fails. public T TryGetConfigItemValue(string configItemName, T defaultValue) { if (String.IsNullOrEmpty(configItemName)) return defaultValue; if (_configCache.ContainsKey(configItemName)) return TryGetConfigItemValue(_configCache[configItemName], defaultValue); var configItem = GetConfigItemByName(configItemName); return TryGetConfigItemValue(configItem, defaultValue); } #endregion } }