UserConfiguration.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 IUserConfigurationSettings : IConfigurationSettings
  9. {
  10. }
  11. public static class UserConfigurationExtensions
  12. {
  13. public static void SaveUser<T>(this T settings)
  14. where T : IUserConfigurationSettings, new()
  15. {
  16. new UserConfiguration<T>().Save(settings);
  17. }
  18. }
  19. [UserTracking(false)]
  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. public UserConfiguration(string section = "") : base(section)
  30. {
  31. }
  32. private UserSettings NewSettings(string? section = null)
  33. {
  34. return new UserSettings { Section = typeof(T).Name, Key = section ?? Section, UserID = ClientFactory.UserID };
  35. }
  36. private UserSettings GetSettings()
  37. {
  38. UserSettings result = null;
  39. if (ClientFactory.ClientType != null)
  40. {
  41. var client = new Client<UserSettings>();
  42. var filter = new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
  43. .And(x => x.Key).IsEqualTo(Section)
  44. .And(x => x.UserID).IsEqualTo(ClientFactory.UserID);
  45. result = client.Query(filter, Columns.All<UserSettings>())
  46. .ToObjects<UserSettings>().FirstOrDefault();
  47. }
  48. return result ?? NewSettings();
  49. }
  50. private string GetKey(string? section = null)
  51. {
  52. return string.Format("{0}.{1}", section ?? Section, ClientFactory.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. result = Serialization.Deserialize<T>(setting.Contents);
  67. ConfigurationCache.Add(ConfigurationCacheType.User, GetKey(), result);
  68. return result;
  69. }
  70. public override Dictionary<string, T> LoadAll(IEnumerable<string>? sections = null)
  71. {
  72. var result = new Dictionary<string, T>();
  73. var filter = new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
  74. .And(x => x.UserID).IsEqualTo(ClientFactory.UserID);
  75. if (sections != null)
  76. {
  77. filter.And(x => x.Key).InList(sections.AsArray());
  78. }
  79. var data = new Client<UserSettings>().Query(
  80. filter,
  81. Columns.None<UserSettings>().Add(x => x.Key, x => x.Contents),
  82. new SortOrder<UserSettings>(x => x.Key)
  83. );
  84. foreach (var row in data.Rows)
  85. result[row.Get<UserSettings, string>(c => c.Key)] = Serialization.Deserialize<T>(row.Get<UserSettings, string>(c => c.Contents));
  86. return result;
  87. }
  88. public override void Save(T obj)
  89. {
  90. ConfigurationCache.Add(ConfigurationCacheType.User, GetKey(), obj);
  91. var setting = GetSettings();
  92. setting.Contents = Serialization.Serialize(obj);
  93. new Client<UserSettings>().Save(setting, "", (o, e) => { });
  94. }
  95. public override void SaveAll(Dictionary<string, T> objs, bool reset = false)
  96. {
  97. foreach (var (section, obj) in objs)
  98. {
  99. ConfigurationCache.Add(ConfigurationCacheType.User, GetKey(section), obj);
  100. }
  101. var client = new Client<UserSettings>();
  102. var data = reset
  103. ? new Dictionary<string,UserSettings>()
  104. : client.Query(
  105. new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
  106. .And(x => x.Key).InList(objs.Keys.ToArray())
  107. .And(x => x.UserID).IsEqualTo(ClientFactory.UserID),
  108. Columns.All<UserSettings>())
  109. .ToObjects<UserSettings>()
  110. .ToDictionary(x => x.Key, x => x);
  111. var settings = new List<UserSettings>(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.User, GetKey());
  127. var client = new Client<UserSettings>();
  128. var filter = new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
  129. .And(x => x.Key).IsEqualTo(Section)
  130. .And(x => x.UserID).IsEqualTo(ClientFactory.UserID);
  131. UserSettings? result;
  132. try
  133. {
  134. result = client.Load(filter).FirstOrDefault();
  135. }
  136. catch (Exception e)
  137. {
  138. Logger.Send(LogType.Error, ClientFactory.UserID, "Error in UserConfiguration.Delete(): " + CoreUtils.FormatException(e));
  139. result = null;
  140. }
  141. if (result != null)
  142. {
  143. if(callback != null)
  144. {
  145. client.Delete(result, "", (o, e) => { callback(e); });
  146. }
  147. else
  148. {
  149. client.Delete(result, "");
  150. }
  151. }
  152. }
  153. public override string[] Sections()
  154. {
  155. var result = new Client<UserSettings>().Query(
  156. new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name).And(x => x.UserID).IsEqualTo(ClientFactory.UserID),
  157. Columns.None<UserSettings>().Add(x => x.Key),
  158. new SortOrder<UserSettings>(x => x.Key)
  159. ).Rows.Select(r => r.Get<UserSettings, string>(c => c.Key)).Distinct().ToArray();
  160. return result;
  161. }
  162. }
  163. }