This commit is contained in:
2008-08-12 11:25:17 +00:00
parent 25458190e6
commit 50e163dae0
8 changed files with 493 additions and 279 deletions

View File

@@ -52,28 +52,36 @@ namespace VEPROMS.CSLA.Library
}
#endregion
#region Collection
protected static List<Entry> _AllList = new List<Entry>();
private static Dictionary<string, List<Entry>> _AllByPrimaryKey = new Dictionary<string, List<Entry>>();
private static List<Entry> _CacheList = new List<Entry>();
protected static void AddToCache(Entry entry)
{
if (!_CacheList.Contains(entry)) _CacheList.Add(entry); // In AddToCache
}
protected static void RemoveFromCache(Entry entry)
{
while (_CacheList.Contains(entry)) _CacheList.Remove(entry); // In RemoveFromCache
}
private static Dictionary<string, List<Entry>> _CacheByPrimaryKey = new Dictionary<string, List<Entry>>();
private static void ConvertListToDictionary()
{
List<Entry> remove = new List<Entry>();
foreach (Entry tmp in _AllList)
foreach (Entry tmp in _CacheList)
{
if (!_AllByPrimaryKey.ContainsKey(tmp.ContentID.ToString()))
if (!_CacheByPrimaryKey.ContainsKey(tmp.ContentID.ToString()))
{
_AllByPrimaryKey[tmp.ContentID.ToString()] = new List<Entry>(); // Add new list for PrimaryKey
_CacheByPrimaryKey[tmp.ContentID.ToString()] = new List<Entry>(); // Add new list for PrimaryKey
}
_AllByPrimaryKey[tmp.ContentID.ToString()].Add(tmp); // Add to Primary Key list
_CacheByPrimaryKey[tmp.ContentID.ToString()].Add(tmp); // Add to Primary Key list
remove.Add(tmp);
}
foreach (Entry tmp in remove)
_AllList.Remove(tmp);
RemoveFromCache(tmp);
}
public static Entry GetExistingByPrimaryKey(int contentID)
protected static Entry GetCachedByPrimaryKey(int contentID)
{
ConvertListToDictionary();
string key = contentID.ToString();
if (_AllByPrimaryKey.ContainsKey(key)) return _AllByPrimaryKey[key][0];
if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
return null;
}
#endregion
@@ -326,24 +334,29 @@ namespace VEPROMS.CSLA.Library
public int CurrentEditLevel
{ get { return EditLevel; } }
private static int _EntryUnique = 0;
private int _MyEntryUnique;
protected static int EntryUnique
{ get { return ++_EntryUnique; } }
private int _MyEntryUnique = EntryUnique;
public int MyEntryUnique
{
get { return _MyEntryUnique; }
}
{ get { return _MyEntryUnique; } }
protected Entry()
{/* require use of factory methods */
_MyEntryUnique = ++_EntryUnique;
_AllList.Add(this);
AddToCache(this);
}
public void Dispose()
{
_AllList.Remove(this);
if (!_AllByPrimaryKey.ContainsKey(ContentID.ToString())) return;
List<Entry> listEntry = _AllByPrimaryKey[ContentID.ToString()]; // Get the list of items
listEntry.Remove(this); // Remove the item from the list
if (listEntry.Count == 0) //If there are no items left in the list
_AllByPrimaryKey.Remove(ContentID.ToString()); // remove the list
RemoveFromDictionaries();
}
private void RemoveFromDictionaries()
{
RemoveFromCache(this);
if (_CacheByPrimaryKey.ContainsKey(ContentID.ToString()))
{
List<Entry> listEntry = _CacheByPrimaryKey[ContentID.ToString()]; // Get the list of items
while (listEntry.Contains(this)) listEntry.Remove(this); // Remove the item from the list
if (listEntry.Count == 0) //If there are no items left in the list
_CacheByPrimaryKey.Remove(ContentID.ToString()); // remove the list
}
}
public static Entry New()
{
@@ -365,6 +378,26 @@ namespace VEPROMS.CSLA.Library
tmp.MyDocument = myDocument;
return tmp;
}
public static Entry MakeEntry(Content myContent, Document myDocument)
{
Entry tmp = Entry.New(myContent, myDocument);
if (tmp.IsSavable)
{
Entry 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 Entry New(Content myContent, Document myDocument, DateTime dts, string userID)
{
Entry tmp = Entry.New();
@@ -378,7 +411,11 @@ namespace VEPROMS.CSLA.Library
{
Entry tmp = Entry.New(myContent, myDocument, dts, userID);
if (tmp.IsSavable)
tmp = tmp.Save();
{
Entry tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else
{
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@@ -404,13 +441,17 @@ namespace VEPROMS.CSLA.Library
throw new System.Security.SecurityException("User not authorized to view a Entry");
try
{
Entry tmp = GetExistingByPrimaryKey(contentID);
Entry tmp = GetCachedByPrimaryKey(contentID);
if (tmp == null)
{
tmp = DataPortal.Fetch<Entry>(new PKCriteria(contentID));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up Entry
tmp = null;
}
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp;
}
catch (Exception ex)
@@ -452,7 +493,8 @@ namespace VEPROMS.CSLA.Library
{
BuildRefreshList();
Entry entry = base.Save();
_AllList.Add(entry);//Refresh the item in AllList
RemoveFromDictionaries(); // if save is successful remove the previous Folder from the cache
AddToCache(entry);//Refresh the item in AllList
ProcessRefreshList();
return entry;
}