This commit is contained in:
Kathy Ruffing 2008-08-12 11:22:10 +00:00
parent d0dba2aad4
commit 0c505e9455
3 changed files with 146 additions and 75 deletions

View File

@ -64,38 +64,46 @@ namespace VEPROMS.CSLA.Library
}
#endregion
#region Collection
protected static List<Connection> _AllList = new List<Connection>();
private static Dictionary<string, List<Connection>> _AllByPrimaryKey = new Dictionary<string, List<Connection>>();
private static Dictionary<string, List<Connection>> _AllByName = new Dictionary<string, List<Connection>>();
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
}
private static Dictionary<string, List<Connection>> _CacheByPrimaryKey = new Dictionary<string, List<Connection>>();
private static Dictionary<string, List<Connection>> _CacheByName = new Dictionary<string, List<Connection>>();
private static void ConvertListToDictionary()
{
List<Connection> remove = new List<Connection>();
foreach (Connection tmp in _AllList)
foreach (Connection tmp in _CacheList)
{
if (!_AllByPrimaryKey.ContainsKey(tmp.DBID.ToString()))
if (!_CacheByPrimaryKey.ContainsKey(tmp.DBID.ToString()))
{
_AllByPrimaryKey[tmp.DBID.ToString()] = new List<Connection>(); // Add new list for PrimaryKey
_AllByName[tmp.Name.ToString()] = new List<Connection>(); // Add new list for Name
_CacheByPrimaryKey[tmp.DBID.ToString()] = new List<Connection>(); // Add new list for PrimaryKey
_CacheByName[tmp.Name.ToString()] = new List<Connection>(); // Add new list for Name
}
_AllByPrimaryKey[tmp.DBID.ToString()].Add(tmp); // Add to Primary Key list
_AllByName[tmp.Name.ToString()].Add(tmp); // Unique Index
_CacheByPrimaryKey[tmp.DBID.ToString()].Add(tmp); // Add to Primary Key list
_CacheByName[tmp.Name.ToString()].Add(tmp); // Unique Index
remove.Add(tmp);
}
foreach (Connection tmp in remove)
_AllList.Remove(tmp);
RemoveFromCache(tmp);
}
public static Connection GetExistingByPrimaryKey(int dbid)
protected static Connection GetCachedByPrimaryKey(int dbid)
{
ConvertListToDictionary();
string key = dbid.ToString();
if (_AllByPrimaryKey.ContainsKey(key)) return _AllByPrimaryKey[key][0];
if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
return null;
}
public static Connection GetExistingByName(string name)
protected static Connection GetCachedByName(string name)
{
ConvertListToDictionary();
string key = name.ToString();
if (_AllByName.ContainsKey(key)) return _AllByName[key][0];
if (_CacheByName.ContainsKey(key)) return _CacheByName[key][0];
return null;
}
#endregion
@ -469,28 +477,41 @@ namespace VEPROMS.CSLA.Library
public int CurrentEditLevel
{ get { return EditLevel; } }
private static int _ConnectionUnique = 0;
private int _MyConnectionUnique;
protected static int ConnectionUnique
{ get { return ++_ConnectionUnique; } }
private int _MyConnectionUnique = ConnectionUnique;
public int MyConnectionUnique
{
get { return _MyConnectionUnique; }
}
{ get { return _MyConnectionUnique; } }
protected Connection()
{/* require use of factory methods */
_MyConnectionUnique = ++_ConnectionUnique;
_AllList.Add(this);
AddToCache(this);
}
public void Dispose()
{
_AllList.Remove(this);
if (!_AllByPrimaryKey.ContainsKey(DBID.ToString())) return;
List<Connection> listConnection = _AllByPrimaryKey[DBID.ToString()]; // 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
_AllByPrimaryKey.Remove(DBID.ToString()); // remove the list
listConnection = _AllByName[DBID.ToString()]; // 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
_AllByName.Remove(DBID.ToString()); // remove the list
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()
{
@ -521,7 +542,11 @@ namespace VEPROMS.CSLA.Library
{
Connection tmp = Connection.New(name, title, connectionString, serverType, config, dts, usrID);
if (tmp.IsSavable)
tmp = tmp.Save();
{
Connection tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else
{
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@ -546,7 +571,11 @@ namespace VEPROMS.CSLA.Library
{
Connection tmp = Connection.New(name, title, connectionString, config);
if (tmp.IsSavable)
tmp = tmp.Save();
{
Connection tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else
{
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@ -564,13 +593,17 @@ namespace VEPROMS.CSLA.Library
throw new System.Security.SecurityException("User not authorized to view a Connection");
try
{
Connection tmp = GetExistingByPrimaryKey(dbid);
Connection tmp = GetCachedByPrimaryKey(dbid);
if (tmp == null)
{
tmp = DataPortal.Fetch<Connection>(new PKCriteria(dbid));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up Connection
tmp = null;
}
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp;
}
catch (Exception ex)
@ -578,19 +611,23 @@ namespace VEPROMS.CSLA.Library
throw new DbCslaException("Error on Connection.Get", ex);
}
}
private static Connection GetByName(string name)
public static Connection GetByName(string name)
{
if (!CanGetObject())
throw new System.Security.SecurityException("User not authorized to view a Connection");
try
{
Connection tmp = GetExistingByName(name);
Connection tmp = GetCachedByName(name);
if (tmp == null)
{
tmp = DataPortal.Fetch<Connection>(new NameCriteria(name));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up Connection
tmp = null;
}
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp;
}
catch (Exception ex)
@ -632,7 +669,8 @@ namespace VEPROMS.CSLA.Library
{
BuildRefreshList();
Connection connection = base.Save();
_AllList.Add(connection);//Refresh the item in AllList
RemoveFromDictionaries(); // if save is successful remove the previous Folder from the cache
AddToCache(connection);//Refresh the item in AllList
ProcessRefreshList();
return connection;
}

View File

@ -179,7 +179,7 @@ namespace VEPROMS.CSLA.Library
set
{
CanWriteProperty("MyFormat", true);
if (_MyFormat != value)
if ((_MyFormat == null ? _FormatID : (int?)_MyFormat.FormatID) != (value == null ? null : (int?)value.FormatID))
{
_MyFormat = value;
_FormatID = (value == null ? null : (int?)value.FormatID);
@ -187,6 +187,26 @@ namespace VEPROMS.CSLA.Library
}
}
}
private double? _ManualOrder;
public double? ManualOrder
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty("ManualOrder", true);
return _ManualOrder;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
set
{
CanWriteProperty("ManualOrder", true);
if (_ManualOrder != value)
{
_ManualOrder = value;
PropertyHasChanged();
}
}
}
private string _Config = string.Empty;
public string Config
{
@ -371,6 +391,8 @@ namespace VEPROMS.CSLA.Library
//AuthorizationRules.AllowWrite(ShortName, "<Role(s)>");
//AuthorizationRules.AllowRead(FormatID, "<Role(s)>");
//AuthorizationRules.AllowWrite(FormatID, "<Role(s)>");
//AuthorizationRules.AllowRead(ManualOrder, "<Role(s)>");
//AuthorizationRules.AllowWrite(ManualOrder, "<Role(s)>");
//AuthorizationRules.AllowRead(Config, "<Role(s)>");
//AuthorizationRules.AllowWrite(Config, "<Role(s)>");
//AuthorizationRules.AllowRead(DTS, "<Role(s)>");
@ -455,6 +477,7 @@ namespace VEPROMS.CSLA.Library
_Title = dr.GetString("Title");
_ShortName = dr.GetString("ShortName");
_FormatID = (int?)dr.GetValue("FormatID");
_ManualOrder = (double?)dr.GetValue("ManualOrder");
_Config = dr.GetString("Config");
_DTS = dr.GetDateTime("DTS");
_UsrID = dr.GetString("UsrID");
@ -472,7 +495,7 @@ namespace VEPROMS.CSLA.Library
// if we're not dirty then don't update the database
if (!this.IsDirty) return;
SqlConnection cn = (SqlConnection)ApplicationContext.LocalContext["cn"];
_LastChanged = Folder.Add(cn, ref _FolderID, Folder.Get(_ParentID), myConnection, _Name, _Title, _ShortName, _MyFormat, _Config, _DTS, _UsrID);
_LastChanged = Folder.Add(cn, ref _FolderID, Folder.Get(_ParentID), myConnection, _Name, _Title, _ShortName, _MyFormat, _ManualOrder, _Config, _DTS, _UsrID);
MarkOld();
}
internal void Update(Connection myConnection)
@ -480,7 +503,7 @@ namespace VEPROMS.CSLA.Library
// if we're not dirty then don't update the database
if (!this.IsDirty) return;
SqlConnection cn = (SqlConnection)ApplicationContext.LocalContext["cn"];
_LastChanged = Folder.Update(cn, ref _FolderID, Folder.Get(_ParentID), myConnection, _Name, _Title, _ShortName, _MyFormat, _Config, _DTS, _UsrID, ref _LastChanged);
_LastChanged = Folder.Update(cn, ref _FolderID, Folder.Get(_ParentID), myConnection, _Name, _Title, _ShortName, _MyFormat, _ManualOrder, _Config, _DTS, _UsrID, ref _LastChanged);
MarkOld();
}
internal void DeleteSelf(Connection myConnection)

View File

@ -36,32 +36,40 @@ namespace VEPROMS.CSLA.Library
private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endregion
#region Collection
protected static List<ConnectionInfo> _AllList = new List<ConnectionInfo>();
private static Dictionary<string, List<ConnectionInfo>> _AllByPrimaryKey = new Dictionary<string, List<ConnectionInfo>>();
private static List<ConnectionInfo> _CacheList = new List<ConnectionInfo>();
protected static void AddToCache(ConnectionInfo connectionInfo)
{
if (!_CacheList.Contains(connectionInfo)) _CacheList.Add(connectionInfo); // In AddToCache
}
protected static void RemoveFromCache(ConnectionInfo connectionInfo)
{
while (_CacheList.Contains(connectionInfo)) _CacheList.Remove(connectionInfo); // In RemoveFromCache
}
private static Dictionary<string, List<ConnectionInfo>> _CacheByPrimaryKey = new Dictionary<string, List<ConnectionInfo>>();
private static void ConvertListToDictionary()
{
List<ConnectionInfo> remove = new List<ConnectionInfo>();
foreach (ConnectionInfo tmp in _AllList)
foreach (ConnectionInfo tmp in _CacheList)
{
if (!_AllByPrimaryKey.ContainsKey(tmp.DBID.ToString()))
if (!_CacheByPrimaryKey.ContainsKey(tmp.DBID.ToString()))
{
_AllByPrimaryKey[tmp.DBID.ToString()] = new List<ConnectionInfo>(); // Add new list for PrimaryKey
_CacheByPrimaryKey[tmp.DBID.ToString()] = new List<ConnectionInfo>(); // Add new list for PrimaryKey
}
_AllByPrimaryKey[tmp.DBID.ToString()].Add(tmp); // Add to Primary Key list
_CacheByPrimaryKey[tmp.DBID.ToString()].Add(tmp); // Add to Primary Key list
remove.Add(tmp);
}
foreach (ConnectionInfo tmp in remove)
_AllList.Remove(tmp);
RemoveFromCache(tmp);
}
internal static void AddList(ConnectionInfoList lst)
{
foreach (ConnectionInfo item in lst) _AllList.Add(item);
foreach (ConnectionInfo item in lst) AddToCache(item);
}
public static ConnectionInfo GetExistingByPrimaryKey(int dbid)
protected static ConnectionInfo GetCachedByPrimaryKey(int dbid)
{
ConvertListToDictionary();
string key = dbid.ToString();
if (_AllByPrimaryKey.ContainsKey(key)) return _AllByPrimaryKey[key][0];
if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
return null;
}
#endregion
@ -194,11 +202,11 @@ namespace VEPROMS.CSLA.Library
return _ConnectionFolders;
}
}
internal void RefreshConnectionFolders()
public void RefreshConnectionFolders()
{
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(_DBID.ToString()))
foreach (ConnectionInfo tmp in _AllByPrimaryKey[_DBID.ToString()])
if (_CacheByPrimaryKey.ContainsKey(_DBID.ToString()))
foreach (ConnectionInfo tmp in _CacheByPrimaryKey[_DBID.ToString()])
tmp._ConnectionFolderCount = -1; // This will cause the data to be requeried
}
// TODO: Replace base ConnectionInfo.ToString function as necessary
@ -222,24 +230,23 @@ namespace VEPROMS.CSLA.Library
#endregion
#region Factory Methods
private static int _ConnectionInfoUnique = 0;
private int _MyConnectionInfoUnique;
private static int ConnectionInfoUnique
{ get { return ++_ConnectionInfoUnique; } }
private int _MyConnectionInfoUnique = ConnectionInfoUnique;
public int MyConnectionInfoUnique
{
get { return _MyConnectionInfoUnique; }
}
private ConnectionInfo()
{ get { return _MyConnectionInfoUnique; } }
protected ConnectionInfo()
{/* require use of factory methods */
_MyConnectionInfoUnique = ++_ConnectionInfoUnique;
_AllList.Add(this);
AddToCache(this);
}
public void Dispose()
{
_AllList.Remove(this);
if (!_AllByPrimaryKey.ContainsKey(DBID.ToString())) return;
List<ConnectionInfo> listConnectionInfo = _AllByPrimaryKey[DBID.ToString()]; // Get the list of items
listConnectionInfo.Remove(this); // Remove the item from the list
RemoveFromCache(this);
if (!_CacheByPrimaryKey.ContainsKey(DBID.ToString())) return;
List<ConnectionInfo> listConnectionInfo = _CacheByPrimaryKey[DBID.ToString()]; // Get the list of items
while (listConnectionInfo.Contains(this)) listConnectionInfo.Remove(this); // Remove the item from the list
if (listConnectionInfo.Count == 0) // If there are no items left in the list
_AllByPrimaryKey.Remove(DBID.ToString()); // remove the list
_CacheByPrimaryKey.Remove(DBID.ToString()); // remove the list
}
public virtual Connection Get()
{
@ -249,11 +256,11 @@ namespace VEPROMS.CSLA.Library
{
string key = tmp.DBID.ToString();
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(key))
foreach (ConnectionInfo tmpInfo in _AllByPrimaryKey[key])
if (_CacheByPrimaryKey.ContainsKey(key))
foreach (ConnectionInfo tmpInfo in _CacheByPrimaryKey[key])
tmpInfo.RefreshFields(tmp);
}
private void RefreshFields(Connection tmp)
protected virtual void RefreshFields(Connection tmp)
{
_Name = tmp.Name;
_Title = tmp.Title;
@ -271,13 +278,17 @@ namespace VEPROMS.CSLA.Library
// throw new System.Security.SecurityException("User not authorized to view a Connection");
try
{
ConnectionInfo tmp = GetExistingByPrimaryKey(dbid);
ConnectionInfo tmp = GetCachedByPrimaryKey(dbid);
if (tmp == null)
{
tmp = DataPortal.Fetch<ConnectionInfo>(new PKCriteria(dbid));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up ConnectionInfo
tmp = null;
}
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp;
}
catch (Exception ex)
@ -289,7 +300,6 @@ namespace VEPROMS.CSLA.Library
#region Data Access Portal
internal ConnectionInfo(SafeDataReader dr)
{
_MyConnectionInfoUnique = ++_ConnectionInfoUnique;
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] ConnectionInfo.Constructor", GetHashCode());
try
{