UserConfiguration.cs 7.2 KB

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