GlobalConfiguration.cs 5.9 KB

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