345 lines
14 KiB
C#
345 lines
14 KiB
C#
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Dbas;
|
|
using Saxonite.Net.Standard.Base.Mgr;
|
|
using Saxonite.Net.Standard.Base.App;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using Saxonite.Net.Standard.Base.Consoles;
|
|
using Saxonite.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
|
|
namespace Saxonite.Net.Standard.Base.App
|
|
{
|
|
//=======================================================================
|
|
public class SaxInternals : SacsDisposable
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
private class DataMembers
|
|
{
|
|
public bool useDataCacheConsole;
|
|
}
|
|
private readonly DataMembers data = new DataMembers();
|
|
private class Managers
|
|
{
|
|
public SaxReferenceMgr referenceMgr = null;
|
|
public SaxAccountMgr accountMgr = null;
|
|
public SaxActivityMgr activityMgr = null;
|
|
public SaxItemDataMgr itemDataMgr = null;
|
|
}
|
|
private readonly Managers mgr = new Managers();
|
|
private class Consoles
|
|
{
|
|
public SaxActivityConsole activityConsole;
|
|
public SaxEmailConsole emailConsole;
|
|
}
|
|
private readonly Consoles con = new Consoles();
|
|
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
public SaxInternals(SaxSessionContext sessionContext, bool useDataCacheConsole = true)
|
|
{
|
|
if (sessionContext == null)
|
|
throw new NotSupportedException("Unable to create Internals with invalid session context");
|
|
data.useDataCacheConsole = useDataCacheConsole;
|
|
System = SaxGlobals.System;
|
|
SessionContext = sessionContext;
|
|
|
|
try
|
|
{
|
|
// Check for data cache.
|
|
// NB Must not do this if Internals constructed by the DataCache console itself as cached data will not be requested from here.
|
|
if (useDataCacheConsole)
|
|
{
|
|
// Inside try..catch as Admin app throws exception on GetDataCacheConsole().
|
|
SaxDataCacheConsole dataCacheConsole = SaxGlobals.ConsoleHelper.GetDataCacheConsole(SessionContext);
|
|
if (dataCacheConsole != null)
|
|
dataCacheConsole.PopulateCachedDataToInternals(this);
|
|
}
|
|
}
|
|
catch (Exception) { }
|
|
}
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (disposing)
|
|
{
|
|
if (mgr.referenceMgr != null) mgr.referenceMgr.Dispose(); mgr.referenceMgr = null;
|
|
if (mgr.accountMgr != null) mgr.accountMgr.Dispose(); mgr.accountMgr = null;
|
|
if (mgr.activityMgr != null) mgr.activityMgr.Dispose(); mgr.activityMgr = null;
|
|
if (mgr.itemDataMgr != null) mgr.itemDataMgr.Dispose(); mgr.itemDataMgr = null;
|
|
if (con.activityConsole != null) con.activityConsole.Dispose(); con.activityConsole = null;
|
|
}
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
public bool DebugMode { get; } = SacsSystem.DebugMode;
|
|
public SaxSystem System { get; }
|
|
public SaxSessionContext SessionContext { get; }
|
|
public ISacsDatabase CoreDatabase { get { return SaxGlobals.App.GetCoreDatabase(SessionContext); } }
|
|
public IDatabaseInfo CreateCoreDbInfo()
|
|
{
|
|
return SaxGlobals.App.CreateCoreDbInfo(SessionContext);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// System codes.
|
|
private readonly Dictionary<Sax.CodeType, List<TypeCodeDesc>> codeTypes = new Dictionary<Sax.CodeType, List<TypeCodeDesc>>();
|
|
public List<TypeCodeDesc> DataItemTypeCodes { get { return GetCodesOfType(Sax.CodeType.DataItemType); } }
|
|
public List<TypeCodeDesc> YesNoCodes { get { return GetCodesOfType(Sax.CodeType.YesNo); } }
|
|
public List<TypeCodeDesc> GenderCodes { get { return GetCodesOfType(Sax.CodeType.Gender); } }
|
|
public List<TypeCodeDesc> SportSpecialityLabelCodes { get { return GetCodesOfType(Sax.CodeType.SportSpecialityLabel); } }
|
|
public List<TypeCodeDesc> SportParticipationCodes { get { return GetCodesOfType(Sax.CodeType.SportParticipation); } }
|
|
public List<TypeCodeDesc> SportOfficiatingCodes { get { return GetCodesOfType(Sax.CodeType.SportOfficiating); } }
|
|
public List<TypeCodeDesc> RegionTypeCodes { get { return GetCodesOfType(Sax.CodeType.RegionType); } }
|
|
public List<TypeCodeDesc> UserRoleCodes { get { return GetCodesOfType(Sax.CodeType.UserRole); } }
|
|
public List<TypeCodeDesc> OrganisationTypeCodes { get { return GetCodesOfType(Sax.CodeType.OrganisationType); } }
|
|
public List<TypeCodeDesc> OrganisationCategoryCodes { get { return GetCodesOfType(Sax.CodeType.OrganisationCategory); } }
|
|
public List<TypeCodeDesc> OrganisationRoleCodes { get { return GetCodesOfType(Sax.CodeType.OrganisationRole); } }
|
|
public List<TypeCodeDesc> AddressTypeCodes { get { return GetCodesOfType(Sax.CodeType.AddressType); } }
|
|
public List<TypeCodeDesc> ContactMethodCodes { get { return GetCodesOfType(Sax.CodeType.ContactMethod); } }
|
|
public List<TypeCodeDesc> EventTypeCodes { get { return GetCodesOfType(Sax.CodeType.EventType); } }
|
|
public List<TypeCodeDesc> OpportunityTypeCodes { get { return GetCodesOfType(Sax.CodeType.OpportunityType); } }
|
|
public List<TypeCodeDesc> ActivityReactionCodes { get { return GetCodesOfType(Sax.CodeType.ActivityReaction); } }
|
|
public List<TypeCodeDesc> GetCodesOfType(Sax.CodeType codeType)
|
|
{
|
|
if (!codeTypes.ContainsKey(codeType))
|
|
codeTypes.Add(codeType, SaxSystem.GetCodesOfType(codeType));
|
|
return new List<TypeCodeDesc>(codeTypes[codeType]);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Consoles.
|
|
public SaxConsoleHelper ConsoleHelper { get { return SaxGlobals.App.ConsoleHelper; } }
|
|
public SaxDataCacheConsole DataCacheConsole { get { return SaxGlobals.ConsoleHelper.GetDataCacheConsole(SessionContext); } }
|
|
public SaxSessionDataConsole SessionDataConsole { get { return SaxGlobals.ConsoleHelper.GetSessionDataConsole(SessionContext); } }
|
|
public SaxActivityConsole ActivityConsole { get { if (con.activityConsole == null) con.activityConsole = ConsoleHelper.CreateActivityConsole(SessionContext); return con.activityConsole; } }
|
|
public SaxEmailConsole EmailConsole { get { if (con.emailConsole == null) con.emailConsole = ConsoleHelper.CreateEmailConsole(SessionContext); return con.emailConsole; } }
|
|
|
|
//-------------------------------------------------------------------
|
|
// Managers.
|
|
public SaxReferenceMgr ReferenceMgr
|
|
{
|
|
get
|
|
{
|
|
if (mgr.referenceMgr == null)
|
|
mgr.referenceMgr = System.CreateReferenceMgr(SessionContext, this);
|
|
return mgr.referenceMgr;
|
|
}
|
|
}
|
|
public SaxAccountMgr AccountMgr
|
|
{
|
|
get
|
|
{
|
|
if (mgr.accountMgr == null)
|
|
mgr.accountMgr = System.CreateAccountMgr(SessionContext, this);
|
|
return mgr.accountMgr;
|
|
}
|
|
}
|
|
public SaxActivityMgr ActivityMgr
|
|
{
|
|
get
|
|
{
|
|
if (mgr.activityMgr == null)
|
|
mgr.activityMgr = System.CreateActivityMgr(SessionContext, this);
|
|
return mgr.activityMgr;
|
|
}
|
|
}
|
|
public SaxItemDataMgr ItemDataMgr
|
|
{
|
|
get
|
|
{
|
|
if (mgr.itemDataMgr == null)
|
|
mgr.itemDataMgr = System.CreateItemDataMgr(SessionContext, this);
|
|
return mgr.itemDataMgr;
|
|
}
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Regions.
|
|
// NB Online application caches regions and sports so these may be pre-populated in the Internals.
|
|
// Otherwise they are populated on first request.
|
|
private readonly Dictionary<SysId, SaxRegion.Data> regionCache = new Dictionary<SysId, SaxRegion.Data>();
|
|
private bool regionCacheFullyPopulated = false;
|
|
public void PopulateRegionCache(List<SaxRegion.Data> regions)
|
|
{
|
|
foreach (SaxRegion.Data regionData in regions)
|
|
regionCache[regionData.Id] = regionData.Clone();
|
|
regionCacheFullyPopulated = true;
|
|
//if (DebugMode) SaLogger.Debug(GetType().Name + ".PopulateRegionCache(regions) Regions populated: " + regionCache.Count);
|
|
}
|
|
public void RefreshRegionCache()
|
|
{
|
|
if (regionCacheFullyPopulated) return;
|
|
DateTime startedAt = DateTime.Now;
|
|
SaxRegion.Params prmRegion = new SaxRegion.Params();
|
|
prmRegion.StatusCat = SaxRegion.StatusCat.NotDeleted;
|
|
int regionsQueried = 0;
|
|
foreach (SaxRegion region in ReferenceMgr.GetRegionList(prmRegion))
|
|
{
|
|
regionsQueried++;
|
|
regionCache[region.Id] = region.GetData();
|
|
region.Dispose();
|
|
}
|
|
regionCacheFullyPopulated = true;
|
|
SaLogger.Debug(GetType().Name + ".RefreshRegionCache() Regions queried: " + regionsQueried + ". Regions in cache: " + regionCache.Count + ". Elapsed: " + DateUtil.FormatNumberOfSeconds((DateTime.Now - startedAt).TotalSeconds));
|
|
}
|
|
private void QueryRegionsToCache(List<SysId> regionsIds)
|
|
{
|
|
if (regionCacheFullyPopulated) return;
|
|
SaxRegion.Params prmRegion = new SaxRegion.Params();
|
|
prmRegion.IdsIncluded.AddRange(regionsIds.FindAll(item => SysId.IsValid(item) && !regionCache.ContainsKey(item)));
|
|
prmRegion.StatusCat = SaxRegion.StatusCat.NotDeleted;
|
|
if (prmRegion.IdsIncluded.Count > 0)
|
|
{
|
|
// At least one region needs to be retrieved.
|
|
DateTime startedAt = DateTime.Now;
|
|
int regionsQueried = 0;
|
|
foreach (SaxRegion region in ReferenceMgr.GetRegionList(prmRegion))
|
|
{
|
|
regionsQueried++;
|
|
regionCache[region.Id] = region.GetData();
|
|
region.Dispose();
|
|
}
|
|
SaLogger.Debug(GetType().Name + ".QueryRegionsToCache(regionIds) Regions queried: " + regionsQueried + ". Regions in cache: " + regionCache.Count + ". Elapsed: " + DateUtil.FormatNumberOfSeconds((DateTime.Now - startedAt).TotalSeconds));
|
|
}
|
|
}
|
|
public SaxRegion.Data GetRegion(SysId regionId)
|
|
{
|
|
QueryRegionsToCache(new List<SysId>() { regionId });
|
|
return regionCache.GetValue(regionId, null);
|
|
}
|
|
public List<SaxRegion.Data> GetRegions(List<SysId> regionIds)
|
|
{
|
|
List<SaxRegion.Data> regions = new List<SaxRegion.Data>();
|
|
QueryRegionsToCache(regionIds);
|
|
foreach (SysId regionId in regionIds)
|
|
{
|
|
if (!SysId.IsValid(regionId)) continue;
|
|
SaxRegion.Data region = regionCache.GetValue(regionId, null);
|
|
if (region != null)
|
|
regions.Add(region);
|
|
}
|
|
return regions;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Sports.
|
|
// NB Online application caches sports and sports so these may be pre-populated in the Internals.
|
|
// Otherwise they are populated on first request.
|
|
private readonly Dictionary<SysId, SaxSport.Data> sportCache = new Dictionary<SysId, SaxSport.Data>();
|
|
private bool sportCacheFullyPopulated = false;
|
|
public void PopulateSportCache(List<SaxSport.Data> sports)
|
|
{
|
|
foreach (SaxSport.Data sportData in sports)
|
|
sportCache[sportData.Id] = sportData.Clone();
|
|
sportCacheFullyPopulated = true;
|
|
//if (DebugMode) SaLogger.Debug(GetType().Name + ".PopulateSportCache(sports) Sports populated: " + sportCache.Count);
|
|
}
|
|
public void RefreshSportCache()
|
|
{
|
|
if (sportCacheFullyPopulated) return;
|
|
DateTime startedAt = DateTime.Now;
|
|
SaxSport.Params prmSport = new SaxSport.Params();
|
|
prmSport.StatusCat = SaxSport.StatusCat.NotDeleted;
|
|
int sportsQueried = 0;
|
|
foreach (SaxSport sport in ReferenceMgr.GetSportList(prmSport))
|
|
{
|
|
sportsQueried++;
|
|
sportCache[sport.Id] = sport.GetData();
|
|
sport.Dispose();
|
|
}
|
|
sportCacheFullyPopulated = true;
|
|
SaLogger.Debug(GetType().Name + ".RefreshSportCache() Sports queried: " + sportsQueried + ". Sports in cache: " + sportCache.Count + ". Elapsed: " + DateUtil.FormatNumberOfSeconds((DateTime.Now - startedAt).TotalSeconds));
|
|
}
|
|
private void QuerySportsToCache(List<SysId> sportsIds)
|
|
{
|
|
if (sportCacheFullyPopulated) return;
|
|
SaxSport.Params prmSport = new SaxSport.Params();
|
|
prmSport.IdsIncluded.AddRange(sportsIds.FindAll(item => SysId.IsValid(item) && !sportCache.ContainsKey(item)));
|
|
prmSport.StatusCat = SaxSport.StatusCat.NotDeleted;
|
|
if (prmSport.IdsIncluded.Count > 0)
|
|
{
|
|
// At least one sport needs to be retrieved.
|
|
DateTime startedAt = DateTime.Now;
|
|
int sportsQueried = 0;
|
|
foreach (SaxSport sport in ReferenceMgr.GetSportList(prmSport))
|
|
{
|
|
sportsQueried++;
|
|
sportCache[sport.Id] = sport.GetData();
|
|
sport.Dispose();
|
|
}
|
|
SaLogger.Debug(GetType().Name + ".QuerySportsToCache(sportIds) Sports queried: " + sportsQueried + ". Sports in cache: " + sportCache.Count + ". Elapsed: " + DateUtil.FormatNumberOfSeconds((DateTime.Now - startedAt).TotalSeconds));
|
|
}
|
|
}
|
|
public SaxSport.Data GetSport(SysId sportId)
|
|
{
|
|
QuerySportsToCache(new List<SysId>() { sportId });
|
|
return sportCache.GetValue(sportId, null);
|
|
}
|
|
public List<SaxSport.Data> GetSports(List<SysId> sportIds)
|
|
{
|
|
List<SaxSport.Data> sports = new List<SaxSport.Data>();
|
|
QuerySportsToCache(sportIds);
|
|
foreach (SysId sportId in sportIds)
|
|
{
|
|
if (!SysId.IsValid(sportId)) continue;
|
|
SaxSport.Data sport = sportCache.GetValue(sportId, null);
|
|
if (sport != null)
|
|
sports.Add(sport);
|
|
}
|
|
return sports;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Common Data Components
|
|
//-------------------------------------------------------------------
|
|
// User Accounts.
|
|
private readonly Dictionary<SysId, SaxUserAccount> userAccountCache = new Dictionary<SysId, SaxUserAccount>();
|
|
public SaxUserAccount GetUserAccount(SysId userAccountId)
|
|
{
|
|
if (!SysId.IsValid(userAccountId)) return null;
|
|
if (!userAccountCache.ContainsKey(userAccountId))
|
|
userAccountCache.Add(userAccountId, AccountMgr.GetUserAccountById(userAccountId));
|
|
return userAccountCache.GetValue(userAccountId, null);
|
|
}
|
|
public String GetUserAccountEmailAddress(SysId userAccountId)
|
|
{
|
|
SaxUserAccount userAccount = GetUserAccount(userAccountId);
|
|
return userAccount?.EmailAddress ?? "";
|
|
}
|
|
public String GetUserAccountDisplayName(SysId userAccountId)
|
|
{
|
|
SaxUserAccount userAccount = GetUserAccount(userAccountId);
|
|
return userAccount?.GetDisplayName() ?? "";
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// User Accounts.
|
|
private readonly Dictionary<SysId, SaxOrganisation> organisationCache = new Dictionary<SysId, SaxOrganisation>();
|
|
public SaxOrganisation GetOrganisation(SysId organisationId)
|
|
{
|
|
if (!SysId.IsValid(organisationId)) return null;
|
|
if (!organisationCache.ContainsKey(organisationId))
|
|
organisationCache.Add(organisationId, AccountMgr.GetOrganisationById(organisationId));
|
|
return organisationCache.GetValue(organisationId, null);
|
|
}
|
|
public String GetOrganisationName(SysId organisationId)
|
|
{
|
|
SaxOrganisation organisation = GetOrganisation(organisationId);
|
|
return organisation?.Name ?? "";
|
|
}
|
|
public SysId GetOrganisationRegionId(SysId organisationId)
|
|
{
|
|
SaxOrganisation organisation = GetOrganisation(organisationId);
|
|
return organisation?.RegionIdPrimary ?? new SysId();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|