GlobalConfiguration.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. {
  59. result = Serialization.Deserialize<T>(setting.Contents);
  60. if(result is BaseObject obj)
  61. {
  62. obj.CommitChanges();
  63. }
  64. }
  65. ConfigurationCache.Add(ConfigurationCacheType.Global, Section, result);
  66. return result;
  67. }
  68. public override Dictionary<string, T> LoadAll(IEnumerable<string>? sections = null)
  69. {
  70. var result = new Dictionary<string, T>();
  71. var filter = new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name);
  72. if(sections != null)
  73. {
  74. filter.And(x => x.Key).InList(sections.AsArray());
  75. }
  76. var data = new Client<GlobalSettings>().Query(
  77. filter,
  78. Columns.None<GlobalSettings>().Add(x => x.Key, x => x.Contents),
  79. new SortOrder<GlobalSettings>(x => x.Key)
  80. );
  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. new Client<GlobalSettings>().Save(setting, "", (o, e) => { });
  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 client = new Client<GlobalSettings>();
  107. var data = reset
  108. ? new Dictionary<string, GlobalSettings>()
  109. : client.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. client.Save(settings, "", (o, e) => { });
  123. }
  124. public override void Delete(Action<Exception?>? callback = null)
  125. {
  126. ConfigurationCache.Clear<T>(ConfigurationCacheType.Global, Section);
  127. var client = new Client<GlobalSettings>();
  128. var result = client.Load(new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name).And(x => x.Key).IsEqualTo(Section))
  129. .FirstOrDefault();
  130. if (result != null)
  131. {
  132. if(callback != null)
  133. {
  134. client.Delete(result, "", (o, e) => { callback(e); });
  135. }
  136. else
  137. {
  138. client.Delete(result, "");
  139. }
  140. }
  141. }
  142. public override string[] Sections()
  143. {
  144. var result = new Client<GlobalSettings>().Query(
  145. new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name),
  146. Columns.None<GlobalSettings>().Add(x => x.Key),
  147. new SortOrder<GlobalSettings>(x => x.Key)
  148. ).Rows.Select(r => r.Get<GlobalSettings, string>(c => c.Key)).Distinct().ToArray();
  149. return result;
  150. }
  151. }
  152. }