GlobalConfiguration.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using InABox.Clients;
  5. using InABox.Core;
  6. namespace InABox.Configuration
  7. {
  8. public interface IGlobalConfigurationSettings : IConfigurationSettings
  9. {
  10. }
  11. [UserTracking(false)]
  12. public class GlobalSettings : Entity, IPersistent, IRemotable, ILicense<CoreLicense>
  13. {
  14. public string Section { get; set; }
  15. public string Key { get; set; }
  16. public string Contents { get; set; }
  17. }
  18. public class GlobalConfiguration<T> : Configuration<T> where T : IGlobalConfigurationSettings, new()
  19. {
  20. public GlobalConfiguration(string section = "") : base(section)
  21. {
  22. }
  23. private GlobalSettings NewSettings(string? section = null)
  24. {
  25. return new GlobalSettings { Section = typeof(T).Name, Key = section ?? Section };
  26. }
  27. private GlobalSettings GetSettings()
  28. {
  29. GlobalSettings result = null;
  30. if (ClientFactory.ClientType != null)
  31. {
  32. var client = new Client<GlobalSettings>();
  33. result = client.Load(new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name).And(x => x.Key).IsEqualTo(Section))
  34. .FirstOrDefault();
  35. }
  36. return result ?? NewSettings();
  37. }
  38. public override T Load(bool useCache = true)
  39. {
  40. if (useCache)
  41. {
  42. var cached = ConfigurationCache.Check<T>(ConfigurationCacheType.Global, Section);
  43. if (cached != null)
  44. //Logger.Send(LogType.Information, "", "Global Config: Returning Cached Configuration Data");
  45. return cached;
  46. }
  47. var result = new T();
  48. var setting = GetSettings();
  49. if (!string.IsNullOrEmpty(setting.Contents))
  50. result = Serialization.Deserialize<T>(setting.Contents);
  51. ConfigurationCache.Add(ConfigurationCacheType.Global, Section, result);
  52. return result;
  53. }
  54. public override Dictionary<string, T> LoadAll()
  55. {
  56. var result = new Dictionary<string, T>();
  57. var data = new Client<GlobalSettings>().Query(
  58. new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name),
  59. new Columns<GlobalSettings>(x => x.Key, x => x.Contents),
  60. new SortOrder<GlobalSettings>(x => x.Key)
  61. );
  62. foreach (var row in data.Rows)
  63. result[row.Get<GlobalSettings, string>(c => c.Key)] = Serialization.Deserialize<T>(row.Get<GlobalSettings, string>(c => c.Contents));
  64. return result;
  65. }
  66. public override void Save(T obj)
  67. {
  68. ConfigurationCache.Add(ConfigurationCacheType.Global, Section, obj);
  69. var setting = GetSettings();
  70. setting.Contents = Serialization.Serialize(obj);
  71. new Client<GlobalSettings>().Save(setting, "", (o, e) => { });
  72. }
  73. public override void SaveAll(Dictionary<string, T> objs)
  74. {
  75. foreach (var (section, obj) in objs)
  76. {
  77. ConfigurationCache.Add(ConfigurationCacheType.Global, section, obj);
  78. }
  79. var client = new Client<GlobalSettings>();
  80. var data = client.Load(
  81. new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
  82. .And(x => x.Key).InList(objs.Keys.ToArray())).ToDictionary(x => x.Key, x => x);
  83. var settings = new List<GlobalSettings>(objs.Count);
  84. foreach(var (section, obj) in objs)
  85. {
  86. var contents = Serialization.Serialize(obj);
  87. if (!data.TryGetValue(section, out var setting))
  88. {
  89. setting = NewSettings(section);
  90. }
  91. setting.Contents = contents;
  92. settings.Add(setting);
  93. }
  94. client.Save(settings, "", (o, e) => { });
  95. }
  96. public override void Delete(Action<Exception?>? callback = null)
  97. {
  98. ConfigurationCache.Clear<T>(ConfigurationCacheType.Global, Section);
  99. var client = new Client<GlobalSettings>();
  100. var result = client.Load(new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name).And(x => x.Key).IsEqualTo(Section))
  101. .FirstOrDefault();
  102. if (result != null)
  103. {
  104. if(callback != null)
  105. {
  106. client.Delete(result, "", (o, e) => { callback(e); });
  107. }
  108. else
  109. {
  110. client.Delete(result, "");
  111. }
  112. }
  113. }
  114. public override string[] Sections()
  115. {
  116. var result = new Client<GlobalSettings>().Query(
  117. new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name),
  118. new Columns<GlobalSettings>(x => x.Key),
  119. new SortOrder<GlobalSettings>(x => x.Key)
  120. ).Rows.Select(r => r.Get<GlobalSettings, string>(c => c.Key)).Distinct().ToArray();
  121. return result;
  122. }
  123. }
  124. }