UserConfiguration.cs 6.8 KB

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