222 lines
6.5 KiB
C#
222 lines
6.5 KiB
C#
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.Text;
|
|
|
|
namespace Sarand.Net.Standard.Base.ItemData
|
|
{
|
|
//=======================================================================
|
|
public class SaItemData : SacsHierarchyItem
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Columns.
|
|
public enum Column
|
|
{
|
|
Pos = 0,
|
|
Id,
|
|
// Item Option properties.
|
|
ItemType,
|
|
ItemType_Desc,
|
|
ItemId,
|
|
DataType,
|
|
ItemData,
|
|
// Common properties.
|
|
Notes,
|
|
Status,
|
|
Status_Desc,
|
|
Status_Ind,
|
|
CreateOn_Date,
|
|
UpdateOn_Date,
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
/// <summary>Properties other than common.</summary>
|
|
public class DataProperties
|
|
{
|
|
public ItemId ItemId { get; set; } = new ItemId();
|
|
public String DataType { get; set; } = String.Empty;
|
|
public String ItemData { 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 ItemId ItemId
|
|
{
|
|
get { return data.ItemId; }
|
|
set { CheckChanged(data.ItemId, value); data.ItemId = value; }
|
|
}
|
|
public String DataType
|
|
{
|
|
get { return data.DataType; }
|
|
set { CheckChanged(data.DataType, value); data.DataType = value; }
|
|
}
|
|
public String ItemData
|
|
{
|
|
get { return data.ItemData; }
|
|
set { CheckChanged(data.ItemData, value); data.ItemData = value; }
|
|
}
|
|
// Population.
|
|
internal void PopulateFrom (Data data)
|
|
{
|
|
ItemId = data.ItemId;
|
|
DataType = data.DataType;
|
|
ItemData = data.ItemData;
|
|
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 ItemType { get; set; } = ValidValue.Valid;
|
|
public ValidValue ItemId { get; set; } = ValidValue.Valid;
|
|
public ValidValue DataType { get; set; } = ValidValue.Valid;
|
|
public ValidValue ItemData { get; set; } = ValidValue.Valid;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
private readonly Data data = new Data();
|
|
// Flags.
|
|
public static readonly Byte Flag_Encrypted = 0x01;
|
|
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
public SaItemData (SysId id, ItemId itemId) :
|
|
base(id)
|
|
{
|
|
base.SetData(data);
|
|
ItemId = itemId;
|
|
Status = DefaultStatus;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Status.
|
|
private List<SaStatus> validStatuses = new List<SaStatus>()
|
|
{
|
|
SaStatus.Current,
|
|
SaStatus.Deleted,
|
|
};
|
|
public static SaStatus DefaultStatus { get { return StatusCurrent; } }
|
|
public static SaStatus StatusCurrent { get { return SaStatus.Current; } }
|
|
public static List<SaStatus> ValidStatuses { get { return new List<SaStatus>(ValidStatuses); } }
|
|
public static DataValue.TriStateType GetStatusTriState (SaStatus status)
|
|
{
|
|
if (status == SaStatus.Current) return DataValue.TriStateType.Green;
|
|
if (status == SaStatus.Deleted) return DataValue.TriStateType.Purple;
|
|
return DataValue.TriStateType.Unknown;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
public Data GetData () { return data.Clone(); }
|
|
public void SetData (Data data)
|
|
{
|
|
// Properties that cannot be changed if previously specified.
|
|
if (ItemId.Type >= 0 && ItemId.Type != data.ItemId.Type)
|
|
throw new NotSupportedException($"{SysUtil.Fn} Item type cannot be changed");
|
|
if (SysId.IsValid(ItemId.Id) && SysId.ToInt(ItemId.Id) != SysId.ToInt(data.ItemId.Id))
|
|
throw new NotSupportedException($"{SysUtil.Fn} Item Id cannot be changed");
|
|
// Populate individual properties to ensure that 'Changed' indicator is kept up to date.
|
|
this.data.PopulateFrom(data);
|
|
}
|
|
public ItemId ItemId
|
|
{
|
|
get { return data.ItemId; }
|
|
protected set { data.ItemId = value; }
|
|
}
|
|
public String DataType
|
|
{
|
|
get { return data.DataType; }
|
|
set { data.DataType = value; }
|
|
}
|
|
public String ItemData
|
|
{
|
|
get { return data.ItemData; }
|
|
set { data.ItemData = value; }
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Flags.
|
|
public bool Encrypted
|
|
{
|
|
get { return GetFlag(Flag_Encrypted); }
|
|
set { SetFlag(Flag_Encrypted, value); }
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Comparison.
|
|
public bool Matches (ItemId itemId)
|
|
{
|
|
if (ItemId.Type != itemId.Type) return false;
|
|
if (SysId.ToInt(ItemId.Id) != SysId.ToInt(itemId.Id)) return false;
|
|
return true;
|
|
}
|
|
public bool Matches (ItemId itemId, String dataType)
|
|
{
|
|
if (!Matches(itemId)) return false;
|
|
if (!DataType.EqualsCI(dataType)) return false;
|
|
return true;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// List parameters.
|
|
public enum StatusCat
|
|
{
|
|
Any,
|
|
NotDeleted,
|
|
CurrentOnly,
|
|
DeletedOnly,
|
|
}
|
|
/// <summary>
|
|
/// Item list parameters.
|
|
/// </summary>
|
|
public new class Params : SacsHierarchyItem.Params
|
|
{
|
|
public HashSet<SysId> IdsIncluded { get; } = new HashSet<SysId>();
|
|
public HashSet<SysId> IdsExcluded { get; } = new HashSet<SysId>();
|
|
public HashSet<ItemId> ItemIdsIncluded { get; } = new HashSet<ItemId>();
|
|
public HashSet<String> DataTypesIncluded { get; } = new HashSet<String>();
|
|
public StatusCat StatusCat { get; set; } = StatusCat.NotDeleted;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
|
|
//=======================================================================
|
|
/// <summary>List extension methods.</summary>
|
|
public static class SaItemDataList
|
|
{
|
|
public static List<SaItemData> FindByItem (this List<SaItemData> values, ItemId itemId)
|
|
{
|
|
return values.FindAll(item => item.Matches(itemId));
|
|
}
|
|
public static List<SaItemData> FindByItemDataType (this List<SaItemData> values, ItemId itemId, String dataType)
|
|
{
|
|
return values.FindAll(item => item.Matches(itemId, dataType));
|
|
}
|
|
}
|
|
|
|
//=======================================================================
|
|
}
|