using Sarand.Net.Standard.Base.Bus; using Sarand.Net.Standard.Base.Core; using Sarand.Net.Standard.Base.Utils; using Saxonite.Net.Standard.Base.App; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; namespace Saxonite.Net.Standard.Base.Bus { //======================================================================= // UserOrganisation. //======================================================================= public abstract class SaxUserOrganisation : SaxDisposableHierarchyItem, IDisposable { //------------------------------------------------------------------- // Columns. public enum Column { Pos, Id, UserAccountId, OrganisationId, OrganisationRoleCount, OrganisationRoleCodes, OrganisationRoleDescs, // User. User_EmailAddress, User_DisplayName, User_Title, User_FirstName, User_LastName, User_Status, User_Status_Desc, // Organisation. Organisation_Name, Organisation_Category, Organisation_Type, Organisation_RegionId, Organisation_Region_Name, Organisation_SportId, Organisation_Sport_Name, Organisation_Member_Count, Organisation_Opportunity_Count, Organisation_LogoImage_Count, // Properties. AdminComment, // Associated data. // Common. Notes, Flags, Status, Status_Desc, Status_Ind, CreateOn_Date, UpdateOn_Date, } //------------------------------------------------------------------- /// Properties other than common. public class DataProperties { public SysId UserAccountId { get; set; } public SysId OrganisationId { get; set; } public HashSet OrganisationRoles { get; } = new HashSet(); public String AdminComment { get; set; } } //------------------------------------------------------------------- // Data. public new class Data : SacsHierarchyItem.Data { // Data members. private readonly DataProperties data = new DataProperties(); // Construction. public Data() { Status = DefaultStatus; } // Properties. public SysId UserAccountId { get { return data.UserAccountId; } set { CheckChanged(data.UserAccountId, value); data.UserAccountId = value; } } public SysId OrganisationId { get { return data.OrganisationId; } set { CheckChanged(data.OrganisationId, value); data.OrganisationId = value; } } // Organisation roles. public byte OrganisationRoleCount { get { return (byte)data.OrganisationRoles.Count; } } public ReadOnlyCollection GetOrganisationRoles() { return data.OrganisationRoles.ToList().AsReadOnly(); } public void ClearOrganisationRoles() { if (!Changed) Changed = !data.OrganisationRoles.IsEmpty(); data.OrganisationRoles.Clear(); } public bool HasAnyOrganisationRole(IEnumerable organisationRoles) { return data.OrganisationRoles.Any(item => organisationRoles.Contains(item)); } public void AddOrganisationRole(Sax.OrganisationRole organisationRole) { if (!data.OrganisationRoles.Contains(organisationRole)) { if (!Changed) Changed = true; data.OrganisationRoles.Add(organisationRole); } } /// Store organisation roles. public IEnumerable OrganisationRoles { set { if (Object.ReferenceEquals(data.OrganisationRoles, value)) { // Lists are the same object. // Assume the list has been modified, sort it and immediately return. // NB Shouldn't happen as HashSet isn't exposed. Changed = true; return; } CheckChanged(data.OrganisationRoles.ToList(), value); data.OrganisationRoles.Clear(); foreach (Sax.OrganisationRole organisationRole in value) data.OrganisationRoles.Add(organisationRole); } } // Admin comment. public String AdminComment { get { return data.AdminComment; } set { CheckChanged(data.AdminComment, value); data.AdminComment = value; } } // Population. internal void PopulateFrom(Data data) { UserAccountId = data.UserAccountId; OrganisationId = data.OrganisationId; OrganisationRoles = data.GetOrganisationRoles().ToList(); AdminComment = data.AdminComment; base.PopulateFrom(data); } public Data Clone() { Data data = new Data(); data.PopulateFrom(this); return data; } } //------------------------------------------------------------------- // Error field - identify properties with validation error. public new class ErrField : SacsHierarchyItem.ErrField { // Identifies the field that has invalid data. public ValidValue UserAccountId { get; set; } = ValidValue.Valid; public ValidValue OrganisationId { get; set; } = ValidValue.Valid; public ValidValue OrganisationRoles { get; set; } = ValidValue.Valid; public ValidValue AdminComment { get; set; } = ValidValue.Valid; } //------------------------------------------------------------------- // Data members. private readonly Data data = new Data(); public static readonly String UserOrgnisationRoleDelim = ","; //------------------------------------------------------------------- // Construction. protected SaxUserOrganisation(SysId id, SysId userAccountId, SysId organisationId) : base(id) { base.SetData(data); UserAccountId = userAccountId; OrganisationId = organisationId; Status = DefaultStatus; } //------------------------------------------------------------------- // Status. /// /// UserOrganisation is either active or deleted. /// Other status are handled by the User Account itself. /// As the User Organisation is simple a link between the user and Organisation and membership is defined by the presence of the 'Member' role. /// private static readonly List validStatuses = new List() { SaStatus.Active, SaStatus.Deleted, }; public static SaStatus DefaultStatus { get { return SaStatus.Active; } } public static List ValidStatuses { get { return new List(validStatuses); } } public static DataValue.TriStateType GetStatusTriState(SaStatus status) { if (status == SaStatus.Active) return DataValue.TriStateType.Green; if (status == SaStatus.Inactive) return DataValue.TriStateType.Red; if (status == SaStatus.Deleted) return DataValue.TriStateType.Purple; return DataValue.TriStateType.Unknown; } //------------------------------------------------------------------- // Utilities. public ItemId GetItemId() { return new ItemId((short)Sax.DataItemType.UserOrganisation, Id); } public static List GetOrganisationRolesFromCodes(String organisationRoleCodes) { return GetOrganisationRolesFromCodes(StringListUtil.Split(organisationRoleCodes, UserOrgnisationRoleDelim, true, true, true)); } public static List GetOrganisationRolesFromCodes(List organisationRoleCodes) { List organisationRoles = new List(); List roles = SaxSystem.GetCodesOfType(Sax.CodeType.OrganisationRole); foreach (String organisationRoleCode in organisationRoleCodes) { Sax.OrganisationRole organisationRole = (Sax.OrganisationRole)roles.GetTypeForCode(organisationRoleCode); if (organisationRole == 0) organisationRole = EnumUtil.ParseEnum(organisationRoleCode, Sax.OrganisationRole.Unknown); if (organisationRole > 0) organisationRoles.Add(organisationRole); } return organisationRoles; } public List GetOrganisationRoleCodes() { List organisationRoleCodes = new List(); List roles = SaxSystem.GetCodesOfType(Sax.CodeType.OrganisationRole); foreach (Sax.OrganisationRole organisationRole in GetOrganisationRoles()) organisationRoleCodes.Add(roles.GetCodeForType(organisationRole)); organisationRoleCodes.Sort(); return organisationRoleCodes; } public String GetOrganisationRoleCodesData() { return GetOrganisationRoleCodes().Concat(UserOrgnisationRoleDelim); } // Query roles. public bool CanManageOrganisation{ get { return HasAnyOrganisationRole(new [] { Sax.OrganisationRole.Admin }); } } public bool CanManageMembers { get { return HasAnyOrganisationRole(new [] { Sax.OrganisationRole.Admin, Sax.OrganisationRole.MemberAdmin }); } } public bool CanManageOpportunities { get { return HasAnyOrganisationRole(new [] { Sax.OrganisationRole.Admin, Sax.OrganisationRole.OpportunityAdmin }); } } public bool CanManageSchedule { get { return HasAnyOrganisationRole(new [] { Sax.OrganisationRole.Admin, Sax.OrganisationRole.ScheduleAdmin }); } } public bool CanManageArticles { get { return HasAnyOrganisationRole(new [] { Sax.OrganisationRole.Admin, Sax.OrganisationRole.ArticleAdmin }); } } public bool HasAnyOrganisationRole(IEnumerable organisationRoles) { return data.HasAnyOrganisationRole(organisationRoles); } //------------------------------------------------------------------- public void Dispose() { } //------------------------------------------------------------------- // Properties. public Data GetData() { return data.Clone(); } public void SetData(Data data) { // Properties that cannot be changed if previously specified. if (SysId.IsValid(Id) && SysId.ToInt(Id) != SysId.ToInt(data.Id)) throw new NotSupportedException($"{SysUtil.Fn} Id cannot be changed"); if (SysId.IsValid(UserAccountId) && SysId.ToInt(UserAccountId) != SysId.ToInt(data.UserAccountId)) throw new NotSupportedException($"{SysUtil.Fn} User Account Id cannot be changed"); if (SysId.IsValid(OrganisationId) && SysId.ToInt(OrganisationId) != SysId.ToInt(data.OrganisationId)) throw new NotSupportedException($"{SysUtil.Fn} Organisation Id cannot be changed"); this.data.PopulateFrom(data); } /// User Account Id. NB Can only be set where previously invalid. public SysId UserAccountId { get { return data.UserAccountId; } set { if (SysId.IsValid(UserAccountId) && SysId.ToInt(UserAccountId) != SysId.ToInt(data.UserAccountId)) throw new NotSupportedException("UserOrganisation.UserAccountId cannot be changed"); data.UserAccountId = value; } } /// Organisation Id. NB Can only be set where previously invalid. public SysId OrganisationId { get { return data.OrganisationId; } set { if (SysId.IsValid(OrganisationId) && SysId.ToInt(OrganisationId) != SysId.ToInt(data.OrganisationId)) throw new NotSupportedException("UserOrganisation.OrganisationId cannot be changed"); data.OrganisationId = value; } } /// Number of Organisation Roles. public byte OrganisationRoleCount { get { return data.OrganisationRoleCount; } } /// Organisation Roles - use setter to populate. public ReadOnlyCollection GetOrganisationRoles() { return data.GetOrganisationRoles(); } /// Store Organisation Roles. public IEnumerable OrganisationRoles { set { data.OrganisationRoles = value; } } public void ClearOrganisationRoles() { data.ClearOrganisationRoles(); } public void AddOrganisationRole(Sax.OrganisationRole organisationRole) { data.AddOrganisationRole(organisationRole); } /// Organisation admin comment. public String AdminComment { get { return StringUtil.EmptyOnNull(data.AdminComment); } set { data.AdminComment = value; } } //------------------------------------------------------------------- // Flags - positions 1-16 available. //------------------------------------------------------------------- // A s s o c i a t e d D a t a //------------------------------------------------------------------- //------------------------------------------------------------------- // P a r a m s //------------------------------------------------------------------- // List parameters. public enum StatusCat { Any, ActiveOnly, NotDeleted, DeletedOnly, } /// /// Item list parameters. /// public new class Params : SacsHierarchyItem.Params { // List filtering. public HashSet IdsIncluded { get; } = new HashSet(); public HashSet IdsExcluded { get; } = new HashSet(); // User Account properties. public HashSet UserAccountIdsIncluded { get; } = new HashSet(); public SaxUserAccount.StatusCat UserAccountStatusCat { get; set; } = SaxUserAccount.StatusCat.NotDeleted; /// Exact match on various user account properties including name, email etc. public String UserFilter { get; set; } /// Partial match on various user account properties including name, countries etc. public String UserFilterPartial { get; set; } // Organisation properties. public HashSet OrganisationIdsIncluded { get; } = new HashSet(); /// Exclude organisations with no upcoming events (from 7 days ago). public bool OrganisationHasScheduledEvents { get; set; } public SaxOrganisation.StatusCat OrganisationStatusCat { get; set; } = SaxOrganisation.StatusCat.NotDeleted; // Organisation User properties. /// Set true to restruct to User Organisations with at least one Organisation Role. public bool OrganisationMembersOnly { get; set; } public HashSet OrganisationRoles { get; } = new HashSet(); public HashSet OrganisationRolesAny { get; } = new HashSet(); // Common properties. public String NotesPartial { get; set; } public StatusCat StatusCat { get; set; } = StatusCat.NotDeleted; // Table aliases - blank if not specified/linked. public String Alias_UserOrganisation { get; set; } public String Alias_UserAccount { get; set; } public String Alias_Organisation { get; set; } } //------------------------------------------------------------------- } //======================================================================= // Item list extension methods. public static class SaxUserOrganisationList { //------------------------------------------------------------------- /// /// Extract list of unique Organisation Ids from the list. /// public static List GetOrganisationIds(this List values) { return new HashSet(values.Select(item => item.OrganisationId)).ToList(); } /// /// Extract list of unique User Account Ids from the list. /// public static List GetUserAccountIds(this List values) { return new HashSet(values.Select(item => item.UserAccountId)).ToList(); } //------------------------------------------------------------------- } //======================================================================= }