SetParentSectionAndDocVersion to eliminate lookups when the parent is known.

Use existing ActiveSection rather than "getting" an item from the database.
Add property "LoadAllAtOnce" for printing.
Allow MyProcedure to be set for "LoadAllAtOnce"
Allow MyDocVersion to be set  for "LoadAllAtOnce"
Created GetCachedByPrimaryKey for ProcedureInfo, SectionInfo and StepInfo.
Added Static Method GetItemAndChildren to get an entire procedure at once.
This commit is contained in:
Rich 2012-11-05 22:01:59 +00:00
parent 1b0023deb5
commit 2c78c091bf

View File

@ -310,6 +310,19 @@ namespace VEPROMS.CSLA.Library
// return ii._ItemID == _ItemID; // return ii._ItemID == _ItemID;
// return false; // return false;
//} //}
internal static void SetParentSectionAndDocVersion(ItemInfo itemInfo, IVEDrillDownReadOnly itemParent, SectionInfo sectionInfo, ProcedureInfo procInfo, DocVersionInfo docVersionInfo)
{
if (itemInfo == null) return;
itemInfo.LoadAllAtOnce = true;
itemInfo.ActiveParent = itemParent;
itemInfo.ActiveSection = sectionInfo;
itemInfo.MyProcedure = procInfo;
itemInfo.MyDocVersion = docVersionInfo;
if (itemInfo.MyContent.ContentPartCount > 0)
foreach (PartInfo pi in itemInfo.MyContent.ContentParts)
foreach (ItemInfo ii in pi.MyItems)
SetParentSectionAndDocVersion(ii, itemInfo, (itemInfo as SectionInfo) ?? sectionInfo, procInfo, docVersionInfo);
}
private int _PrintBias = 0; private int _PrintBias = 0;
public int PrintBias public int PrintBias
{ {
@ -730,7 +743,7 @@ namespace VEPROMS.CSLA.Library
throw new DbCslaException("ItemInfoList.DataPortal_Fetch", ex); throw new DbCslaException("ItemInfoList.DataPortal_Fetch", ex);
} }
} }
private void AddPart(SafeDataReader dr, ItemInfo itemInfo) internal void AddPart(SafeDataReader dr, ItemInfo itemInfo)
{ {
// Either a new PartInfo or an existing PartInfo // Either a new PartInfo or an existing PartInfo
if (dr.IsDBNull(dr.GetOrdinal("PreviousID"))) if (dr.IsDBNull(dr.GetOrdinal("PreviousID")))
@ -971,7 +984,9 @@ namespace VEPROMS.CSLA.Library
// check config value on my section, if null/default use Pmode of active format // check config value on my section, if null/default use Pmode of active format
if (ActiveSection != null) if (ActiveSection != null)
{ {
SectionInfo si = SectionInfo.Get(ActiveSection.ItemID); //ActiveSection as SectionInfo; SectionInfo si = ActiveSection as SectionInfo;
if(si == null)
si = SectionInfo.Get(ActiveSection.ItemID); //ActiveSection as SectionInfo;
if (si != null) if (si != null)
{ {
// there is no longer a "default" ENUM item - SectionConfig will return format default if needed // there is no longer a "default" ENUM item - SectionConfig will return format default if needed
@ -1185,8 +1200,8 @@ namespace VEPROMS.CSLA.Library
} }
#endregion #endregion
#region Ordinal CslaType and Tostring #region Ordinal CslaType and Tostring
[NonSerialized] //[NonSerialized]
private int? _Ordinal; internal int? _Ordinal;
public int Ordinal public int Ordinal
{ {
get get
@ -1569,14 +1584,19 @@ namespace VEPROMS.CSLA.Library
} }
} }
} }
internal ItemInfo _MyParent;
public ItemInfo MyParent public ItemInfo MyParent
{ {
get get
{ {
if (_MyParent == null)
{
//if (ItemDocVersionCount > 0) return ItemDocVersions[0]; Need to create one interface to support Folders, DocVersions and Items //if (ItemDocVersionCount > 0) return ItemDocVersions[0]; Need to create one interface to support Folders, DocVersions and Items
ContentInfo parentContent = ParentContent; ContentInfo parentContent = ParentContent;
if (parentContent == null || parentContent.ContentItemCount == 0) return null; if (parentContent == null || parentContent.ContentItemCount == 0) return null;
return parentContent.ContentItems[0]; _MyParent = parentContent.ContentItems[0];
}
return _MyParent;
} }
} }
#endregion #endregion
@ -1587,6 +1607,12 @@ namespace VEPROMS.CSLA.Library
itemInfoList = Lookup(fromType); itemInfoList = Lookup(fromType);
return itemInfoList; return itemInfoList;
} }
private bool _LoadAllAtOnce;
public bool LoadAllAtOnce
{
get { return _LoadAllAtOnce; }
set { _LoadAllAtOnce = value; }
}
internal ItemInfoList Lookup(int fromType) internal ItemInfoList Lookup(int fromType)
{ {
ItemInfoList itemInfoList = null; ItemInfoList itemInfoList = null;
@ -1594,6 +1620,9 @@ namespace VEPROMS.CSLA.Library
foreach (PartInfo partInfo in MyContent.ContentParts) foreach (PartInfo partInfo in MyContent.ContentParts)
if (partInfo.FromType == fromType) if (partInfo.FromType == fromType)
{ {
if(LoadAllAtOnce)
itemInfoList = partInfo._MyItems;// = ItemInfoList.GetList(partInfo.ItemID, partInfo.FromType);
else
itemInfoList = partInfo._MyItems = ItemInfoList.GetList(partInfo.ItemID, partInfo.FromType); itemInfoList = partInfo._MyItems = ItemInfoList.GetList(partInfo.ItemID, partInfo.FromType);
return itemInfoList; return itemInfoList;
} }
@ -1798,17 +1827,27 @@ namespace VEPROMS.CSLA.Library
return true; return true;
} }
} }
private ProcedureInfo _MyProcedure;
public ProcedureInfo MyProcedure public ProcedureInfo MyProcedure
{ {
get get
{ {
// Walk up active parents until the parent is not an item if (_MyProcedure == null)
ItemInfo tmp = this; {
while (tmp.ActiveParent != null && !tmp.ActiveParent.IsDocVersion) // Walk up active parents until the parent is not an item
tmp = (ItemInfo)tmp.ActiveParent; ItemInfo tmp = this;
if (tmp is ProcedureInfo) while (tmp.ActiveParent != null && !tmp.ActiveParent.IsDocVersion)
return tmp as ProcedureInfo; tmp = (ItemInfo)tmp.ActiveParent;
return ProcedureInfo.Get(tmp.ItemID); if (tmp is ProcedureInfo)
_MyProcedure = tmp as ProcedureInfo;
else
_MyProcedure = ProcedureInfo.Get(tmp.ItemID);
}
return _MyProcedure;
}
set
{
_MyProcedure = value;
} }
} }
private ItemInfo _MyHLS = null; private ItemInfo _MyHLS = null;
@ -1847,8 +1886,9 @@ namespace VEPROMS.CSLA.Library
} }
return _MyDocVersion; return _MyDocVersion;
} }
set { _MyDocVersion = value; }
} }
private IVEDrillDownReadOnly _ActiveParent = null; internal IVEDrillDownReadOnly _ActiveParent = null;
public IVEDrillDownReadOnly MyActiveParent { get { return _ActiveParent; } } public IVEDrillDownReadOnly MyActiveParent { get { return _ActiveParent; } }
public IVEDrillDownReadOnly ActiveParent public IVEDrillDownReadOnly ActiveParent
{ {
@ -1895,7 +1935,7 @@ namespace VEPROMS.CSLA.Library
_ActiveParent = value; _ActiveParent = value;
} }
} }
private ItemInfo _ActiveSection = null; internal ItemInfo _ActiveSection = null;
/// <summary> /// <summary>
/// MyActiveSection is used to determine if _ActiveSection is null or not. /// MyActiveSection is used to determine if _ActiveSection is null or not.
/// </summary> /// </summary>
@ -3666,7 +3706,7 @@ namespace VEPROMS.CSLA.Library
// throw new System.Security.SecurityException("User not authorized to view a Item"); // throw new System.Security.SecurityException("User not authorized to view a Item");
try try
{ {
ProcedureInfo tmp = GetCachedByPrimaryKey(itemID) as ProcedureInfo; ProcedureInfo tmp = GetCachedByPrimaryKey(itemID);
if (tmp == null) if (tmp == null)
{ {
tmp = DataPortal.Fetch<ProcedureInfo>(new PKCriteria(itemID)); tmp = DataPortal.Fetch<ProcedureInfo>(new PKCriteria(itemID));
@ -3680,6 +3720,16 @@ namespace VEPROMS.CSLA.Library
throw new DbCslaException("Error on Item.Get", ex); throw new DbCslaException("Error on Item.Get", ex);
} }
} }
protected new static ProcedureInfo GetCachedByPrimaryKey(int itemID)
{
ConvertListToDictionary();
string key = itemID.ToString();
if (_CacheByPrimaryKey.ContainsKey(key))
foreach (ItemInfo ii in _CacheByPrimaryKey[key])
if (ii is ProcedureInfo)
return ii as ProcedureInfo;
return null;
}
public void MoveProcedure(IVEDrillDownReadOnly pInfo, int index) public void MoveProcedure(IVEDrillDownReadOnly pInfo, int index)
{ {
using (Item ii = Item.Get(this.ItemID)) using (Item ii = Item.Get(this.ItemID))
@ -3706,6 +3756,99 @@ namespace VEPROMS.CSLA.Library
{ {
get { return ActiveParent as DocVersionInfo; } get { return ActiveParent as DocVersionInfo; }
} }
public static ProcedureInfo GetItemAndChildren(int? itemID)
{
try
{
ProcedureInfo tmp = DataPortal.Fetch<ProcedureInfo>(new ItemAndChildrenCriteria(itemID));
//AddToCache(tmp);
if (tmp.ErrorMessage == "No Record Found") tmp = null;
if (tmp != null)
SetParentSectionAndDocVersion(tmp, tmp.MyDocVersion, null, tmp, tmp.MyDocVersion);
return tmp;
}
catch (Exception ex)
{
throw new DbCslaException("Error on ItemInfoList.GetChildren", ex);
}
}
[Serializable()]
private class ItemAndChildrenCriteria
{
public ItemAndChildrenCriteria(int? itemID)
{
_ItemID = itemID;
}
private int? _ItemID;
public int? ItemID
{
get { return _ItemID; }
set { _ItemID = value; }
}
}
// Data Portal to Get Item and Children
private void DataPortal_Fetch(ItemAndChildrenCriteria criteria)
{
//ItemInfo tmp = null;
Dictionary<int, ItemInfo> lookup = new Dictionary<int, ItemInfo>(); ;
try
{
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
{
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "vesp_ListItemAndChildren";
cm.Parameters.AddWithValue("@ItemID", criteria.ItemID);
cm.Parameters.AddWithValue("@ParentID", 0);
cm.CommandTimeout = Database.DefaultTimeout;
using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
{
while (dr.Read())
{
if (dr.GetInt32("Level") == 0)
{
//tmp = itemInfo;
ReadData(dr);
AddContent(dr);
lookup[this.ItemID] = this;
}
else
{
ItemInfo itemInfo = null;
int itemType = dr.GetInt32("Type") / 10000;
switch (itemType)
{
case 0:
itemInfo = new ProcedureInfo(dr);
break;
case 1:
itemInfo = new SectionInfo(dr);
break;
case 2:
itemInfo = new StepInfo(dr);
break;
}
// Load Children
itemInfo.AddContent(dr);
ItemInfo parent = lookup[dr.GetInt32("ParentID")];
itemInfo._ActiveParent = parent;
itemInfo._ActiveSection = (itemInfo.IsSection ? itemInfo : parent._ActiveSection);
parent.AddPart(dr, itemInfo);
lookup.Add(itemInfo.ItemID, itemInfo);
}
}
//Console.WriteLine("I'm here {0}",this.MyContent.ContentPartCount);
}
}
}
}
catch (Exception ex)
{
Database.LogException("ItemInfoList.DataPortal_Fetch", ex);
throw new DbCslaException("ItemInfoList.DataPortal_Fetch", ex);
}
}
} }
#endregion #endregion
#region Procedure #region Procedure
@ -3854,7 +3997,7 @@ namespace VEPROMS.CSLA.Library
// throw new System.Security.SecurityException("User not authorized to view a Item"); // throw new System.Security.SecurityException("User not authorized to view a Item");
try try
{ {
SectionInfo tmp = GetCachedByPrimaryKey(itemID) as SectionInfo; SectionInfo tmp = GetCachedByPrimaryKey(itemID);
if (tmp == null) if (tmp == null)
{ {
tmp = DataPortal.Fetch<SectionInfo>(new PKCriteria(itemID)); tmp = DataPortal.Fetch<SectionInfo>(new PKCriteria(itemID));
@ -3868,6 +4011,16 @@ namespace VEPROMS.CSLA.Library
throw new DbCslaException("Error on Item.Get", ex); throw new DbCslaException("Error on Item.Get", ex);
} }
} }
protected new static SectionInfo GetCachedByPrimaryKey(int itemID)
{
ConvertListToDictionary();
string key = itemID.ToString();
if (_CacheByPrimaryKey.ContainsKey(key))
foreach (ItemInfo ii in _CacheByPrimaryKey[key])
if (ii is SectionInfo)
return ii as SectionInfo;
return null;
}
public void MoveSection(IVEDrillDownReadOnly pInfo, int index) public void MoveSection(IVEDrillDownReadOnly pInfo, int index)
{ {
using (Item ii = Item.Get(this.ItemID)) using (Item ii = Item.Get(this.ItemID))
@ -3986,7 +4139,7 @@ namespace VEPROMS.CSLA.Library
// throw new System.Security.SecurityException("User not authorized to view a Item"); // throw new System.Security.SecurityException("User not authorized to view a Item");
try try
{ {
StepInfo tmp = GetCachedByPrimaryKey(itemID) as StepInfo; StepInfo tmp = GetCachedByPrimaryKey(itemID);
if (tmp == null) if (tmp == null)
{ {
tmp = DataPortal.Fetch<StepInfo>(new PKCriteria(itemID)); tmp = DataPortal.Fetch<StepInfo>(new PKCriteria(itemID));
@ -4000,6 +4153,16 @@ namespace VEPROMS.CSLA.Library
throw new DbCslaException("Error on Item.Get", ex); throw new DbCslaException("Error on Item.Get", ex);
} }
} }
protected new static StepInfo GetCachedByPrimaryKey(int itemID)
{
ConvertListToDictionary();
string key = itemID.ToString();
if (_CacheByPrimaryKey.ContainsKey(key))
foreach (ItemInfo ii in _CacheByPrimaryKey[key])
if (ii is StepInfo)
return ii as StepInfo;
return null;
}
public void MoveStep(IVEDrillDownReadOnly pInfo, int index) public void MoveStep(IVEDrillDownReadOnly pInfo, int index)
{ {
using (Item ii = Item.Get(this.ItemID)) using (Item ii = Item.Get(this.ItemID))