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

@@ -53,28 +53,36 @@ namespace VEPROMS.CSLA.Library
}
#endregion
#region Collection
protected static List<Part> _AllList = new List<Part>();
private static Dictionary<string, List<Part>> _AllByPrimaryKey = new Dictionary<string, List<Part>>();
private static List<Part> _CacheList = new List<Part>();
protected static void AddToCache(Part part)
{
if (!_CacheList.Contains(part)) _CacheList.Add(part); // In AddToCache
}
protected static void RemoveFromCache(Part part)
{
while (_CacheList.Contains(part)) _CacheList.Remove(part); // In RemoveFromCache
}
private static Dictionary<string, List<Part>> _CacheByPrimaryKey = new Dictionary<string, List<Part>>();
private static void ConvertListToDictionary()
{
List<Part> remove = new List<Part>();
foreach (Part tmp in _AllList)
foreach (Part tmp in _CacheList)
{
if (!_AllByPrimaryKey.ContainsKey(tmp.ContentID.ToString() + "_" + tmp.FromType.ToString()))
if (!_CacheByPrimaryKey.ContainsKey(tmp.ContentID.ToString() + "_" + tmp.FromType.ToString()))
{
_AllByPrimaryKey[tmp.ContentID.ToString() + "_" + tmp.FromType.ToString()] = new List<Part>(); // Add new list for PrimaryKey
_CacheByPrimaryKey[tmp.ContentID.ToString() + "_" + tmp.FromType.ToString()] = new List<Part>(); // Add new list for PrimaryKey
}
_AllByPrimaryKey[tmp.ContentID.ToString() + "_" + tmp.FromType.ToString()].Add(tmp); // Add to Primary Key list
_CacheByPrimaryKey[tmp.ContentID.ToString() + "_" + tmp.FromType.ToString()].Add(tmp); // Add to Primary Key list
remove.Add(tmp);
}
foreach (Part tmp in remove)
_AllList.Remove(tmp);
RemoveFromCache(tmp);
}
public static Part GetExistingByPrimaryKey(int contentID, int fromType)
protected static Part GetCachedByPrimaryKey(int contentID, int fromType)
{
ConvertListToDictionary();
string key = contentID.ToString() + "_" + fromType.ToString();
if (_AllByPrimaryKey.ContainsKey(key)) return _AllByPrimaryKey[key][0];
if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
return null;
}
#endregion
@@ -339,24 +347,29 @@ namespace VEPROMS.CSLA.Library
public int CurrentEditLevel
{ get { return EditLevel; } }
private static int _PartUnique = 0;
private int _MyPartUnique;
protected static int PartUnique
{ get { return ++_PartUnique; } }
private int _MyPartUnique = PartUnique;
public int MyPartUnique
{
get { return _MyPartUnique; }
}
{ get { return _MyPartUnique; } }
protected Part()
{/* require use of factory methods */
_MyPartUnique = ++_PartUnique;
_AllList.Add(this);
AddToCache(this);
}
public void Dispose()
{
_AllList.Remove(this);
if (!_AllByPrimaryKey.ContainsKey(ContentID.ToString() + "_" + FromType.ToString())) return;
List<Part> listPart = _AllByPrimaryKey[ContentID.ToString() + "_" + FromType.ToString()]; // Get the list of items
listPart.Remove(this); // Remove the item from the list
if (listPart.Count == 0) //If there are no items left in the list
_AllByPrimaryKey.Remove(ContentID.ToString() + "_" + FromType.ToString()); // remove the list
RemoveFromDictionaries();
}
private void RemoveFromDictionaries()
{
RemoveFromCache(this);
if (_CacheByPrimaryKey.ContainsKey(ContentID.ToString() + "_" + FromType.ToString()))
{
List<Part> listPart = _CacheByPrimaryKey[ContentID.ToString() + "_" + FromType.ToString()]; // Get the list of items
while (listPart.Contains(this)) listPart.Remove(this); // Remove the item from the list
if (listPart.Count == 0) //If there are no items left in the list
_CacheByPrimaryKey.Remove(ContentID.ToString() + "_" + FromType.ToString()); // remove the list
}
}
public static Part New()
{
@@ -379,6 +392,26 @@ namespace VEPROMS.CSLA.Library
tmp.MyItem = myItem;
return tmp;
}
public static Part MakePart(Content myContent, int fromType, Item myItem)
{
Part tmp = Part.New(myContent, fromType, myItem);
if (tmp.IsSavable)
{
Part 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 Part New(Content myContent, int fromType, Item myItem, DateTime dts, string userID)
{
Part tmp = Part.New();
@@ -393,7 +426,11 @@ namespace VEPROMS.CSLA.Library
{
Part tmp = Part.New(myContent, fromType, myItem, dts, userID);
if (tmp.IsSavable)
tmp = tmp.Save();
{
Part tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else
{
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@@ -411,13 +448,17 @@ namespace VEPROMS.CSLA.Library
throw new System.Security.SecurityException("User not authorized to view a Part");
try
{
Part tmp = GetExistingByPrimaryKey(contentID, fromType);
Part tmp = GetCachedByPrimaryKey(contentID, fromType);
if (tmp == null)
{
tmp = DataPortal.Fetch<Part>(new PKCriteria(contentID, fromType));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up Part
tmp = null;
}
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp;
}
catch (Exception ex)
@@ -459,7 +500,8 @@ namespace VEPROMS.CSLA.Library
{
BuildRefreshList();
Part part = base.Save();
_AllList.Add(part);//Refresh the item in AllList
RemoveFromDictionaries(); // if save is successful remove the previous Folder from the cache
AddToCache(part);//Refresh the item in AllList
ProcessRefreshList();
return part;
}