GlobalConfiguration.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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(IEnumerable<string>? sections = null)
  63. {
  64. var result = new Dictionary<string, T>();
  65. var filter = new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name);
  66. if(sections != null)
  67. {
  68. filter.And(x => x.Key).InList(sections.AsArray());
  69. }
  70. var data = new Client<GlobalSettings>().Query(
  71. filter,
  72. new Columns<GlobalSettings>(x => x.Key, x => x.Contents),
  73. new SortOrder<GlobalSettings>(x => x.Key)
  74. );
  75. foreach (var row in data.Rows)
  76. result[row.Get<GlobalSettings, string>(c => c.Key)] = Serialization.Deserialize<T>(row.Get<GlobalSettings, string>(c => c.Contents));
  77. return result;
  78. }
  79. void IGlobalConfiguration.Save(object obj) => Save((T)obj);
  80. public override void Save(T obj)
  81. {
  82. ConfigurationCache.Add(ConfigurationCacheType.Global, Section, obj);
  83. var setting = GetSettings();
  84. setting.Contents = Serialization.Serialize(obj);
  85. new Client<GlobalSettings>().Save(setting, "", (o, e) => { });
  86. }
  87. public override void SaveAll(Dictionary<string, T> objs, bool reset = false)
  88. {
  89. foreach (var (section, obj) in objs)
  90. {
  91. ConfigurationCache.Add(ConfigurationCacheType.Global, section, obj);
  92. }
  93. var client = new Client<GlobalSettings>();
  94. var data = reset
  95. ? new Dictionary<string, GlobalSettings>()
  96. : client.Load(new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
  97. .And(x => x.Key).InList(objs.Keys.ToArray())).ToDictionary(x => x.Key, x => x);
  98. var settings = new List<GlobalSettings>(objs.Count);
  99. foreach(var (section, obj) in objs)
  100. {
  101. var contents = Serialization.Serialize(obj);
  102. if (!data.TryGetValue(section, out var setting))
  103. {
  104. setting = NewSettings(section);
  105. }
  106. setting.Contents = contents;
  107. settings.Add(setting);
  108. }
  109. client.Save(settings, "", (o, e) => { });
  110. }
  111. public override void Delete(Action<Exception?>? callback = null)
  112. {
  113. ConfigurationCache.Clear<T>(ConfigurationCacheType.Global, Section);
  114. var client = new Client<GlobalSettings>();
  115. var result = client.Load(new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name).And(x => x.Key).IsEqualTo(Section))
  116. .FirstOrDefault();
  117. if (result != null)
  118. {
  119. if(callback != null)
  120. {
  121. client.Delete(result, "", (o, e) => { callback(e); });
  122. }
  123. else
  124. {
  125. client.Delete(result, "");
  126. }
  127. }
  128. }
  129. public override string[] Sections()
  130. {
  131. var result = new Client<GlobalSettings>().Query(
  132. new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name),
  133. new Columns<GlobalSettings>(x => x.Key),
  134. new SortOrder<GlobalSettings>(x => x.Key)
  135. ).Rows.Select(r => r.Get<GlobalSettings, string>(c => c.Key)).Distinct().ToArray();
  136. return result;
  137. }
  138. }
  139. }