UserConfiguration.cs 6.3 KB

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