BusinessMgr
This commit is contained in:
@@ -20,8 +20,8 @@ namespace Sarand.Net.Standard.Base.Core
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Construction.
|
||||
public DataValue () { }
|
||||
public DataValue (DataValue dataValue)
|
||||
public DataValue() { }
|
||||
public DataValue(DataValue dataValue)
|
||||
{
|
||||
if (dataValue != null)
|
||||
{
|
||||
@@ -29,37 +29,37 @@ namespace Sarand.Net.Standard.Base.Core
|
||||
value = dataValue.value;
|
||||
}
|
||||
}
|
||||
public DataValue (bool value)
|
||||
public DataValue(bool value)
|
||||
{
|
||||
this.value = value;
|
||||
this.dataType = DataType.Bool;
|
||||
}
|
||||
public DataValue (TriStateType value)
|
||||
public DataValue(TriStateType value)
|
||||
{
|
||||
this.value = value;
|
||||
this.dataType = DataType.TriState;
|
||||
}
|
||||
public DataValue (long value)
|
||||
public DataValue(long value)
|
||||
{
|
||||
this.value = value;
|
||||
this.dataType = DataType.Long;
|
||||
}
|
||||
public DataValue (double value)
|
||||
public DataValue(double value)
|
||||
{
|
||||
this.value = value;
|
||||
this.dataType = DataType.Double;
|
||||
}
|
||||
public DataValue (decimal value)
|
||||
public DataValue(decimal value)
|
||||
{
|
||||
this.value = value;
|
||||
this.dataType = DataType.Decimal;
|
||||
}
|
||||
public DataValue (String value)
|
||||
public DataValue(String value)
|
||||
{
|
||||
this.value = value;
|
||||
this.dataType = DataType.String;
|
||||
}
|
||||
public DataValue (DateTime value, DataType type = DataType.DateTime)
|
||||
public DataValue(DateTime value, DataType type = DataType.DateTime)
|
||||
{
|
||||
this.value = value;
|
||||
switch (type)
|
||||
@@ -74,7 +74,7 @@ namespace Sarand.Net.Standard.Base.Core
|
||||
break;
|
||||
}
|
||||
}
|
||||
public DataValue (Byte[] value)
|
||||
public DataValue(Byte[] value)
|
||||
{
|
||||
this.value = value;
|
||||
this.dataType = DataType.Blob;
|
||||
@@ -83,11 +83,11 @@ namespace Sarand.Net.Standard.Base.Core
|
||||
//-------------------------------------------------------------------
|
||||
// Properties.
|
||||
public static readonly DataValue NullValue = new DataValue();
|
||||
public DataType GetDataType ()
|
||||
public DataType GetDataType()
|
||||
{
|
||||
return dataType;
|
||||
}
|
||||
public static String GetTypeDesc (DataType type)
|
||||
public static String GetTypeDesc(DataType type)
|
||||
{
|
||||
// Converts type to string.
|
||||
switch (type)
|
||||
@@ -105,38 +105,38 @@ namespace Sarand.Net.Standard.Base.Core
|
||||
}
|
||||
return "<Unknown>";
|
||||
}
|
||||
public String GetTypeDesc ()
|
||||
public String GetTypeDesc()
|
||||
{
|
||||
return GetTypeDesc(dataType);
|
||||
}
|
||||
public bool IsValid ()
|
||||
public bool IsValid()
|
||||
{
|
||||
return (dataType != DataType.Unknown);
|
||||
}
|
||||
public bool IsNumeric ()
|
||||
public bool IsNumeric()
|
||||
{
|
||||
return (dataType == DataType.Long || dataType == DataType.Double || dataType == DataType.Decimal);
|
||||
}
|
||||
public bool IsDate ()
|
||||
public bool IsDate()
|
||||
{
|
||||
return (dataType == DataType.Date || dataType == DataType.DateTime);
|
||||
}
|
||||
public bool IsTime ()
|
||||
public bool IsTime()
|
||||
{
|
||||
return (dataType == DataType.Time || dataType == DataType.DateTime);
|
||||
}
|
||||
public bool IsDateTime ()
|
||||
public bool IsDateTime()
|
||||
{
|
||||
return (dataType == DataType.Date || dataType == DataType.Time || dataType == DataType.DateTime);
|
||||
}
|
||||
public bool IsBlob ()
|
||||
public bool IsBlob()
|
||||
{
|
||||
return (dataType == DataType.Blob);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Conversion.
|
||||
public override String ToString ()
|
||||
public override String ToString()
|
||||
{
|
||||
if (value == null) return String.Empty;
|
||||
// Forces any data type to string.
|
||||
@@ -154,7 +154,7 @@ namespace Sarand.Net.Standard.Base.Core
|
||||
}
|
||||
return String.Empty;
|
||||
}
|
||||
public double ToNumeric ()
|
||||
public double ToNumeric()
|
||||
{
|
||||
if (value == null) return 0.0;
|
||||
// Convert any type to double numeric.
|
||||
@@ -172,8 +172,26 @@ namespace Sarand.Net.Standard.Base.Core
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
public decimal ToDecimal()
|
||||
{
|
||||
if (value == null) return 0.0m;
|
||||
// Convert any type to double numeric.
|
||||
switch (dataType)
|
||||
{
|
||||
case DataType.Bool: return ((bool)value ? 1 : 0);
|
||||
case DataType.TriState: return (int)(TriStateType)value;
|
||||
case DataType.Long: return (long)value;
|
||||
case DataType.Double: return (decimal)(double)value;
|
||||
case DataType.Decimal: return (decimal)value;
|
||||
case DataType.String: return StringUtil.ToDecimal((String)value);
|
||||
case DataType.Date: return ((DateTime)value).Ticks;
|
||||
case DataType.Time: return ((DateTime)value).Ticks;
|
||||
case DataType.DateTime: return ((DateTime)value).Ticks;
|
||||
}
|
||||
return 0.0m;
|
||||
}
|
||||
/// <summary>Return value interpretted as boolean.</summary>
|
||||
public bool ToBool ()
|
||||
public bool ToBool()
|
||||
{
|
||||
if (value == null) return false;
|
||||
// Numeric <> 0, String: 'Yes', 'True', numeric value.
|
||||
@@ -192,7 +210,7 @@ namespace Sarand.Net.Standard.Base.Core
|
||||
return false;
|
||||
}
|
||||
/// <summary>Return explicit boolean value or false for other types.</summary>
|
||||
public bool AsBool ()
|
||||
public bool AsBool()
|
||||
{
|
||||
// Convert to specific type of data - fails if type is not correct:
|
||||
if (dataType == DataType.Bool) return (bool)value;
|
||||
@@ -200,21 +218,21 @@ namespace Sarand.Net.Standard.Base.Core
|
||||
if (SacsSystem.DebugMode) SaLogger.Debug($"{SysUtil.Fn} Incorrect data type: {dataType.ToString()}.");
|
||||
return false;
|
||||
}
|
||||
public TriStateType AsTriState ()
|
||||
public TriStateType AsTriState()
|
||||
{
|
||||
if (dataType == DataType.TriState) return (TriStateType)value;
|
||||
if (dataType == DataType.Unknown) return TriStateType.Unknown;
|
||||
if (SacsSystem.DebugMode) SaLogger.Debug($"{SysUtil.Fn} Incorrect data type: {dataType.ToString()}.");
|
||||
return TriStateType.Unknown;
|
||||
}
|
||||
public long AsLong ()
|
||||
public long AsLong()
|
||||
{
|
||||
if (dataType == DataType.Long) return (long)value;
|
||||
if (dataType == DataType.Unknown) return 0;
|
||||
if (SacsSystem.DebugMode) SaLogger.Debug($"{SysUtil.Fn} Incorrect data type: {dataType.ToString()}.");
|
||||
return 0;
|
||||
}
|
||||
public double AsDouble ()
|
||||
public double AsDouble()
|
||||
{
|
||||
if (dataType == DataType.Double) return (double)value;
|
||||
if (dataType == DataType.Unknown) return 0.0;
|
||||
@@ -227,21 +245,21 @@ namespace Sarand.Net.Standard.Base.Core
|
||||
if (SacsSystem.DebugMode) SaLogger.Debug($"{SysUtil.Fn} Incorrect data type: {dataType.ToString()}.");
|
||||
return 0.0;
|
||||
}
|
||||
public decimal AsDecimal ()
|
||||
public decimal AsDecimal()
|
||||
{
|
||||
if (dataType == DataType.Decimal) return (decimal)value;
|
||||
if (dataType == DataType.Unknown) return 0.00m;
|
||||
if (SacsSystem.DebugMode) SaLogger.Debug($"{SysUtil.Fn} Incorrect data type: {dataType.ToString()}.");
|
||||
return 0.00m;
|
||||
}
|
||||
public String AsString ()
|
||||
public String AsString()
|
||||
{
|
||||
if (dataType == DataType.String) return (String)value;
|
||||
if (dataType == DataType.Unknown) return "";
|
||||
if (SacsSystem.DebugMode) SaLogger.Debug($"{SysUtil.Fn} Incorrect data type: {dataType.ToString()}.");
|
||||
return "";
|
||||
}
|
||||
public DateTime AsDateTime ()
|
||||
public DateTime AsDateTime()
|
||||
{
|
||||
// NB DateTime.Ticks is NotFiniteNumberException of 100-nanosecond intervals since beginning of 1st January 0001.
|
||||
if (dataType == DataType.Date || dataType == DataType.Time || dataType == DataType.DateTime) return (DateTime)value;
|
||||
@@ -249,21 +267,21 @@ namespace Sarand.Net.Standard.Base.Core
|
||||
if (SacsSystem.DebugMode) SaLogger.Debug($"{SysUtil.Fn} Incorrect data type: {dataType.ToString()}.");
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
public DateTime AsDate ()
|
||||
public DateTime AsDate()
|
||||
{
|
||||
if (dataType == DataType.Date || dataType == DataType.DateTime) return ((DateTime)value).Date;
|
||||
if (dataType == DataType.Unknown) return DateTime.MinValue;
|
||||
if (SacsSystem.DebugMode) SaLogger.Debug($"{SysUtil.Fn} Incorrect data type: {dataType.ToString()}.");
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
public DateTime AsTime ()
|
||||
public DateTime AsTime()
|
||||
{
|
||||
if (dataType == DataType.Time || dataType == DataType.DateTime) return (DateTime)value;
|
||||
if (dataType == DataType.Unknown) return DateTime.MinValue;
|
||||
if (SacsSystem.DebugMode) SaLogger.Debug($"{SysUtil.Fn} Incorrect data type: {dataType.ToString()}.");
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
public Byte[] AsBlob ()
|
||||
public Byte[] AsBlob()
|
||||
{
|
||||
if (dataType == DataType.Blob) return (Byte[])value;
|
||||
if (dataType == DataType.Unknown) return null;
|
||||
@@ -273,18 +291,18 @@ namespace Sarand.Net.Standard.Base.Core
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Comparison.
|
||||
public bool Equals (DataValue value)
|
||||
public bool Equals(DataValue value)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
public int CompareTo (DataValue value)
|
||||
public int CompareTo(DataValue value)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Utilities.
|
||||
public void Clear ()
|
||||
public void Clear()
|
||||
{
|
||||
dataType = DataType.Unknown;
|
||||
value = null;
|
||||
@@ -292,7 +310,7 @@ namespace Sarand.Net.Standard.Base.Core
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Arithmeic.
|
||||
public void Increment (DataValue value)
|
||||
public void Increment(DataValue value)
|
||||
{
|
||||
if (dataType == value.GetDataType())
|
||||
{
|
||||
@@ -310,7 +328,7 @@ namespace Sarand.Net.Standard.Base.Core
|
||||
}
|
||||
}
|
||||
}
|
||||
public void Decrement (DataValue value)
|
||||
public void Decrement(DataValue value)
|
||||
{
|
||||
if (dataType == value.GetDataType())
|
||||
{
|
||||
@@ -328,7 +346,7 @@ namespace Sarand.Net.Standard.Base.Core
|
||||
}
|
||||
}
|
||||
}
|
||||
public void MultiplyBy (DataValue value)
|
||||
public void MultiplyBy(DataValue value)
|
||||
{
|
||||
if (dataType == value.GetDataType())
|
||||
{
|
||||
@@ -346,7 +364,7 @@ namespace Sarand.Net.Standard.Base.Core
|
||||
}
|
||||
}
|
||||
}
|
||||
public void DivideBy (DataValue value)
|
||||
public void DivideBy(DataValue value)
|
||||
{
|
||||
if (dataType == value.GetDataType())
|
||||
{
|
||||
@@ -422,7 +440,7 @@ namespace Sarand.Net.Standard.Base.Core
|
||||
// Conversion.
|
||||
|
||||
/// <summary>Convert all data values to strings.</summary>
|
||||
public static List<String> ToStringList (this List<DataValue> values)
|
||||
public static List<String> ToStringList(this List<DataValue> values)
|
||||
{
|
||||
List<String> stringValues = new List<String>();
|
||||
foreach (var dataValue in values)
|
||||
@@ -434,7 +452,7 @@ namespace Sarand.Net.Standard.Base.Core
|
||||
/// Convert the data values to raw objects.
|
||||
/// <para>eg. For population to Excel spreadsheet cells.</para>
|
||||
/// </summary>
|
||||
public static List<Object> ToObjectList (this List<DataValue> values)
|
||||
public static List<Object> ToObjectList(this List<DataValue> values)
|
||||
{
|
||||
List<Object> cellValues = new List<Object>();
|
||||
foreach (DataValue value in values)
|
||||
@@ -460,7 +478,7 @@ namespace Sarand.Net.Standard.Base.Core
|
||||
// Numeric - only with compatible numeric values.
|
||||
|
||||
/// <summary>Minimum numeric value in the list.</summary>
|
||||
public static DataValue GetMinValue (this List<DataValue> values)
|
||||
public static DataValue GetMinValue(this List<DataValue> values)
|
||||
{
|
||||
DataValue minValue = null;
|
||||
foreach (var dataValue in values)
|
||||
@@ -477,7 +495,7 @@ namespace Sarand.Net.Standard.Base.Core
|
||||
}
|
||||
|
||||
/// <summary>Maximum numeric value in the list.</summary>
|
||||
public static DataValue GetMaxValue (this List<DataValue> values)
|
||||
public static DataValue GetMaxValue(this List<DataValue> values)
|
||||
{
|
||||
DataValue maxValue = null;
|
||||
foreach (var dataValue in values)
|
||||
|
||||
@@ -100,6 +100,12 @@ namespace Sarand.Net.Standard.Base.Core
|
||||
Trace,
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
public enum SaConfirmActions
|
||||
{
|
||||
YesNo,
|
||||
}
|
||||
|
||||
//=====================================================================
|
||||
// I n t e r f a c e s
|
||||
//=====================================================================
|
||||
|
||||
@@ -97,7 +97,7 @@ namespace Sarand.Net.Standard.Base.Security
|
||||
// Login dialog has a 'Require debug login' option.
|
||||
public bool RequireDebugLogin { get; protected set; } = true;
|
||||
#endif
|
||||
protected List<SaSecurityRight> SecurityRights { get; } = new List<SaSecurityRight>();
|
||||
protected List<SaSecurityRight> RegisteredRights { get; } = new List<SaSecurityRight>();
|
||||
protected Dictionary<short, DataItemDfn> DataItemDfns { get; } = new Dictionary<short, DataItemDfn>();
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
@@ -155,16 +155,20 @@ namespace Sarand.Net.Standard.Base.Security
|
||||
//-------------------------------------------------------------------
|
||||
// Security rights are optionally associated with a specified data item type (>0),
|
||||
// in which case they are always associated with a specific item Id of that type.
|
||||
public bool RegisterSecurityRight(String code, String desc, short dataItemType = 0)
|
||||
public bool RegisterSecurityRight(int type, String code, String desc, short dataItemType = 0)
|
||||
{
|
||||
SecurityRights.Add(code, desc, dataItemType);
|
||||
RegisteredRights.Add(type, code, desc, dataItemType);
|
||||
return true;
|
||||
}
|
||||
public List<SaSecurityRight> GetRegisteredRights()
|
||||
{
|
||||
return new List<SaSecurityRight>(RegisteredRights);
|
||||
}
|
||||
// Replace previous rights.
|
||||
public bool RegisterSecurityRights(List<SaSecurityRight> securityRights)
|
||||
{
|
||||
SecurityRights.Clear();
|
||||
SecurityRights.AddRange(securityRights);
|
||||
RegisteredRights.Clear();
|
||||
RegisteredRights.AddRange(securityRights);
|
||||
return true;
|
||||
}
|
||||
// Determine whether any security items are present and allow them to be populated.
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
using System;
|
||||
using Sarand.Net.Standard.Base.Core;
|
||||
using Sarand.Net.Standard.Base.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Sarand.Net.Standard.Base.Security
|
||||
{
|
||||
@@ -9,17 +12,19 @@ namespace Sarand.Net.Standard.Base.Security
|
||||
{
|
||||
//-------------------------------------------------------------------
|
||||
// Construction.
|
||||
public SaSecurityRight (String code, String desc, int dataItemType = 0)
|
||||
public SaSecurityRight(int type, String code, String desc, int dataItemType = 0)
|
||||
{
|
||||
Type = type;
|
||||
Code = code;
|
||||
Desc = desc;
|
||||
DataItemType = dataItemType;
|
||||
}
|
||||
//-------------------------------------------------------------------
|
||||
// Properties.
|
||||
public String Code { get; set; }
|
||||
public String Desc { get; set; }
|
||||
public int DataItemType { get; set; }
|
||||
public int Type { get; }
|
||||
public String Code { get; }
|
||||
public String Desc { get; }
|
||||
public int DataItemType { get; }
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
}
|
||||
@@ -27,9 +32,42 @@ namespace Sarand.Net.Standard.Base.Security
|
||||
//=======================================================================
|
||||
public static class SaSecurityRightList
|
||||
{
|
||||
public static void Add (this List<SaSecurityRight> securityRights, String code, String desc, int dataItemType = 0)
|
||||
// Conversion by type.
|
||||
public static String GetCodeForType(this List<SaSecurityRight> securityRights, int type, String defaultValue = "")
|
||||
{
|
||||
securityRights.Add(new SaSecurityRight(code, desc, dataItemType));
|
||||
return securityRights.FirstOrDefault(item => item.Type == type)?.Code ?? defaultValue;
|
||||
}
|
||||
public static String GetCodeForType<T>(this List<SaSecurityRight> values, T type, String defaultValue = "") where T : struct, IConvertible
|
||||
{
|
||||
if (!(type is Enum))
|
||||
throw new ArgumentException("SaSecurityRightList.GetCodeForType parameter must be an enumerated type. Type supplied: " + type.GetType().Name);
|
||||
return values.GetCodeForType((int)(type as IConvertible), defaultValue);
|
||||
}
|
||||
public static String GetDescForType(this List<SaSecurityRight> securityRights, int type)
|
||||
{
|
||||
return securityRights.FirstOrDefault(item => item.Type == type)?.Desc ?? "";
|
||||
}
|
||||
public static String GetDescForType<T>(this List<SaSecurityRight> values, T type, String defaultValue = "") where T : struct, IConvertible
|
||||
{
|
||||
if (!(type is Enum))
|
||||
throw new ArgumentException("SaSecurityRightList.GetDescForType parameter must be an enumerated type. Type supplied: " + type.GetType().Name);
|
||||
return values.GetDescForType((int)(type as IConvertible), defaultValue);
|
||||
}
|
||||
|
||||
// Conversion by code.
|
||||
public static int GetTypeForCode(this List<SaSecurityRight> securityRights, String code)
|
||||
{
|
||||
return securityRights.FirstOrDefault(item => item.Code.EqualsCI(code))?.Type ?? 0;
|
||||
}
|
||||
public static String GetDescForCode(this List<SaSecurityRight> securityRights, String code)
|
||||
{
|
||||
return securityRights.FirstOrDefault(item => item.Code.EqualsCI(code))?.Desc ?? "";
|
||||
}
|
||||
|
||||
// Population.
|
||||
public static void Add(this List<SaSecurityRight> securityRights, int type, String code, String desc, int dataItemType = 0)
|
||||
{
|
||||
securityRights.Add(new SaSecurityRight(type, code, desc, dataItemType));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -82,6 +82,21 @@ namespace Sarand.Net.Standard.Base.Utils
|
||||
dictToAdd.ForEach(x => dict[x.Key] = x.Value);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Removing
|
||||
//-------------------------------------------------------------------
|
||||
/// <summary>
|
||||
/// Remove items from a dictionary that comply with the specified predicate.
|
||||
/// <para>- Ref. https://stackoverflow.com/questions/469202/best-way-to-remove-multiple-items-matching-a-predicate-from-a-net-dictionary</para>
|
||||
/// </summary>
|
||||
public static void RemoveAll<TKey, TValue>(this Dictionary<TKey, TValue> source,
|
||||
Predicate<KeyValuePair<TKey, TValue>> predicate)
|
||||
{
|
||||
foreach (var pair in source)
|
||||
if (predicate(pair))
|
||||
source.Remove(pair.Key);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Orderd Dictionary
|
||||
//-------------------------------------------------------------------
|
||||
|
||||
@@ -179,6 +179,10 @@ namespace Sarand.Net.Standard.Base.Utils
|
||||
if (!IsBlank(value)) return value;
|
||||
return "";
|
||||
}
|
||||
public static String NullOnBlank(String value)
|
||||
{
|
||||
return StringUtil.IsBlank(value) ? null : value;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// C o n t e n t
|
||||
|
||||
Reference in New Issue
Block a user