Files
BusinessMgr-2019/SaBusMgrStandardBase/Base/App/SbmSysParamCache.cs
T
2021-05-05 21:00:33 +01:00

148 lines
5.1 KiB
C#

using Sarand.BusinessMgr.Net.Standard.Base.Bus;
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<SbmSysParam> userSysParams = new List<SbmSysParam>();
//-------------------------------------------------------------------
// Protected Construction - use derived objects.
public SbmSysParamCache(SbmUserContext userContext)
{
this.UserContext = userContext;
}
//-------------------------------------------------------------------
// Properties.
public SbmUserContext UserContext { get; }
public SysId AffiliateIdUser { get; private set; }
//-------------------------------------------------------------------
/// <summary>Retrieve the value for the user parameter.</summary>
public String GetParamValue(String name, SysId affiliateIdTarget = null, SysId associationId = null)
{
if (!SysId.IsValid(UserContext.User.AffiliateId)) return "";
return GetParam(name, affiliateIdTarget, associationId)?.Value ?? "";
}
//-------------------------------------------------------------------
/// <summary>Store value against user parameter.</summary>
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;
}
//-------------------------------------------------------------------
/// <summary>Extract parameter from the cache list.</summary>
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));
}
/// <summary>Extract parameter from the cache list or retrieve from database if not present.</summary>
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;
}
//-------------------------------------------------------------------
/// <summary>Store the params cached for the current user.</summary>
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;
}
//-------------------------------------------------------------------
/// <summary>Clear the params cached for the current user.</summary>
public void Clear()
{
AffiliateIdUser = null;
ListUtil.ClearAndDispose(userSysParams);
}
//-------------------------------------------------------------------
// Data managers.
protected SbmAccountMgr CreateAccountMgr()
{
return SbmGlobals.System.CreateAccountMgr(UserContext);
}
//-------------------------------------------------------------------
}
//=======================================================================
}