UserConfiguration.cs 6.0 KB

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