UserConfiguration.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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()
  62. {
  63. var result = new Dictionary<string, T>();
  64. var data = new Client<UserSettings>().Query(
  65. new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name).And(x => x.UserID).IsEqualTo(ClientFactory.UserID),
  66. new Columns<UserSettings>(x => x.Key, x => x.Contents),
  67. new SortOrder<UserSettings>(x => x.Key)
  68. );
  69. foreach (var row in data.Rows)
  70. result[row.Get<UserSettings, string>(c => c.Key)] = Serialization.Deserialize<T>(row.Get<UserSettings, string>(c => c.Contents));
  71. return result;
  72. }
  73. public override void Save(T obj)
  74. {
  75. ConfigurationCache.Add(ConfigurationCacheType.User, GetKey(), obj);
  76. var setting = GetSettings();
  77. setting.Contents = Serialization.Serialize(obj);
  78. new Client<UserSettings>().Save(setting, "", (o, e) => { });
  79. }
  80. public override void SaveAll(Dictionary<string, T> objs)
  81. {
  82. foreach (var (section, obj) in objs)
  83. {
  84. ConfigurationCache.Add(ConfigurationCacheType.User, GetKey(section), obj);
  85. }
  86. var client = new Client<UserSettings>();
  87. var data = client.Load(
  88. new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
  89. .And(x => x.Key).InList(objs.Keys.ToArray())
  90. .And(x => x.UserID).IsEqualTo(ClientFactory.UserID))
  91. .ToDictionary(x => x.Key, x => x);
  92. var settings = new List<UserSettings>(objs.Count);
  93. foreach (var (section, obj) in objs)
  94. {
  95. var contents = Serialization.Serialize(obj);
  96. if (!data.TryGetValue(section, out var setting))
  97. {
  98. setting = NewSettings(section);
  99. }
  100. setting.Contents = contents;
  101. settings.Add(setting);
  102. }
  103. client.Save(settings, "", (o, e) => { });
  104. }
  105. public override void Delete(Action<Exception?>? callback = null)
  106. {
  107. ConfigurationCache.Clear<T>(ConfigurationCacheType.User, GetKey());
  108. var client = new Client<UserSettings>();
  109. var filter = new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
  110. .And(x => x.Key).IsEqualTo(Section)
  111. .And(x => x.UserID).IsEqualTo(ClientFactory.UserID);
  112. UserSettings? result;
  113. try
  114. {
  115. result = client.Load(filter).FirstOrDefault();
  116. }
  117. catch (Exception e)
  118. {
  119. Logger.Send(LogType.Error, ClientFactory.UserID, "Error in UserConfiguration.Delete(): " + CoreUtils.FormatException(e));
  120. result = null;
  121. }
  122. if (result != null)
  123. {
  124. if(callback != null)
  125. {
  126. client.Delete(result, "", (o, e) => { callback(e); });
  127. }
  128. else
  129. {
  130. client.Delete(result, "");
  131. }
  132. }
  133. }
  134. public override string[] Sections()
  135. {
  136. var result = new Client<UserSettings>().Query(
  137. new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name).And(x => x.UserID).IsEqualTo(ClientFactory.UserID),
  138. new Columns<UserSettings>(x => x.Key),
  139. new SortOrder<UserSettings>(x => x.Key)
  140. ).Rows.Select(r => r.Get<UserSettings, string>(c => c.Key)).Distinct().ToArray();
  141. return result;
  142. }
  143. }
  144. }