using Microsoft.IdentityModel.Tokens; using Sarand.Net.Standard.Base.App; using Sarand.Net.Standard.Base.Bus; using Sarand.Net.Standard.Base.Utils; using Saxonite.Net.Standard.Base.App; using Saxonite.Net.Standard.Base.Bus; using Saxonite.Net.Standard.Base.Consoles; using System; using System.Collections.Generic; using System.Text; namespace Saxonite.Net.Core.Web.Base.Consoles { //======================================================================= public class SaxDataCacheConsole_Web : SaxDataCacheConsole { //------------------------------------------------------------------- // Data members. private readonly SaxInternals internals; //------------------------------------------------------------------- // Protected construction. protected SaxDataCacheConsole_Web(SaxSessionContext sessionContext) : base(sessionContext) { internals = new SaxInternals(sessionContext); } protected override void Dispose(bool disposing) { if (disposing) { internals.Dispose(); } base.Dispose(disposing); } //------------------------------------------------------------------- // Regions. public bool RegionsSupported { get; protected set; } = false; private class RegionsData { public Dictionary regions = null; public List activeRegions = null; } private readonly RegionsData regionsData = new RegionsData(); public override List GetActiveRegions() { String fn = GetType().Name + ".ActiveRegions"; if (!RegionsSupported) throw new NotSupportedException(fn + " Regions not supported."); List activeRegions = new List(); lock (regionsData) { if (regionsData.regions == null) throw new NotSupportedException(fn + " Regions not populated."); activeRegions = new List(regionsData.activeRegions); } return activeRegions; } private bool IsRegionActive(SaxRegion region) { if (region == null) return false; if (!region.IsStatusActive) return false; if (SysId.IsValid(region.RegionIdParent)) return IsRegionActive(regionsData.regions.GetValue(region.RegionIdParent, null)); return true; } protected void RefreshRegions() { String fn = GetType().Name + ".RefreshRegions"; if (!RegionsSupported) throw new NotSupportedException(fn + " Regions not supported."); SaLogger.Info(fn + " START.", false); DateTime startedAt = DateTime.Now; List oldRegions = null; Dictionary newRegions = new Dictionary(); // Query Regions. SaxRegion.Params prmRegion = new SaxRegion.Params(); prmRegion.StatusCat = SaxRegion.StatusCat.NotDeleted; foreach (SaxRegion region in internals.ReferenceMgr.GetRegionList(prmRegion)) newRegions[region.Id] = region; // Replace the list of Regions. lock (regionsData) { oldRegions = regionsData.regions?.GetValues() ?? new List(); regionsData.regions = newRegions; // Populate list of active regions. regionsData.activeRegions = new List(); foreach (SaxRegion region in regionsData.regions.GetValues()) { if (IsRegionActive(region)) { // Clone region to ensure internal list isn't modified. SaxRegion activeRegion = internals.ReferenceMgr.CreateRegion(); activeRegion.SetData(region.GetData()); regionsData.activeRegions.Add(activeRegion); } } SaLogger.Info(fn + " END. Regions found: " + regionsData.regions.Count + ". Elapsed: " + DateUtil.FormatNumberOfSeconds((DateTime.Now - startedAt).TotalSeconds), true); } // Dispose old regions. oldRegions.ClearAndDispose(); oldRegions = null; } //------------------------------------------------------------------- // Sports. public bool SportsSupported { get; protected set; } = false; private class SportsData { public Dictionary sports = null; public List activeSports = null; } private readonly SportsData sportsData = new SportsData(); public override List GetActiveSports() { String fn = GetType().Name + ".ActiveSports"; if (!SportsSupported) throw new NotSupportedException(fn + " Sports not supported."); List activeSports = new List(); lock (sportsData) { if (sportsData.sports == null) throw new NotSupportedException(fn + " Sports not populated."); foreach (SaxSport sport in sportsData.sports.GetValues()) { if (IsSportActive(sport)) { // Clone sport to ensure internal list isn't modified. SaxSport activeSport = internals.ReferenceMgr.CreateSport(); activeSport.SetData(sport.GetData()); activeSports.Add(activeSport); } } } return activeSports; } private bool IsSportActive(SaxSport sport) { if (sport == null) return false; if (!sport.IsStatusActive) return false; return true; } protected void RefreshSports() { String fn = GetType().Name + ".RefreshSports"; if (!SportsSupported) throw new NotSupportedException(fn + " Sports not supported."); SaLogger.Info(fn + " START.", false); DateTime startedAt = DateTime.Now; List oldSports = null; Dictionary newSports = new Dictionary(); // Query Sports. SaxSport.Params prmSport = new SaxSport.Params(); prmSport.StatusCat = SaxSport.StatusCat.NotDeleted; foreach (SaxSport sport in internals.ReferenceMgr.GetSportList(prmSport)) newSports[sport.Id] = sport; // Replace the list of Sports. lock (sportsData) { oldSports = sportsData.sports?.GetValues() ?? new List(); sportsData.sports = newSports; // Populate list of active sports. sportsData.activeSports = new List(); foreach (SaxSport sport in sportsData.sports.GetValues()) { if (IsSportActive(sport)) sportsData.activeSports.Add(sport); } SaLogger.Info(fn + " END. Sports found: " + sportsData.sports.Count + ". Elapsed: " + DateUtil.FormatNumberOfSeconds((DateTime.Now - startedAt).TotalSeconds), true); } // Dispose old sports. oldSports.ClearAndDispose(); oldSports = null; } //------------------------------------------------------------------- } //======================================================================= }