401 lines
15 KiB
C#
401 lines
15 KiB
C#
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 : SacsHierarchyItem
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// 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,
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
/// <summary>Properties other than common.</summary>
|
|
public class DataProperties
|
|
{
|
|
public SysId UserAccountId { get; set; }
|
|
public SysId OrganisationId { get; set; }
|
|
public HashSet<Sax.OrganisationRole> OrganisationRoles { get; } = new HashSet<Sax.OrganisationRole>();
|
|
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<Sax.OrganisationRole> GetOrganisationRoles()
|
|
{
|
|
return data.OrganisationRoles.ToList().AsReadOnly();
|
|
}
|
|
public void ClearOrganisationRoles()
|
|
{
|
|
if (!Changed) Changed = !data.OrganisationRoles.IsEmpty();
|
|
data.OrganisationRoles.Clear();
|
|
}
|
|
public bool HasAnyOrganisationRole(IEnumerable<Sax.OrganisationRole> 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);
|
|
}
|
|
}
|
|
/// <summary>Store organisation roles.</summary>
|
|
public IEnumerable<Sax.OrganisationRole> 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.
|
|
/// <summary>
|
|
/// UserOrganisation is either active or deleted.
|
|
/// <para>Other status are handled by the User Account itself.</para>
|
|
/// <para>As the User Organisation is simple a link between the user and Organisation and membership is defined by the presence of the 'Member' role.</para>
|
|
/// </summary>
|
|
private static readonly List<SaStatus> validStatuses = new List<SaStatus>()
|
|
{
|
|
SaStatus.Active,
|
|
SaStatus.Deleted,
|
|
};
|
|
public static SaStatus DefaultStatus { get { return SaStatus.Active; } }
|
|
public static List<SaStatus> ValidStatuses { get { return new List<SaStatus>(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<Sax.OrganisationRole> GetOrganisationRolesFromCodes(String organisationRoleCodes)
|
|
{
|
|
return GetOrganisationRolesFromCodes(StringListUtil.Split(organisationRoleCodes, UserOrgnisationRoleDelim, true, true, true));
|
|
}
|
|
public static List<Sax.OrganisationRole> GetOrganisationRolesFromCodes(List<String> organisationRoleCodes)
|
|
{
|
|
List<Sax.OrganisationRole> organisationRoles = new List<Sax.OrganisationRole>();
|
|
List<TypeCodeDesc> 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<String> GetOrganisationRoleCodes()
|
|
{
|
|
List<String> organisationRoleCodes = new List<string>();
|
|
List<TypeCodeDesc> 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<Sax.OrganisationRole> organisationRoles)
|
|
{
|
|
return data.HasAnyOrganisationRole(organisationRoles);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
public Data GetData() { return data.Clone(); }
|
|
public void SetData(Data data)
|
|
{
|
|
String fn = GetType().Name + ".SetData";
|
|
// Properties that cannot be changed if previously specified.
|
|
if (SysId.IsValid(Id) && SysId.ToInt(Id) != SysId.ToInt(data.Id))
|
|
throw new NotSupportedException(fn + " Id cannot be changed");
|
|
if (SysId.IsValid(UserAccountId) && SysId.ToInt(UserAccountId) != SysId.ToInt(data.UserAccountId))
|
|
throw new NotSupportedException(fn + " User Account Id cannot be changed");
|
|
if (SysId.IsValid(OrganisationId) && SysId.ToInt(OrganisationId) != SysId.ToInt(data.OrganisationId))
|
|
throw new NotSupportedException(fn + " Organisation Id cannot be changed");
|
|
this.data.PopulateFrom(data);
|
|
}
|
|
/// <summary>User Account Id. NB Can only be set where previously invalid.</summary>
|
|
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;
|
|
}
|
|
}
|
|
/// <summary>Organisation Id. NB Can only be set where previously invalid.</summary>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>Number of Organisation Roles.</summary>
|
|
public byte OrganisationRoleCount
|
|
{
|
|
get { return data.OrganisationRoleCount; }
|
|
}
|
|
/// <summary>Organisation Roles - use setter to populate.</summary>
|
|
public ReadOnlyCollection<Sax.OrganisationRole> GetOrganisationRoles()
|
|
{
|
|
return data.GetOrganisationRoles();
|
|
}
|
|
/// <summary>Store Organisation Roles.</summary>
|
|
public IEnumerable<Sax.OrganisationRole> OrganisationRoles
|
|
{
|
|
set { data.OrganisationRoles = value; }
|
|
}
|
|
public void ClearOrganisationRoles()
|
|
{
|
|
data.ClearOrganisationRoles();
|
|
}
|
|
public void AddOrganisationRole(Sax.OrganisationRole organisationRole)
|
|
{
|
|
data.AddOrganisationRole(organisationRole);
|
|
}
|
|
|
|
/// <summary>Organisation admin comment.</summary>
|
|
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,
|
|
}
|
|
/// <summary>
|
|
/// Item list parameters.
|
|
/// </summary>
|
|
public new class Params : SacsHierarchyItem.Params
|
|
{
|
|
// List filtering.
|
|
public List<SysId> IdsIncluded { get; } = new List<SysId>();
|
|
public List<SysId> IdsExcluded { get; } = new List<SysId>();
|
|
public List<SysId> UserAccountIdsIncluded { get; } = new List<SysId>();
|
|
public SaxUserAccount.StatusCat UserAccountStatusCat { get; set; } = SaxUserAccount.StatusCat.NotDeleted;
|
|
public List<SysId> OrganisationIdsIncluded { get; } = new List<SysId>();
|
|
public SaxOrganisation.StatusCat OrganisationStatusCat { get; set; } = SaxOrganisation.StatusCat.NotDeleted;
|
|
/// <summary>Set true to restruct to User Organisations with at least one Organisation Role.</summary>
|
|
public bool OrganisationMembersOnly { get; set; }
|
|
public List<Sax.OrganisationRole> OrganisationRoles { get; } = new List<Sax.OrganisationRole>();
|
|
public List<Sax.OrganisationRole> OrganisationRolesAny { get; } = new List<Sax.OrganisationRole>();
|
|
/// <summary>Exact match on various user account properties including name, email etc.</summary>
|
|
public String UserFilter { get; set; }
|
|
/// <summary>Partial match on various user account properties including name, countries etc.</summary>
|
|
public String UserFilterPartial { get; set; }
|
|
// 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
|
|
{
|
|
//-------------------------------------------------------------------
|
|
/// <summary>
|
|
/// Extract list of unique Organisation Ids from the list.
|
|
/// </summary>
|
|
public static List<SysId> GetOrganisationIds(this List<SaxUserOrganisation> values)
|
|
{
|
|
return new HashSet<SysId>(values.Select(item => item.OrganisationId)).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract list of unique User Account Ids from the list.
|
|
/// </summary>
|
|
public static List<SysId> GetUserAccountIds(this List<SaxUserOrganisation> values)
|
|
{
|
|
return new HashSet<SysId>(values.Select(item => item.UserAccountId)).ToList();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
|
|
//=======================================================================
|
|
}
|