1113 lines
47 KiB
C#
1113 lines
47 KiB
C#
// ========================================================================
|
|
// Copyright 2007 - Volian Enterprises, Inc. All rights reserved.
|
|
// Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
|
|
// ------------------------------------------------------------------------
|
|
// $Workfile: $ $Revision: $
|
|
// $Author: $ $Date: $
|
|
//
|
|
// $History: $
|
|
// ========================================================================
|
|
|
|
using System;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using Csla;
|
|
using Csla.Data;
|
|
using System.ComponentModel;
|
|
using System.Collections.Generic;
|
|
using Csla.Validation;
|
|
namespace VEPROMS.CSLA.Library
|
|
{
|
|
/// <summary>
|
|
/// Connection Generated by MyGeneration using the CSLA Object Mapping template
|
|
/// </summary>
|
|
[Serializable()]
|
|
[TypeConverter(typeof(ConnectionConverter))]
|
|
public partial class Connection : BusinessBase<Connection>, IDisposable, IVEHasBrokenRules
|
|
{
|
|
#region Log4Net
|
|
private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
#endregion
|
|
#region Refresh
|
|
private List<Connection> _RefreshConnections = new List<Connection>();
|
|
private List<ConnectionFolder> _RefreshConnectionFolders = new List<ConnectionFolder>();
|
|
private void AddToRefreshList(List<Connection> refreshConnections, List<ConnectionFolder> refreshConnectionFolders)
|
|
{
|
|
if (IsDirty)
|
|
refreshConnections.Add(this);
|
|
if (_ConnectionFolders != null && _ConnectionFolders.IsDirty)
|
|
{
|
|
foreach (ConnectionFolder tmp in _ConnectionFolders)
|
|
{
|
|
if (tmp.IsDirty) refreshConnectionFolders.Add(tmp);
|
|
}
|
|
}
|
|
}
|
|
private void ClearRefreshList()
|
|
{
|
|
_RefreshConnections = new List<Connection>();
|
|
_RefreshConnectionFolders = new List<ConnectionFolder>();
|
|
}
|
|
private void BuildRefreshList()
|
|
{
|
|
ClearRefreshList();
|
|
AddToRefreshList(_RefreshConnections, _RefreshConnectionFolders);
|
|
}
|
|
private void ProcessRefreshList()
|
|
{
|
|
foreach (Connection tmp in _RefreshConnections)
|
|
{
|
|
ConnectionInfo.Refresh(tmp);
|
|
}
|
|
foreach (ConnectionFolder tmp in _RefreshConnectionFolders)
|
|
{
|
|
FolderInfo.Refresh(tmp);
|
|
}
|
|
ClearRefreshList();
|
|
}
|
|
#endregion
|
|
#region Collection
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Keeping Collections Not ReadOnly")]
|
|
private static List<Connection> _CacheList = new List<Connection>();
|
|
protected static void AddToCache(Connection connection)
|
|
{
|
|
if (!_CacheList.Contains(connection)) _CacheList.Add(connection); // In AddToCache
|
|
}
|
|
protected static void RemoveFromCache(Connection connection)
|
|
{
|
|
while (_CacheList.Contains(connection)) _CacheList.Remove(connection); // In RemoveFromCache
|
|
}
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Keeping Collections Not ReadOnly")]
|
|
private static Dictionary<string, List<Connection>> _CacheByPrimaryKey = new Dictionary<string, List<Connection>>();
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Keeping Collections Not ReadOnly")]
|
|
private static Dictionary<string, List<Connection>> _CacheByName = new Dictionary<string, List<Connection>>();
|
|
private static void ConvertListToDictionary()
|
|
{
|
|
while (_CacheList.Count > 0) // Move Connection(s) from temporary _CacheList to _CacheByPrimaryKey
|
|
{
|
|
Connection tmp = _CacheList[0]; // Get the first Connection
|
|
string pKey = tmp.DBID.ToString();
|
|
if (!_CacheByPrimaryKey.ContainsKey(pKey))
|
|
{
|
|
_CacheByPrimaryKey[pKey] = new List<Connection>(); // Add new list for PrimaryKey
|
|
_CacheByName[tmp.Name.ToString()] = new List<Connection>(); // Add new list for Name
|
|
}
|
|
_CacheByPrimaryKey[pKey].Add(tmp); // Add to Primary Key list
|
|
_CacheByName[tmp.Name.ToString()].Add(tmp); // Unique Index
|
|
_CacheList.RemoveAt(0); // Remove the first Connection
|
|
}
|
|
}
|
|
protected static Connection GetCachedByPrimaryKey(int dbid)
|
|
{
|
|
ConvertListToDictionary();
|
|
string key = dbid.ToString();
|
|
if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
|
|
return null;
|
|
}
|
|
protected static Connection GetCachedByName(string name)
|
|
{
|
|
ConvertListToDictionary();
|
|
string key = name.ToString();
|
|
if (_CacheByName.ContainsKey(key)) return _CacheByName[key][0];
|
|
return null;
|
|
}
|
|
#endregion
|
|
#region Business Methods
|
|
private string _ErrorMessage = string.Empty;
|
|
public string ErrorMessage => _ErrorMessage;
|
|
private static int _nextDBID = -1;
|
|
public static int NextDBID => _nextDBID--;
|
|
private int _DBID;
|
|
[System.ComponentModel.DataObjectField(true, true)]
|
|
public int DBID
|
|
{
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
get
|
|
{
|
|
return _DBID;
|
|
}
|
|
}
|
|
private string _Name = string.Empty;
|
|
public string Name
|
|
{
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
get
|
|
{
|
|
return _Name;
|
|
}
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
set
|
|
{
|
|
if (value == null) value = string.Empty;
|
|
if (_Name != value)
|
|
{
|
|
_Name = value;
|
|
PropertyHasChanged();
|
|
}
|
|
}
|
|
}
|
|
private string _Title = string.Empty;
|
|
public string Title
|
|
{
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
get
|
|
{
|
|
return _Title;
|
|
}
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
set
|
|
{
|
|
if (value == null) value = string.Empty;
|
|
if (_Title != value)
|
|
{
|
|
_Title = value;
|
|
PropertyHasChanged();
|
|
}
|
|
}
|
|
}
|
|
private string _ConnectionString = string.Empty;
|
|
public string ConnectionString
|
|
{
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
get
|
|
{
|
|
return _ConnectionString;
|
|
}
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
set
|
|
{
|
|
if (value == null) value = string.Empty;
|
|
if (_ConnectionString != value)
|
|
{
|
|
_ConnectionString = value;
|
|
PropertyHasChanged();
|
|
}
|
|
}
|
|
}
|
|
private int _ServerType;
|
|
/// <summary>
|
|
/// 0 SQL Server
|
|
/// </summary>
|
|
public int ServerType
|
|
{
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
get
|
|
{
|
|
return _ServerType;
|
|
}
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
set
|
|
{
|
|
if (_ServerType != value)
|
|
{
|
|
_ServerType = value;
|
|
PropertyHasChanged();
|
|
}
|
|
}
|
|
}
|
|
private string _Config = string.Empty;
|
|
public string Config
|
|
{
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
get
|
|
{
|
|
return _Config;
|
|
}
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
set
|
|
{
|
|
if (value == null) value = string.Empty;
|
|
if (_Config != value)
|
|
{
|
|
_Config = value;
|
|
PropertyHasChanged();
|
|
}
|
|
}
|
|
}
|
|
private DateTime _DTS = new DateTime();
|
|
public DateTime DTS
|
|
{
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
get
|
|
{
|
|
return _DTS;
|
|
}
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
set
|
|
{
|
|
if (_DTS != value)
|
|
{
|
|
_DTS = value;
|
|
PropertyHasChanged();
|
|
}
|
|
}
|
|
}
|
|
private string _UsrID = string.Empty;
|
|
public string UsrID
|
|
{
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
get
|
|
{
|
|
return _UsrID;
|
|
}
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
set
|
|
{
|
|
if (value == null) value = string.Empty;
|
|
if (_UsrID != value)
|
|
{
|
|
_UsrID = value;
|
|
PropertyHasChanged();
|
|
}
|
|
}
|
|
}
|
|
private byte[] _LastChanged = new byte[8];//timestamp
|
|
private int _ConnectionFolderCount = 0;
|
|
/// <summary>
|
|
/// Count of ConnectionFolders for this Connection
|
|
/// </summary>
|
|
public int ConnectionFolderCount
|
|
{
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
get
|
|
{
|
|
return _ConnectionFolderCount;
|
|
}
|
|
}
|
|
private ConnectionFolders _ConnectionFolders = null;
|
|
/// <summary>
|
|
/// Related Field
|
|
/// </summary>
|
|
[TypeConverter(typeof(ConnectionFoldersConverter))]
|
|
public ConnectionFolders ConnectionFolders
|
|
{
|
|
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
|
get
|
|
{
|
|
if (_ConnectionFolderCount < 0 || (_ConnectionFolderCount > 0 && _ConnectionFolders == null))
|
|
_ConnectionFolders = ConnectionFolders.GetByDBID(DBID);
|
|
if (_ConnectionFolderCount < 0)
|
|
_ConnectionFolderCount = _ConnectionFolders == null ? 0 : _ConnectionFolders.Count;
|
|
if (_ConnectionFolders == null)
|
|
_ConnectionFolders = ConnectionFolders.New();
|
|
return _ConnectionFolders;
|
|
}
|
|
}
|
|
public override bool IsDirty
|
|
{
|
|
get
|
|
{
|
|
if (base.IsDirty)
|
|
return true;
|
|
return IsDirtyList(new List<object>());
|
|
}
|
|
}
|
|
public bool IsDirtyList(List<object> list)
|
|
{
|
|
if (base.IsDirty || list.Contains(this))
|
|
return base.IsDirty;
|
|
list.Add(this);
|
|
return base.IsDirty || (_ConnectionFolders != null && _ConnectionFolders.IsDirtyList(list));
|
|
}
|
|
public bool IsValidList(List<object> list)
|
|
{
|
|
if (list.Contains(this))
|
|
return (IsNew && !IsDirty) || base.IsValid;
|
|
list.Add(this);
|
|
return ((IsNew && !IsDirty) || base.IsValid) && (_ConnectionFolders == null || _ConnectionFolders.IsValidList(list));
|
|
}
|
|
// CSLATODO: Check Connection.GetIdValue to assure that the ID returned is unique
|
|
/// <summary>
|
|
/// Overrides Base GetIdValue - Used internally by CSLA to determine equality
|
|
/// </summary>
|
|
/// <returns>A Unique ID for the current Connection</returns>
|
|
protected override object GetIdValue() => MyConnectionUnique; // Absolutely Unique ID
|
|
#endregion
|
|
#region ValidationRules
|
|
[NonSerialized]
|
|
private bool _CheckingBrokenRules = false;
|
|
public IVEHasBrokenRules HasBrokenRules
|
|
{
|
|
get
|
|
{
|
|
if (_CheckingBrokenRules) return null;
|
|
if ((IsDirty || !IsNew) && BrokenRulesCollection.Count > 0) return this;
|
|
try
|
|
{
|
|
_CheckingBrokenRules = true;
|
|
IVEHasBrokenRules hasBrokenRules = null;
|
|
if (_ConnectionFolders != null && (hasBrokenRules = _ConnectionFolders.HasBrokenRules) != null) return hasBrokenRules;
|
|
return hasBrokenRules;
|
|
}
|
|
finally
|
|
{
|
|
_CheckingBrokenRules = false;
|
|
}
|
|
}
|
|
}
|
|
public BrokenRulesCollection BrokenRules
|
|
{
|
|
get
|
|
{
|
|
IVEHasBrokenRules hasBrokenRules = HasBrokenRules;
|
|
if (Equals(hasBrokenRules)) return BrokenRulesCollection;
|
|
return (hasBrokenRules?.BrokenRules);
|
|
}
|
|
}
|
|
protected override void AddBusinessRules()
|
|
{
|
|
ValidationRules.AddRule(
|
|
Csla.Validation.CommonRules.StringMaxLength,
|
|
new Csla.Validation.CommonRules.MaxLengthRuleArgs("Name", 100));
|
|
ValidationRules.AddRule(
|
|
Csla.Validation.CommonRules.StringMaxLength,
|
|
new Csla.Validation.CommonRules.MaxLengthRuleArgs("Title", 510));
|
|
ValidationRules.AddRule(
|
|
Csla.Validation.CommonRules.StringMaxLength,
|
|
new Csla.Validation.CommonRules.MaxLengthRuleArgs("ConnectionString", 510));
|
|
ValidationRules.AddRule(
|
|
Csla.Validation.CommonRules.StringMaxLength,
|
|
new Csla.Validation.CommonRules.MaxLengthRuleArgs("Config", 1073741823));
|
|
ValidationRules.AddRule(
|
|
Csla.Validation.CommonRules.StringRequired, "UsrID");
|
|
ValidationRules.AddRule(
|
|
Csla.Validation.CommonRules.StringMaxLength,
|
|
new Csla.Validation.CommonRules.MaxLengthRuleArgs("UsrID", 100));
|
|
//ValidationRules.AddDependantProperty("x", "y");
|
|
_ConnectionExtension.AddValidationRules(ValidationRules);
|
|
// CSLATODO: Add other validation rules
|
|
}
|
|
protected override void AddInstanceBusinessRules()
|
|
{
|
|
_ConnectionExtension.AddInstanceValidationRules(ValidationRules);
|
|
// CSLATODO: Add other validation rules
|
|
}
|
|
#endregion
|
|
#region Factory Methods
|
|
public int CurrentEditLevel => EditLevel;
|
|
private static int _ConnectionUnique = 0;
|
|
protected static int ConnectionUnique => ++_ConnectionUnique;
|
|
private readonly int _MyConnectionUnique = ConnectionUnique;
|
|
// Absolutely Unique ID - Editable
|
|
public int MyConnectionUnique => _MyConnectionUnique;
|
|
protected Connection()
|
|
{/* require use of factory methods */
|
|
AddToCache(this);
|
|
}
|
|
private bool _Disposed = false;
|
|
private static int _CountCreated = 0;
|
|
private static int _CountDisposed = 0;
|
|
private static int _CountFinalized = 0;
|
|
private static int IncrementCountCreated => ++_CountCreated;
|
|
private readonly int _CountWhenCreated = IncrementCountCreated;
|
|
public static int CountCreated => _CountCreated;
|
|
public static int CountNotDisposed => _CountCreated - _CountDisposed;
|
|
public static int CountNotFinalized => _CountCreated - _CountFinalized;
|
|
~Connection()
|
|
{
|
|
_CountFinalized++;
|
|
}
|
|
public void Dispose()
|
|
{
|
|
if (_Disposed) return;
|
|
_CountDisposed++;
|
|
_Disposed = true;
|
|
RemoveFromDictionaries();
|
|
}
|
|
private void RemoveFromDictionaries()
|
|
{
|
|
RemoveFromCache(this);
|
|
if (_CacheByPrimaryKey.ContainsKey(DBID.ToString()))
|
|
{
|
|
List<Connection> listConnection = _CacheByPrimaryKey[DBID.ToString()]; // Get the list of items
|
|
while (listConnection.Contains(this)) listConnection.Remove(this); // Remove the item from the list
|
|
if (listConnection.Count == 0) //If there are no items left in the list
|
|
_CacheByPrimaryKey.Remove(DBID.ToString()); // remove the list
|
|
}
|
|
string myKey;
|
|
myKey = null;
|
|
foreach (string key in _CacheByName.Keys)
|
|
if (_CacheByName[key].Contains(this))
|
|
myKey = key;
|
|
if (myKey != null)
|
|
{
|
|
List<Connection> listConnection = _CacheByName[myKey]; // Get the list of items
|
|
listConnection.Remove(this); // Remove the item from the list
|
|
if (listConnection.Count == 0) //If there are no items left in the list
|
|
_CacheByName.Remove(myKey); // remove the list
|
|
}
|
|
}
|
|
public static Connection New()
|
|
{
|
|
try
|
|
{
|
|
return DataPortal.Create<Connection>();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new DbCslaException("Error on Connection.New", ex);
|
|
}
|
|
}
|
|
public static Connection New(string name, string title, string connectionString, int serverType, string config, DateTime dts, string usrID)
|
|
{
|
|
Connection tmp = Connection.New();
|
|
tmp.Name = name;
|
|
tmp.Title = title;
|
|
tmp.ConnectionString = connectionString;
|
|
tmp.ServerType = serverType;
|
|
tmp.Config = config;
|
|
tmp.DTS = dts;
|
|
tmp.UsrID = usrID;
|
|
return tmp;
|
|
}
|
|
public static Connection MakeConnection(string name, string title, string connectionString, int serverType, string config, DateTime dts, string usrID)
|
|
{
|
|
Connection tmp = Connection.New(name, title, connectionString, serverType, config, dts, usrID);
|
|
if (tmp.IsSavable)
|
|
tmp = tmp.Save();
|
|
else
|
|
{
|
|
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
|
|
tmp._ErrorMessage = "Failed Validation:";
|
|
foreach (Csla.Validation.BrokenRule br in brc)
|
|
{
|
|
tmp._ErrorMessage += "\r\n\tFailure: " + br.RuleName;
|
|
}
|
|
}
|
|
return tmp;
|
|
}
|
|
public static Connection New(string name, string title, string connectionString, string config)
|
|
{
|
|
Connection tmp = Connection.New();
|
|
tmp.Name = name;
|
|
tmp.Title = title;
|
|
tmp.ConnectionString = connectionString;
|
|
tmp.Config = config;
|
|
return tmp;
|
|
}
|
|
public static Connection MakeConnection(string name, string title, string connectionString, string config)
|
|
{
|
|
Connection tmp = Connection.New(name, title, connectionString, config);
|
|
if (tmp.IsSavable)
|
|
tmp = tmp.Save();
|
|
else
|
|
{
|
|
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
|
|
tmp._ErrorMessage = "Failed Validation:";
|
|
foreach (Csla.Validation.BrokenRule br in brc)
|
|
{
|
|
tmp._ErrorMessage += "\r\n\tFailure: " + br.RuleName;
|
|
}
|
|
}
|
|
return tmp;
|
|
}
|
|
public static Connection Get(int dbid)
|
|
{
|
|
try
|
|
{
|
|
Connection tmp = GetCachedByPrimaryKey(dbid);
|
|
if (tmp == null)
|
|
{
|
|
tmp = DataPortal.Fetch<Connection>(new PKCriteria(dbid));
|
|
AddToCache(tmp);
|
|
}
|
|
if (tmp.ErrorMessage == "No Record Found")
|
|
{
|
|
tmp.Dispose(); // Clean-up Connection
|
|
tmp = null;
|
|
}
|
|
return tmp;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new DbCslaException("Error on Connection.Get", ex);
|
|
}
|
|
}
|
|
public static Connection GetByName(string name)
|
|
{
|
|
try
|
|
{
|
|
Connection tmp = GetCachedByName(name);
|
|
if (tmp == null)
|
|
{
|
|
tmp = DataPortal.Fetch<Connection>(new NameCriteria(name));
|
|
AddToCache(tmp);
|
|
}
|
|
if (tmp.ErrorMessage == "No Record Found")
|
|
{
|
|
tmp.Dispose(); // Clean-up Connection
|
|
tmp = null;
|
|
}
|
|
return tmp;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new DbCslaException("Error on Connection.GetByName", ex);
|
|
}
|
|
}
|
|
public static Connection Get(SafeDataReader dr)
|
|
{
|
|
if (dr.Read()) return new Connection(dr);
|
|
return null;
|
|
}
|
|
internal Connection(SafeDataReader dr) => ReadData(dr);
|
|
public static void Delete(int dbid)
|
|
{
|
|
try
|
|
{
|
|
DataPortal.Delete(new PKCriteria(dbid));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new DbCslaException("Error on Connection.Delete", ex);
|
|
}
|
|
}
|
|
public override Connection Save()
|
|
{
|
|
try
|
|
{
|
|
BuildRefreshList();
|
|
Connection connection = base.Save();
|
|
RemoveFromDictionaries(); // if save is successful remove the previous Folder from the cache
|
|
AddToCache(connection);//Refresh the item in AllList
|
|
ProcessRefreshList();
|
|
return connection;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new DbCslaException("Error on CSLA Save", ex);
|
|
}
|
|
}
|
|
#endregion
|
|
#region Data Access Portal
|
|
[Serializable()]
|
|
protected class PKCriteria
|
|
{
|
|
private readonly int _DBID;
|
|
public int DBID => _DBID;
|
|
public PKCriteria(int dbid) => _DBID = dbid;
|
|
}
|
|
[Serializable()]
|
|
private class NameCriteria
|
|
{
|
|
private readonly string _Name;
|
|
public string Name => _Name;
|
|
public NameCriteria(string name) => _Name = name;
|
|
}
|
|
// CSLATODO: If Create needs to access DB - It should not be marked RunLocal
|
|
[RunLocal()]
|
|
private new void DataPortal_Create()
|
|
{
|
|
_DBID = NextDBID;
|
|
// Database Defaults
|
|
_ServerType = _ConnectionExtension.DefaultServerType;
|
|
_DTS = _ConnectionExtension.DefaultDTS;
|
|
_UsrID = _ConnectionExtension.DefaultUsrID;
|
|
// CSLATODO: Add any defaults that are necessary
|
|
ValidationRules.CheckRules();
|
|
}
|
|
private void ReadData(SafeDataReader dr)
|
|
{
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Connection.ReadData", GetHashCode());
|
|
try
|
|
{
|
|
_DBID = dr.GetInt32("DBID");
|
|
_Name = dr.GetString("Name");
|
|
_Title = dr.GetString("Title");
|
|
_ConnectionString = dr.GetString("ConnectionString");
|
|
_ServerType = dr.GetInt32("ServerType");
|
|
_Config = dr.GetString("Config");
|
|
_DTS = dr.GetDateTime("DTS");
|
|
_UsrID = dr.GetString("UsrID");
|
|
dr.GetBytes("LastChanged", 0, _LastChanged, 0, 8);
|
|
_ConnectionFolderCount = dr.GetInt32("FolderCount");
|
|
MarkOld();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_MyLog.IsErrorEnabled) _MyLog.Error("Connection.ReadData", ex);
|
|
_ErrorMessage = ex.Message;
|
|
throw new DbCslaException("Connection.ReadData", ex);
|
|
}
|
|
}
|
|
private void DataPortal_Fetch(PKCriteria criteria)
|
|
{
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Connection.DataPortal_Fetch", GetHashCode());
|
|
try
|
|
{
|
|
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
|
|
{
|
|
ApplicationContext.LocalContext["cn"] = cn;
|
|
using (SqlCommand cm = cn.CreateCommand())
|
|
{
|
|
cm.CommandType = CommandType.StoredProcedure;
|
|
cm.CommandText = "getConnection";
|
|
cm.Parameters.AddWithValue("@DBID", criteria.DBID);
|
|
cm.CommandTimeout = Database.DefaultTimeout;
|
|
using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
|
|
{
|
|
if (!dr.Read())
|
|
{
|
|
_ErrorMessage = "No Record Found";
|
|
return;
|
|
}
|
|
ReadData(dr);
|
|
// load child objects
|
|
dr.NextResult();
|
|
_ConnectionFolders = ConnectionFolders.Get(dr);
|
|
}
|
|
}
|
|
// removing of item only needed for local data portal
|
|
if (ApplicationContext.ExecutionLocation == ApplicationContext.ExecutionLocations.Client)
|
|
ApplicationContext.LocalContext.Remove("cn");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_MyLog.IsErrorEnabled) _MyLog.Error("Connection.DataPortal_Fetch", ex);
|
|
_ErrorMessage = ex.Message;
|
|
throw new DbCslaException("Connection.DataPortal_Fetch", ex);
|
|
}
|
|
}
|
|
private void DataPortal_Fetch(NameCriteria criteria)
|
|
{
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Connection.DataPortal_Fetch", GetHashCode());
|
|
try
|
|
{
|
|
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
|
|
{
|
|
ApplicationContext.LocalContext["cn"] = cn;
|
|
using (SqlCommand cm = cn.CreateCommand())
|
|
{
|
|
cm.CommandType = CommandType.StoredProcedure;
|
|
cm.CommandText = "getConnectionByName";
|
|
cm.Parameters.AddWithValue("@Name", criteria.Name);
|
|
cm.CommandTimeout = Database.DefaultTimeout;
|
|
using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
|
|
{
|
|
if (!dr.Read())
|
|
{
|
|
_ErrorMessage = "No Record Found";
|
|
return;
|
|
}
|
|
ReadData(dr);
|
|
}
|
|
}
|
|
// removing of item only needed for local data portal
|
|
if (ApplicationContext.ExecutionLocation == ApplicationContext.ExecutionLocations.Client)
|
|
ApplicationContext.LocalContext.Remove("cn");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_MyLog.IsErrorEnabled) _MyLog.Error("Connection.DataPortal_Fetch", ex);
|
|
_ErrorMessage = ex.Message;
|
|
throw new DbCslaException("Connection.DataPortal_Fetch", ex);
|
|
}
|
|
}
|
|
[Transactional(TransactionalTypes.TransactionScope)]
|
|
protected override void DataPortal_Insert()
|
|
{
|
|
try
|
|
{
|
|
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
|
|
{
|
|
ApplicationContext.LocalContext["cn"] = cn;
|
|
SQLInsert();
|
|
// removing of item only needed for local data portal
|
|
if (ApplicationContext.ExecutionLocation == ApplicationContext.ExecutionLocations.Client)
|
|
ApplicationContext.LocalContext.Remove("cn");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_MyLog.IsErrorEnabled) _MyLog.Error("Connection.DataPortal_Insert", ex);
|
|
_ErrorMessage = ex.Message;
|
|
throw new DbCslaException("Connection.DataPortal_Insert", ex);
|
|
}
|
|
finally
|
|
{
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Connection.DataPortal_Insert", GetHashCode());
|
|
}
|
|
}
|
|
[Transactional(TransactionalTypes.TransactionScope)]
|
|
internal void SQLInsert()
|
|
{
|
|
if (!IsDirty) return;
|
|
try
|
|
{
|
|
using (SqlConnection cn = (SqlConnection)ApplicationContext.LocalContext["cn"])
|
|
{
|
|
using (SqlCommand cm = cn.CreateCommand())
|
|
{
|
|
cm.CommandType = CommandType.StoredProcedure;
|
|
cm.CommandTimeout = Database.SQLTimeout;
|
|
cm.CommandText = "addConnection";
|
|
// Input All Fields - Except Calculated Columns
|
|
cm.Parameters.AddWithValue("@Name", _Name);
|
|
cm.Parameters.AddWithValue("@Title", _Title);
|
|
cm.Parameters.AddWithValue("@ConnectionString", _ConnectionString);
|
|
cm.Parameters.AddWithValue("@ServerType", _ServerType);
|
|
cm.Parameters.AddWithValue("@Config", _Config);
|
|
if (_DTS.Year >= 1753 && _DTS.Year <= 9999) cm.Parameters.AddWithValue("@DTS", _DTS);
|
|
cm.Parameters.AddWithValue("@UsrID", _UsrID);
|
|
// Output Calculated Columns
|
|
SqlParameter param_DBID = new SqlParameter("@newDBID", SqlDbType.Int)
|
|
{
|
|
Direction = ParameterDirection.Output
|
|
};
|
|
cm.Parameters.Add(param_DBID);
|
|
SqlParameter param_LastChanged = new SqlParameter("@newLastChanged", SqlDbType.Timestamp)
|
|
{
|
|
Direction = ParameterDirection.Output
|
|
};
|
|
cm.Parameters.Add(param_LastChanged);
|
|
// CSLATODO: Define any additional output parameters
|
|
cm.ExecuteNonQuery();
|
|
// Save all values being returned from the Procedure
|
|
_DBID = (int)cm.Parameters["@newDBID"].Value;
|
|
_LastChanged = (byte[])cm.Parameters["@newLastChanged"].Value;
|
|
}
|
|
}
|
|
|
|
MarkOld();
|
|
// update child objects
|
|
_ConnectionFolders?.Update(this);
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Connection.SQLInsert", GetHashCode());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_MyLog.IsErrorEnabled) _MyLog.Error("Connection.SQLInsert", ex);
|
|
_ErrorMessage = ex.Message;
|
|
throw new DbCslaException("Connection.SQLInsert", ex);
|
|
}
|
|
}
|
|
[Transactional(TransactionalTypes.TransactionScope)]
|
|
public static byte[] Add(SqlConnection cn, ref int dbid, string name, string title, string connectionString, int serverType, string config, DateTime dts, string usrID)
|
|
{
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Connection.Add", 0);
|
|
try
|
|
{
|
|
using (SqlCommand cm = cn.CreateCommand())
|
|
{
|
|
cm.CommandType = CommandType.StoredProcedure;
|
|
cm.CommandTimeout = Database.SQLTimeout;
|
|
cm.CommandText = "addConnection";
|
|
// Input All Fields - Except Calculated Columns
|
|
cm.Parameters.AddWithValue("@Name", name);
|
|
cm.Parameters.AddWithValue("@Title", title);
|
|
cm.Parameters.AddWithValue("@ConnectionString", connectionString);
|
|
cm.Parameters.AddWithValue("@ServerType", serverType);
|
|
cm.Parameters.AddWithValue("@Config", config);
|
|
if (dts.Year >= 1753 && dts.Year <= 9999) cm.Parameters.AddWithValue("@DTS", dts);
|
|
cm.Parameters.AddWithValue("@UsrID", usrID);
|
|
// Output Calculated Columns
|
|
SqlParameter param_DBID = new SqlParameter("@newDBID", SqlDbType.Int)
|
|
{
|
|
Direction = ParameterDirection.Output
|
|
};
|
|
cm.Parameters.Add(param_DBID);
|
|
SqlParameter param_LastChanged = new SqlParameter("@newLastChanged", SqlDbType.Timestamp)
|
|
{
|
|
Direction = ParameterDirection.Output
|
|
};
|
|
cm.Parameters.Add(param_LastChanged);
|
|
// CSLATODO: Define any additional output parameters
|
|
cm.ExecuteNonQuery();
|
|
// Save all values being returned from the Procedure
|
|
dbid = (int)cm.Parameters["@newDBID"].Value;
|
|
return (byte[])cm.Parameters["@newLastChanged"].Value;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_MyLog.IsErrorEnabled) _MyLog.Error("Connection.Add", ex);
|
|
throw new DbCslaException("Connection.Add", ex);
|
|
}
|
|
}
|
|
[Transactional(TransactionalTypes.TransactionScope)]
|
|
protected override void DataPortal_Update()
|
|
{
|
|
if (!IsDirty) return; // If not dirty - nothing to do
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Connection.DataPortal_Update", GetHashCode());
|
|
try
|
|
{
|
|
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
|
|
{
|
|
ApplicationContext.LocalContext["cn"] = cn;
|
|
SQLUpdate();
|
|
// removing of item only needed for local data portal
|
|
if (ApplicationContext.ExecutionLocation == ApplicationContext.ExecutionLocations.Client)
|
|
ApplicationContext.LocalContext.Remove("cn");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_MyLog.IsErrorEnabled) _MyLog.Error("Connection.DataPortal_Update", ex);
|
|
_ErrorMessage = ex.Message;
|
|
if (!ex.Message.EndsWith("has been edited by another user.")) throw ex;
|
|
}
|
|
}
|
|
[Transactional(TransactionalTypes.TransactionScope)]
|
|
internal void SQLUpdate()
|
|
{
|
|
if (!IsDirty) return; // If not dirty - nothing to do
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Connection.SQLUpdate", GetHashCode());
|
|
try
|
|
{
|
|
using (SqlConnection cn = (SqlConnection)ApplicationContext.LocalContext["cn"])
|
|
{
|
|
if (base.IsDirty)
|
|
{
|
|
using (SqlCommand cm = cn.CreateCommand())
|
|
{
|
|
cm.CommandType = CommandType.StoredProcedure;
|
|
cm.CommandTimeout = Database.SQLTimeout;
|
|
cm.CommandText = "updateConnection";
|
|
// All Fields including Calculated Fields
|
|
cm.Parameters.AddWithValue("@DBID", _DBID);
|
|
cm.Parameters.AddWithValue("@Name", _Name);
|
|
cm.Parameters.AddWithValue("@Title", _Title);
|
|
cm.Parameters.AddWithValue("@ConnectionString", _ConnectionString);
|
|
cm.Parameters.AddWithValue("@ServerType", _ServerType);
|
|
cm.Parameters.AddWithValue("@Config", _Config);
|
|
if (_DTS.Year >= 1753 && _DTS.Year <= 9999) cm.Parameters.AddWithValue("@DTS", _DTS);
|
|
cm.Parameters.AddWithValue("@UsrID", _UsrID);
|
|
cm.Parameters.AddWithValue("@LastChanged", _LastChanged);
|
|
// Output Calculated Columns
|
|
SqlParameter param_LastChanged = new SqlParameter("@newLastChanged", SqlDbType.Timestamp)
|
|
{
|
|
Direction = ParameterDirection.Output
|
|
};
|
|
cm.Parameters.Add(param_LastChanged);
|
|
// CSLATODO: Define any additional output parameters
|
|
cm.ExecuteNonQuery();
|
|
// Save all values being returned from the Procedure
|
|
_LastChanged = (byte[])cm.Parameters["@newLastChanged"].Value;
|
|
}
|
|
}
|
|
}
|
|
|
|
MarkOld();
|
|
// use the open connection to update child objects
|
|
_ConnectionFolders?.Update(this);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_MyLog.IsErrorEnabled) _MyLog.Error("Connection.SQLUpdate", ex);
|
|
_ErrorMessage = ex.Message;
|
|
if (!ex.Message.EndsWith("has been edited by another user.")) throw ex;
|
|
}
|
|
}
|
|
internal void Update()
|
|
{
|
|
if (!IsDirty) return;
|
|
if (base.IsDirty)
|
|
{
|
|
using (SqlConnection cn = (SqlConnection)ApplicationContext.LocalContext["cn"])
|
|
{
|
|
if (IsNew)
|
|
_LastChanged = Connection.Add(cn, ref _DBID, _Name, _Title, _ConnectionString, _ServerType, _Config, _DTS, _UsrID);
|
|
else
|
|
_LastChanged = Connection.Update(cn, ref _DBID, _Name, _Title, _ConnectionString, _ServerType, _Config, _DTS, _UsrID, ref _LastChanged);
|
|
}
|
|
|
|
MarkOld();
|
|
}
|
|
_ConnectionFolders?.Update(this);
|
|
}
|
|
[Transactional(TransactionalTypes.TransactionScope)]
|
|
public static byte[] Update(SqlConnection cn, ref int dbid, string name, string title, string connectionString, int serverType, string config, DateTime dts, string usrID, ref byte[] lastChanged)
|
|
{
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Connection.Update", 0);
|
|
try
|
|
{
|
|
using (SqlCommand cm = cn.CreateCommand())
|
|
{
|
|
cm.CommandType = CommandType.StoredProcedure;
|
|
cm.CommandTimeout = Database.SQLTimeout;
|
|
cm.CommandText = "updateConnection";
|
|
// Input All Fields - Except Calculated Columns
|
|
cm.Parameters.AddWithValue("@DBID", dbid);
|
|
cm.Parameters.AddWithValue("@Name", name);
|
|
cm.Parameters.AddWithValue("@Title", title);
|
|
cm.Parameters.AddWithValue("@ConnectionString", connectionString);
|
|
cm.Parameters.AddWithValue("@ServerType", serverType);
|
|
cm.Parameters.AddWithValue("@Config", config);
|
|
if (dts.Year >= 1753 && dts.Year <= 9999) cm.Parameters.AddWithValue("@DTS", dts);
|
|
cm.Parameters.AddWithValue("@UsrID", usrID);
|
|
cm.Parameters.AddWithValue("@LastChanged", lastChanged);
|
|
// Output Calculated Columns
|
|
SqlParameter param_LastChanged = new SqlParameter("@newLastChanged", SqlDbType.Timestamp)
|
|
{
|
|
Direction = ParameterDirection.Output
|
|
};
|
|
cm.Parameters.Add(param_LastChanged);
|
|
// CSLATODO: Define any additional output parameters
|
|
cm.ExecuteNonQuery();
|
|
// Save all values being returned from the Procedure
|
|
return (byte[])cm.Parameters["@newLastChanged"].Value;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_MyLog.IsErrorEnabled) _MyLog.Error("Connection.Update", ex);
|
|
throw new DbCslaException("Connection.Update", ex);
|
|
}
|
|
}
|
|
[Transactional(TransactionalTypes.TransactionScope)]
|
|
protected override void DataPortal_DeleteSelf()
|
|
{
|
|
DataPortal_Delete(new PKCriteria(_DBID));
|
|
}
|
|
[Transactional(TransactionalTypes.TransactionScope)]
|
|
private void DataPortal_Delete(PKCriteria criteria)
|
|
{
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Connection.DataPortal_Delete", GetHashCode());
|
|
try
|
|
{
|
|
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
|
|
{
|
|
using (SqlCommand cm = cn.CreateCommand())
|
|
{
|
|
cm.CommandType = CommandType.StoredProcedure;
|
|
cm.CommandTimeout = Database.SQLTimeout;
|
|
cm.CommandText = "deleteConnection";
|
|
cm.Parameters.AddWithValue("@DBID", criteria.DBID);
|
|
cm.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_MyLog.IsErrorEnabled) _MyLog.Error("Connection.DataPortal_Delete", ex);
|
|
_ErrorMessage = ex.Message;
|
|
throw new DbCslaException("Connection.DataPortal_Delete", ex);
|
|
}
|
|
}
|
|
[Transactional(TransactionalTypes.TransactionScope)]
|
|
public static void Remove(SqlConnection cn, int dbid)
|
|
{
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Connection.Remove", 0);
|
|
try
|
|
{
|
|
using (SqlCommand cm = cn.CreateCommand())
|
|
{
|
|
cm.CommandType = CommandType.StoredProcedure;
|
|
cm.CommandTimeout = Database.SQLTimeout;
|
|
cm.CommandText = "deleteConnection";
|
|
// Input PK Fields
|
|
cm.Parameters.AddWithValue("@DBID", dbid);
|
|
// CSLATODO: Define any additional output parameters
|
|
cm.ExecuteNonQuery();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_MyLog.IsErrorEnabled) _MyLog.Error("Connection.Remove", ex);
|
|
throw new DbCslaException("Connection.Remove", ex);
|
|
}
|
|
}
|
|
#endregion
|
|
#region Exists
|
|
public static bool Exists(int dbid)
|
|
{
|
|
ExistsCommand result;
|
|
try
|
|
{
|
|
result = DataPortal.Execute<ExistsCommand>(new ExistsCommand(dbid));
|
|
return result.Exists;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new DbCslaException("Error on Connection.Exists", ex);
|
|
}
|
|
}
|
|
[Serializable()]
|
|
private class ExistsCommand : CommandBase
|
|
{
|
|
private readonly int _DBID;
|
|
private bool _exists;
|
|
public bool Exists => _exists;
|
|
public ExistsCommand(int dbid) => _DBID = dbid;
|
|
protected override void DataPortal_Execute()
|
|
{
|
|
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Connection.DataPortal_Execute", GetHashCode());
|
|
try
|
|
{
|
|
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
|
|
{
|
|
cn.Open();
|
|
using (SqlCommand cm = cn.CreateCommand())
|
|
{
|
|
cm.CommandType = CommandType.StoredProcedure;
|
|
cm.CommandTimeout = Database.SQLTimeout;
|
|
cm.CommandText = "existsConnection";
|
|
cm.Parameters.AddWithValue("@DBID", _DBID);
|
|
int count = (int)cm.ExecuteScalar();
|
|
_exists = (count > 0);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_MyLog.IsErrorEnabled) _MyLog.Error("Connection.DataPortal_Execute", ex);
|
|
throw new DbCslaException("Connection.DataPortal_Execute", ex);
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
// Standard Default Code
|
|
#region extension
|
|
readonly ConnectionExtension _ConnectionExtension = new ConnectionExtension();
|
|
[Serializable()]
|
|
partial class ConnectionExtension : extensionBase
|
|
{
|
|
}
|
|
[Serializable()]
|
|
class extensionBase
|
|
{
|
|
// Default Values
|
|
public virtual int DefaultServerType => 1;
|
|
public virtual DateTime DefaultDTS => DateTime.Now;
|
|
public virtual string DefaultUsrID => Volian.Base.Library.VlnSettings.UserID;
|
|
// Authorization Rules
|
|
public virtual void AddAuthorizationRules(Csla.Security.AuthorizationRules rules)
|
|
{
|
|
// Needs to be overriden to add new authorization rules
|
|
}
|
|
// Instance Authorization Rules
|
|
public virtual void AddInstanceAuthorizationRules(Csla.Security.AuthorizationRules rules)
|
|
{
|
|
// Needs to be overriden to add new authorization rules
|
|
}
|
|
// Validation Rules
|
|
public virtual void AddValidationRules(Csla.Validation.ValidationRules rules)
|
|
{
|
|
// Needs to be overriden to add new validation rules
|
|
}
|
|
// InstanceValidation Rules
|
|
public virtual void AddInstanceValidationRules(Csla.Validation.ValidationRules rules)
|
|
{
|
|
// Needs to be overriden to add new validation rules
|
|
}
|
|
}
|
|
#endregion
|
|
} // Class
|
|
#region Converter
|
|
internal class ConnectionConverter : ExpandableObjectConverter
|
|
{
|
|
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destType)
|
|
{
|
|
if (destType == typeof(string) && value is Connection conn)
|
|
{
|
|
// Return the ToString value
|
|
return conn.ToString();
|
|
}
|
|
return base.ConvertTo(context, culture, value, destType);
|
|
}
|
|
}
|
|
#endregion
|
|
} // Namespace
|
|
|