This commit is contained in:
2008-08-12 11:23:45 +00:00
parent f42f176ae9
commit 25458190e6
4 changed files with 191 additions and 119 deletions

View File

@@ -64,28 +64,36 @@ namespace VEPROMS.CSLA.Library
}
#endregion
#region Collection
protected static List<Document> _AllList = new List<Document>();
private static Dictionary<string, List<Document>> _AllByPrimaryKey = new Dictionary<string, List<Document>>();
private static List<Document> _CacheList = new List<Document>();
protected static void AddToCache(Document document)
{
if (!_CacheList.Contains(document)) _CacheList.Add(document); // In AddToCache
}
protected static void RemoveFromCache(Document document)
{
while (_CacheList.Contains(document)) _CacheList.Remove(document); // In RemoveFromCache
}
private static Dictionary<string, List<Document>> _CacheByPrimaryKey = new Dictionary<string, List<Document>>();
private static void ConvertListToDictionary()
{
List<Document> remove = new List<Document>();
foreach (Document tmp in _AllList)
foreach (Document tmp in _CacheList)
{
if (!_AllByPrimaryKey.ContainsKey(tmp.DocID.ToString()))
if (!_CacheByPrimaryKey.ContainsKey(tmp.DocID.ToString()))
{
_AllByPrimaryKey[tmp.DocID.ToString()] = new List<Document>(); // Add new list for PrimaryKey
_CacheByPrimaryKey[tmp.DocID.ToString()] = new List<Document>(); // Add new list for PrimaryKey
}
_AllByPrimaryKey[tmp.DocID.ToString()].Add(tmp); // Add to Primary Key list
_CacheByPrimaryKey[tmp.DocID.ToString()].Add(tmp); // Add to Primary Key list
remove.Add(tmp);
}
foreach (Document tmp in remove)
_AllList.Remove(tmp);
RemoveFromCache(tmp);
}
public static Document GetExistingByPrimaryKey(int docID)
protected static Document GetCachedByPrimaryKey(int docID)
{
ConvertListToDictionary();
string key = docID.ToString();
if (_AllByPrimaryKey.ContainsKey(key)) return _AllByPrimaryKey[key][0];
if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
return null;
}
#endregion
@@ -436,24 +444,29 @@ namespace VEPROMS.CSLA.Library
public int CurrentEditLevel
{ get { return EditLevel; } }
private static int _DocumentUnique = 0;
private int _MyDocumentUnique;
protected static int DocumentUnique
{ get { return ++_DocumentUnique; } }
private int _MyDocumentUnique = DocumentUnique;
public int MyDocumentUnique
{
get { return _MyDocumentUnique; }
}
{ get { return _MyDocumentUnique; } }
protected Document()
{/* require use of factory methods */
_MyDocumentUnique = ++_DocumentUnique;
_AllList.Add(this);
AddToCache(this);
}
public void Dispose()
{
_AllList.Remove(this);
if (!_AllByPrimaryKey.ContainsKey(DocID.ToString())) return;
List<Document> listDocument = _AllByPrimaryKey[DocID.ToString()]; // Get the list of items
listDocument.Remove(this); // Remove the item from the list
if (listDocument.Count == 0) //If there are no items left in the list
_AllByPrimaryKey.Remove(DocID.ToString()); // remove the list
RemoveFromDictionaries();
}
private void RemoveFromDictionaries()
{
RemoveFromCache(this);
if (_CacheByPrimaryKey.ContainsKey(DocID.ToString()))
{
List<Document> listDocument = _CacheByPrimaryKey[DocID.ToString()]; // Get the list of items
while (listDocument.Contains(this)) listDocument.Remove(this); // Remove the item from the list
if (listDocument.Count == 0) //If there are no items left in the list
_CacheByPrimaryKey.Remove(DocID.ToString()); // remove the list
}
}
public static Document New()
{
@@ -483,7 +496,11 @@ namespace VEPROMS.CSLA.Library
{
Document tmp = Document.New(libTitle, docContent, docAscii, config, dts, userID);
if (tmp.IsSavable)
tmp = tmp.Save();
{
Document tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else
{
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@@ -508,7 +525,11 @@ namespace VEPROMS.CSLA.Library
{
Document tmp = Document.New(libTitle, docContent, docAscii, config);
if (tmp.IsSavable)
tmp = tmp.Save();
{
Document tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else
{
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@@ -526,13 +547,17 @@ namespace VEPROMS.CSLA.Library
throw new System.Security.SecurityException("User not authorized to view a Document");
try
{
Document tmp = GetExistingByPrimaryKey(docID);
Document tmp = GetCachedByPrimaryKey(docID);
if (tmp == null)
{
tmp = DataPortal.Fetch<Document>(new PKCriteria(docID));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up Document
tmp = null;
}
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp;
}
catch (Exception ex)
@@ -574,7 +599,8 @@ namespace VEPROMS.CSLA.Library
{
BuildRefreshList();
Document document = base.Save();
_AllList.Add(document);//Refresh the item in AllList
RemoveFromDictionaries(); // if save is successful remove the previous Folder from the cache
AddToCache(document);//Refresh the item in AllList
ProcessRefreshList();
return document;
}