Files
BusinessMgr-2019/SaBusMgrStandardBase/Base/App/SbmSysParamCache.cs
T

237 lines
8.1 KiB
C#

using Org.BouncyCastle.Asn1.IsisMtt;
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.Core;
using Sarand.Net.Standard.Base.Utils;
using System;
using System.Collections.Generic;
using System.Xml.Linq;
namespace Sarand.BusinessMgr.Net.Standard.Base.App
{
//=======================================================================
public class SbmSysParamCache : SacsDisposable
{
//-------------------------------------------------------------------
// Definitions.
public enum ParamName
{
/// <summary>User's selected organisation.</summary>
SelectedCompany,
SelectedOwner = SelectedCompany,
DefaultView,
ApplicationSize,
LeftPanelWidth,
SelectedAssociation,
SelectedTransaction,
SelectedPortfolio,
SelectedPortfolioItem,
SelectedCommodity,
AssocMaintTab,
}
public static String ParamName_SelectedCompany = "SelectedCompany";
public static String ParamName_DefaultView = "DefaultView";
public static String ParamName_ApplicationSize = "AppSize";
public static String ParamName_LeftPanelWidth = "LeftPanelWidth";
public static String ParamName_SelectedAssociation = "SelectedAssociation";
public static String ParamName_SelectedTransaction = "SelectedTransaction";
public static String ParamName_SelectedPortfolio = "SelectedPortfolio";
public static String ParamName_SelectedPortfolioItem = "SelectedPortfolioItem";
public static String ParamName_SelectedCommodity = "SelectedCommodity";
public static String ParamName_AssocMaintTab = "AssocMaintTab";
//-------------------------------------------------------------------
// Data members.
protected readonly List<SbmSysParam> userSysParams = new List<SbmSysParam>();
//-------------------------------------------------------------------
// Construction.
public SbmSysParamCache(ISbmUserOrganisationContext userContext, SysId affiliateIdUser)
{
this.UserContext = userContext;
this.AffiliateIdUser = affiliateIdUser;
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
}
}
//-------------------------------------------------------------------
// Properties.
public ISbmUserOrganisationContext UserContext { get; }
public SysId AffiliateIdUser { get; }
public bool Modified { get; private set; }
//-------------------------------------------------------------------
// Utilities.
public static String GetParamName(ParamName paramName)
{
switch (paramName)
{
case ParamName.SelectedCompany:
return ParamName_SelectedCompany;
case ParamName.DefaultView:
return ParamName_DefaultView;
case ParamName.ApplicationSize:
return ParamName_ApplicationSize;
case ParamName.LeftPanelWidth:
return ParamName_LeftPanelWidth;
case ParamName.SelectedAssociation:
return ParamName_SelectedAssociation;
case ParamName.SelectedTransaction:
return ParamName_SelectedTransaction;
case ParamName.SelectedPortfolio:
return ParamName_SelectedPortfolio;
case ParamName.SelectedPortfolioItem:
return ParamName_SelectedPortfolioItem;
case ParamName.SelectedCommodity:
return ParamName_SelectedCommodity;
case ParamName.AssocMaintTab:
return ParamName_AssocMaintTab;
}
return "";
}
//-------------------------------------------------------------------
/// <summary>Retrieve the value for the user parameter.</summary>
public String GetUserParamValue(ParamName paramName, SysId affiliateIdTarget = null, SysId associationId = null)
{
return GetUserParamValue(GetParamName(paramName), affiliateIdTarget, associationId);
}
/// <summary>Retrieve the value for the user parameter.</summary>
public String GetUserParamValue(String name, SysId affiliateIdTarget = null, SysId associationId = null)
{
if (!SysId.IsValid(AffiliateIdUser)) return "";
return GetParam(name, affiliateIdTarget, associationId)?.Value ?? "";
}
//-------------------------------------------------------------------
/// <summary>Store value against user parameter.</summary>
public bool SetUserParamValue(ParamName paramName, String value, SysId affiliateIdTarget = null, SysId associationId = null)
{
return SetUserParamValue(GetParamName(paramName), value, affiliateIdTarget, associationId);
}
/// <summary>Store value against user parameter.</summary>
public bool SetUserParamValue(String name, String value, SysId affiliateIdTarget = null, SysId associationId = null)
{
bool success = false;
if (!SysId.IsValid(AffiliateIdUser)) return false;
SbmSysParam sysParam = GetParam(name, affiliateIdTarget, associationId);
if (sysParam != null)
{
if (!sysParam.Value.Equals(value))
{
// Update the value.
sysParam.Value = value;
Modified = true;
}
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(AffiliateIdUser) &&
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, AffiliateIdUser, affiliateIdTarget, associationId);
if (sysParam == null)
{
// Parameter not found - create a new one.
sysParam = accountMgr.CreateSysParam(Sbm.SysParamCategory.UserParam, name);
if (sysParam != null)
{
sysParam.AffiliateIdOwner = AffiliateIdUser;
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()
{
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($"{SysUtil.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($"{SysUtil.Fn} Failed to update parameter - {accountMgr.ErrorStateMessage}");
}
}
}
}
if (success) Modified = false;
return success;
}
//-------------------------------------------------------------------
/// <summary>Clear the params cached for the current user.</summary>
public void Clear()
{
ListUtil.ClearAndDispose(userSysParams);
Modified = false;
}
//-------------------------------------------------------------------
// Data managers.
protected SbmAccountMgr CreateAccountMgr()
{
return SbmGlobals.System.CreateAccountMgr(UserContext);
}
//-------------------------------------------------------------------
}
//=======================================================================
}