using System; using System.Collections.Generic; using System.Linq; using InABox.Core; namespace InABox.Configuration { public interface IGlobalConfigurationSettings : IConfigurationSettings { } [UserTracking(false)] public class GlobalSettings : Entity, IPersistent, IRemotable, ILicense { public string Section { get; set; } = ""; public string Key { get; set; } = ""; public string Contents { get; set; } = ""; } public interface IGlobalConfiguration { string[] Sections(); object Load(bool useCache = true); void Save(object obj); void Delete(Action? callback = null); } public class GlobalConfiguration : Configuration, IGlobalConfiguration where T : IGlobalConfigurationSettings, new() { private IConfigurationProvider Provider; public GlobalConfiguration(string section = "") : this(section, ClientConfigurationProvider.Provider) { } public GlobalConfiguration(string section, IConfigurationProvider provider) : base(section) { Provider = provider; } private GlobalSettings NewSettings(string? section = null) { return new GlobalSettings { Section = typeof(T).Name, Key = section ?? Section }; } private GlobalSettings GetSettings() { GlobalSettings? result = null; result = Provider.Load( new Filter(x => x.Section).IsEqualTo(typeof(T).Name).And(x => x.Key).IsEqualTo(Section)) .FirstOrDefault(); return result ?? NewSettings(); } object IGlobalConfiguration.Load(bool useCache) => Load(useCache); public override T Load(bool useCache = true) { if (useCache) { var cached = ConfigurationCache.Check(ConfigurationCacheType.Global, Section); if (cached != null) //Logger.Send(LogType.Information, "", "Global Config: Returning Cached Configuration Data"); return cached; } var result = new T(); var setting = GetSettings(); if (!string.IsNullOrEmpty(setting.Contents)) { result = Serialization.Deserialize(setting.Contents); if(result is BaseObject obj) { obj.CommitChanges(); } } ConfigurationCache.Add(ConfigurationCacheType.Global, Section, result); return result; } public override Dictionary LoadAll(IEnumerable? sections = null) { var result = new Dictionary(); var filter = new Filter(x => x.Section).IsEqualTo(typeof(T).Name); if(sections != null) { filter.And(x => x.Key).InList(sections.AsArray()); } var data = Provider.Query( filter, Columns.None().Add(x => x.Key, x => x.Contents), new SortOrder(x => x.Key)); foreach (var row in data.Rows) { var tObj = Serialization.Deserialize(row.Get(c => c.Contents)); if(tObj is BaseObject obj) { obj.CommitChanges(); } result[row.Get(c => c.Key)] = tObj; } return result; } void IGlobalConfiguration.Save(object obj) => Save((T)obj); public override void Save(T obj) { ConfigurationCache.Add(ConfigurationCacheType.Global, Section, obj); var setting = GetSettings(); setting.Contents = Serialization.Serialize(obj); Provider.Save(setting); } public override void SaveAll(Dictionary objs, bool reset = false) { foreach (var (section, obj) in objs) { ConfigurationCache.Add(ConfigurationCacheType.Global, section, obj); } var data = reset ? new Dictionary() : Provider.Load(new Filter(x => x.Section).IsEqualTo(typeof(T).Name) .And(x => x.Key).InList(objs.Keys.ToArray())).ToDictionary(x => x.Key, x => x); var settings = new List(objs.Count); foreach(var (section, obj) in objs) { var contents = Serialization.Serialize(obj); if (!data.TryGetValue(section, out var setting)) { setting = NewSettings(section); } setting.Contents = contents; settings.Add(setting); } Provider.Save(settings); } public override void Delete(Action? callback = null) { ConfigurationCache.Clear(ConfigurationCacheType.Global, Section); var result = Provider.Load(new Filter(x => x.Section).IsEqualTo(typeof(T).Name).And(x => x.Key).IsEqualTo(Section)) .FirstOrDefault(); if (result != null) { Provider.Delete(result, callback); } } public override string[] Sections() { var result = Provider.Query( new Filter(x => x.Section).IsEqualTo(typeof(T).Name), Columns.None().Add(x => x.Key), new SortOrder(x => x.Key) ).Rows.Select(r => r.Get(c => c.Key)).Distinct().ToArray(); return result; } } }