GlobalConfiguration.cs 5.9 KB

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