UserConfiguration.cs 6.7 KB

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