Saxonite v1.2.3

This commit is contained in:
2022-02-16 11:18:53 +00:00
parent 51df9e1ff6
commit 966204d643
11 changed files with 478 additions and 197 deletions
+33 -32
View File
@@ -77,12 +77,12 @@ namespace Sarand.Net.Standard.Base.Bus
}
}
/// <summary>Get flag value - position 1 - 16</summary>
public bool GetFlag (Byte flagPos)
public bool GetFlag(Byte flagPos)
{
return SacsHierarchyItem.GetFlag(Flags, flagPos);
}
/// <summary>Set flag value - position 1 - 16</summary>
public void SetFlag (Byte flagPos, bool value)
public void SetFlag(Byte flagPos, bool value)
{
CheckChanged(GetFlag(flagPos), value);
Flags = SacsHierarchyItem.SetFlag(Flags, flagPos, value);
@@ -126,7 +126,7 @@ namespace Sarand.Net.Standard.Base.Bus
//---------------------------------------------------------------
// Population.
/// <summary>Copy common properties to this object.</summary>
protected void PopulateFrom (Data sourceData)
protected void PopulateFrom(Data sourceData)
{
Id = sourceData.Id;
Notes = sourceData.Notes;
@@ -136,7 +136,7 @@ namespace Sarand.Net.Standard.Base.Bus
UpdateStamp = sourceData.UpdateStamp;
}
/// <summary>Copy comon properties to the target object.</summary>
internal void PopulateTo (Data targetData)
internal void PopulateTo(Data targetData)
{
targetData.PopulateFrom(this);
}
@@ -155,30 +155,30 @@ namespace Sarand.Net.Standard.Base.Bus
}
}
/// <summary>Clear 'Changed' flag.</summary>
public void AcceptChanges ()
public void AcceptChanges()
{
Changed = false;
}
//---------------------------------------------------------------
// Compare old and new property values and set 'Changed' if different.
protected void CheckChanged (SysId oldValue, SysId newValue)
protected void CheckChanged(SysId oldValue, SysId newValue)
{
if (Changed) return;
// Compare by numeric value so that invalid values are equal.
Changed = SysId.ToInt(oldValue) != SysId.ToInt(newValue);
}
protected void CheckChanged (ItemId oldValue, ItemId newValue)
protected void CheckChanged(ItemId oldValue, ItemId newValue)
{
if (Changed) return;
Changed = (oldValue.Type != newValue.Type) || (!oldValue.Id.Equals(newValue.Id));
}
protected void CheckChanged (String oldValue, String newValue)
protected void CheckChanged(String oldValue, String newValue)
{
if (Changed) return;
Changed = !StringUtil.EmptyOnNull(oldValue).Equals(StringUtil.EmptyOnNull(newValue));
}
protected void CheckChanged (bool oldValue, bool newValue)
protected void CheckChanged(bool oldValue, bool newValue)
{
if (Changed) return;
Changed = (oldValue != newValue);
@@ -186,44 +186,45 @@ namespace Sarand.Net.Standard.Base.Bus
protected void CheckChangedEnum<T>(T oldValue, T newValue) where T : struct, IConvertible
{
if (!(oldValue is Enum) || !(newValue is Enum))
throw new ArgumentException(GetType().Name+ ".CheckChangedEnum<T> Parameters must be enumerated types. Type supplied: " + oldValue.GetType().Name + ", " + newValue.GetType().Name);
throw new ArgumentException(GetType().Name + ".CheckChangedEnum<T> Parameters must be enumerated types. Type supplied: " + oldValue.GetType().Name + ", " + newValue.GetType().Name);
if (Changed) return;
Changed = ((int)(oldValue as IConvertible) != (int)(newValue as IConvertible));
}
protected void CheckChanged (long oldValue, long newValue)
protected void CheckChanged(long oldValue, long newValue)
{
if (Changed) return;
Changed = (oldValue != newValue);
}
protected void CheckChanged (double oldValue, double newValue)
protected void CheckChanged(double oldValue, double newValue)
{
if (Changed) return;
Changed = (oldValue != newValue);
}
protected void CheckChanged (decimal oldValue, decimal newValue)
protected void CheckChanged(decimal oldValue, decimal newValue)
{
if (Changed) return;
Changed = (oldValue != newValue);
}
protected void CheckChanged (DateTime oldValue, DateTime newValue)
protected void CheckChanged(DateTime oldValue, DateTime newValue)
{
if (Changed) return;
Changed = !oldValue.Equals(newValue);
}
protected void CheckChanged (List<String> oldValues, List<String> newValues)
protected void CheckChanged(IEnumerable<String> oldValues, IEnumerable<String> newValues)
{
if (Changed) return;
Changed = !oldValues.ContentEquals(newValues);
}
protected void CheckChanged<T> (List<T> oldValues, List<T> newValues)
protected void CheckChanged<T>(IEnumerable<T> oldValues, IEnumerable<T> newValues)
{
if (Changed) return;
if (oldValues.Count() != newValues.Count()) Changed = true;
else
{
for (int idx = 0; idx < oldValues.Count(); idx++)
var combinedValues = oldValues.Zip(newValues, Tuple.Create);
foreach (var combinedValue in combinedValues)
{
if (!oldValues[idx].Equals(newValues[idx]))
if (!combinedValue.Item1.Equals(combinedValue.Item2))
{
Changed = true;
return;
@@ -231,7 +232,7 @@ namespace Sarand.Net.Standard.Base.Bus
}
}
}
protected void CheckChanged (byte[] oldValue, byte[] newValue)
protected void CheckChanged(byte[] oldValue, byte[] newValue)
{
if (Changed) return;
if ((oldValue == null) != (newValue != null)) Changed = true;
@@ -271,17 +272,17 @@ namespace Sarand.Net.Standard.Base.Bus
//-------------------------------------------------------------------
// Construction.
protected SacsHierarchyItem ()
protected SacsHierarchyItem()
{
}
protected SacsHierarchyItem (SysId id)
protected SacsHierarchyItem(SysId id)
{
data.Id = id;
}
//-------------------------------------------------------------------
// Disposal.
protected override void Dispose (bool disposing)
protected override void Dispose(bool disposing)
{
if (disposing)
{
@@ -344,7 +345,7 @@ namespace Sarand.Net.Standard.Base.Bus
protected set { data.Flags = value; }
}
/// <summary>Flags are positioned from 1, up to 16 allowed.</summary>
public static bool GetFlag (UInt16 flags, Byte flagPos)
public static bool GetFlag(UInt16 flags, Byte flagPos)
{
bool flag = false;
if (flagPos > 0 && flagPos <= 16)
@@ -352,12 +353,12 @@ namespace Sarand.Net.Standard.Base.Bus
return flag;
}
/// <summary>Flags are positioned from 1, up to 16 allowed.</summary>
public bool GetFlag (Byte flagPos)
public bool GetFlag(Byte flagPos)
{
return GetFlag(data.Flags, flagPos);
}
/// <summary>Set flag value - position 1 - 16</summary>
public static UInt16 SetFlag (UInt16 flags, Byte flagPos, bool value)
public static UInt16 SetFlag(UInt16 flags, Byte flagPos, bool value)
{
if (flagPos > 0 && flagPos <= 16)
{
@@ -370,7 +371,7 @@ namespace Sarand.Net.Standard.Base.Bus
return flags;
}
/// <summary>Set flag value - position 1 - 16</summary>
public void SetFlag (Byte flagPos, bool value)
public void SetFlag(Byte flagPos, bool value)
{
data.Flags = SetFlag(data.Flags, flagPos, value);
}
@@ -378,14 +379,14 @@ namespace Sarand.Net.Standard.Base.Bus
//-------------------------------------------------------------------
// Update stamps.
public UpdateStamp CreateStamp { get { return data.CreateStamp; } }
public void SetCreateStamp (DateTime on, SysId by, String host)
public void SetCreateStamp(DateTime on, SysId by, String host)
{
CreateStamp.On = on;
CreateStamp.By = by;
CreateStamp.Host = host;
}
public UpdateStamp UpdateStamp { get { return data.UpdateStamp; } }
public void SetUpdateStamp (DateTime on, SysId by, String host)
public void SetUpdateStamp(DateTime on, SysId by, String host)
{
UpdateStamp.On = on;
UpdateStamp.By = by;
@@ -394,7 +395,7 @@ namespace Sarand.Net.Standard.Base.Bus
//-------------------------------------------------------------------
// Populate data item - only once on construction.
protected void SetData (Data data)
protected void SetData(Data data)
{
String fn = GetType().Name + ".SetData";
// Can only be replaced if currently internal class.
@@ -422,14 +423,14 @@ namespace Sarand.Net.Standard.Base.Bus
}
}
/// <summary>Clear the 'Changed' flag to indicate that no changes have been made.</summary>
public void AcceptChanges ()
public void AcceptChanges()
{
Changed = false;
}
//-------------------------------------------------------------------
// Format value for display.
public static String FormatForDisplay (String value, int maxLength = 0)
public static String FormatForDisplay(String value, int maxLength = 0)
{
if (StringUtil.IsEmpty(value)) return String.Empty;
// Replace non-display characters.
@@ -450,7 +451,7 @@ namespace Sarand.Net.Standard.Base.Bus
public class Params
{
// Construction.
protected Params ()
protected Params()
{
}
/// <summary>Primary table alias to use if different from default.</summary>
@@ -10,7 +10,7 @@ namespace Sarand.Net.Standard.Base.Core
{
//=======================================================================
public sealed class DisposableCollection<T> : Collection<T>, IDisposable
where T : IDisposable
where T : class, IDisposable
{
//-------------------------------------------------------------------
// Construction.
@@ -47,10 +47,31 @@ namespace Sarand.Net.Standard.Base.Core
/// </summary>
public new void Add(T item)
{
if (item != null)
if (item != null && !Contains(item))
base.Add(item);
}
/// <summary>
/// Add list of items to the collection.
/// <para>Null items are ignored.</para>
/// <para>Items that can't be cast to the required object type cause a debug break.</para>
/// </summary>
public void AddRange<O>(List<O> items) where O : class, IDisposable
{
if (items != null && !items.IsEmpty())
{
foreach (O item in items)
{
if (item == null) continue;
T obj = item as T;
if (obj != null)
Add(obj);
else if (SacsSystem.DebugMode)
Debugger.Break();
}
}
}
//-------------------------------------------------------------------
}
//=======================================================================
@@ -0,0 +1,115 @@
using Sarand.Net.Standard.Base.App;
using System;
using System.Collections.Generic;
using System.Text;
namespace Sarand.Net.Standard.Base.Core
{
//=======================================================================
/// <summary>
/// Dictionary with items queued as if in FIFO list.
/// </summary>
/// <typeparam name="K">Type of key</typeparam>
/// <typeparam name="V">Type of value</typeparam>
public class SacsQueueDictionary<K, V>
{
//-------------------------------------------------------------------
// Data memners.
private readonly Queue<K> queue = new Queue<K>();
private readonly Dictionary<K, V> dictionary = new Dictionary<K, V>();
//-------------------------------------------------------------------
// Properties.
public int Count { get { lock (dictionary) return queue.Count; } }
//-------------------------------------------------------------------
/// <summary>
/// Is the KLey defined in the queue dictionary.
/// </summary>
/// <param name="key"></param>
/// <returns>True if found, else false.</returns>
public bool HasValue(K key)
{
lock (dictionary)
{
return dictionary.ContainsKey(key);
}
}
//-------------------------------------------------------------------
/// <summary>
/// Retrieve the value determined by the key.
/// </summary>
/// <param name="key"></param>
/// <returns>Corresponding value if present, else default value.</returns>
public V GetValue(K key)
{
V value = default(V);
lock (dictionary)
{
dictionary.TryGetValue(key, out value);
}
return value;
}
//-------------------------------------------------------------------
public bool SetEntry(K key, V value)
{
if (key == null || value == null) return false;
lock (dictionary)
{
if (dictionary.ContainsKey(key))
{
// Existsing key.
// Replace value in dictionary.
dictionary[key] = value;
return true;
}
// New Key.
// Add to queue and set value in dictionary.
queue.Enqueue(key);
dictionary[key] = value;
return true;
}
if (SacsSystem.DebugMode) Validate();
}
//-------------------------------------------------------------------
/// <summary>
/// Remove the top n entries from the queue dictionary.
/// </summary>
/// <param name="maxEntries"></param>
/// <returns></returns>
public List<KeyValuePair<K, V>> RemoveTopEntries(int maxEntries)
{
List<KeyValuePair<K, V>> entries = new List<KeyValuePair<K, V>>();
lock (dictionary)
{
while (queue.Count > 0 && entries.Count < maxEntries)
{
K key = queue.Dequeue();
V value = dictionary[key];
entries.Add(new KeyValuePair<K, V>(key, value));
dictionary.Remove(key);
}
}
if (SacsSystem.DebugMode) Validate();
return entries;
}
//-------------------------------------------------------------------
// Internal utilities.
//-------------------------------------------------------------------
private void Validate()
{
lock (dictionary)
{
if (queue.Count != dictionary.Count)
throw new NotSupportedException("SacsQueueDictionary.Validate Inconsistent entry counts. Quete: " + queue.Count + ". Dictionary: " + dictionary.Count);
}
}
//-------------------------------------------------------------------
}
//=======================================================================
}
+1 -1
View File
@@ -39,7 +39,7 @@ namespace Sarand.Net.Standard.Base.Dbas
/// <returns>the name of an ODBC driver for Microsoft SQL Server, if one is present; null, otherwise.</returns>
public static string GetOdbcSqlDriverName ()
{
List<string> driverPrecedence = new List<string>() { "SQL Server Native Client 14.0", "SQL Server Native Client 13.0", "SQL Server Native Client 12.0", "SQL Server Native Client 11.0", "SQL Server Native Client 10.0", "SQL Server Native Client 9.0", "SQL Server" };
List<string> driverPrecedence = new List<string>() { "ODBC Driver 17 for SQL Server", "ODBC Driver 13 for SQL Server", "ODBC Driver 11 for SQL Server", "SQL Server Native Client 11.0", "SQL Server Native Client 10.0", "SQL Server Native Client 9.0", "SQL Server" };
string[] availableOdbcDrivers = GetOdbcDriverNames();
string driverName = null;
if (availableOdbcDrivers != null)
@@ -1,4 +1,5 @@
using System;
using Sarand.Net.Standard.Base.Utils;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
@@ -49,6 +50,20 @@ namespace Sarand.Net.Standard.Base.Helpers
//return Random.Next(fromInclusive, toExclusive);
}
//-------------------------------------------------------------------
public static String getRandomLengthNumber(ushort maxLength=4)
{
String value = "";
int len = GetInt32(1, maxLength + 1);
for (int i = 0; i < len; i++)
value += GetInt32(0,10);
if (StringUtil.IsEmpty(value))
throw new NotSupportedException("getRandomLengthNumber Generated blank value");
if (value.Length > maxLength)
throw new NotSupportedException("getRandomLengthNumber Generated excess length value");
return value;
}
//-------------------------------------------------------------------
}
//=======================================================================
+31 -4
View File
@@ -1,5 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
namespace Sarand.Net.Standard.Base.Utils
@@ -40,7 +42,7 @@ namespace Sarand.Net.Standard.Base.Utils
// S t r i n g - I n t U t i l i t i e s
//-------------------------------------------------------------------
/// <summary>Increment the count against the specified key by the supplied value, creating the key's entry if needed.</summary>
public static void Increment (this IDictionary<String, int> dictionary, String key, int increment = 1)
public static void Increment(this IDictionary<String, int> dictionary, String key, int increment = 1)
{
if (!dictionary.ContainsKey(key))
dictionary[key] = increment;
@@ -52,14 +54,14 @@ namespace Sarand.Net.Standard.Base.Utils
// A d d T o L i s t V a l u e
//-------------------------------------------------------------------
/// <summary>Add key:object to dictionary where each value is a list of objects.</summary>
public static void Add<TKey, TValue> (this Dictionary<TKey, List<TValue>> dict, TKey key, TValue value)
public static void Add<TKey, TValue>(this Dictionary<TKey, List<TValue>> dict, TKey key, TValue value)
{
if (!dict.ContainsKey(key))
dict[key] = new List<TValue>();
dict[key].Add(value);
}
/// <summary>Add key:objects to dictionary where each value is a list of objects.</summary>
public static void Add<TKey, TValue> (this Dictionary<TKey, List<TValue>> dict, TKey key, IEnumerable<TValue> values)
public static void Add<TKey, TValue>(this Dictionary<TKey, List<TValue>> dict, TKey key, IEnumerable<TValue> values)
{
if (!dict.ContainsKey(key))
dict[key] = new List<TValue>();
@@ -70,11 +72,36 @@ namespace Sarand.Net.Standard.Base.Utils
// A d d R a n g e
//-------------------------------------------------------------------
/// <summary>Add entries from another dictionary.</summary>
public static void AddRange<TKey, TValue> (this Dictionary<TKey, TValue> dict, Dictionary<TKey, TValue> dictToAdd)
public static void AddRange<TKey, TValue>(this Dictionary<TKey, TValue> dict, Dictionary<TKey, TValue> dictToAdd)
{
dictToAdd.ForEach(x => dict.Add(x.Key, x.Value));
}
//-------------------------------------------------------------------
// Orderd Dictionary
//-------------------------------------------------------------------
public static DictionaryEntry GetElementAt(this OrderedDictionary dictionary, int index)
{
return dictionary.Cast<DictionaryEntry>().ElementAt(index);
}
public static K GetKeyAt<K>(this OrderedDictionary dictionary, int index)
{
return (K)dictionary.GetElementAt(index).Key;
}
public static V GetValue<K, V>(this OrderedDictionary dictionary, K key)
{
if (dictionary == null || key == null)
return default(V);
try
{
return (V)dictionary.Cast<DictionaryEntry>().AsQueryable().Single(kvp => ((K)kvp.Key).Equals(key)).Value;
}
catch (Exception)
{
return default(V);
}
}
//-------------------------------------------------------------------
}
//=======================================================================
+17 -8
View File
@@ -12,7 +12,7 @@ namespace Sarand.Net.Standard.Base.Utils
/// <para>- \r\n replaced with \n</para>
/// <para>- Duplicate lines removed.</para>
/// </summary>
public static String GetMessage (this Exception exc)
public static String GetMessage(this Exception exc)
{
List<String> messageLines = new List<string>();
foreach (String messageLine in (StringUtil.EmptyOnNull(exc?.Message) ?? "").Trim().ReplaceAll("\r\n", "\n").Split('\n'))
@@ -29,7 +29,7 @@ namespace Sarand.Net.Standard.Base.Utils
/// <para>[ExceptionType]: [ExceptionMessage]</para>
/// <para>followed by stack entries on separate lines.</para>
/// </summary>
public static String GetErrorStateMessage (this Exception exc, bool includeExceptionType = true)
public static String GetErrorStateMessage(this Exception exc, bool includeExceptionType = true)
{
return (includeExceptionType ? exc.GetType().Name + ": " : "") + GetMessage(exc);
}
@@ -40,14 +40,23 @@ namespace Sarand.Net.Standard.Base.Utils
/// <para>[ExceptionType]: [ExceptionMessage]</para>
/// <para>followed by stack entries on separate lines.</para>
/// </summary>
public static String GetLogMessage (this Exception exc, int entries = 8, String stackEntryDelim = "\n\t")
public static String GetLogMessage(this Exception exc, int entries = 8, String stackEntryDelim = "\n\t")
{
return exc.GetType().Name + ": " + GetMessage(exc) + exc.GetStackEntries(entries).Concat("", stackEntryDelim);
return exc.GetLogMessage("", entries, stackEntryDelim);
}
/// <summary>
/// Generate standard exception log message.
/// <para>[ExceptionType] [message]: [ExceptionMessage]</para>
/// <para>followed by stack entries on separate lines.</para>
/// </summary>
public static String GetLogMessage(this Exception exc, String message, int entries = 8, String stackEntryDelim = "\n\t")
{
return (exc.GetType().Name + " " + message + ": " + GetMessage(exc) + exc.GetStackEntries(entries).Concat("", stackEntryDelim)).TrimAll();
}
//-------------------------------------------------------------------
/// <summary>Retrieve stack entries, up to maximum number.</summary>
public static List<String> GetStackEntries (this Exception exc, int entries = 0)
public static List<String> GetStackEntries(this Exception exc, int entries = 0)
{
List<String> stackEntries = new List<string>();
stackEntries.AddRange(exc.StackTrace.Split(new String[] { "\r\n" }, (entries > 0 ? entries : 999), StringSplitOptions.None));
@@ -63,19 +72,19 @@ namespace Sarand.Net.Standard.Base.Utils
// File handling exceptions.
public class FileException : Exception
{
public FileException (string message) :
public FileException(string message) :
base(message)
{ }
}
public class ArchiveFileException : FileException
{
public ArchiveFileException (string message) :
public ArchiveFileException(string message) :
base(message)
{ }
}
public class FileExistsException : FileException
{
public FileExistsException (string message) :
public FileExistsException(string message) :
base(message)
{ }
}
@@ -42,18 +42,18 @@ namespace Sarand.Net.Standard.Base.Utils
// C o m p a r i s o n
//-------------------------------------------------------------------
/// <summary>Compare lists to verify that they contain the same value in the corresponding position.</summary>
public static bool ContentEquals (this List<String> values, List<String> target)
public static bool ContentEquals (this IEnumerable<String> values, IEnumerable<String> target)
{
if (values.Count != target.Count) return false;
if (values.Count() != target.Count()) return false;
var combinedValues = values.Zip(target, Tuple.Create);
foreach (var combinedValue in combinedValues)
if (!combinedValue.Item1.Equals(combinedValue.Item2)) return false;
return true;
}
/// <summary>Compare lists to verify that they contain the same case-insensitive value in the corresponding position.</summary>
public static bool ContentEqualsCI (this List<String> values, List<String> target)
public static bool ContentEqualsCI (this IEnumerable<String> values, IEnumerable<String> target)
{
if (values.Count != target.Count) return false;
if (values.Count() != target.Count()) return false;
var combinedValues = values.Zip(target, Tuple.Create);
foreach (var combinedValue in combinedValues)
if (!combinedValue.Item1.EqualsCI(combinedValue.Item2)) return false;