using Sarand.BusinessMgr.Net.Standard.Base.App; using Sarand.BusinessMgr.Net.Standard.Base.Bus; using Sarand.BusinessMgr.Net.Standard.Base.Core; using Sarand.Net.Standard.Base.App; using Sarand.Net.Standard.Base.Bus; using Sarand.Net.Standard.Base.ItemOptions; using Sarand.Net.Standard.Base.Utils; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Sarand.BusinessMgr.Net.Standard.Dbas.App { //======================================================================= /// /// Practically persistant/static data stored in instance database, eg. Owner configuration options. /// public class SbmInstanceDataCache_Dbas : SbmInstanceDataCache { //------------------------------------------------------------------- // Definitions. private class ItemOptionData { public ItemOptionData(ItemId itemId) { ItemId = itemId; } // Irem Id - options' owning item. public ItemId ItemId { get; private set; } = new ItemId(); public List ItemOptions { get; } = new List(); public DateTime LastRefreshed { get; set; } = DateTime.Now; } private class ItemOptionsCacheData { public ItemOptionsCacheData(ItemId itemIdRef) { ItemIdRef = itemIdRef; } // Reference Id - Owner Organisation Affiliate Id. public ItemId ItemIdRef { get; private set; } = new ItemId(); // Item option identified by their owning item. public Dictionary ItemOptions { get; } = new Dictionary(); } //------------------------------------------------------------------- // Data members. private SbmInternals internals = null; // Instance Item Options - keyed on Owner Affiliate Id. private readonly Dictionary itemOptionCaches = new Dictionary(); //------------------------------------------------------------------- // Construction. public SbmInstanceDataCache_Dbas(SbmSessionContext sessionContext) { this.SessionContext = sessionContext; } protected override void Dispose(bool disposing) { if (internals != null) internals.Dispose(); internals = null; base.Dispose(disposing); } //------------------------------------------------------------------- // Properties. public SbmSessionContext SessionContext { get; } private SbmInternals Internals { get { if (internals == null) { internals = new SbmInternals(SessionContext); }; return internals; } } //------------------------------------------------------------------- // Retrieve Item Options for Owner Organisation configuration. public override List GetOwnerOptions(ISbmUserOrganisationContext userContext) { List ownerOptions = new List(); if (!SysId.IsValid(userContext.OrganisationAffiliateId) || !SysId.IsValid(userContext.OrganisationAssociationId)) return ownerOptions; SbmItemId affiliateItemId_Owner = new SbmItemId(Sbm.ItemType.Affiliate, userContext.OrganisationAffiliateId); SbmItemId associationItemId_Internal = new SbmItemId(Sbm.ItemType.Association, userContext.OrganisationAssociationId); lock (itemOptionCaches) { // Retrieve the Owner Organisation item options from the cache. ItemOptionsCacheData itemOptionsCache = itemOptionCaches.GetValueOrDefault(affiliateItemId_Owner); if (itemOptionsCache == null) { // Add new entry in the cache for this Owner Organisation. itemOptionsCache = new ItemOptionsCacheData(affiliateItemId_Owner); itemOptionCaches[affiliateItemId_Owner] = itemOptionsCache; } // Retieve the Internal Association's item options from the cache. ItemOptionData ownerItemOptions = itemOptionsCache.ItemOptions.GetValueOrDefault(associationItemId_Internal); if (ownerItemOptions == null || (DateTime.Now - ownerItemOptions.LastRefreshed).TotalMinutes > 30) { // Initialise the cache for this item's options. ownerItemOptions = new ItemOptionData(associationItemId_Internal); itemOptionsCache.ItemOptions[associationItemId_Internal] = ownerItemOptions; // Populate options for this item. ownerItemOptions.ItemOptions.AddRange(Internals.ItemOptionConsole.GetItemOptionsForItem(associationItemId_Internal)); SaLogger.Debug($"{SysUtil.Fn} AffilIdOwner: {userContext.OrganisationAffiliateId}. AssocIdInternal: {userContext.OrganisationAssociationId}. Item options populated: {ownerItemOptions.ItemOptions.Count}."); } ownerOptions = new List(ownerItemOptions.ItemOptions); } return ownerOptions; } //------------------------------------------------------------------- } //======================================================================= }