UserConfiguration.cs 6.1 KB

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