using Sarand.BusinessMgr.Net.Standard.Base.App; using Sarand.BusinessMgr.Net.Standard.Base.Bus.Control; using Sarand.BusinessMgr.Net.Standard.Base.Core; 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.Linq; using System.Text.RegularExpressions; namespace Sarand.BusinessMgr.Net.Core.Web.App { //======================================================================= public class SbmInstanceConfig_Control : SbmInstanceConfig { //------------------------------------------------------------------- // Definitions. protected const String ParamContext_OptionAllowed = "OptionAllowed"; protected const String ParamContext_AffiliateTypeAllowed = "AffiliateTypeAllowed"; protected const String ParamContext_AssociationTypeAllowed = "AssociationTypeAllowed"; protected const String ParamContext_TransactionTypeAllowed = "TransactionTypeAllowed"; protected const String ParamContext_TransactionCreation = "TransactionCreation"; protected const String ParamContext_TransactionReferencePrefix = "TransactionReferencePrefix"; protected const String ParamContext_StockControl = "StockControl"; protected const String ParamContext_InterfaceCategoryAllowed = "InterfaceCategoryAllowed"; protected const String ParamContext_InterfaceTypeAllowed = "InterfaceTYpeAllowed"; protected const String ParamContext_DefaultValue = "DefaultValue"; protected const String ParamContext_ColumnChangeLogged = "ColumnChangeLogged"; //------------------------------------------------------------------- // 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 { AffiliateTypeAllowed, AssociationTypeAllowed, TransactionTypeAllowed, ApplicationOptionAllowed, } public class ConfigParamData { public ConfigParamType ConfigParam { get; set; } public String Context { get; set; } public String Name { get; set; } public HashSet Values { get; } = new HashSet(); public bool Password { get; set; } } //------------------------------------------------------------------- // Populate Repository Configuration parameters. private void PopulateControlParams() { using (DisposableCollection disposables = new DisposableCollection()) { 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 disposables = new DisposableCollection()) { // Query item params. SbmcItemParam.Params prmParam = new SbmcItemParam.Params(); prmParam.ItemIdsIncluded.Add(paramItemId); prmParam.StatusCat = SbmcItemParam.StatusCat.ActiveOnly; List itemParams = controlItemMgr.GetItemParamList(prmParam); foreach (SbmcItemParam itemParam in itemParams) PopulateControlParam(itemParam); } } private void PopulateControlParam(SbmcItemParam itemParam) { if (itemParam.Context.EqualsCI(ParamContext_OptionAllowed)) { // Application options. Sbm.ConfigOption option = EnumUtil.ParseEnum(itemParam.Name, Sbm.ConfigOption.Unknown); if (option != Sbm.ConfigOption.Unknown) { if (StringUtil.ToBool(itemParam.Value)) ApplicationOptions_Allowed.Add(option); else ApplicationOptions_Disallowed.Add(option); } else SaLogger.Warn($"{SysUtil.Fn} Unrecognised application 'Option Allowed': {itemParam.Name}."); } else if (itemParam.Context.EqualsCI(ParamContext_AffiliateTypeAllowed)) { // Affiliate types. Sbm.AffiliateType affiliateType = EnumUtil.ParseEnum(itemParam.Name, Sbm.AffiliateType.Unknown); if (affiliateType != Sbm.AffiliateType.Unknown) { if (StringUtil.ToBool(itemParam.Value)) AffiliateTypes_Allowed.Add(affiliateType); else AffiliateTypes_Disallowed.Add(affiliateType); } else SaLogger.Warn($"{SysUtil.Fn} Unrecognised Affiliate type: {itemParam.Name}."); } else if (itemParam.Context.EqualsCI(ParamContext_AssociationTypeAllowed)) { // Association types. Sbm.AssociationType associationType = EnumUtil.ParseEnum(itemParam.Name, Sbm.AssociationType.Unknown); if (associationType != Sbm.AssociationType.Unknown) { if (StringUtil.ToBool(itemParam.Value)) AssociationTypes_Allowed.Add(associationType); else AssociationTypes_Disallowed.Add(associationType); } else SaLogger.Warn($"{SysUtil.Fn} Unrecognised Association type: {itemParam.Name}."); } else if (itemParam.Context.EqualsCI(ParamContext_TransactionTypeAllowed)) { // Transaction types. Sbm.TransactionType transactionType = EnumUtil.ParseEnum(itemParam.Name, Sbm.TransactionType.Unknown); if (transactionType != Sbm.TransactionType.Unknown) { if (StringUtil.ToBool(itemParam.Value)) TransactionTypes_Allowed.Add(transactionType); else TransactionTypes_Disallowed.Add(transactionType); } else SaLogger.Warn($"{SysUtil.Fn} Unrecognised Transaction type: {itemParam.Name}."); } else if (itemParam.Context.EqualsCI(ParamContext_TransactionCreation)) { // Transaction Creation. } else if (itemParam.Context.EqualsCI(ParamContext_TransactionReferencePrefix)) { // Transaction Reference Prefix. Sbm.TransactionType transactionType = EnumUtil.ParseEnum(itemParam.Name, Sbm.TransactionType.Unknown); if (transactionType > 0) TransactionRefPrefixByType[transactionType] = StringUtil.TrimWhitespace(itemParam.Value); else SaLogger.Debug($"{SysUtil.Fn} Unrecognised Transaction Type: '{itemParam.Name}'. Context: {itemParam.Context}."); } else if (itemParam.Context.EqualsCI(ParamContext_StockControl)) { // Stock Control. } else if (itemParam.Context.EqualsCI(ParamContext_InterfaceCategoryAllowed)) { // Interface Category. } else if (itemParam.Context.EqualsCI(ParamContext_InterfaceTypeAllowed)) { // Interface Type. } else if (itemParam.Context.EqualsCI(ParamContext_DefaultValue)) { // Default Value. if (itemParam.Name.EqualsCI("CommodityType")) { Sbm.CommodityType commodityType = EnumUtil.ParseEnum(itemParam.Value, Sbm.CommodityType.Unknown); if (commodityType > 0) Defaults.CommodityType = commodityType; else SaLogger.Debug($"{SysUtil.Fn} Unrecognised Default Commodity Type: '{itemParam.Value}'. Context: {itemParam.Context}. Name: {itemParam.Name}."); } else SaLogger.Debug($"{SysUtil.Fn} Unrecognised Default Value. Context: {itemParam.Context}. Name: {itemParam.Name}."); } else if (itemParam.Context.EqualsCI(ParamContext_ColumnChangeLogged)) { // Update Log. SbmDbDfn.ColumnIdx column = EnumUtil.ParseEnum(itemParam.Name, SbmDbDfn.ColumnIdx.Unknown); if (column != SbmDbDfn.ColumnIdx.Unknown) { if (StringUtil.ToBool(itemParam.Value)) ColumnChangeLogged_Allowed.Add(column); else ColumnChangeLogged_Disallowed.Add(column); } else SaLogger.Debug($"{SysUtil.Fn} Unrecognised Change Log column. Context: {itemParam.Context}. Name: {itemParam.Name}."); } else SaLogger.Debug($"{SysUtil.Fn} Unrecognised option. Context: {itemParam.Context}. Name: {itemParam.Name}."); } //------------------------------------------------------------------- // Configuration rules. //------------------------------------------------------------------- // Application options. // Supported application options. private readonly HashSet ApplicationOptions_Allowed = new HashSet(); private readonly HashSet ApplicationOptions_Disallowed = new HashSet(); 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; } //------------------------------------------------------------------- // Defaults. private ISbmInstanceConfig.DefaultsData Defaults { get; } = new ISbmInstanceConfig.DefaultsData(); public override ISbmInstanceConfig.DefaultsData GetDefaults() { return Defaults; } //------------------------------------------------------------------- // Contacts. // Supported Affiliate types. private readonly HashSet AffiliateTypes_Allowed = new HashSet(); private readonly HashSet AffiliateTypes_Disallowed = new HashSet(); 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 Association types. private readonly HashSet AssociationTypes_Allowed = new HashSet(); private readonly HashSet AssociationTypes_Disallowed = new HashSet(); 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; } //------------------------------------------------------------------- // Transactions. // Supported Transaction types. private readonly HashSet TransactionTypes_Allowed = new HashSet(); private readonly HashSet TransactionTypes_Disallowed = new HashSet(); 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; } // Transaction reference sequence length. private readonly Dictionary TransactionRefSeqLengthByType = new Dictionary(); public override int GetTransactionRefSeqLength(SysId affiliateId_Owner, SysId idAssoc, Sbm.TransactionType transactionType, int defaultResponse) { if (TransactionRefSeqLengthByType.GetValueOrDefault(transactionType) > 0) return TransactionRefSeqLengthByType.GetValueOrDefault(transactionType); return defaultResponse; } // Transaction reference prefix by type. private readonly Dictionary TransactionRefPrefixByType = new Dictionary(); public override String GetTransactionRefPrefix(SysId affiliateId_Owner, SysId idAssoc, Sbm.TransactionType transactionType, String defaultResponse) { return StringUtil.OnBlank(TransactionRefPrefixByType.GetValueOrDefault(transactionType), defaultResponse); } //------------------------------------------------------------------- // Interfaces. private readonly HashSet InterfaceCategories_Allowed = new HashSet(); private readonly HashSet InterfaceCategories_Disallowed = new HashSet(); /// /// Whether the Interface Category is allowed. /// public override bool AllowInterfaceCategory(SaInterfaceCategory interfaceCategory, bool defaultResponse) { if (InterfaceCategories_Disallowed.Contains(interfaceCategory)) return false; if (InterfaceCategories_Allowed.Contains(interfaceCategory)) return true; return defaultResponse; } //------------------------------------------------------------------- // Update Log. private readonly HashSet ColumnChangeLogged_Allowed = new HashSet(); private readonly HashSet ColumnChangeLogged_Disallowed = new HashSet(); /// /// Are changes to values in the specified database column recorded in the Change Log. /// public override bool IsColumnChangeLogged(SbmDbDfn.ColumnIdx column, bool defaultResponse) { if (ColumnChangeLogged_Disallowed.Any(item => item == column)) return false; if (ColumnChangeLogged_Allowed.Any(item => item == column)) return true; return false; } //------------------------------------------------------------------- } //======================================================================= }