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

@@ -77,38 +77,46 @@ namespace VEPROMS.CSLA.Library
}
#endregion
#region Collection
protected static List<Role> _AllList = new List<Role>();
private static Dictionary<string, List<Role>> _AllByPrimaryKey = new Dictionary<string, List<Role>>();
private static Dictionary<string, List<Role>> _AllByName = new Dictionary<string, List<Role>>();
private static List<Role> _CacheList = new List<Role>();
protected static void AddToCache(Role role)
{
if (!_CacheList.Contains(role)) _CacheList.Add(role); // In AddToCache
}
protected static void RemoveFromCache(Role role)
{
while (_CacheList.Contains(role)) _CacheList.Remove(role); // In RemoveFromCache
}
private static Dictionary<string, List<Role>> _CacheByPrimaryKey = new Dictionary<string, List<Role>>();
private static Dictionary<string, List<Role>> _CacheByName = new Dictionary<string, List<Role>>();
private static void ConvertListToDictionary()
{
List<Role> remove = new List<Role>();
foreach (Role tmp in _AllList)
foreach (Role tmp in _CacheList)
{
if (!_AllByPrimaryKey.ContainsKey(tmp.RID.ToString()))
if (!_CacheByPrimaryKey.ContainsKey(tmp.RID.ToString()))
{
_AllByPrimaryKey[tmp.RID.ToString()] = new List<Role>(); // Add new list for PrimaryKey
_AllByName[tmp.Name.ToString()] = new List<Role>(); // Add new list for Name
_CacheByPrimaryKey[tmp.RID.ToString()] = new List<Role>(); // Add new list for PrimaryKey
_CacheByName[tmp.Name.ToString()] = new List<Role>(); // Add new list for Name
}
_AllByPrimaryKey[tmp.RID.ToString()].Add(tmp); // Add to Primary Key list
_AllByName[tmp.Name.ToString()].Add(tmp); // Unique Index
_CacheByPrimaryKey[tmp.RID.ToString()].Add(tmp); // Add to Primary Key list
_CacheByName[tmp.Name.ToString()].Add(tmp); // Unique Index
remove.Add(tmp);
}
foreach (Role tmp in remove)
_AllList.Remove(tmp);
RemoveFromCache(tmp);
}
public static Role GetExistingByPrimaryKey(int rid)
protected static Role GetCachedByPrimaryKey(int rid)
{
ConvertListToDictionary();
string key = rid.ToString();
if (_AllByPrimaryKey.ContainsKey(key)) return _AllByPrimaryKey[key][0];
if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
return null;
}
public static Role GetExistingByName(string name)
protected static Role 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
@@ -442,28 +450,41 @@ namespace VEPROMS.CSLA.Library
public int CurrentEditLevel
{ get { return EditLevel; } }
private static int _RoleUnique = 0;
private int _MyRoleUnique;
protected static int RoleUnique
{ get { return ++_RoleUnique; } }
private int _MyRoleUnique = RoleUnique;
public int MyRoleUnique
{
get { return _MyRoleUnique; }
}
{ get { return _MyRoleUnique; } }
protected Role()
{/* require use of factory methods */
_MyRoleUnique = ++_RoleUnique;
_AllList.Add(this);
AddToCache(this);
}
public void Dispose()
{
_AllList.Remove(this);
if (!_AllByPrimaryKey.ContainsKey(RID.ToString())) return;
List<Role> listRole = _AllByPrimaryKey[RID.ToString()]; // Get the list of items
listRole.Remove(this); // Remove the item from the list
if (listRole.Count == 0) //If there are no items left in the list
_AllByPrimaryKey.Remove(RID.ToString()); // remove the list
listRole = _AllByName[RID.ToString()]; // Get the list of items
listRole.Remove(this); // Remove the item from the list
if (listRole.Count == 0) //If there are no items left in the list
_AllByName.Remove(RID.ToString()); // remove the list
RemoveFromDictionaries();
}
private void RemoveFromDictionaries()
{
RemoveFromCache(this);
if (_CacheByPrimaryKey.ContainsKey(RID.ToString()))
{
List<Role> listRole = _CacheByPrimaryKey[RID.ToString()]; // Get the list of items
while (listRole.Contains(this)) listRole.Remove(this); // Remove the item from the list
if (listRole.Count == 0) //If there are no items left in the list
_CacheByPrimaryKey.Remove(RID.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<Role> listRole = _CacheByName[myKey]; // Get the list of items
listRole.Remove(this); // Remove the item from the list
if (listRole.Count == 0) //If there are no items left in the list
_CacheByName.Remove(myKey); // remove the list
}
}
public static Role New()
{
@@ -485,6 +506,26 @@ namespace VEPROMS.CSLA.Library
tmp.Title = title;
return tmp;
}
public static Role MakeRole(string name, string title)
{
Role tmp = Role.New(name, title);
if (tmp.IsSavable)
{
Role tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
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 Role New(string name, string title, DateTime dts, string usrID)
{
Role tmp = Role.New();
@@ -498,7 +539,11 @@ namespace VEPROMS.CSLA.Library
{
Role tmp = Role.New(name, title, dts, usrID);
if (tmp.IsSavable)
tmp = tmp.Save();
{
Role tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else
{
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@@ -516,13 +561,17 @@ namespace VEPROMS.CSLA.Library
throw new System.Security.SecurityException("User not authorized to view a Role");
try
{
Role tmp = GetExistingByPrimaryKey(rid);
Role tmp = GetCachedByPrimaryKey(rid);
if (tmp == null)
{
tmp = DataPortal.Fetch<Role>(new PKCriteria(rid));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up Role
tmp = null;
}
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp;
}
catch (Exception ex)
@@ -530,19 +579,23 @@ namespace VEPROMS.CSLA.Library
throw new DbCslaException("Error on Role.Get", ex);
}
}
private static Role GetByName(string name)
public static Role GetByName(string name)
{
if (!CanGetObject())
throw new System.Security.SecurityException("User not authorized to view a Role");
try
{
Role tmp = GetExistingByName(name);
Role tmp = GetCachedByName(name);
if (tmp == null)
{
tmp = DataPortal.Fetch<Role>(new NameCriteria(name));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up Role
tmp = null;
}
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp;
}
catch (Exception ex)
@@ -584,7 +637,8 @@ namespace VEPROMS.CSLA.Library
{
BuildRefreshList();
Role role = base.Save();
_AllList.Add(role);//Refresh the item in AllList
RemoveFromDictionaries(); // if save is successful remove the previous Folder from the cache
AddToCache(role);//Refresh the item in AllList
ProcessRefreshList();
return role;
}