using Swift.DAL.Common; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Web; namespace Swift.web.Library { public class CustomConfigHelper { public CustomConfigHelper() { // // TODO: Add constructor logic here // } public static class PartnerAgentConfig { /// /// A list of known Configurations /// public static IEnumerable ConfigListSdn { get { foreach (var config in _configListSdn) { yield return config; } } } private static List _configListSdn = new List(); public static IEnumerable ConfigList { get { foreach (var config in _configList) { yield return config; } } } private static List _configList = new List(); /// /// Constructor. /// static PartnerAgentConfig() { try { System.Configuration.Configuration configuration = null; if (System.Web.HttpContext.Current != null) { configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~"); } else { configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); } MySectionGroup group = configuration.SectionGroups["CustomSection"] as MySectionGroup; foreach (ConfigurationSection section in group.Sections) { if (section.GetType() == typeof(SDNListSection)) { SDNListSection c = (SDNListSection)section; CustomConfigCollection coll = c.SDNListSetting; foreach (CustomConfigElement element in coll) { AddCustomItem(element, SectionCategory.SDNListSection); } } else if (section.GetType() == typeof(PartnerAgentSection)) { PartnerAgentSection c = (PartnerAgentSection)section; CustomConfigCollection coll = c.PartnerAgentConfig; foreach (CustomConfigElement element in coll) { AddCustomItem(element, SectionCategory.PartnerAgentSection); } } } } catch (Exception ex) { throw ex; } } private static void AddCustomItem(CustomConfigElement element, SectionCategory sectionCat) { var configObj = new ConfigDTO() { Key = element.Key, Name = element.Name, UserName = element.UserName, Password = element.Password, Field1 = element.Field1, Field2 = element.Field2, Field3 = element.Field3, Field4 = element.Field4, ToEmail = element.ToEmail, ToEmailCc = element.ToEmailCc, ToEmailBCc = element.ToEmailBCc, URL = element.URL, Active = element.Active }; AddConfig(configObj, sectionCat); } public static bool ParseBoolean(string val) { bool retVal; Boolean.TryParse(val, out retVal); return retVal; } public static void AddConfig(ConfigDTO config, SectionCategory section) { if (config == null || !config.IsValid) return; if (section== SectionCategory.PartnerAgentSection) { if (!_configList.Contains(config)) _configList.Add(config); } else if(section == SectionCategory.SDNListSection) { if (!_configListSdn.Contains(config)) _configListSdn.Add(config); } } /// /// Get single record by Key /// /// /// public static ConfigDTO GetParentConfig(string key, SectionCategory section = SectionCategory.PartnerAgentSection) { if (ConfigList.Any()) { var single = ConfigList.SingleOrDefault(x => x.Key == key); if (single != null) return single; else throw new ArgumentException("No Configurations found for: " + key); } else throw new ArgumentException("No Configurations found"); } public static List GetParentConfig(SectionCategory section = SectionCategory.SDNListSection) { if (section == SectionCategory.SDNListSection) { if (ConfigListSdn.Any()) { return ConfigListSdn.ToList(); } } else if (section == SectionCategory.PartnerAgentSection) { if (ConfigList.Any()) { return ConfigList.ToList(); } } else { throw new ArgumentException("No Configurations found for: " + section); } return new List(); } } public enum SectionCategory { PartnerAgentSection, SDNListSection } } #region Custom Config Helper #region MySection Group public class MySectionGroup : ConfigurationSectionGroup { [ConfigurationProperty("SDNListSetting", IsRequired = false)] public SDNListSection SDNListSetting { get { return (SDNListSection)base.Sections["SDNListSetting"]; } } [ConfigurationProperty("PartnerAgentSection", IsRequired = false)] public PartnerAgentSection PartnerAgentSettings { get { return (PartnerAgentSection)base.Sections["PartnerAgentSection"]; } } } #endregion #region ConfigurationSection public class PartnerAgentSection : ConfigurationSection { public const string SectionName = "PartnerAgentSection"; [ConfigurationProperty("PartnerAgentConfig")] public CustomConfigCollection PartnerAgentConfig { get { return ((CustomConfigCollection)(base["PartnerAgentConfig"])); } } } public class SDNListSection : ConfigurationSection { public const string SectionName = "SDNListSection"; [ConfigurationProperty("SDNListSetting")] public CustomConfigCollection SDNListSetting { get { return ((CustomConfigCollection)(base["SDNListSetting"])); } } } #endregion #region ConfigurationElementCollection [ConfigurationCollection(typeof(CustomConfigElement))] public class CustomConfigCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new CustomConfigElement(); } protected override object GetElementKey(ConfigurationElement element) { return ((CustomConfigElement)(element)).Key; } public CustomConfigElement this[int idx] { get { return (CustomConfigElement)BaseGet(idx); } } } #endregion #region ConfigurationElement public class CustomConfigElement : ConfigurationElement { [ConfigurationProperty("key", DefaultValue = "", IsKey = true, IsRequired = false)] public string Key { get { return (string)this["key"]; } set { this["key"] = value; } } [ConfigurationProperty("name", DefaultValue = "", IsKey = false, IsRequired = true)] public string Name { get { return (string)this["name"]; } set { this["name"] = value; } } [ConfigurationProperty("username", DefaultValue = "", IsKey = false, IsRequired = false)] public string UserName { get { return (string)this["username"]; } set { this["username"] = value; } } [ConfigurationProperty("password", DefaultValue = "", IsKey = false, IsRequired = false)] public string Password { get { return (string)this["password"]; } set { this["password"] = value; } } [ConfigurationProperty("field1", DefaultValue = "", IsKey = false, IsRequired = false)] public string Field1 { get { return (string)this["field1"]; } set { this["field1"] = value; } } [ConfigurationProperty("field2", DefaultValue = "", IsKey = false, IsRequired = false)] public string Field2 { get { return (string)this["field2"]; } set { this["field2"] = value; } } [ConfigurationProperty("field3", DefaultValue = "", IsKey = false, IsRequired = false)] public string Field3 { get { return (string)this["field3"]; } set { this["field3"] = value; } } [ConfigurationProperty("field4", DefaultValue = "", IsKey = false, IsRequired = false)] public string Field4 { get { return (string)this["field4"]; } set { this["field4"] = value; } } [ConfigurationProperty("toEmail", DefaultValue = "", IsKey = false, IsRequired = false)] public string ToEmail { get { return (string)this["toEmail"]; } set { this["toEmail"] = value; } } [ConfigurationProperty("toEmailCc", DefaultValue = "", IsKey = false, IsRequired = false)] public string ToEmailCc { get { return (string)this["toEmailCc"]; } set { this["toEmailCc"] = value; } } [ConfigurationProperty("toEmailBCc", DefaultValue = "", IsKey = false, IsRequired = false)] public string ToEmailBCc { get { return (string)this["toEmailBCc"]; } set { this["toEmailBCc"] = value; } } [ConfigurationProperty("url", DefaultValue = "", IsKey = false, IsRequired = false)] public string URL { get { return (string)this["url"]; } set { this["url"] = value; } } [ConfigurationProperty("active", DefaultValue = "", IsKey = false, IsRequired = false)] public string Active { get { return (string)this["active"]; } set { this["active"] = value; } } } #endregion #endregion }