This commit is contained in:
2008-08-12 11:27:04 +00:00
parent 50e163dae0
commit dfafac5a3a
9 changed files with 352 additions and 202 deletions

View File

@@ -77,38 +77,46 @@ namespace VEPROMS.CSLA.Library
}
#endregion
#region Collection
protected static List<Group> _AllList = new List<Group>();
private static Dictionary<string, List<Group>> _AllByPrimaryKey = new Dictionary<string, List<Group>>();
private static Dictionary<string, List<Group>> _AllByGroupName = new Dictionary<string, List<Group>>();
private static List<Group> _CacheList = new List<Group>();
protected static void AddToCache(Group group)
{
if (!_CacheList.Contains(group)) _CacheList.Add(group); // In AddToCache
}
protected static void RemoveFromCache(Group group)
{
while (_CacheList.Contains(group)) _CacheList.Remove(group); // In RemoveFromCache
}
private static Dictionary<string, List<Group>> _CacheByPrimaryKey = new Dictionary<string, List<Group>>();
private static Dictionary<string, List<Group>> _CacheByGroupName = new Dictionary<string, List<Group>>();
private static void ConvertListToDictionary()
{
List<Group> remove = new List<Group>();
foreach (Group tmp in _AllList)
foreach (Group tmp in _CacheList)
{
if (!_AllByPrimaryKey.ContainsKey(tmp.GID.ToString()))
if (!_CacheByPrimaryKey.ContainsKey(tmp.GID.ToString()))
{
_AllByPrimaryKey[tmp.GID.ToString()] = new List<Group>(); // Add new list for PrimaryKey
_AllByGroupName[tmp.GroupName.ToString()] = new List<Group>(); // Add new list for GroupName
_CacheByPrimaryKey[tmp.GID.ToString()] = new List<Group>(); // Add new list for PrimaryKey
_CacheByGroupName[tmp.GroupName.ToString()] = new List<Group>(); // Add new list for GroupName
}
_AllByPrimaryKey[tmp.GID.ToString()].Add(tmp); // Add to Primary Key list
_AllByGroupName[tmp.GroupName.ToString()].Add(tmp); // Unique Index
_CacheByPrimaryKey[tmp.GID.ToString()].Add(tmp); // Add to Primary Key list
_CacheByGroupName[tmp.GroupName.ToString()].Add(tmp); // Unique Index
remove.Add(tmp);
}
foreach (Group tmp in remove)
_AllList.Remove(tmp);
RemoveFromCache(tmp);
}
public static Group GetExistingByPrimaryKey(int gid)
protected static Group GetCachedByPrimaryKey(int gid)
{
ConvertListToDictionary();
string key = gid.ToString();
if (_AllByPrimaryKey.ContainsKey(key)) return _AllByPrimaryKey[key][0];
if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
return null;
}
public static Group GetExistingByGroupName(string groupName)
protected static Group GetCachedByGroupName(string groupName)
{
ConvertListToDictionary();
string key = groupName.ToString();
if (_AllByGroupName.ContainsKey(key)) return _AllByGroupName[key][0];
if (_CacheByGroupName.ContainsKey(key)) return _CacheByGroupName[key][0];
return null;
}
#endregion
@@ -462,28 +470,41 @@ namespace VEPROMS.CSLA.Library
public int CurrentEditLevel
{ get { return EditLevel; } }
private static int _GroupUnique = 0;
private int _MyGroupUnique;
protected static int GroupUnique
{ get { return ++_GroupUnique; } }
private int _MyGroupUnique = GroupUnique;
public int MyGroupUnique
{
get { return _MyGroupUnique; }
}
{ get { return _MyGroupUnique; } }
protected Group()
{/* require use of factory methods */
_MyGroupUnique = ++_GroupUnique;
_AllList.Add(this);
AddToCache(this);
}
public void Dispose()
{
_AllList.Remove(this);
if (!_AllByPrimaryKey.ContainsKey(GID.ToString())) return;
List<Group> listGroup = _AllByPrimaryKey[GID.ToString()]; // Get the list of items
listGroup.Remove(this); // Remove the item from the list
if (listGroup.Count == 0) //If there are no items left in the list
_AllByPrimaryKey.Remove(GID.ToString()); // remove the list
listGroup = _AllByGroupName[GID.ToString()]; // Get the list of items
listGroup.Remove(this); // Remove the item from the list
if (listGroup.Count == 0) //If there are no items left in the list
_AllByGroupName.Remove(GID.ToString()); // remove the list
RemoveFromDictionaries();
}
private void RemoveFromDictionaries()
{
RemoveFromCache(this);
if (_CacheByPrimaryKey.ContainsKey(GID.ToString()))
{
List<Group> listGroup = _CacheByPrimaryKey[GID.ToString()]; // Get the list of items
while (listGroup.Contains(this)) listGroup.Remove(this); // Remove the item from the list
if (listGroup.Count == 0) //If there are no items left in the list
_CacheByPrimaryKey.Remove(GID.ToString()); // remove the list
}
string myKey;
myKey = null;
foreach (string key in _CacheByGroupName.Keys)
if (_CacheByGroupName[key].Contains(this))
myKey = key;
if (myKey != null)
{
List<Group> listGroup = _CacheByGroupName[myKey]; // Get the list of items
listGroup.Remove(this); // Remove the item from the list
if (listGroup.Count == 0) //If there are no items left in the list
_CacheByGroupName.Remove(myKey); // remove the list
}
}
public static Group New()
{
@@ -518,7 +539,11 @@ namespace VEPROMS.CSLA.Library
{
Group tmp = Group.New(groupName, groupType, config, dts, usrID);
if (tmp.IsSavable)
tmp = tmp.Save();
{
Group tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else
{
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@@ -542,7 +567,11 @@ namespace VEPROMS.CSLA.Library
{
Group tmp = Group.New(groupName, groupType, config);
if (tmp.IsSavable)
tmp = tmp.Save();
{
Group tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else
{
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@@ -560,13 +589,17 @@ namespace VEPROMS.CSLA.Library
throw new System.Security.SecurityException("User not authorized to view a Group");
try
{
Group tmp = GetExistingByPrimaryKey(gid);
Group tmp = GetCachedByPrimaryKey(gid);
if (tmp == null)
{
tmp = DataPortal.Fetch<Group>(new PKCriteria(gid));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up Group
tmp = null;
}
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp;
}
catch (Exception ex)
@@ -574,19 +607,23 @@ namespace VEPROMS.CSLA.Library
throw new DbCslaException("Error on Group.Get", ex);
}
}
private static Group GetByGroupName(string groupName)
public static Group GetByGroupName(string groupName)
{
if (!CanGetObject())
throw new System.Security.SecurityException("User not authorized to view a Group");
try
{
Group tmp = GetExistingByGroupName(groupName);
Group tmp = GetCachedByGroupName(groupName);
if (tmp == null)
{
tmp = DataPortal.Fetch<Group>(new GroupNameCriteria(groupName));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up Group
tmp = null;
}
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp;
}
catch (Exception ex)
@@ -628,7 +665,8 @@ namespace VEPROMS.CSLA.Library
{
BuildRefreshList();
Group group = base.Save();
_AllList.Add(group);//Refresh the item in AllList
RemoveFromDictionaries(); // if save is successful remove the previous Folder from the cache
AddToCache(group);//Refresh the item in AllList
ProcessRefreshList();
return group;
}