This commit is contained in:
2008-08-12 11:42:50 +00:00
parent dfafac5a3a
commit d81b4d8af0
19 changed files with 941 additions and 569 deletions

View File

@@ -52,28 +52,36 @@ namespace VEPROMS.CSLA.Library
}
#endregion
#region Collection
protected static List<Permission> _AllList = new List<Permission>();
private static Dictionary<string, List<Permission>> _AllByPrimaryKey = new Dictionary<string, List<Permission>>();
private static List<Permission> _CacheList = new List<Permission>();
protected static void AddToCache(Permission permission)
{
if (!_CacheList.Contains(permission)) _CacheList.Add(permission); // In AddToCache
}
protected static void RemoveFromCache(Permission permission)
{
while (_CacheList.Contains(permission)) _CacheList.Remove(permission); // In RemoveFromCache
}
private static Dictionary<string, List<Permission>> _CacheByPrimaryKey = new Dictionary<string, List<Permission>>();
private static void ConvertListToDictionary()
{
List<Permission> remove = new List<Permission>();
foreach (Permission tmp in _AllList)
foreach (Permission tmp in _CacheList)
{
if (!_AllByPrimaryKey.ContainsKey(tmp.PID.ToString()))
if (!_CacheByPrimaryKey.ContainsKey(tmp.PID.ToString()))
{
_AllByPrimaryKey[tmp.PID.ToString()] = new List<Permission>(); // Add new list for PrimaryKey
_CacheByPrimaryKey[tmp.PID.ToString()] = new List<Permission>(); // Add new list for PrimaryKey
}
_AllByPrimaryKey[tmp.PID.ToString()].Add(tmp); // Add to Primary Key list
_CacheByPrimaryKey[tmp.PID.ToString()].Add(tmp); // Add to Primary Key list
remove.Add(tmp);
}
foreach (Permission tmp in remove)
_AllList.Remove(tmp);
RemoveFromCache(tmp);
}
public static Permission GetExistingByPrimaryKey(int pid)
protected static Permission GetCachedByPrimaryKey(int pid)
{
ConvertListToDictionary();
string key = pid.ToString();
if (_AllByPrimaryKey.ContainsKey(key)) return _AllByPrimaryKey[key][0];
if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
return null;
}
#endregion
@@ -538,24 +546,29 @@ namespace VEPROMS.CSLA.Library
public int CurrentEditLevel
{ get { return EditLevel; } }
private static int _PermissionUnique = 0;
private int _MyPermissionUnique;
protected static int PermissionUnique
{ get { return ++_PermissionUnique; } }
private int _MyPermissionUnique = PermissionUnique;
public int MyPermissionUnique
{
get { return _MyPermissionUnique; }
}
{ get { return _MyPermissionUnique; } }
protected Permission()
{/* require use of factory methods */
_MyPermissionUnique = ++_PermissionUnique;
_AllList.Add(this);
AddToCache(this);
}
public void Dispose()
{
_AllList.Remove(this);
if (!_AllByPrimaryKey.ContainsKey(PID.ToString())) return;
List<Permission> listPermission = _AllByPrimaryKey[PID.ToString()]; // Get the list of items
listPermission.Remove(this); // Remove the item from the list
if (listPermission.Count == 0) //If there are no items left in the list
_AllByPrimaryKey.Remove(PID.ToString()); // remove the list
RemoveFromDictionaries();
}
private void RemoveFromDictionaries()
{
RemoveFromCache(this);
if (_CacheByPrimaryKey.ContainsKey(PID.ToString()))
{
List<Permission> listPermission = _CacheByPrimaryKey[PID.ToString()]; // Get the list of items
while (listPermission.Contains(this)) listPermission.Remove(this); // Remove the item from the list
if (listPermission.Count == 0) //If there are no items left in the list
_CacheByPrimaryKey.Remove(PID.ToString()); // remove the list
}
}
public static Permission New()
{
@@ -598,7 +611,11 @@ namespace VEPROMS.CSLA.Library
{
Permission tmp = Permission.New(myRole, permLevel, versionType, permValue, permAD, startDate, endDate, config, dts, usrID);
if (tmp.IsSavable)
tmp = tmp.Save();
{
Permission tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else
{
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@@ -625,7 +642,11 @@ namespace VEPROMS.CSLA.Library
{
Permission tmp = Permission.New(myRole, permLevel, versionType, permValue, endDate, config);
if (tmp.IsSavable)
tmp = tmp.Save();
{
Permission tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else
{
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@@ -643,13 +664,17 @@ namespace VEPROMS.CSLA.Library
throw new System.Security.SecurityException("User not authorized to view a Permission");
try
{
Permission tmp = GetExistingByPrimaryKey(pid);
Permission tmp = GetCachedByPrimaryKey(pid);
if (tmp == null)
{
tmp = DataPortal.Fetch<Permission>(new PKCriteria(pid));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up Permission
tmp = null;
}
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp;
}
catch (Exception ex)
@@ -691,7 +716,8 @@ namespace VEPROMS.CSLA.Library
{
BuildRefreshList();
Permission permission = base.Save();
_AllList.Add(permission);//Refresh the item in AllList
RemoveFromDictionaries(); // if save is successful remove the previous Folder from the cache
AddToCache(permission);//Refresh the item in AllList
ProcessRefreshList();
return permission;
}