using System; using System.Collections.Generic; using Sarand.Net.Standard.Base.Bus; using Sarand.Net.Standard.Base.Core; using Sarand.Net.Standard.Base.Utils; namespace Sarand.Net.Standard.Base.Interfaces { //======================================================================= public class SaInterface : SacsHierarchyItem { //------------------------------------------------------------------- // Columns. public enum Column { Pos = 0, // Interface. Id, Context, Category, Category_Desc, Type, Type_Desc, Code, Caption, Active_Y, Active_TickCross, Active_Ind, // Derived properties. Process_Count, DataItem_Count, // Common properties. Notes, Status, Status_Desc, Status_Ind, CreateOn_Date, UpdateOn_Date, } //------------------------------------------------------------------- /// Properties other than common. public class DataProperties { public String Context { get; set; } = String.Empty; public SaInterfaceCategory Category { get; set; } = SaInterfaceCategory.Unknown; public short Type { get; set; } = 0; public String Code { get; set; } = String.Empty; public String Caption { get; set; } = String.Empty; } //------------------------------------------------------------------- // Data. public new class Data : SacsHierarchyItem.Data { // Data members. private readonly DataProperties data = new DataProperties(); // Construction. public Data () { Status = DefaultStatus; } // Properties. public String Context { get { return data.Context; } set { CheckChanged(data.Context, value); data.Context = value; } } public SaInterfaceCategory Category { get { return data.Category; } set { CheckChanged((int)data.Category, (int)value); data.Category = value; } } public short Type { get { return data.Type; } set { CheckChanged(data.Type, value); data.Type = value; } } public String Code { get { return data.Code; } set { CheckChanged(data.Code, value); data.Code = value; } } public String Caption { get { return data.Caption; } set { CheckChanged(data.Caption, value); data.Caption = value; } } // Flags. public bool Active { get { return GetFlag(Flag_Active); } set { SetFlag(Flag_Active, value); } } // Population. internal void PopulateFrom (Data data) { Context = data.Context; Category = data.Category; Type = data.Type; Code = data.Code; Caption = data.Caption; 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 Context { get; set; } = ValidValue.Valid; public ValidValue Category { get; set; } = ValidValue.Valid; public ValidValue Type { get; set; } = ValidValue.Valid; public ValidValue Code { get; set; } = ValidValue.Valid; public ValidValue Caption { get; set; } = ValidValue.Valid; } //------------------------------------------------------------------- // Data members. private readonly Data data = new Data(); // Flags. public static readonly Byte Flag_Active = 0x01; //------------------------------------------------------------------- // Construction. public SaInterface (SysId id, String context, SaInterfaceCategory category, short type) : base(id) { base.SetData(data); Context = context; Category = category; Type = type; Status = DefaultStatus; } //------------------------------------------------------------------- // Statuses. private static readonly List validStatuses = new List() { SaStatus.Active, }; public static SaStatus DefaultStatus { get { return ActiveStatus; } } public static SaStatus ActiveStatus { get { return SaStatus.Active; } } public static List ValidStatuses { get { return new List(validStatuses); } } public static DataValue.TriStateType GetStatusTriState (SaStatus status) { switch (status) { case SaStatus.Active: return Core.DataValue.TriStateType.Green; case SaStatus.Deleted: return Core.DataValue.TriStateType.Purple; } return Core.DataValue.TriStateType.Unknown; } //------------------------------------------------------------------- // 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 (!StringUtil.IsEmpty(Context) && !Context.Equals(data.Context)) throw new NotSupportedException(fn + " Context cannot be changed"); if ((int)Category >= 0 && (int)Category != (int)data.Category) throw new NotSupportedException(fn + " Category cannot be changed"); if (Type >= 0 && Type != data.Type) throw new NotSupportedException(fn + " Type cannot be changed"); // Populate individual properties to ensure that 'Changed' indicator is kept up to date. this.data.PopulateFrom(data); } public String Context { get { return data.Context; } protected set { data.Context = value; } } public SaInterfaceCategory Category { get { return data.Category; } protected set { data.Category = value; } } public short Type { get { return data.Type; } protected set { data.Type = value; } } public String Code { get { return data.Code; } set { data.Code = value; } } public String Caption { get { return data.Caption; } set { data.Caption = value; } } //------------------------------------------------------------------- // Flags. public bool Active { get { return GetFlag(Flag_Active); } set { SetFlag(Flag_Active, value); } } //------------------------------------------------------------------- // List parameters. public enum StatusCat { Any, /// NB Interface activity controlled by flag rather than status. ActiveOnly, } /// /// Item list parameters. /// public new class Params : SacsHierarchyItem.Params { public List IncludedIds { get; } = new List(); public List ExcludedIds { get; } = new List(); public String Context { get; set; } public SaInterfaceCategory InterfaceCategory { get; set; } = SaInterfaceCategory.Unknown; public List IncludedInterfaceTypes { get; } = new List(); /// Exact code match public String Code { get; set; } public String CaptionPartial { get; set; } /// Text to parially match code or caption public String InterfaceText { get; set; } /// Active flag public bool ActiveOnly { get; set; } = true; public StatusCat StatusCat { get; set; } = StatusCat.Any; } //------------------------------------------------------------------- } //======================================================================= // L i s t //======================================================================= public static class SaInterfaceList { //------------------------------------------------------------------- // E x i s t s //------------------------------------------------------------------- public static bool ExistsByContextCategoryType (this List interfaces, String context, SaInterfaceCategory interfaceCategory, short interfaceType) { return interfaces.Exists(item => item.Context.EqualsCI(context) && item.Category == interfaceCategory && item.Type == interfaceType); } public static bool ExistsByContextCategoryType (this List interfaces, String context, SaInterfaceCategory interfaceCategory, short interfaceType) { return interfaces.Exists(item => item.Context.EqualsCI(context) && item.Category == interfaceCategory && item.Type == interfaceType); } //------------------------------------------------------------------- // F i n d A l l //------------------------------------------------------------------- public static List FindAllByContextCategory (this List interfaces, String context, SaInterfaceCategory interfaceCategory) { return interfaces.FindAll(item => item.Context.EqualsCI(context) && item.Category == interfaceCategory); } public static List FindAllByContextCategory (this List interfaces, String context, SaInterfaceCategory interfaceCategory) { return interfaces.FindAll(item => item.Context.EqualsCI(context) && item.Category == interfaceCategory); } public static List FindAllByContextCategoryType (this List interfaces, String context, SaInterfaceCategory interfaceCategory, short interfaceType) { return interfaces.FindAll(item => item.Context.EqualsCI(context) && item.Category == interfaceCategory && item.Type == interfaceType); } public static List FindAllByContextCategoryType (this List interfaces, String context, SaInterfaceCategory interfaceCategory, short interfaceType) { return interfaces.FindAll(item => item.Context.EqualsCI(context) && item.Category == interfaceCategory && item.Type == interfaceType); } //------------------------------------------------------------------- } //======================================================================= }