Files
BusinessMgr-2019/SaBusMgrStandardDbas/Dbas/App/SbmInstanceDataCache_Dbas.cs
T

113 lines
4.7 KiB
C#

using Sarand.BusinessMgr.Net.Standard.Base.App;
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
{
//=======================================================================
/// <summary>
/// Practically persistant/static data stored in instance database, eg. Owner configuration options.
/// </summary>
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<SaItemOption> ItemOptions { get; } = new List<SaItemOption>();
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<ItemId, ItemOptionData> ItemOptions { get; } = new Dictionary<ItemId, ItemOptionData>();
}
//-------------------------------------------------------------------
// Data members.
private SbmInternals internals = null;
// Instance Item Options - keyed on Owner Affiliate Id.
private readonly Dictionary<ItemId, ItemOptionsCacheData> itemOptionCaches = new Dictionary<ItemId, ItemOptionsCacheData>();
//-------------------------------------------------------------------
// 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<SaItemOption> GetOwnerOptions(ISbmUserOrganisationContext userContext)
{
List<SaItemOption> ownerOptions = new List<SaItemOption>();
if (!SysId.IsValid(userContext.OrganisationAffiliateId) || !SysId.IsValid(userContext.OrganisationAssociationId))
return ownerOptions;
ItemId affiliateItemId_Owner = new ItemId((short)Sbm.ItemType.Affiliate, userContext.OrganisationAffiliateId);
ItemId associationItemId_Internal = new ItemId((short)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<SaItemOption>(ownerItemOptions.ItemOptions);
}
return ownerOptions;
}
//-------------------------------------------------------------------
}
//=======================================================================
}