This commit is contained in:
2008-08-12 11:21:19 +00:00
parent 610f408c6d
commit d0dba2aad4
14 changed files with 937 additions and 240 deletions

View File

@@ -53,28 +53,36 @@ namespace VEPROMS.CSLA.Library
}
#endregion
#region Collection
protected static List<Annotation> _AllList = new List<Annotation>();
private static Dictionary<string, List<Annotation>> _AllByPrimaryKey = new Dictionary<string, List<Annotation>>();
private static List<Annotation> _CacheList = new List<Annotation>();
protected static void AddToCache(Annotation annotation)
{
if (!_CacheList.Contains(annotation)) _CacheList.Add(annotation); // In AddToCache
}
protected static void RemoveFromCache(Annotation annotation)
{
while (_CacheList.Contains(annotation)) _CacheList.Remove(annotation); // In RemoveFromCache
}
private static Dictionary<string, List<Annotation>> _CacheByPrimaryKey = new Dictionary<string, List<Annotation>>();
private static void ConvertListToDictionary()
{
List<Annotation> remove = new List<Annotation>();
foreach (Annotation tmp in _AllList)
foreach (Annotation tmp in _CacheList)
{
if (!_AllByPrimaryKey.ContainsKey(tmp.AnnotationID.ToString()))
if (!_CacheByPrimaryKey.ContainsKey(tmp.AnnotationID.ToString()))
{
_AllByPrimaryKey[tmp.AnnotationID.ToString()] = new List<Annotation>(); // Add new list for PrimaryKey
_CacheByPrimaryKey[tmp.AnnotationID.ToString()] = new List<Annotation>(); // Add new list for PrimaryKey
}
_AllByPrimaryKey[tmp.AnnotationID.ToString()].Add(tmp); // Add to Primary Key list
_CacheByPrimaryKey[tmp.AnnotationID.ToString()].Add(tmp); // Add to Primary Key list
remove.Add(tmp);
}
foreach (Annotation tmp in remove)
_AllList.Remove(tmp);
RemoveFromCache(tmp);
}
public static Annotation GetExistingByPrimaryKey(int annotationID)
protected static Annotation GetCachedByPrimaryKey(int annotationID)
{
ConvertListToDictionary();
string key = annotationID.ToString();
if (_AllByPrimaryKey.ContainsKey(key)) return _AllByPrimaryKey[key][0];
if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
return null;
}
#endregion
@@ -442,24 +450,29 @@ namespace VEPROMS.CSLA.Library
public int CurrentEditLevel
{ get { return EditLevel; } }
private static int _AnnotationUnique = 0;
private int _MyAnnotationUnique;
protected static int AnnotationUnique
{ get { return ++_AnnotationUnique; } }
private int _MyAnnotationUnique = AnnotationUnique;
public int MyAnnotationUnique
{
get { return _MyAnnotationUnique; }
}
{ get { return _MyAnnotationUnique; } }
protected Annotation()
{/* require use of factory methods */
_MyAnnotationUnique = ++_AnnotationUnique;
_AllList.Add(this);
AddToCache(this);
}
public void Dispose()
{
_AllList.Remove(this);
if (!_AllByPrimaryKey.ContainsKey(AnnotationID.ToString())) return;
List<Annotation> listAnnotation = _AllByPrimaryKey[AnnotationID.ToString()]; // Get the list of items
listAnnotation.Remove(this); // Remove the item from the list
if (listAnnotation.Count == 0) //If there are no items left in the list
_AllByPrimaryKey.Remove(AnnotationID.ToString()); // remove the list
RemoveFromDictionaries();
}
private void RemoveFromDictionaries()
{
RemoveFromCache(this);
if (_CacheByPrimaryKey.ContainsKey(AnnotationID.ToString()))
{
List<Annotation> listAnnotation = _CacheByPrimaryKey[AnnotationID.ToString()]; // Get the list of items
while (listAnnotation.Contains(this)) listAnnotation.Remove(this); // Remove the item from the list
if (listAnnotation.Count == 0) //If there are no items left in the list
_CacheByPrimaryKey.Remove(AnnotationID.ToString()); // remove the list
}
}
public static Annotation New()
{
@@ -497,7 +510,11 @@ namespace VEPROMS.CSLA.Library
{
Annotation tmp = Annotation.New(myItem, myAnnotationType, rtfText, searchText, config, dts, userID);
if (tmp.IsSavable)
tmp = tmp.Save();
{
Annotation tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else
{
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@@ -523,7 +540,11 @@ namespace VEPROMS.CSLA.Library
{
Annotation tmp = Annotation.New(myItem, myAnnotationType, rtfText, searchText, config);
if (tmp.IsSavable)
tmp = tmp.Save();
{
Annotation tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else
{
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@@ -541,13 +562,17 @@ namespace VEPROMS.CSLA.Library
throw new System.Security.SecurityException("User not authorized to view a Annotation");
try
{
Annotation tmp = GetExistingByPrimaryKey(annotationID);
Annotation tmp = GetCachedByPrimaryKey(annotationID);
if (tmp == null)
{
tmp = DataPortal.Fetch<Annotation>(new PKCriteria(annotationID));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up Annotation
tmp = null;
}
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp;
}
catch (Exception ex)
@@ -589,7 +614,8 @@ namespace VEPROMS.CSLA.Library
{
BuildRefreshList();
Annotation annotation = base.Save();
_AllList.Add(annotation);//Refresh the item in AllList
RemoveFromDictionaries(); // if save is successful remove the previous Folder from the cache
AddToCache(annotation);//Refresh the item in AllList
ProcessRefreshList();
return annotation;
}