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.Core; using Sarand.Net.Standard.Base.Dbas; using Sarand.Net.Standard.Base.ItemOptions; using Sarand.Net.Standard.Base.Utils; using System; using System.Collections.Generic; namespace Sarand.BusinessMgr.Net.Standard.Base.App { //======================================================================= public class SbmUserContext { //------------------------------------------------------------------- // Definitions. // User profile. public class UserData { protected UserData() { } protected readonly List supervisedOwners = new List(); /// User Security Item Id. public SysId SecItemId { get; protected set; } = new SysId(); /// User Affiliate Id. public SysId AffiliateId { get; protected set; } = new SysId(); /// User display name. public String DisplayName { get; protected set; } = ""; /// User managed associations - Id and display name. public List SupervisedOwners { get { return new List(supervisedOwners); } } } private class UserData_Internal : UserData { internal UserData_Internal() { } internal void Clear() { SecItemId = new SysId(); AffiliateId = new SysId(); DisplayName = ""; supervisedOwners.Clear(); } internal void Refresh(SysId securityItemId, SysId affiliateId, String displayName, List supervisedOwners) { SecItemId = SysId.InvalidOnNull(securityItemId); AffiliateId = SysId.InvalidOnNull(affiliateId); DisplayName = StringUtil.EmptyOnNull(displayName).TrimAll(); this.supervisedOwners.Clear(); this.supervisedOwners.AddRange(supervisedOwners); } } // Owner profile - selected organisation to manage. public class OrganisationData { internal OrganisationData() { } // Organisation options. internal readonly SaItemOption.Cache configOptionsCache = new SaItemOption.Cache(); /// Organisation Affiliate Id. public SysId AffiliateId { get; protected set; } = new SysId(); /// Organisation Affiliate Type. public Sbm.AffiliateType AffiliateType { get; protected set; } = Sbm.AffiliateType.Unknown; /// Organisation display name. public String DisplayName { get; protected set; } = ""; /// Organisation Interface context - to qualify interfaces belonging to the organisation. public String InterfaceContext { get; protected set; } = ""; /// Organisation 'Internal' Association Id. public SysId AssociationId { get; protected set; } = new SysId(); /// Organisation configuration options public List ConfigOptions { get { return new List(configOptionsCache.ItemOptions); } } } private class OrganisationData_Internal : OrganisationData { internal OrganisationData_Internal() { } internal void Clear() { AffiliateId = new SysId(); AffiliateType = Sbm.AffiliateType.Unknown; DisplayName = ""; InterfaceContext = ""; configOptionsCache.Clear(); } internal void Refresh(SbmAffiliate affiliateOrganisation, SbmAssociation associationInternal, String interfaceContext) { AffiliateId = SysId.InvalidOnNull(affiliateOrganisation.Id); AffiliateType = affiliateOrganisation.AffiliateType; DisplayName = StringUtil.EmptyOnNull(affiliateOrganisation.GetDisplayName()).TrimAll(); InterfaceContext = StringUtil.EmptyOnNull(interfaceContext); AssociationId = associationInternal.Id; } } //------------------------------------------------------------------- // Data members. private readonly UserData_Internal userData = new UserData_Internal(); private readonly OrganisationData_Internal organisationData; private List assocOptions; private List contextValues; private List contextFolders; private List contextAllowOptions; private List contextCaptions; private List contextShowColumns; private List contextShowFields; private List contextFieldLabels; private List contextMinutes; private List contextPeriodDays; private List contextPeriodWeeks; private List contextNumericDecimals; private List contextCommodityTypes; private List commodityTypes; private List contextSalesLocations; //------------------------------------------------------------------- // Construction. protected SbmUserContext(SbmSessionContext sessionContext) { SessionContext = sessionContext; organisationData = new OrganisationData_Internal(); SecurityProfile = new SbmSecurityProfile(this, true); DbUpdateInfo = new DbUpdateInfo(User.AffiliateId, User.DisplayName, SacsSystem.ComputerName); } //------------------------------------------------------------------- // Properties. public SbmSessionContext SessionContext { get; protected set; } public UserData User { get { return userData; } } public bool ValidUser { get { return SysId.IsValid(userData.SecItemId) && SysId.IsValid(userData.AffiliateId); } } public OrganisationData Organisation { get { return organisationData; } } public DbUpdateInfo DbUpdateInfo { get; private set; } //------------------------------------------------------------------- // Security profile. public SbmSecurityProfile SecurityProfile { get; protected set; } //------------------------------------------------------------------- // Organisation configuration. public String GetOrganisationOption(Sbm.OrganisationOption option, String context) { if (assocOptions == null) assocOptions = SbmSystem.GetCodesOfType(this, Sbm.SystemCode.AssociationOption); String optionName = assocOptions.GetCodeForType(option); return GetOrganisationOption(optionName, context); } public String GetOrganisationOption(String optionName, String context) { // Retrieve the requested value from the cache. return Organisation.configOptionsCache.ItemOptions.GetOptionValue(optionName, context); } public String GetOwnerOption_Value(Sbm.OrgOption_Value eCol) { if (contextValues == null) contextValues = SbmSystem.GetCodesOfType(this, Sbm.SystemCode.OrgOption_Value); String context = contextValues.GetCodeForType(eCol); return GetOrganisationOption(Sbm.OrganisationOption.Value, context).Trim(); } public String GetOwnerOption_Folder(Sbm.OrgOption_Folder eCol) { if (contextFolders == null) contextFolders = SbmSystem.GetCodesOfType(this, Sbm.SystemCode.OrgOption_Folder); String context = contextFolders.GetCodeForType(eCol); return GetOrganisationOption(Sbm.OrganisationOption.Folder, context).Trim(); } public bool GetOwnerOption_AllowOption(Sbm.OrgOption_AllowOption eCol) { if (contextAllowOptions == null) contextAllowOptions = SbmSystem.GetCodesOfType(this, Sbm.SystemCode.OrgOption_AllowOption); String context = contextAllowOptions.GetCodeForType(eCol); String value = GetOrganisationOption(Sbm.OrganisationOption.AllowOption, context).Trim(); return StringUtil.ToBool(value); } public String GetOwnerOption_Caption(Sbm.OrgOption_Caption eCol, String defaultValue) { if (contextCaptions == null) contextCaptions = SbmSystem.GetCodesOfType(this, Sbm.SystemCode.OrgOption_Caption); String context = contextCaptions.GetCodeForType(eCol); String value = GetOrganisationOption(Sbm.OrganisationOption.Caption, context).Trim(); return (!value.IsEmpty() ? value : defaultValue); } public bool GetOwnerOption_ShowField(SbmDbDfn.ColumnIdx eCol) { if (contextShowFields == null) contextShowFields = SbmSystem.GetCodesOfType(this, Sbm.SystemCode.OrgOption_ShowField); String context = contextShowFields.GetCodeForType(eCol); if (context.IsEmpty()) return true; // Automatically show fields that are not in the 'Show Field' list. String value = GetOrganisationOption(Sbm.OrganisationOption.ShowField, context).Trim(); return StringUtil.ToBool(value); } public bool GetOwnerOption_ShowColumn(Sbm.OrgOption_ShowColumn eCol) { if (contextShowColumns == null) contextShowColumns = SbmSystem.GetCodesOfType(this, Sbm.SystemCode.OrgOption_ShowColumn); String context = contextShowColumns.GetCodeForType(eCol); String value = GetOrganisationOption(Sbm.OrganisationOption.ShowColumn, context).Trim(); return StringUtil.ToBool(value); } public String GetOwnerOption_FieldLabel(SbmDbDfn.ColumnIdx eCol, String sDefault) { if (contextFieldLabels == null) contextFieldLabels = SbmSystem.GetCodesOfType(this, Sbm.SystemCode.OrgOption_FieldLabel); String context = contextFieldLabels.GetCodeForType(eCol); String value = GetOrganisationOption(Sbm.OrganisationOption.FieldLabel, context).Trim(); return (!value.IsEmpty() ? value : sDefault); // Return default if no label defined. } public int GetOwnerOption_Minutes(Sbm.OrgOption_Minutes eLimit, int iDefault) { if (contextMinutes == null) contextMinutes = SbmSystem.GetCodesOfType(this, Sbm.SystemCode.OrgOption_Minutes); String context = contextMinutes.GetCodeForType(eLimit); String value = GetOrganisationOption(Sbm.OrganisationOption.Minutes, context).Trim(); if (!value.IsEmpty()) { int iUpdateLimit = value.ToShort(); if (iUpdateLimit > 0) return iUpdateLimit; } return iDefault; } public int GetOwnerOption_UpdateLimit(Sbm.OrgOption_PeriodDays eLimit, int iDefault) { if (contextPeriodDays == null) contextPeriodDays = SbmSystem.GetCodesOfType(this, Sbm.SystemCode.OrgOption_PeriodDays); String context = contextPeriodDays.GetCodeForType(eLimit); String value = GetOrganisationOption(Sbm.OrganisationOption.PeriodDays, context).Trim(); if (!value.IsEmpty()) { int iUpdateLimit = value.ToShort(); if (iUpdateLimit > 0) return iUpdateLimit; } return iDefault; } public int GetOwnerOption_UpdateLimit(Sbm.OrgOption_PeriodWeeks eLimit, int iDefault) { if (contextPeriodWeeks == null) contextPeriodWeeks = SbmSystem.GetCodesOfType(this, Sbm.SystemCode.OrgOption_PeriodWeeks); String context = contextPeriodWeeks.GetCodeForType(eLimit); String value = GetOrganisationOption(Sbm.OrganisationOption.PeriodWeeks, context).Trim(); if (!value.IsEmpty()) { int iUpdateLimit = value.ToShort(); if (iUpdateLimit > 0) return iUpdateLimit; } return iDefault; } public int GetOwnerOption_NumericDecimals(Sbm.OrgOption_NumericDecimals eNumDecimals, int iDefault = 2) { if (contextNumericDecimals == null) contextNumericDecimals = SbmSystem.GetCodesOfType(this, Sbm.SystemCode.OrgOption_NumericDecimals); String context = contextNumericDecimals.GetCodeForType(eNumDecimals); String value = GetOrganisationOption(Sbm.OrganisationOption.NumericDecimals, context).Trim(); int iNumDecimals = iDefault; if (!value.IsEmpty()) iNumDecimals = value.ToShort(); iNumDecimals = Math.Min(4, Math.Max(0, iNumDecimals)); return iNumDecimals; } public Sbm.CommodityType GetOwnerOption_CommodityType(Sbm.OrgOption_CommodityType commodityTypeContext) { if (contextCommodityTypes == null) contextCommodityTypes = SbmSystem.GetCodesOfType(this, Sbm.SystemCode.OrgOption_CommodityType); if (commodityTypes == null) commodityTypes = SbmSystem.GetCodesOfType(this, Sbm.SystemCode.CommodityType); String context = contextCommodityTypes.GetCodeForType(commodityTypeContext); String value = GetOrganisationOption(Sbm.OrganisationOption.CommodityType, context).Trim(); Sbm.CommodityType eCmdType = (Sbm.CommodityType)commodityTypes.GetTypeForCode(value); return eCmdType; } public SysId GetOwnerOption_StockLocation(Sbm.OrgOption_StockLocation eLoc) { if (contextSalesLocations == null) contextSalesLocations = SbmSystem.GetCodesOfType(this, Sbm.SystemCode.OrgOption_StockLocation); String context = contextSalesLocations.GetCodeForType(eLoc); String value = GetOrganisationOption(Sbm.OrganisationOption.SalesLocation, context).Trim(); SysId idLoc = new SysId(); if (!StringUtil.IsEmpty(value)) idLoc = new SysId(StringUtil.ToInt(value)); return idLoc; } //------------------------------------------------------------------- // Modify properties - internal only. protected void SetUserData(SbmAffiliate affiliateUser, List supervisedOwners) { userData.Refresh(affiliateUser.UserId, affiliateUser.Id, affiliateUser.GetDisplayName(), supervisedOwners); DbUpdateInfo = new DbUpdateInfo(User.AffiliateId, User.DisplayName, SacsSystem.ComputerName); } protected void ClearUserData() { userData.Clear(); organisationData.Clear(); SetSecurityProfile(new SbmSecurityProfile(this, true)); DbUpdateInfo = new DbUpdateInfo(User.AffiliateId, User.DisplayName, SacsSystem.ComputerName); } protected void SetOrganisationData(SysId affiliateIdOrganisation) { String fn = GetType().Name + ".SetOrganisationData"; // Interface context for selected organisation. String interfaceContext = ""; if (SysId.IsValid(affiliateIdOrganisation)) interfaceContext = "ORG" + StringUtil.PadLeft(SysId.ToString(affiliateIdOrganisation), 5, '0'); SbmAffiliate affiliateOrganisation = null; SbmAssociation associationInternal = null; if (SysId.IsValid(affiliateIdOrganisation)) { using (SbmAccountMgr accountMgr = SbmGlobals.System.CreateAccountMgr(this)) { // Affiliate. affiliateOrganisation = accountMgr.GetAffiliateById(affiliateIdOrganisation); // 'Internal' association. associationInternal = accountMgr.GetAssociationByOwnerTypeTarget(affiliateIdOrganisation, Sbm.AssociationType.Internal, affiliateIdOrganisation); } } if (affiliateOrganisation != null && associationInternal != null) { // Refresh the organisation properties. organisationData.Refresh(affiliateOrganisation, associationInternal, interfaceContext); // Refresh organisation options if required. ItemId affiliateItemId_Owner = new ItemId((short)Sbm.ItemType.Affiliate, organisationData.AffiliateId); ItemId associationItemId_Owner = new ItemId((short)Sbm.ItemType.Association, organisationData.AssociationId); if (!organisationData.configOptionsCache.ItemIdRef.Id.Equals(affiliateItemId_Owner)) { using (SaItemOptionConsole itemOptionConsole = SbmGlobals.Application.CreateItemOptionConsole(this)) { organisationData.configOptionsCache.Clear(); if (itemOptionConsole != null) { // Retrieve the options for this association. organisationData.configOptionsCache.Reset(associationItemId_Owner, affiliateItemId_Owner); organisationData.configOptionsCache.ItemOptions.AddRange(itemOptionConsole.GetItemOptionsForItem(associationItemId_Owner)); SaLogger.Debug(fn + " Populated organisation options: " + organisationData.configOptionsCache.ItemOptions.Count); } } } } else ClearOrganisationData(); if (affiliateOrganisation != null) affiliateOrganisation.Dispose(); affiliateOrganisation = null; if (associationInternal != null) associationInternal.Dispose(); associationInternal = null; } protected void ClearOrganisationData() { organisationData.Clear(); } protected void SetSecurityProfile(SbmSecurityProfile securityProfile) { SecurityProfile = securityProfile; } //------------------------------------------------------------------- } //======================================================================= }