/////////////////////////////////////////////////////////// // WorkorderItemParts.cs // Implementation of Class WorkorderItemParts // CSLA type: Editable Grandchild collection // Created on: 07-Jun-2004 8:41:49 AM // Object design: Joyce // Coded: John 28-July-2004 /////////////////////////////////////////////////////////// using System; using GZTW.Data; using System.Data; using CSLA.Data; using CSLA; namespace GZTW.AyaNova.BLL { /// /// Editable Grandchild collection of objects residing in parent object /// [Serializable] public class WorkorderItemParts : BusinessCollectionBase { //Type of workorder //used to flag whether to track serial numbers //etc //internal WorkorderTypes mWorkorderType; #region Constructor //Private constructor prevents direction instantiation private WorkorderItemParts() { //Child MarkAsChild(); AllowSort=false; AllowFind=true; AllowEdit=true; AllowNew=true; AllowRemove=true; } #endregion #region Business properties and methods /// /// Retrieve WorkorderItemPart by index /// /// Index public WorkorderItemPart this[int Item] { get { return (WorkorderItemPart) List[Item]; } } /// /// Retrieve WorkorderItemPart by string containing Guid value /// public WorkorderItemPart this[string WorkorderItemPartID] { get { System.Guid sid = new System.Guid(WorkorderItemPartID); foreach (WorkorderItemPart child in List) { if(child.ID==sid) return child; } return null; } } /// /// Retrieve WorkorderItemPart by Guid value /// public WorkorderItemPart this[Guid WorkorderItemPartID] { get { foreach (WorkorderItemPart child in List) { if(child.ID==WorkorderItemPartID) return child; } return null; } } /// /// Remove WorkorderItemPart by passing it in /// /// public void Remove(WorkorderItemPart obj) { List.Remove(obj); } /// /// Remove WorkorderItemPart by string of id value /// /// public void Remove(string WorkorderItemPartID) { System.Guid sid = new System.Guid(WorkorderItemPartID); Remove(sid); } /// /// Remove by Guid value of ID /// /// public void Remove(Guid WorkorderItemPartID) { WorkorderItemPart delete = null; foreach (WorkorderItemPart child in List) { if (child.ID == WorkorderItemPartID) { delete = child; break; } } if (delete != null) Remove(delete); } /// /// Add a new WorkorderItemPart to the collection /// /// public WorkorderItemPart Add(WorkorderItem obj) { WorkorderItemPart child=WorkorderItemPart.NewItem(obj); List.Add(child); return child; } /// /// Sets all items to read / write status /// used by workorder object when it's closed or service completed is true /// /// true=Set to read only explicitly, false=set to whatever rights allowed public void SetReadOnly(bool RO) { foreach (WorkorderItemPart child in List) { if(RO==false) RO=AyaBizUtils.Right("Object.WorkorderItemPart")<(int)SecurityLevelTypes.ReadWrite; child.SetReadOnly(RO); } } #endregion #region Contains /// /// Check if item in collection /// /// public bool Contains(WorkorderItemPart obj) { foreach (WorkorderItemPart child in List) { if(child.Equals(obj)) return true; } return false; } /// /// Check if item in collection by string of ID value /// /// public bool Contains(string WorkorderItemPartID) { System.Guid sid = new System.Guid(WorkorderItemPartID); foreach (WorkorderItemPart child in List) { if(child.ID==sid) return true; } return false; } /// /// Check if item in deleted collection /// /// public bool ContainsDeleted(WorkorderItemPart obj) { foreach (WorkorderItemPart child in deletedList) { if(child.Equals(obj)) return true; } return false; } /// /// Check if item in deleted collection by string of ID value /// /// public bool ContainsDeleted(string WorkorderItemPartID) { System.Guid sid = new System.Guid(WorkorderItemPartID); foreach (WorkorderItemPart child in deletedList) { if(child.ID==sid) return true; } return false; } #endregion #region Static methods /// /// NewItems /// /// internal static WorkorderItemParts NewItems() { return new WorkorderItemParts(); } /// /// Get items - Grandchild style /// /// /// internal static WorkorderItemParts GetItems(WorkorderItem obj) { WorkorderItemParts col = new WorkorderItemParts(); //case 1317 if (AyaBizUtils.Right(RootObjectTypes.WorkorderItemPart) > (int)SecurityLevelTypes.NoAccess) col.Fetch(obj); return col; } #endregion #region DAL DATA ACCESS /// /// Fetch children - grandchild style /// /// private void Fetch(WorkorderItem obj) { //Load data Grandchild style SafeDataReader dr = null; try { dr=DBUtil.GetReaderFromSQLString("SELECT * " + "FROM aWorkorderItemPart " + "WHERE aWorkorderItemID=@ID",obj.ID); while(dr.Read()) { List.Add(WorkorderItemPart.GetItem(dr, obj.mWorkorderType, obj)); } } finally { if(dr!=null) dr.Close(); } } /// /// Update children /// /// /// internal void Update(WorkorderItem obj,IDbTransaction tr) { //update (thus deleting) any deleted child objects foreach (WorkorderItemPart child in deletedList) { child.Update(obj,tr); } //Now that they are deleted remove them from memory deletedList.Clear(); foreach (WorkorderItemPart child in List) { child.Update(obj,tr); } } #region Shared delete method internal struct pinfo { public Guid id; public bool hasaffected; } /// /// Given a workorder item ID deletes all items for that ID /// /// Called by workorderitems delete /// /// ID of parent workorder item /// Database transaction from ascendant item internal static void DeleteItems(Guid WorkorderItemID,IDbTransaction transaction) { //This one is more tricky, here we need to iterate through all the matching part records //and call the workorderitempart static delete method to ensure inventory get's updated properly SafeDataReader dr = null; System.Collections.ArrayList al = new System.Collections.ArrayList(); try { dr=DBUtil.GetReaderFromSQLString("SELECT aID, aHasAffectedInventory " + "FROM aWorkorderItemPart " + "WHERE aWorkorderItemID=@ID",WorkorderItemID,transaction); while(dr.Read()) { pinfo pp=new pinfo(); pp.id=dr.GetGuid("aID"); pp.hasaffected=dr.GetBoolean("aHasAffectedInventory"); al.Add(pp); } dr.Close(); foreach(object o in al) WorkorderItemPart.DeleteItem(((pinfo)o).id,((pinfo)o).hasaffected,transaction); } finally { if(dr!=null) dr.Close(); } } #endregion #endregion }//end WorkorderItemParts }//end namespace GZTW.AyaNova.BLL