You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

356 lines
12 KiB

  1. using Swift.DAL.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Linq;
  6. using System.Web;
  7. namespace Swift.web.Library
  8. {
  9. public class CustomConfigHelper
  10. {
  11. public CustomConfigHelper()
  12. {
  13. //
  14. // TODO: Add constructor logic here
  15. //
  16. }
  17. public static class PartnerAgentConfig
  18. {
  19. /// <summary>
  20. /// A list of known Configurations
  21. /// </summary>
  22. public static IEnumerable<ConfigDTO> ConfigListSdn { get { foreach (var config in _configListSdn) { yield return config; } } }
  23. private static List<ConfigDTO> _configListSdn = new List<ConfigDTO>();
  24. public static IEnumerable<ConfigDTO> ConfigList { get { foreach (var config in _configList) { yield return config; } } }
  25. private static List<ConfigDTO> _configList = new List<ConfigDTO>();
  26. /// <summary>
  27. /// Constructor.
  28. /// </summary>
  29. static PartnerAgentConfig()
  30. {
  31. try
  32. {
  33. System.Configuration.Configuration configuration = null;
  34. if (System.Web.HttpContext.Current != null)
  35. {
  36. configuration =
  37. System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
  38. }
  39. else
  40. {
  41. configuration =
  42. ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  43. }
  44. MySectionGroup group = configuration.SectionGroups["CustomSection"] as MySectionGroup;
  45. foreach (ConfigurationSection section in group.Sections)
  46. {
  47. if (section.GetType() == typeof(SDNListSection))
  48. {
  49. SDNListSection c = (SDNListSection)section;
  50. CustomConfigCollection coll = c.SDNListSetting;
  51. foreach (CustomConfigElement element in coll)
  52. {
  53. AddCustomItem(element, SectionCategory.SDNListSection);
  54. }
  55. }
  56. else if (section.GetType() == typeof(PartnerAgentSection))
  57. {
  58. PartnerAgentSection c = (PartnerAgentSection)section;
  59. CustomConfigCollection coll = c.PartnerAgentConfig;
  60. foreach (CustomConfigElement element in coll)
  61. {
  62. AddCustomItem(element, SectionCategory.PartnerAgentSection);
  63. }
  64. }
  65. }
  66. }
  67. catch (Exception ex)
  68. {
  69. throw ex;
  70. }
  71. }
  72. private static void AddCustomItem(CustomConfigElement element, SectionCategory sectionCat)
  73. {
  74. var configObj = new ConfigDTO()
  75. {
  76. Key = element.Key,
  77. Name = element.Name,
  78. UserName = element.UserName,
  79. Password = element.Password,
  80. Field1 = element.Field1,
  81. Field2 = element.Field2,
  82. Field3 = element.Field3,
  83. Field4 = element.Field4,
  84. ToEmail = element.ToEmail,
  85. ToEmailCc = element.ToEmailCc,
  86. ToEmailBCc = element.ToEmailBCc,
  87. URL = element.URL,
  88. Active = element.Active
  89. };
  90. AddConfig(configObj, sectionCat);
  91. }
  92. public static bool ParseBoolean(string val)
  93. {
  94. bool retVal;
  95. Boolean.TryParse(val, out retVal);
  96. return retVal;
  97. }
  98. public static void AddConfig(ConfigDTO config, SectionCategory section)
  99. {
  100. if (config == null || !config.IsValid)
  101. return;
  102. if (section== SectionCategory.PartnerAgentSection)
  103. {
  104. if (!_configList.Contains(config))
  105. _configList.Add(config);
  106. }
  107. else if(section == SectionCategory.SDNListSection)
  108. {
  109. if (!_configListSdn.Contains(config))
  110. _configListSdn.Add(config);
  111. }
  112. }
  113. /// <summary>
  114. /// Get single record by Key
  115. /// </summary>
  116. /// <param name="key"></param>
  117. /// <returns></returns>
  118. public static ConfigDTO GetParentConfig(string key, SectionCategory section = SectionCategory.PartnerAgentSection)
  119. {
  120. if (ConfigList.Any())
  121. {
  122. var single = ConfigList.SingleOrDefault(x => x.Key == key);
  123. if (single != null)
  124. return single;
  125. else
  126. throw new ArgumentException("No Configurations found for: " + key);
  127. }
  128. else
  129. throw new ArgumentException("No Configurations found");
  130. }
  131. public static List<ConfigDTO> GetParentConfig(SectionCategory section = SectionCategory.SDNListSection)
  132. {
  133. if (section == SectionCategory.SDNListSection)
  134. {
  135. if (ConfigListSdn.Any())
  136. {
  137. return ConfigListSdn.ToList();
  138. }
  139. }
  140. else if (section == SectionCategory.PartnerAgentSection)
  141. {
  142. if (ConfigList.Any())
  143. {
  144. return ConfigList.ToList();
  145. }
  146. }
  147. else
  148. {
  149. throw new ArgumentException("No Configurations found for: " + section);
  150. }
  151. return new List<ConfigDTO>();
  152. }
  153. }
  154. public enum SectionCategory
  155. {
  156. PartnerAgentSection,
  157. SDNListSection
  158. }
  159. }
  160. #region Custom Config Helper
  161. #region MySection Group
  162. public class MySectionGroup : ConfigurationSectionGroup
  163. {
  164. [ConfigurationProperty("SDNListSetting", IsRequired = false)]
  165. public SDNListSection SDNListSetting
  166. {
  167. get { return (SDNListSection)base.Sections["SDNListSetting"]; }
  168. }
  169. [ConfigurationProperty("PartnerAgentSection", IsRequired = false)]
  170. public PartnerAgentSection PartnerAgentSettings
  171. {
  172. get { return (PartnerAgentSection)base.Sections["PartnerAgentSection"]; }
  173. }
  174. }
  175. #endregion
  176. #region ConfigurationSection
  177. public class PartnerAgentSection : ConfigurationSection
  178. {
  179. public const string SectionName = "PartnerAgentSection";
  180. [ConfigurationProperty("PartnerAgentConfig")]
  181. public CustomConfigCollection PartnerAgentConfig
  182. {
  183. get { return ((CustomConfigCollection)(base["PartnerAgentConfig"])); }
  184. }
  185. }
  186. public class SDNListSection : ConfigurationSection
  187. {
  188. public const string SectionName = "SDNListSection";
  189. [ConfigurationProperty("SDNListSetting")]
  190. public CustomConfigCollection SDNListSetting
  191. {
  192. get { return ((CustomConfigCollection)(base["SDNListSetting"])); }
  193. }
  194. }
  195. #endregion
  196. #region ConfigurationElementCollection
  197. [ConfigurationCollection(typeof(CustomConfigElement))]
  198. public class CustomConfigCollection : ConfigurationElementCollection
  199. {
  200. protected override ConfigurationElement CreateNewElement()
  201. {
  202. return new CustomConfigElement();
  203. }
  204. protected override object GetElementKey(ConfigurationElement element)
  205. {
  206. return ((CustomConfigElement)(element)).Key;
  207. }
  208. public CustomConfigElement this[int idx]
  209. {
  210. get
  211. {
  212. return (CustomConfigElement)BaseGet(idx);
  213. }
  214. }
  215. }
  216. #endregion
  217. #region ConfigurationElement
  218. public class CustomConfigElement : ConfigurationElement
  219. {
  220. [ConfigurationProperty("key", DefaultValue = "", IsKey = true, IsRequired = false)]
  221. public string Key
  222. {
  223. get { return (string)this["key"]; }
  224. set { this["key"] = value; }
  225. }
  226. [ConfigurationProperty("name", DefaultValue = "",
  227. IsKey = false, IsRequired = true)]
  228. public string Name
  229. {
  230. get { return (string)this["name"]; }
  231. set { this["name"] = value; }
  232. }
  233. [ConfigurationProperty("username", DefaultValue = "", IsKey = false, IsRequired = false)]
  234. public string UserName
  235. {
  236. get { return (string)this["username"]; }
  237. set { this["username"] = value; }
  238. }
  239. [ConfigurationProperty("password", DefaultValue = "", IsKey = false, IsRequired = false)]
  240. public string Password
  241. {
  242. get { return (string)this["password"]; }
  243. set { this["password"] = value; }
  244. }
  245. [ConfigurationProperty("field1", DefaultValue = "", IsKey = false, IsRequired = false)]
  246. public string Field1
  247. {
  248. get { return (string)this["field1"]; }
  249. set { this["field1"] = value; }
  250. }
  251. [ConfigurationProperty("field2", DefaultValue = "", IsKey = false, IsRequired = false)]
  252. public string Field2
  253. {
  254. get { return (string)this["field2"]; }
  255. set { this["field2"] = value; }
  256. }
  257. [ConfigurationProperty("field3", DefaultValue = "", IsKey = false, IsRequired = false)]
  258. public string Field3
  259. {
  260. get { return (string)this["field3"]; }
  261. set { this["field3"] = value; }
  262. }
  263. [ConfigurationProperty("field4", DefaultValue = "", IsKey = false, IsRequired = false)]
  264. public string Field4
  265. {
  266. get { return (string)this["field4"]; }
  267. set { this["field4"] = value; }
  268. }
  269. [ConfigurationProperty("toEmail", DefaultValue = "", IsKey = false, IsRequired = false)]
  270. public string ToEmail
  271. {
  272. get { return (string)this["toEmail"]; }
  273. set { this["toEmail"] = value; }
  274. }
  275. [ConfigurationProperty("toEmailCc", DefaultValue = "", IsKey = false, IsRequired = false)]
  276. public string ToEmailCc
  277. {
  278. get { return (string)this["toEmailCc"]; }
  279. set { this["toEmailCc"] = value; }
  280. }
  281. [ConfigurationProperty("toEmailBCc", DefaultValue = "", IsKey = false, IsRequired = false)]
  282. public string ToEmailBCc
  283. {
  284. get { return (string)this["toEmailBCc"]; }
  285. set { this["toEmailBCc"] = value; }
  286. }
  287. [ConfigurationProperty("url", DefaultValue = "", IsKey = false, IsRequired = false)]
  288. public string URL
  289. {
  290. get { return (string)this["url"]; }
  291. set { this["url"] = value; }
  292. }
  293. [ConfigurationProperty("active", DefaultValue = "", IsKey = false, IsRequired = false)]
  294. public string Active
  295. {
  296. get { return (string)this["active"]; }
  297. set { this["active"] = value; }
  298. }
  299. }
  300. #endregion
  301. #endregion
  302. }