Files
BusinessMgr-2019/SaBusMgrCoreWeb/Web/App/SbmInstanceConfig_Control.cs
T

224 lines
9.8 KiB
C#

using Sarand.BusinessMgr.Net.Standard.Base.App;
using Sarand.BusinessMgr.Net.Standard.Base.Bus.Control;
using Sarand.BusinessMgr.Net.Standard.Base.Mgr.Control;
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.Text.RegularExpressions;
namespace Sarand.BusinessMgr.Net.Core.Web.App
{
//=======================================================================
public class SbmInstanceConfig_Control : SbmInstanceConfig
{
//-------------------------------------------------------------------
// Definitions.
protected const String ParamContext_Config = "Config";
protected const String ParamContext_ConfigPrefix = "Config-";
protected const String ApplicationOptionValue_Allow = "Allow";
protected const String ApplicationOptionValue_Disallow = "Disallow";
//-------------------------------------------------------------------
// Data members.
private readonly SysId controlOwnerId;
private readonly SysId controlRepositoryId;
//-------------------------------------------------------------------
// Construction.
public SbmInstanceConfig_Control(SysId controlOwnerId, SysId controlRepositoryId)
{
this.controlOwnerId = controlOwnerId;
this.controlRepositoryId = controlRepositoryId;
PopulateControlParams();
}
//-------------------------------------------------------------------
// Owner/Instance configuration parameters.
public enum ConfigParamType
{
// Affiliate types.
AllowedAffiliateTypes,
DisallowedAffiliateTypes,
// Association types.
AllowedAssociationTypes,
DisallowedAssociationTypes,
// Transaction types.
AllowedTransactionTypes,
DisallowedTransactionTypes,
// Application options.
ApplicationOption,
}
public class ConfigParamData
{
public ConfigParamType ConfigParam { get; set; }
public String Context { get; set; }
public String Name { get; set; }
public HashSet<String> Values { get; } = new HashSet<string>();
public bool Password { get; set; }
}
public bool IsConfigParamOfType(ConfigParamType configParam, SbmcItemParam itemParam)
{
switch (configParam)
{
// Affiliate types.
case ConfigParamType.AllowedAffiliateTypes:
case ConfigParamType.DisallowedAffiliateTypes:
// Association types.
case ConfigParamType.AllowedAssociationTypes:
case ConfigParamType.DisallowedAssociationTypes:
// Transaction types.
case ConfigParamType.AllowedTransactionTypes:
case ConfigParamType.DisallowedTransactionTypes:
return itemParam.Context.EqualsCI(ParamContext_Config) && itemParam.Name.EqualsCI(configParam.ToString());
// Application options.
case ConfigParamType.ApplicationOption:
return itemParam.Context.EqualsCI($"{ParamContext_ConfigPrefix}{ConfigParamType.ApplicationOption.ToString()}");
}
return false;
}
public ConfigParamData GetConfigParamData(ConfigParamType configParam)
{
switch (configParam)
{
// Affiliate types.
case ConfigParamType.AllowedAffiliateTypes:
case ConfigParamType.DisallowedAffiliateTypes:
// Association types.
case ConfigParamType.AllowedAssociationTypes:
case ConfigParamType.DisallowedAssociationTypes:
// Transaction types.
case ConfigParamType.AllowedTransactionTypes:
case ConfigParamType.DisallowedTransactionTypes:
return new ConfigParamData() { ConfigParam = configParam, Context = ParamContext_Config, Name = configParam.ToString() };
}
return null;
}
public ConfigParamData GetAllowOptionConfigParamData(Sbm.ConfigOption option)
{
ConfigParamData configParamData = new ConfigParamData() { ConfigParam = ConfigParamType.ApplicationOption, Context = $"{ParamContext_ConfigPrefix}{ConfigParamType.ApplicationOption.ToString()}", Name = option.ToString() };
configParamData.Values.UnionWith(new List<String>() { ApplicationOptionValue_Allow, ApplicationOptionValue_Disallow });
return configParamData;
}
//-------------------------------------------------------------------
// Populate Repository Configuration parameters.
private void PopulateControlParams()
{
using (DisposableCollection<SacsDisposable> disposables = new DisposableCollection<SacsDisposable>())
{
SbmcItemMgr controlItemMgr = SbmGlobals_Web.System.CreateControlItemMgr();
if (controlItemMgr != null)
{
disposables.Add(controlItemMgr);
PopulateControlParams(controlItemMgr, new ItemId((short)SbmcHierarchyItem.ItemType.Owner, controlOwnerId));
PopulateControlParams(controlItemMgr, new ItemId((short)SbmcHierarchyItem.ItemType.Repository, controlRepositoryId));
}
}
}
private void PopulateControlParams(SbmcItemMgr controlItemMgr, ItemId paramItemId)
{
if (!ItemId.IsValid(paramItemId)) return;
using (DisposableCollection<SacsHierarchyItem> disposables = new DisposableCollection<SacsHierarchyItem>())
{
// Query item params.
SbmcItemParam.Params prmParam = new SbmcItemParam.Params();
prmParam.ItemIdsIncluded.Add(paramItemId);
prmParam.StatusCat = SbmcItemParam.StatusCat.ActiveOnly;
List<SbmcItemParam> itemParams = controlItemMgr.GetItemParamList(prmParam);
disposables.AddRange(itemParams);
foreach (SbmcItemParam itemParam in itemParams)
PopulateControlParam(itemParam);
}
}
private void PopulateControlParam(SbmcItemParam itemParam)
{
// Affiliate types.
if (IsConfigParamOfType(ConfigParamType.AllowedAffiliateTypes, itemParam))
AffiliateTypes_Allowed.UnionWith(EnumUtil.ParseEnums<Sbm.AffiliateType>(Regex.Split(itemParam.Value, "[\\s,;\\-]+")));
else if (IsConfigParamOfType(ConfigParamType.DisallowedAffiliateTypes, itemParam))
AffiliateTypes_Disallowed.UnionWith(EnumUtil.ParseEnums<Sbm.AffiliateType>(Regex.Split(itemParam.Value, "[\\s,;\\-]+")));
// Association types.
else if (IsConfigParamOfType(ConfigParamType.AllowedAssociationTypes, itemParam))
AssociationTypes_Allowed.UnionWith(EnumUtil.ParseEnums<Sbm.AssociationType>(Regex.Split(itemParam.Value, "[\\s,;\\-]+")));
else if (IsConfigParamOfType(ConfigParamType.DisallowedAssociationTypes, itemParam))
AssociationTypes_Disallowed.UnionWith(EnumUtil.ParseEnums<Sbm.AssociationType>(Regex.Split(itemParam.Value, "[\\s,;\\-]+")));
// Transaction types.
else if (IsConfigParamOfType(ConfigParamType.AllowedTransactionTypes, itemParam))
TransactionTypes_Allowed.UnionWith(EnumUtil.ParseEnums<Sbm.TransactionType>(Regex.Split(itemParam.Value, "[\\s,;\\-]+")));
else if (IsConfigParamOfType(ConfigParamType.DisallowedTransactionTypes, itemParam))
TransactionTypes_Disallowed.UnionWith(EnumUtil.ParseEnums<Sbm.TransactionType>(Regex.Split(itemParam.Value, "[\\s,;\\-]+")));
// Application options.
else if (IsConfigParamOfType(ConfigParamType.ApplicationOption, itemParam))
{
Sbm.ConfigOption option = EnumUtil.ParseEnum(itemParam.Name, Sbm.ConfigOption.Unknown);
if (option != Sbm.ConfigOption.Unknown)
{
if (itemParam.Value.EqualsCI(ApplicationOptionValue_Allow))
ApplicationOptions_Allowed.Add(option);
else if (itemParam.Value.EqualsCI(ApplicationOptionValue_Disallow))
ApplicationOptions_Disallowed.Add(option);
else
SaLogger.Warn($"{SysUtil.Fn} Unrecognised application option value: {itemParam.Value}. Option: {option}.");
}
else
SaLogger.Warn($"{SysUtil.Fn} Unrecognised application option: {itemParam.Name}.");
}
}
//-------------------------------------------------------------------
// Allowed data component types.
//-------------------------------------------------------------------
// Supported affiliate types.
private readonly HashSet<Sbm.AffiliateType> AffiliateTypes_Allowed = new HashSet<Sbm.AffiliateType>();
private readonly HashSet<Sbm.AffiliateType> AffiliateTypes_Disallowed = new HashSet<Sbm.AffiliateType>();
public override bool AllowAffiliateType(Sbm.AffiliateType affiliateType, bool defaultResponse)
{
if (AffiliateTypes_Disallowed.Contains(affiliateType)) return false;
if (AffiliateTypes_Allowed.Contains(affiliateType)) return true;
return defaultResponse;
}
// Supported affiliate types.
private readonly HashSet<Sbm.AssociationType> AssociationTypes_Allowed = new HashSet<Sbm.AssociationType>();
private readonly HashSet<Sbm.AssociationType> AssociationTypes_Disallowed = new HashSet<Sbm.AssociationType>();
public override bool AllowAssociationType(Sbm.AssociationType affiliateType, bool defaultResponse)
{
if (AssociationTypes_Disallowed.Contains(affiliateType)) return false;
if (AssociationTypes_Allowed.Contains(affiliateType)) return true;
return defaultResponse;
}
// Supported affiliate types.
private readonly HashSet<Sbm.TransactionType> TransactionTypes_Allowed = new HashSet<Sbm.TransactionType>();
private readonly HashSet<Sbm.TransactionType> TransactionTypes_Disallowed = new HashSet<Sbm.TransactionType>();
public override bool AllowTransactionType(Sbm.TransactionType affiliateType, bool defaultResponse)
{
if (TransactionTypes_Disallowed.Contains(affiliateType)) return false;
if (TransactionTypes_Allowed.Contains(affiliateType)) return true;
return defaultResponse;
}
//-------------------------------------------------------------------
// Supported application options.
private readonly HashSet<Sbm.ConfigOption> ApplicationOptions_Allowed = new HashSet<Sbm.ConfigOption>();
private readonly HashSet<Sbm.ConfigOption> ApplicationOptions_Disallowed = new HashSet<Sbm.ConfigOption>();
public override bool AllowApplicationOption(Sbm.ConfigOption option, bool defaultResponse)
{
if (ApplicationOptions_Disallowed.Contains(option)) return false;
if (ApplicationOptions_Allowed.Contains(option)) return true;
return defaultResponse;
}
//-------------------------------------------------------------------
}
//=======================================================================
}