GlobalConfiguration.cs 5.0 KB

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