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

@@ -36,32 +36,40 @@ namespace VEPROMS.CSLA.Library
private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endregion
#region Collection
protected static List<ItemInfo> _AllList = new List<ItemInfo>();
private static Dictionary<string, List<ItemInfo>> _AllByPrimaryKey = new Dictionary<string, List<ItemInfo>>();
private static List<ItemInfo> _CacheList = new List<ItemInfo>();
protected static void AddToCache(ItemInfo itemInfo)
{
if (!_CacheList.Contains(itemInfo)) _CacheList.Add(itemInfo); // In AddToCache
}
protected static void RemoveFromCache(ItemInfo itemInfo)
{
while (_CacheList.Contains(itemInfo)) _CacheList.Remove(itemInfo); // In RemoveFromCache
}
private static Dictionary<string, List<ItemInfo>> _CacheByPrimaryKey = new Dictionary<string, List<ItemInfo>>();
private static void ConvertListToDictionary()
{
List<ItemInfo> remove = new List<ItemInfo>();
foreach (ItemInfo tmp in _AllList)
foreach (ItemInfo tmp in _CacheList)
{
if (!_AllByPrimaryKey.ContainsKey(tmp.ItemID.ToString()))
if (!_CacheByPrimaryKey.ContainsKey(tmp.ItemID.ToString()))
{
_AllByPrimaryKey[tmp.ItemID.ToString()] = new List<ItemInfo>(); // Add new list for PrimaryKey
_CacheByPrimaryKey[tmp.ItemID.ToString()] = new List<ItemInfo>(); // Add new list for PrimaryKey
}
_AllByPrimaryKey[tmp.ItemID.ToString()].Add(tmp); // Add to Primary Key list
_CacheByPrimaryKey[tmp.ItemID.ToString()].Add(tmp); // Add to Primary Key list
remove.Add(tmp);
}
foreach (ItemInfo tmp in remove)
_AllList.Remove(tmp);
RemoveFromCache(tmp);
}
internal static void AddList(ItemInfoList lst)
{
foreach (ItemInfo item in lst) _AllList.Add(item);
foreach (ItemInfo item in lst) AddToCache(item);
}
public static ItemInfo GetExistingByPrimaryKey(int itemID)
protected static ItemInfo GetCachedByPrimaryKey(int itemID)
{
ConvertListToDictionary();
string key = itemID.ToString();
if (_AllByPrimaryKey.ContainsKey(key)) return _AllByPrimaryKey[key][0];
if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
return null;
}
#endregion
@@ -185,11 +193,11 @@ namespace VEPROMS.CSLA.Library
return _ItemAnnotations;
}
}
internal void RefreshItemAnnotations()
public void RefreshItemAnnotations()
{
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(_ItemID.ToString()))
foreach (ItemInfo tmp in _AllByPrimaryKey[_ItemID.ToString()])
if (_CacheByPrimaryKey.ContainsKey(_ItemID.ToString()))
foreach (ItemInfo tmp in _CacheByPrimaryKey[_ItemID.ToString()])
tmp._ItemAnnotationCount = -1; // This will cause the data to be requeried
}
private int _ItemDocVersionCount = 0;
@@ -220,11 +228,11 @@ namespace VEPROMS.CSLA.Library
return _ItemDocVersions;
}
}
internal void RefreshItemDocVersions()
public void RefreshItemDocVersions()
{
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(_ItemID.ToString()))
foreach (ItemInfo tmp in _AllByPrimaryKey[_ItemID.ToString()])
if (_CacheByPrimaryKey.ContainsKey(_ItemID.ToString()))
foreach (ItemInfo tmp in _CacheByPrimaryKey[_ItemID.ToString()])
tmp._ItemDocVersionCount = -1; // This will cause the data to be requeried
}
private int _NextItemCount = 0;
@@ -255,11 +263,11 @@ namespace VEPROMS.CSLA.Library
return _NextItems;
}
}
internal void RefreshNextItems()
public void RefreshNextItems()
{
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(_ItemID.ToString()))
foreach (ItemInfo tmp in _AllByPrimaryKey[_ItemID.ToString()])
if (_CacheByPrimaryKey.ContainsKey(_ItemID.ToString()))
foreach (ItemInfo tmp in _CacheByPrimaryKey[_ItemID.ToString()])
tmp._NextItemCount = -1; // This will cause the data to be requeried
}
private int _ItemPartCount = 0;
@@ -290,11 +298,11 @@ namespace VEPROMS.CSLA.Library
return _ItemParts;
}
}
internal void RefreshItemParts()
public void RefreshItemParts()
{
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(_ItemID.ToString()))
foreach (ItemInfo tmp in _AllByPrimaryKey[_ItemID.ToString()])
if (_CacheByPrimaryKey.ContainsKey(_ItemID.ToString()))
foreach (ItemInfo tmp in _CacheByPrimaryKey[_ItemID.ToString()])
tmp._ItemPartCount = -1; // This will cause the data to be requeried
}
private int _ItemTransition_RangeIDCount = 0;
@@ -325,11 +333,11 @@ namespace VEPROMS.CSLA.Library
return _ItemTransitions_RangeID;
}
}
internal void RefreshItemTransitions_RangeID()
public void RefreshItemTransitions_RangeID()
{
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(_ItemID.ToString()))
foreach (ItemInfo tmp in _AllByPrimaryKey[_ItemID.ToString()])
if (_CacheByPrimaryKey.ContainsKey(_ItemID.ToString()))
foreach (ItemInfo tmp in _CacheByPrimaryKey[_ItemID.ToString()])
tmp._ItemTransition_RangeIDCount = -1; // This will cause the data to be requeried
}
private int _ItemTransition_ToIDCount = 0;
@@ -360,11 +368,11 @@ namespace VEPROMS.CSLA.Library
return _ItemTransitions_ToID;
}
}
internal void RefreshItemTransitions_ToID()
public void RefreshItemTransitions_ToID()
{
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(_ItemID.ToString()))
foreach (ItemInfo tmp in _AllByPrimaryKey[_ItemID.ToString()])
if (_CacheByPrimaryKey.ContainsKey(_ItemID.ToString()))
foreach (ItemInfo tmp in _CacheByPrimaryKey[_ItemID.ToString()])
tmp._ItemTransition_ToIDCount = -1; // This will cause the data to be requeried
}
// TODO: Replace base ItemInfo.ToString function as necessary
@@ -388,24 +396,23 @@ namespace VEPROMS.CSLA.Library
#endregion
#region Factory Methods
private static int _ItemInfoUnique = 0;
private int _MyItemInfoUnique;
private static int ItemInfoUnique
{ get { return ++_ItemInfoUnique; } }
private int _MyItemInfoUnique = ItemInfoUnique;
public int MyItemInfoUnique
{
get { return _MyItemInfoUnique; }
}
private ItemInfo()
{ get { return _MyItemInfoUnique; } }
protected ItemInfo()
{/* require use of factory methods */
_MyItemInfoUnique = ++_ItemInfoUnique;
_AllList.Add(this);
AddToCache(this);
}
public void Dispose()
{
_AllList.Remove(this);
if (!_AllByPrimaryKey.ContainsKey(ItemID.ToString())) return;
List<ItemInfo> listItemInfo = _AllByPrimaryKey[ItemID.ToString()]; // Get the list of items
listItemInfo.Remove(this); // Remove the item from the list
RemoveFromCache(this);
if (!_CacheByPrimaryKey.ContainsKey(ItemID.ToString())) return;
List<ItemInfo> listItemInfo = _CacheByPrimaryKey[ItemID.ToString()]; // Get the list of items
while (listItemInfo.Contains(this)) listItemInfo.Remove(this); // Remove the item from the list
if (listItemInfo.Count == 0) // If there are no items left in the list
_AllByPrimaryKey.Remove(ItemID.ToString()); // remove the list
_CacheByPrimaryKey.Remove(ItemID.ToString()); // remove the list
}
public virtual Item Get()
{
@@ -415,22 +422,22 @@ namespace VEPROMS.CSLA.Library
{
string key = tmp.ItemID.ToString();
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(key))
foreach (ItemInfo tmpInfo in _AllByPrimaryKey[key])
if (_CacheByPrimaryKey.ContainsKey(key))
foreach (ItemInfo tmpInfo in _CacheByPrimaryKey[key])
tmpInfo.RefreshFields(tmp);
}
private void RefreshFields(Item tmp)
protected virtual void RefreshFields(Item tmp)
{
if (_PreviousID != tmp.PreviousID)
{
MyPrevious.RefreshNextItems(); // Update List for old value
if (MyPrevious != null) MyPrevious.RefreshNextItems(); // Update List for old value
_PreviousID = tmp.PreviousID; // Update the value
}
_MyPrevious = null; // Reset list so that the next line gets a new list
if (MyPrevious != null) MyPrevious.RefreshNextItems(); // Update List for new value
if (_ContentID != tmp.ContentID)
{
MyContent.RefreshContentItems(); // Update List for old value
if (MyContent != null) MyContent.RefreshContentItems(); // Update List for old value
_ContentID = tmp.ContentID; // Update the value
}
_MyContent = null; // Reset list so that the next line gets a new list
@@ -446,15 +453,15 @@ namespace VEPROMS.CSLA.Library
{
string key = tmp.ItemID.ToString();
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(key))
foreach (ItemInfo tmpInfo in _AllByPrimaryKey[key])
if (_CacheByPrimaryKey.ContainsKey(key))
foreach (ItemInfo tmpInfo in _CacheByPrimaryKey[key])
tmpInfo.RefreshFields(tmp);
}
private void RefreshFields(ContentItem tmp)
protected virtual void RefreshFields(ContentItem tmp)
{
if (_PreviousID != tmp.PreviousID)
{
MyPrevious.RefreshNextItems(); // Update List for old value
if (MyPrevious != null) MyPrevious.RefreshNextItems(); // Update List for old value
_PreviousID = tmp.PreviousID; // Update the value
}
_MyPrevious = null; // Reset list so that the next line gets a new list
@@ -472,13 +479,17 @@ namespace VEPROMS.CSLA.Library
// throw new System.Security.SecurityException("User not authorized to view a Item");
try
{
ItemInfo tmp = GetExistingByPrimaryKey(itemID);
ItemInfo tmp = GetCachedByPrimaryKey(itemID);
if (tmp == null)
{
tmp = DataPortal.Fetch<ItemInfo>(new PKCriteria(itemID));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up ItemInfo
tmp = null;
}
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp;
}
catch (Exception ex)
@@ -490,7 +501,6 @@ namespace VEPROMS.CSLA.Library
#region Data Access Portal
internal ItemInfo(SafeDataReader dr)
{
_MyItemInfoUnique = ++_ItemInfoUnique;
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] ItemInfo.Constructor", GetHashCode());
try
{