GlobalConfiguration.cs 5.6 KB

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