using Sarand.BusinessMgr.Net.Standard.Base.Bus; using Sarand.BusinessMgr.Net.Standard.Base.Core; using Sarand.BusinessMgr.Net.Standard.Base.Mgr; using Sarand.Net.Standard.Base.App; using Sarand.Net.Standard.Base.Bus; using Sarand.Net.Standard.Base.Utils; using System; using System.Collections.Generic; namespace Sarand.BusinessMgr.Net.Standard.Base.App { //======================================================================= public class SbmSysParamCache { //------------------------------------------------------------------- // Data members. protected readonly List userSysParams = new List(); //------------------------------------------------------------------- // Protected Construction - use derived objects. public SbmSysParamCache(ISbmUserContext userContext) { this.UserContext = userContext; } //------------------------------------------------------------------- // Properties. public ISbmUserContext UserContext { get; } public SysId AffiliateIdUser { get; private set; } //------------------------------------------------------------------- /// Retrieve the value for the user parameter. public String GetParamValue(String name, SysId affiliateIdTarget = null, SysId associationId = null) { if (!SysId.IsValid(UserContext.User.AffiliateId)) return ""; return GetParam(name, affiliateIdTarget, associationId)?.Value ?? ""; } //------------------------------------------------------------------- /// Store value against user parameter. public bool SetSessionParam(String name, String value, SysId affiliateIdTarget = null, SysId associationId = null) { bool success = false; if (!SysId.IsValid(UserContext.User.AffiliateId)) return false; SbmSysParam sysParam = GetParam(name, affiliateIdTarget, associationId); if (sysParam != null) { // Update the value. sysParam.Value = value; success = true; } return success; } //------------------------------------------------------------------- /// Extract parameter from the cache list. private SbmSysParam FindParam(String name, SysId affiliateIdTarget = null, SysId associationId = null) { return userSysParams.Find (item => item.Category == Sbm.SysParamCategory.UserParam && item.Name.EqualsCI(name) && item.AffiliateIdOwner.Equals(UserContext.User.AffiliateId) && SysId.ToInt(item.AffiliateIdTarget) == SysId.ToInt(affiliateIdTarget) && SysId.ToInt(item.AssociationId) == SysId.ToInt(associationId)); } /// Extract parameter from the cache list or retrieve from database if not present. private SbmSysParam GetParam(String name, SysId affiliateIdTarget = null, SysId associationId = null) { SbmSysParam sysParam = FindParam(name, affiliateIdTarget, associationId); if (sysParam == null) { using (SbmAccountMgr accountMgr = CreateAccountMgr()) { // Retrieve a matching parameter from the database. sysParam = accountMgr.GetSysParam(Sbm.SysParamCategory.UserParam, name, UserContext.User.AffiliateId, affiliateIdTarget, associationId); if (sysParam == null) { // Parameter not found - create a new one. sysParam = accountMgr.CreateSysParam(Sbm.SysParamCategory.UserParam, name); if (sysParam != null) { sysParam.AffiliateIdOwner = UserContext.User.AffiliateId; sysParam.AffiliateIdTarget = affiliateIdTarget; sysParam.AssociationId = associationId; } } if (sysParam != null) { // Store parameter in the cache list. userSysParams.Add(sysParam); } } } return sysParam; } //------------------------------------------------------------------- /// Store the params cached for the current user. public bool Store() { String fn = GetType().Name + ".Store"; bool success = true; using (SbmAccountMgr accountMgr = CreateAccountMgr()) { foreach (SbmSysParam sysParam in userSysParams) { if (!SysId.IsValid(sysParam.Id)) { // Parameter doesn't yet have a valid Id - create a new one. if (!accountMgr.InsertSysParam(sysParam, UserContext.DbUpdateInfo)) { success = false; SaLogger.Warn(fn + " Failed to add parameter - " + accountMgr.ErrorStateMessage); } } else { // Parameter already has a valid Id - update it. if (!accountMgr.UpdateSysParam(sysParam, UserContext.DbUpdateInfo)) { success = false; SaLogger.Warn(fn + " Failed to update parameter - " + accountMgr.ErrorStateMessage); } } } } return success; } //------------------------------------------------------------------- /// Clear the params cached for the current user. public void Clear() { AffiliateIdUser = null; ListUtil.ClearAndDispose(userSysParams); } //------------------------------------------------------------------- // Data managers. protected SbmAccountMgr CreateAccountMgr() { return SbmGlobals.System.CreateAccountMgr(UserContext); } //------------------------------------------------------------------- } //======================================================================= }