1082 lines
37 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using Csla;
using Csla.Data;
namespace VEPROMS.CSLA.Library
{
public delegate void ItemInfoInsertEvent(object sender, ItemInfoInsertEventArgs args);
public class ItemInfoInsertEventArgs
{
private ItemInfo _ItemInserted;
public ItemInfo ItemInserted
{
get { return _ItemInserted; }
set { _ItemInserted = value; }
}
private ItemInfo.EAddpingPart _AddingPart;
public ItemInfo.EAddpingPart AddingPart
{
get { return _AddingPart; }
set { _AddingPart = value; }
}
public ItemInfoInsertEventArgs(ItemInfo itemInserted,ItemInfo.EAddpingPart addingPart)
{
_ItemInserted = itemInserted;
_AddingPart = addingPart;
}
}
public partial class ItemInfo
{
#region Old Insert
//public ItemInfo InsertSiblingBeforeOld(string text)
//{
// return InsertSiblingBefore(text, null);
//}
//public ItemInfo InsertSiblingBeforeOld(string text, string number)
//{
// ItemInfo prevItemInfo = MyPrevious;
// ItemInfo newItemInfo = null;
// PartInfoList partInfoList = null;
// if (MyParent != null && MyParent.MyContent != null)
// {
// MyParent.MyContent.RefreshContentParts();
// partInfoList = MyParent.MyContent.ContentParts;
// }
// using (Item prevItem = prevItemInfo == null ? null : prevItemInfo.Get()) // Get the previous Item
// {
// using (Item newItem = Item.MakeItem(prevItem, Content.MakeContent(number, text, MyContent.Type, null, null))) // Create the new Item
// {
// using (Item thisItem = Get()) // Get the next item in the list
// {
// thisItem.MyPrevious = newItem; // Point to the new item
// thisItem.Save(); // Save Changes
// if (prevItem == null)
// {
// PartInfo partInfo = partInfoList.Find(this);
// using (Part part = partInfo.Get())
// {
// part.MyItem = newItem;
// part.Save();
// }
// }
// }
// // ToDo: Need change PartInfo in PartInfoList
// newItemInfo = ItemInfo.Get(newItem.ItemID);
// newItemInfo.ResetOrdinal();
// }
// }
// return newItemInfo;
//}
//public ItemInfo InsertSiblingAfterOld(string text)
//{
// return InsertSiblingAfter(text, null);
//}
//public ItemInfo InsertSiblingAfterOld(string text, string number)
//{
// return InsertSiblingAfter(text, number, MyContent.Type);
//}
//public ItemInfo InsertSiblingAfterOld(string text, string number, int? type)
//{
// ItemInfo nextItemInfo = NextItem;
// ItemInfo newItemInfo = null;
// using (Item thisItem = Get()) // Get the Current Item
// {
// using (Item newItem = Item.MakeItem(thisItem, Content.MakeContent(number, text, type, null, null))) // Create the new Item
// {
// if (nextItemInfo != null) // Simple case, adding to end of list:
// using (Item nextItem = nextItemInfo.Get()) // Get the next item in the list
// {
// nextItem.MyPrevious = newItem; // Point to the new item
// nextItem.Save(); // Save Changes
// }
// // ToDo: Need change PartInfo in PartInfoList
// newItemInfo = ItemInfo.Get(newItem.ItemID);
// newItemInfo.ResetOrdinal();
// }
// }
// return newItemInfo;
//}
//public ItemInfo InsertChildOld(E_FromType fromType, int type, string text)
//{
// return InsertChild(fromType, type, text, null);
//}
//public ItemInfo InsertChildOld(E_FromType fromType, int type, string text, string number)
//{
// ItemInfo newItemInfo = null;
// using (Item thisItem = Get()) // Get the Current Item
// {
// using (Item newItem = Item.MakeItem(null, Content.MakeContent(number, text, type, null, null))) // Create the new Item
// {
// PartInfo partInfo = MyContent.ContentParts == null ? null : MyContent.ContentParts.Find(fromType);
// if (partInfo != null)
// {
// //this could be equivalent to adding a sibling with a specific type
// using (Part part = partInfo.Get())
// {
// part.MyItem.MyPrevious = newItem;
// part.MyItem.Save();
// part.MyItem = newItem;
// part.Save();
// }
// }
// else
// {
// // This means that a part needs to be added to point to the new item
// //using (Part part = Part.MakePart(thisItem.MyContent, ))
// //{ ;}
// thisItem.MyContent.ContentParts.Add((int)fromType, newItem);
// thisItem.Save();
// }
// newItemInfo = ItemInfo.Get(newItem.ItemID);
// }
// }
// ResetParts();
// return newItemInfo;
//}
#endregion
#region Events
public event ItemInfoEvent BeforeDelete;
public void OnBeforeDelete()
{
OnBeforeDelete(ItemID);
if (MyContent.ContentPartCount > 0)
{
foreach (PartInfo pi in MyContent.ContentParts)
{
foreach (ItemInfo ii in pi.MyItems)
{
ii.OnBeforeDelete();
}
}
}
//if (ItemPartCount > 0)
//{
// foreach (PartInfo pi in ItemParts)
// {
// foreach (ItemInfo ii in pi.MyItems)
// {
// ii.OnBeforeDelete();
// }
// }
//}
}
private void OnBeforeDelete(int ItemID)
{
if (_CacheByPrimaryKey.ContainsKey(ItemID.ToString()))
{
List<ItemInfo> itmlst = _CacheByPrimaryKey[ItemID.ToString()];
foreach (ItemInfo itm in itmlst)
if (itm.BeforeDelete != null)
{
itm.BeforeDelete(itm);
}
}
}
public static event ItemInfoEvent ItemDeleted;
public event ItemInfoEvent Deleted;
internal void OnDeleted(object sender)
{
if (Deleted != null) Deleted(sender);
if (ItemDeleted != null) ItemDeleted(sender);
if (MyParent != null)
{
MyParent.OnChildrenDeleted(sender);
MyParent.MyContent.RefreshContentParts();
}
}
//internal void OnDeleted(object sender, Volian.Base.Library.VlnTimer tmr)
//{
//tmr.ActiveProcess = "Deleted";
//if (Deleted != null) Deleted(sender);
//tmr.ActiveProcess = "ItemDeleted";
//if (ItemDeleted != null) ItemDeleted(sender);
//if (MyParent != null)
//{
//tmr.ActiveProcess = "OnChildrenDeleted";
//MyParent.OnChildrenDeleted(sender);
//tmr.ActiveProcess = "RefreshContentParts";
//MyParent.MyContent.RefreshContentParts();
//}
//}
public event ItemInfoEvent ChildrenDeleted;
internal void OnChildrenDeleted(object sender)
{
if (_CacheByPrimaryKey.ContainsKey(ItemID.ToString()))
{
List<ItemInfo> itmlst = _CacheByPrimaryKey[ItemID.ToString()];
foreach (ItemInfo itm in itmlst)
if (itm.ChildrenDeleted != null)
{
itm.ChildrenDeleted(itm);
}
}
}
public event ItemInfoInsertEvent NewSiblingAfter;
internal void OnNewSiblingAfter(object sender, ItemInfoInsertEventArgs args)
{
if (NewSiblingAfter != null) NewSiblingAfter(sender,args);
if (MyParent != null)
MyParent.MyContent.RefreshContentParts();
}
internal void OnNewSiblingAfter(ItemInfoInsertEventArgs args)
{
string key = ItemID.ToString();
if (_CacheByPrimaryKey.ContainsKey(key))
{
ItemInfo[] items = _CacheByPrimaryKey[key].ToArray();
foreach (ItemInfo item in items)
{
item.OnNewSiblingAfter(item,args);
}
}
}
public event ItemInfoInsertEvent NewSiblingBefore;
internal void OnNewSiblingBefore(object sender, ItemInfoInsertEventArgs args)
{
if (NewSiblingBefore != null) NewSiblingBefore(sender,args);
if (MyParent != null)
MyParent.MyContent.RefreshContentParts();
}
internal void OnNewSiblingBefore(ItemInfoInsertEventArgs args)
{
string key = ItemID.ToString();
if (_CacheByPrimaryKey.ContainsKey(key))
{
ItemInfo[] items = _CacheByPrimaryKey[key].ToArray();
foreach (ItemInfo item in items)
{
item.OnNewSiblingBefore(item,args);
}
}
}
public event ItemInfoInsertEvent NewChild;
internal void OnNewChild(object sender, ItemInfoInsertEventArgs args)
{
if (NewChild != null) NewChild(sender, args);
MyContent.RefreshContentParts();
}
internal void OnNewChild(ItemInfoInsertEventArgs args)
{
string key = ItemID.ToString();
if (_CacheByPrimaryKey.ContainsKey(key))
{
ItemInfo[] items = _CacheByPrimaryKey[key].ToArray();
foreach (ItemInfo item in items)
{
item.OnNewChild(item, args);
}
}
}
#endregion
#region Insert Before
public ItemInfo InsertSiblingBefore(string text)
{
return InsertSiblingBefore(text, null);
}
public ItemInfo InsertSiblingBefore(string text, string number)
{
ItemInfo tmp = NewItemInfoFetch(ItemID, EAddpingPart.Before, number, text, null, null, null, null, DateTime.Now, Volian.Base.Library.VlnSettings.UserID);
// this item is updated in SQL so we have to manually force the iteminfo updates
// Refresh ItemInfo to update Previous
using (Item item = Get()) ItemInfo.Refresh(item);
// Update all of the content records that have transitions that point to the Siblings or Sibling Children of the new item
tmp.UpdateTransitionText();
OnNewSiblingBefore(new ItemInfoInsertEventArgs(tmp, EAddpingPart.Before));
((ItemInfo)ActiveParent).MyContent.RefreshContentParts();
return tmp;
}
#endregion
#region PasteSiblingBefore
public ItemInfo PasteSiblingBefore(int copyStartID)
{
// To determine 'type' of pasted item, if it's a step (type >=20000), use the originating
// item, i.e. item inserting after. If it's a section or procedure, use the copied item's type.
ItemInfo cpItem = ItemInfo.Get(copyStartID);
int? type = MyContent.Type >= 20000 ? MyContent.Type : cpItem.MyContent.Type;
ItemInfo tmp = CopyPasteItemInfoFetch(copyStartID, this.ItemID, type, type, EAddpingPart.Before);
using (Item item = Get()) ItemInfo.Refresh(item);
tmp.UpdateTransitionText();
tmp.UpdatePastedStepTransitionText();
OnNewSiblingBefore(new ItemInfoInsertEventArgs(tmp, EAddpingPart.Before));
return tmp;
}
public ItemInfo PasteSiblingAfter(int copyStartID)
{
// To determine 'type' of pasted item, if it's a step (type >=20000), use the originating
// item, i.e. item inserting after. If it's a section or procedure, use the copied item's type.
ItemInfo cpItem = ItemInfo.Get(copyStartID);
int? type = MyContent.Type >= 20000 ? MyContent.Type : cpItem.MyContent.Type;
ItemInfo tmp = CopyPasteItemInfoFetch(copyStartID, this.ItemID, type, type, EAddpingPart.After);
using (Item item = Get()) ItemInfo.Refresh(item);
if (tmp.NextItem != null) using (Item item = tmp.NextItem.Get()) ItemInfo.Refresh(item);
RefreshNextItems();
// if inserting after a caution or note, refreshes tabs. This will adjust bullets
// of any previous cautions or notes.
if (tmp.IsCaution || tmp.IsNote) ResetOrdinal();
tmp.UpdateTransitionText();
tmp.UpdatePastedStepTransitionText();
OnNewSiblingAfter(new ItemInfoInsertEventArgs(tmp, EAddpingPart.After));
return tmp;
}
private ItemInfo CopyPasteItemInfoFetch(int copyStartID, int itemID, int? type, int? fromType, EAddpingPart addType)
{
ItemInfo tmp=null;
if (addType == EAddpingPart.Child) // has to add in child relationship - uses 'fromtype'
{
// adding children. Type is based on 'fromType'
switch ((E_FromType)fromType)
{
case E_FromType.Procedure:
tmp = DataPortal.Fetch<ProcedureInfo>(new PastingPartCriteria(copyStartID, itemID, addType, type, fromType, DateTime.Now, Volian.Base.Library.VlnSettings.UserID));
break;
case E_FromType.Section:
tmp = DataPortal.Fetch<SectionInfo>(new PastingPartCriteria(copyStartID, itemID, addType, type, fromType, DateTime.Now, Volian.Base.Library.VlnSettings.UserID));
break;
default:
tmp = DataPortal.Fetch<StepInfo>(new PastingPartCriteria(copyStartID, itemID, addType, type, fromType, DateTime.Now, Volian.Base.Library.VlnSettings.UserID));
break;
}
}
else
{
if (GetType() == typeof(ProcedureInfo))
tmp = DataPortal.Fetch<ProcedureInfo>(new PastingPartCriteria(copyStartID, itemID, addType, type, fromType, DateTime.Now, Volian.Base.Library.VlnSettings.UserID));
else if (GetType() == typeof(SectionInfo))
tmp = DataPortal.Fetch<SectionInfo>(new PastingPartCriteria(copyStartID, itemID, addType, type, fromType, DateTime.Now, Volian.Base.Library.VlnSettings.UserID));
else
tmp = DataPortal.Fetch<StepInfo>(new PastingPartCriteria(copyStartID, itemID, addType, type, fromType, DateTime.Now, Volian.Base.Library.VlnSettings.UserID));
}
return tmp;
}
#endregion
#region Insert After
public ItemInfo InsertSiblingAfter(string text)
{
return InsertSiblingAfter(text, null);
}
public ItemInfo InsertSiblingAfter(string text, string number)
{
return InsertSiblingAfter(text, number, MyContent.Type);
}
public ItemInfo InsertSiblingAfter(string text, string number, int? type)
{
ItemInfo tmp = NewItemInfoFetch(ItemID, EAddpingPart.After, number, text, type, null, null, null, DateTime.Now, Volian.Base.Library.VlnSettings.UserID);
//tmp = DataPortal.Fetch<StepInfo>(new AddingPartCriteria(ItemID, EAddpingPart.After, number, text, type, null, null, null, DateTime.Now, Volian.Base.Library.VlnSettings.UserID));
// if next exists, it is updated in SQL so we have to manually force the iteminfo updates
// Refresh ItemInfo to update PreviousID field
if (tmp.NextItem != null) using (Item item = tmp.NextItem.Get()) ItemInfo.Refresh(item);
RefreshNextItems();
// if inserting after a caution or note, refreshes tabs. This will adjust bullets
// of any previous cautions or notes.
if (tmp.IsCaution || tmp.IsNote) ResetOrdinal();
// Update all of the content records that have transitions that point to the Siblings or Sibling Children of the new item
tmp.UpdateTransitionText();
OnNewSiblingAfter(new ItemInfoInsertEventArgs(tmp, EAddpingPart.After));
return tmp;
}
private ItemInfo NewItemInfoFetch(int itemID, EAddpingPart addType, string number, string text, int? type, int? fromType, int? formatID, string config, DateTime dts, string userID)
{
ItemInfo tmp;
if (addType == EAddpingPart.Child)
{
// adding children. Type is based on 'fromType'
switch ((E_FromType)fromType)
{
case E_FromType.Procedure:
tmp = DataPortal.Fetch<ProcedureInfo>(new AddingPartCriteria(itemID, addType, number, text, type, fromType, formatID, config, dts, userID));
break;
case E_FromType.Section:
tmp = DataPortal.Fetch<SectionInfo>(new AddingPartCriteria(itemID, addType, number, text, type, fromType, formatID, config, dts, userID));
break;
default:
tmp = DataPortal.Fetch<StepInfo>(new AddingPartCriteria(itemID, addType, number, text, type, fromType, formatID, config, dts, userID));
break;
}
}
else
{
if (GetType() == typeof(ProcedureInfo))
tmp = DataPortal.Fetch<ProcedureInfo>(new AddingPartCriteria(itemID, addType, number, text, type, fromType, formatID, config, dts, userID));
else if (GetType() == typeof(SectionInfo))
tmp = DataPortal.Fetch<SectionInfo>(new AddingPartCriteria(itemID, addType, number, text, type, fromType, formatID, config, dts, userID));
else
tmp = DataPortal.Fetch<StepInfo>(new AddingPartCriteria(itemID, addType, number, text, type, fromType, formatID, config, dts, userID));
}
return tmp;
}
public void UpdateTransitionText()
{
// Update Ordinals from here down for Steps
if (!IsStep) return;
ResetOrdinal();
// This returns a list of all of the transitions that may have been affected
using(TransitionInfoList trans = TransitionInfoList.GetAffected(this.ItemID))
{
#region B2012-071 fix JCB
foreach (TransitionInfo tran in trans)
{
Content oldContent = null;
using (Content content = tran.MyContent.Get())
{
if (oldContent != null && content.ContentID != oldContent.ContentID)
{
if (oldContent.IsDirty)
{
oldContent.DTS = DateTime.Now;
#region B2012-079 fix JCB
oldContent.UserID = Volian.Base.Library.VlnSettings.UserID + " - Renumber";
#endregion B2012-079 fix JCB
oldContent.Save();
}
else // Update ContentInfo objects to reflect the change in the transition
ContentInfo.Refresh(oldContent);
oldContent = content;
}
if (oldContent == null) oldContent = content;
oldContent.FixTransitionText(tran);
//if (content.IsDirty)
//{
// content.DTS = DateTime.Now;
// content.Save();
//}
if (oldContent != null)
{
if (oldContent.IsDirty)
{
oldContent.DTS = DateTime.Now;
#region B2012-079 fix JCB
oldContent.UserID = Volian.Base.Library.VlnSettings.UserID + " - Renumber";
#endregion B2012-079 fix JCB
oldContent.Save();
}
else // Update ContentInfo objects to reflect the change in the transition
ContentInfo.Refresh(oldContent);
}
}
}
#endregion B2012-071 fix JCB
}
}
public void UpdatePastedStepTransitionText()
{
// Get a list of all of the transitions included in this step & its children.
// Their transition text may have been affected, i.e. depending on the format, etc
using (TransitionInfoList trans = TransitionInfoList.GetPastedAffected(this.ItemID))
{
foreach (TransitionInfo tran in trans)
{
using (Content content = tran.MyContent.Get())
{
content.FixTransitionText(tran);
if (content.IsDirty)
content.Save();
else // Update ContentInfo objects to reflect the change in the transition
ContentInfo.Refresh(content);
}
}
}
}
#endregion
#region Insert Child
public ItemInfo InsertChild(E_FromType fromType, int type, string text)
{
return InsertChild(fromType, type, text, null);
}
public ItemInfo InsertChild(E_FromType fromType, int type, string text, string number)
{
ItemInfo tmp = NewItemInfoFetch(ItemID, EAddpingPart.Child, number, text, type, (int?)fromType, null, null, DateTime.Now, Volian.Base.Library.VlnSettings.UserID);
// if next exists, it is updated in SQL so we have to manually force the iteminfo updates
// Refresh ItemInfo to update PreviousID field
if (tmp.NextItem != null) using (Item item = tmp.NextItem.Get()) ItemInfo.Refresh(item);
// Update all of the content records that have transitions that point to the Siblings or Sibling Children of the new item
tmp.UpdateTransitionText();
MyContent.RefreshContentParts();
OnNewChild(new ItemInfoInsertEventArgs(tmp, EAddpingPart.Child));
return tmp;
}
#endregion
#region DataPortal
private void DataPortal_Fetch(PastingPartCriteria criteria)
{
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] ItemInfo.DataPortal_Fetch", GetHashCode());
try
{
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
{
ApplicationContext.LocalContext["cn"] = cn;
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.AddWithValue("@StartItemID", criteria.StartItemID);
cm.Parameters.AddWithValue("@ItemID", criteria.ItemID); //ABC After Before Child
cm.Parameters.AddWithValue("@Type", criteria.Type); //ABC
cm.Parameters.AddWithValue("@DTS", criteria.DTS); //ABC
cm.Parameters.AddWithValue("@UserID", criteria.UserID); //ABC
SqlParameter param_ContentID = new SqlParameter("@NewItemID", SqlDbType.Int);
param_ContentID.Direction = ParameterDirection.Output;
cm.Parameters.Add(param_ContentID);
switch (criteria.AddType)
{
case EAddpingPart.Child:
//cm.CommandText = "addItemChild";
//cm.Parameters.AddWithValue("@FromType", criteria.FromType); //--C
break;
case EAddpingPart.Before:
cm.CommandText = "PasteItemSiblingBefore";
break;
case EAddpingPart.After:
cm.CommandText = "PasteItemSiblingAfter";
break;
case EAddpingPart.Replace:
cm.CommandText = "PasteItemReplace";
break;
}
cm.CommandTimeout = Database.DefaultTimeout;
using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
{
//newItemID = (int)cm.Parameters["@newContentID"].Value;
if (!dr.Read())
{
_ErrorMessage = "No Record Found";
return;
}
ReadData(dr);
}
}
// removing of item only needed for local data portal
if (ApplicationContext.ExecutionLocation == ApplicationContext.ExecutionLocations.Client)
ApplicationContext.LocalContext.Remove("cn");
}
}
catch (Exception ex)
{
if (_MyLog.IsErrorEnabled) _MyLog.Error("ItemInfo.DataPortal_Fetch", ex);
_ErrorMessage = ex.Message;
throw new DbCslaException("ItemInfo.DataPortal_Fetch", ex);
}
}
private void DataPortal_Fetch(AddingPartCriteria criteria)
{
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] ItemInfo.DataPortal_Fetch", GetHashCode());
try
{
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
{
ApplicationContext.LocalContext["cn"] = cn;
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.AddWithValue("@ItemID", criteria.ItemID); //ABC After Before Child
cm.Parameters.AddWithValue("@Number", criteria.Number); //ABC
cm.Parameters.AddWithValue("@Text", criteria.Text); //ABC
cm.Parameters.AddWithValue("@FormatID", criteria.FormatID); //ABC
cm.Parameters.AddWithValue("@Config", criteria.Config); //ABC
cm.Parameters.AddWithValue("@Type", criteria.Type); //ABC
cm.Parameters.AddWithValue("@DTS", criteria.DTS); //ABC
cm.Parameters.AddWithValue("@UserID", criteria.UserID); //ABC
SqlParameter param_ContentID = new SqlParameter("@newItemID", SqlDbType.Int);
param_ContentID.Direction = ParameterDirection.Output;
cm.Parameters.Add(param_ContentID);
switch (criteria.AddType)
{
case EAddpingPart.Child:
cm.CommandText = "addItemChild";
cm.Parameters.AddWithValue("@FromType", criteria.FromType); //--C
break;
case EAddpingPart.Before:
cm.CommandText = "addItemSiblingBefore";
break;
case EAddpingPart.After:
cm.CommandText = "addItemSiblingAfter";
break;
}
cm.CommandTimeout = Database.DefaultTimeout;
using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
{
//newItemID = (int)cm.Parameters["@newContentID"].Value;
if (!dr.Read())
{
_ErrorMessage = "No Record Found";
return;
}
ReadData(dr);
}
}
// removing of item only needed for local data portal
if (ApplicationContext.ExecutionLocation == ApplicationContext.ExecutionLocations.Client)
ApplicationContext.LocalContext.Remove("cn");
}
}
catch (Exception ex)
{
if (_MyLog.IsErrorEnabled) _MyLog.Error("ItemInfo.DataPortal_Fetch", ex);
_ErrorMessage = ex.Message;
throw new DbCslaException("ItemInfo.DataPortal_Fetch", ex);
}
}
#endregion
#region PastingPartCriteria
[Serializable()]
public class PastingPartCriteria
{
#region Properties
private int _StartItemID;
public int StartItemID
{
get { return _StartItemID; }
set { _StartItemID = value; }
}
//private int _CopyEndID;
//public int CopyEndID
//{
// get { return _CopyEndID; }
// set { _CopyEndID = value; }
//}
private int _ItemID; // paste relative to this itemid
public int ItemID
{
get { return _ItemID; }
set { _ItemID = value; }
}
private EAddpingPart _AddType;
public EAddpingPart AddType
{
get { return _AddType; }
set { _AddType = value; }
}
private int? _FromType = null;
public int? FromType
{
get { return _FromType; }
set { _FromType = value; }
}
private int? _Type = null;
public int? Type
{
get { return _Type; }
set { _Type = value; }
}
private DateTime _DTS;
public DateTime DTS
{
get { return _DTS; }
set { _DTS = value; }
}
private string _UserID;
public string UserID
{
get { return _UserID; }
set { _UserID = value; }
}
#endregion
#region Constructor
public PastingPartCriteria(int startItemid, int itemID, EAddpingPart addType, int? type, int? fromType, DateTime dts, string userID)
{
_StartItemID = startItemid;
_ItemID = itemID;
_AddType = addType;
_Type = type;
_FromType = fromType;
_DTS = dts;
_UserID = userID;
}
#endregion
}
#endregion
[Serializable()]
protected class AddingPartCriteria
{
#region Properties
private int _ItemID;
public int ItemID
{
get { return _ItemID; }
set { _ItemID = value; }
}
private EAddpingPart _AddType;
public EAddpingPart AddType
{
get { return _AddType; }
set { _AddType = value; }
}
private string _Number=null;
public string Number
{
get { return _Number; }
set { _Number = value; }
}
private string _Text=null;
public string Text
{
get { return _Text; }
set { _Text = value; }
}
private int? _FromType = null;
public int? FromType
{
get { return _FromType; }
set { _FromType = value; }
}
private int? _Type=null;
public int? Type
{
get { return _Type; }
set { _Type = value; }
}
private int? _FormatID=null;
public int? FormatID
{
get { return _FormatID; }
set { _FormatID = value; }
}
private string _Config=null;
public string Config
{
get { return _Config; }
set { _Config = value; }
}
private DateTime _DTS;
public DateTime DTS
{
get { return _DTS; }
set { _DTS = value; }
}
private string _UserID;
public string UserID
{
get { return _UserID; }
set { _UserID = value; }
}
#endregion
#region Constructor
public AddingPartCriteria(int itemID, EAddpingPart addType, string number, string text, int? type, int? fromType, int? formatID, string config, DateTime dts, string userID)
{
_ItemID = itemID;
_AddType = addType;
_Number = number;
_Text = text;
_Type = type;
_FromType = fromType;
_FormatID = formatID;
_Config = config;
_DTS = dts;
_UserID = userID;
}
#endregion
}
public enum EAddpingPart
{
Child=0,
Before=1,
After=2,
Replace=3
}
public ItemInfo NextItem
{
get
{
if (NextItemCount > 0 && NextItems != null && NextItems.Count > 0)
return NextItems[0];
return null;
}
}
private static void ResetOrdinal(int itemID)
{
ConvertListToDictionary();
string key = itemID.ToString();
while (key != null && _CacheByPrimaryKey.ContainsKey(key))
{
ItemInfo[] items = _CacheByPrimaryKey[key].ToArray();
key = null;
foreach (ItemInfo item in items)
{
item._Ordinal = null;
item._TagsSetup = false;
//item.MyContent.ShowChange(); // Replaced with OnOrdinalChange below
if (key == null && item.NextItem != null)
key = item.NextItem.ItemID.ToString();
}
ItemInfo.OnOrdinalChange(items[0].ItemID); // After _Ordinal reset trigger event
}
}
public void ResetOrdinal()
{
ResetOrdinal(ItemID);
}
public static void DeleteItemInfoAndChildren(int itemID)
{
//Volian.Base.Library.VlnTimer _MyTimer = new Volian.Base.Library.VlnTimer();
//_MyTimer.ActiveProcess = "ConvertListToDictionary";
ConvertListToDictionary();
string key = itemID.ToString();
if(_CacheByPrimaryKey.ContainsKey(key))
{
ItemInfo [] items = _CacheByPrimaryKey[key].ToArray();
ItemInfo parent = items[0].ActiveParent as ItemInfo;
//_MyTimer.ActiveProcess = "ResetParts";
if (parent != null)
ItemInfo.ResetParts(parent.ItemID);
//Console.WriteLine("\r\n\r\n'Deleting Item {0}'\r\n{0}'Type ','ParentID','ParentUnique','ItemID','Unique'",itemID);
foreach (ItemInfo item in items)
{
//_MyTimer.ActiveProcess = "IsDeleted";
item.IsDeleted = true;
//_MyTimer.ActiveProcess = string.Format("OnDeleted {0}", items.Length);
item.OnDeleted(item);
//item.OnDeleted(item, _MyTimer);
//_MyTimer.ActiveProcess = "DeleteItemInfoAndChildren";
item.DeleteItemInfoAndChildren(" ");
}
//Console.WriteLine("\r\n'Item Deleted'");
}
//_MyTimer.ShowElapsedTimes("DeleteItemInfoAndChildren");
}
private void DeleteItemInfoAndChildren(string depth)
{
if (MyContent.LocalContentParts != null)
{
foreach (PartInfo part in MyContent.LocalContentParts)
{
if (part._MyItems != null)
{
int whereami = 1;
try
{
List<ItemInfo> items = new List<ItemInfo>();
foreach (ItemInfo item in part._MyItems)
{
items.Add(item);
}
foreach (ItemInfo item in items)
{
string key = item.ItemID.ToString();
if (_CacheByPrimaryKey.ContainsKey(key))
{
ItemInfo[] itemz = _CacheByPrimaryKey[key].ToArray();
foreach (ItemInfo itm in itemz)
{
//Console.WriteLine("'{0}cache ',{1},{2},{3},{4}", depth + depth, this.ItemID, this._MyItemInfoUnique, itm.ItemID, itm._MyItemInfoUnique);
//whereami = 2;
itm.OnDeleted(itm);
//whereami = 4;
itm.DeleteItemInfoAndChildren(depth + " ");
//whereami = 3;
}
}
else
{
//Console.WriteLine("'{0}noncache',{1},{2},{3},{4}", depth + depth, this.ItemID, this._MyItemInfoUnique, item.ItemID, item._MyItemInfoUnique);
//whereami = 5;
item.OnDeleted(item);
//whereami = 7;
item.DeleteItemInfoAndChildren(depth + " ");
//whereami = 6;
}
}
}
catch (Exception ex)
{
Console.WriteLine("where = {0}, type = {1}, msg = {2}", whereami, ex.GetType().Name, ex.Message);
}
}
}
}
Dispose();
}
public static void RefreshPrevious(int? itemID, int? previousID)
{
if (itemID == null) return;
string key = itemID.ToString();
ConvertListToDictionary();
if (_CacheByPrimaryKey.ContainsKey(key))
foreach (ItemInfo tmpInfo in _CacheByPrimaryKey[key])
tmpInfo.RefreshPrevious(previousID);
}
private void RefreshPrevious(int? previousID)
{
if (_PreviousID != previousID)
{
if (MyPrevious != null) MyPrevious.RefreshNextItems(); // Update List for old value
_PreviousID = 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
}
}
public partial class Item
{
#region DeleteItemAndChildren
public static void DeleteItemAndChildren(ItemInfo item)
{
//Volian.Base.Library.VlnTimer _MyTimer = new Volian.Base.Library.VlnTimer();
if (!CanDeleteObject())
throw new System.Security.SecurityException("User not authorized to remove a Item");
try
{
ItemInfo nextItem = item.NextItem;
ItemInfo prevItem = item.MyPrevious;
DocVersionInfo docVersion = item.ItemDocVersionCount == 1 ? item.ItemDocVersions[0] : null;
item.OnBeforeDelete();
//_MyTimer.ActiveProcess = "DataPortal.Delete";
DataPortal.Delete(new DeleteCriteria(item.ItemID, Volian.Base.Library.VlnSettings.UserID));
if (nextItem != null) // Adjust PreviousID for NextItem
{
if (docVersion != null)
using (DocVersion dv = docVersion.Get())
DocVersionInfo.Refresh(dv);
//_MyTimer.ActiveProcess = "RefreshPrevious";
ItemInfo.RefreshPrevious(nextItem.ItemID, item.PreviousID);
// The order of the next two methods was required to fix a null reference
// when getting myparent. This bug was found when deleting a node from the
// tree when the RTBItem was not open (i.e. in the step editor window).
//_MyTimer.ActiveProcess = "RefreshItemParts";
nextItem.RefreshItemParts();
//nextItem.ResetOrdinal(); - UpdateTransitionText calls ResetOrdinal
//_MyTimer.ActiveProcess = "UpdateTransitionText";
nextItem.UpdateTransitionText();
}
else if (prevItem != null)
{
//_MyTimer.ActiveProcess = "RefreshNextItems";
prevItem.RefreshNextItems();
//_MyTimer.ActiveProcess = "ResetOrdinal";
if (prevItem.IsCaution || prevItem.IsNote) prevItem.ResetOrdinal();
//_MyTimer.ActiveProcess = "UpdateTransitionText";
prevItem.UpdateTransitionText();
}
//_MyTimer.ActiveProcess = "DeleteItemInfoAndChildren";
ItemInfo.DeleteItemInfoAndChildren(item.ItemID); // Dispose ItemInfo and Children
}
catch (Exception ex)
{
Console.WriteLine("ItemInsertExt: Stacktrace = {0}", ex.StackTrace);
System.Data.SqlClient.SqlException exSQL = SqlException(ex);
if (exSQL != null && exSQL.Message.Contains("###Cannot Delete Item###"))
//return false;
throw exSQL;
else
throw new DbCslaException("Error on Item.DeleteItemAndChildren", ex);
}
//_MyTimer.ShowElapsedTimes("DeleteItemAndChildren");
}
private static System.Data.SqlClient.SqlException SqlException(Exception ex)
{
Type sqlExType = typeof(System.Data.SqlClient.SqlException);
while (ex != null)
{
if (ex.GetType() == sqlExType) return ex as System.Data.SqlClient.SqlException;
ex = ex.InnerException;
}
return null;
}
[Serializable()]
protected class DeleteCriteria
{
private int _ItemID;
public int ItemID
{ get { return _ItemID; } }
private string _UserID;
public string UserID
{
get { return _UserID; }
set { _UserID = value; }
}
public DeleteCriteria(int itemID,String userID)
{
_ItemID = itemID;
_UserID = userID;
}
}
[Transactional(TransactionalTypes.TransactionScope)]
private void DataPortal_Delete(DeleteCriteria criteria)
{
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Item.DataPortal_Delete", GetHashCode());
try
{
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
{
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "DeleteItemAndChildren";
cm.Parameters.AddWithValue("@ItemID", criteria.ItemID);
cm.Parameters.AddWithValue("@UserID", criteria.UserID);
cm.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
if (_MyLog.IsErrorEnabled) _MyLog.Error("Item.DataPortal_Delete", ex);
_ErrorMessage = ex.Message;
throw new DbCslaException("Item.DataPortal_Delete", ex);
}
}
#endregion
#region PasteReplace
public static ItemInfo PasteReplace(ItemInfo itemInfo, int copyStartID)
{
if (!CanDeleteObject())
throw new System.Security.SecurityException("User not authorized to remove a Item");
ItemInfo newItemInfo = CopyPasteReplaceItemInfoFetch(copyStartID, itemInfo); //itemInfo.ItemID, itemInfo.MyContent.Type, itemInfo.MyContent.Type);
// Delete business objects, including remove from tree
ItemInfo.DeleteItemInfoAndChildren(itemInfo.ItemID); // Dispose ItemInfo and Children
using (Item item = Get(newItemInfo.ItemID)) ItemInfo.Refresh(item);
if (newItemInfo.NextItem != null) using (Item item = newItemInfo.NextItem.Get()) ItemInfo.Refresh(item);
newItemInfo.RefreshNextItems();
// if inserting after a caution or note, refreshes tabs. This will adjust bullets
// of any previous cautions or notes.
if (newItemInfo.IsCaution || newItemInfo.IsNote) newItemInfo.ResetOrdinal();
newItemInfo.UpdateTransitionText();
newItemInfo.UpdatePastedStepTransitionText();
// Add to tree
if (newItemInfo.NextItemCount > 0)
{
using (ItemInfo itm = ItemInfo.Get(newItemInfo.NextItem.ItemID))
{
itm.OnNewSiblingBefore(new ItemInfoInsertEventArgs(newItemInfo, ItemInfo.EAddpingPart.Before));
}
}
else if (newItemInfo.PreviousID != null)
{
using (ItemInfo itm2 = ItemInfo.Get((int)newItemInfo.PreviousID))
{
itm2.OnNewSiblingAfter(new ItemInfoInsertEventArgs(newItemInfo, ItemInfo.EAddpingPart.After));
}
}
else
{
newItemInfo.MyParent.OnNewChild(new ItemInfoInsertEventArgs(newItemInfo, ItemInfo.EAddpingPart.Child));
}
return newItemInfo;
}
private static ItemInfo CopyPasteReplaceItemInfoFetch(int copyStartID, ItemInfo itemInfo) // int itemID, int? type, int? fromType)
{
ItemInfo tmp = null;
if (itemInfo.GetType() == typeof(ProcedureInfo))
tmp = DataPortal.Fetch<ProcedureInfo>(new ItemInfo.PastingPartCriteria(copyStartID, itemInfo.ItemID, ItemInfo.EAddpingPart.Replace, itemInfo.MyContent.Type, itemInfo.MyContent.Type, DateTime.Now, Volian.Base.Library.VlnSettings.UserID));
else if (itemInfo.GetType() == typeof(SectionInfo))
tmp = DataPortal.Fetch<SectionInfo>(new ItemInfo.PastingPartCriteria(copyStartID, itemInfo.ItemID, ItemInfo.EAddpingPart.Replace, itemInfo.MyContent.Type, itemInfo.MyContent.Type, DateTime.Now, Volian.Base.Library.VlnSettings.UserID));
else
tmp = DataPortal.Fetch<StepInfo>(new ItemInfo.PastingPartCriteria(copyStartID, itemInfo.ItemID, ItemInfo.EAddpingPart.Replace, itemInfo.MyContent.Type, itemInfo.MyContent.Type, DateTime.Now, Volian.Base.Library.VlnSettings.UserID));
return tmp;
}
#endregion
}
}