UserConfiguration.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.Load(filter).FirstOrDefault(x => string.Equals(x.UserID, ClientFactory.UserID));
  46. }
  47. return result ?? NewSettings();
  48. }
  49. private string GetKey(string? section = null)
  50. {
  51. return string.Format("{0}.{1}", section ?? Section, ClientFactory.UserID);
  52. }
  53. public override T Load(bool useCache = true)
  54. {
  55. if (useCache)
  56. {
  57. var cached = ConfigurationCache.Check<T>(ConfigurationCacheType.User, GetKey());
  58. if (cached != null)
  59. //Logger.Send(LogType.Information, "", "User Config: Returning Cached Configuration Data");
  60. return cached;
  61. }
  62. var result = new T();
  63. var setting = GetSettings();
  64. if (!string.IsNullOrEmpty(setting.Contents))
  65. result = Serialization.Deserialize<T>(setting.Contents);
  66. ConfigurationCache.Add(ConfigurationCacheType.User, GetKey(), result);
  67. return result;
  68. }
  69. public override Dictionary<string, T> LoadAll(IEnumerable<string>? sections = null)
  70. {
  71. var result = new Dictionary<string, T>();
  72. var filter = new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
  73. .And(x => x.UserID).IsEqualTo(ClientFactory.UserID);
  74. if (sections != null)
  75. {
  76. filter.And(x => x.Key).InList(sections.AsArray());
  77. }
  78. var data = new Client<UserSettings>().Query(
  79. filter,
  80. new Columns<UserSettings>(x => x.Key, x => x.Contents),
  81. new SortOrder<UserSettings>(x => x.Key)
  82. );
  83. foreach (var row in data.Rows)
  84. result[row.Get<UserSettings, string>(c => c.Key)] = Serialization.Deserialize<T>(row.Get<UserSettings, string>(c => c.Contents));
  85. return result;
  86. }
  87. public override void Save(T obj)
  88. {
  89. ConfigurationCache.Add(ConfigurationCacheType.User, GetKey(), obj);
  90. var setting = GetSettings();
  91. setting.Contents = Serialization.Serialize(obj);
  92. new Client<UserSettings>().Save(setting, "", (o, e) => { });
  93. }
  94. public override void SaveAll(Dictionary<string, T> objs, bool reset = false)
  95. {
  96. foreach (var (section, obj) in objs)
  97. {
  98. ConfigurationCache.Add(ConfigurationCacheType.User, GetKey(section), obj);
  99. }
  100. var client = new Client<UserSettings>();
  101. var data = reset
  102. ? new Dictionary<string,UserSettings>()
  103. : client.Load(new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
  104. .And(x => x.Key).InList(objs.Keys.ToArray())
  105. .And(x => x.UserID).IsEqualTo(ClientFactory.UserID))
  106. .ToDictionary(x => x.Key, x => x);
  107. var settings = new List<UserSettings>(objs.Count);
  108. foreach (var (section, obj) in objs)
  109. {
  110. var contents = Serialization.Serialize(obj);
  111. if (!data.TryGetValue(section, out var setting))
  112. {
  113. setting = NewSettings(section);
  114. }
  115. setting.Contents = contents;
  116. settings.Add(setting);
  117. }
  118. client.Save(settings, "", (o, e) => { });
  119. }
  120. public override void Delete(Action<Exception?>? callback = null)
  121. {
  122. ConfigurationCache.Clear<T>(ConfigurationCacheType.User, GetKey());
  123. var client = new Client<UserSettings>();
  124. var filter = new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
  125. .And(x => x.Key).IsEqualTo(Section)
  126. .And(x => x.UserID).IsEqualTo(ClientFactory.UserID);
  127. UserSettings? result;
  128. try
  129. {
  130. result = client.Load(filter).FirstOrDefault();
  131. }
  132. catch (Exception e)
  133. {
  134. Logger.Send(LogType.Error, ClientFactory.UserID, "Error in UserConfiguration.Delete(): " + CoreUtils.FormatException(e));
  135. result = null;
  136. }
  137. if (result != null)
  138. {
  139. if(callback != null)
  140. {
  141. client.Delete(result, "", (o, e) => { callback(e); });
  142. }
  143. else
  144. {
  145. client.Delete(result, "");
  146. }
  147. }
  148. }
  149. public override string[] Sections()
  150. {
  151. var result = new Client<UserSettings>().Query(
  152. new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name).And(x => x.UserID).IsEqualTo(ClientFactory.UserID),
  153. new Columns<UserSettings>(x => x.Key),
  154. new SortOrder<UserSettings>(x => x.Key)
  155. ).Rows.Select(r => r.Get<UserSettings, string>(c => c.Key)).Distinct().ToArray();
  156. return result;
  157. }
  158. }
  159. }