/////////////////////////////////////////////////////////// // WorkorderItems.cs // Implementation of Class WorkorderItems // CSLA type: Editable child collection // Created on: 07-Jun-2004 8:41:50 AM // Object design: Joyce // Coded: John 28-July-2004 /////////////////////////////////////////////////////////// using System; using CSLA.Data; using CSLA; using System.Data; //using GZTW.Data; namespace GZTW.AyaNova.BLL { /// /// Editable child collection of objects residing in parent object /// [Serializable] public class WorkorderItems : BusinessCollectionBase { #region Constructor //Private constructor prevents direction instantiation private WorkorderItems() { //Child MarkAsChild(); AllowSort=false; AllowFind=true; AllowEdit=true; AllowNew=true; AllowRemove=true; } #endregion #region Business properties and methods /// /// Retrieve WorkorderItem by index /// /// Index public WorkorderItem this[int Item] { get { return (WorkorderItem) List[Item]; } } /// /// Retrieve WorkorderItem by string containing Guid value /// public WorkorderItem this[string WorkorderItemID] { get { if (string.IsNullOrEmpty(WorkorderItemID)) return null; System.Guid sid = new System.Guid(WorkorderItemID); foreach (WorkorderItem child in List) { if(child.ID==sid) return child; } return null; } } /// /// Retrieve WorkorderItem by Guid value /// public WorkorderItem this[Guid WorkorderItemID] { get { foreach (WorkorderItem child in List) { if(child.ID==WorkorderItemID) return child; } return null; } } /// /// Called by any one of the delete methods /// if there is only one left then it throws /// and exception (UI should ensure this never happens) /// public void EnsureAtLeastOneWorkorderItem() { if(List.Count<2) { throw new Exception(LocalizedTextTable.GetLocalizedTextDirect("Workorder.Label.Error.DeleteLastWorkorderItem")); } } /// /// Remove WorkorderItem by passing it in /// Will throw an exception if attempt to delete /// the last workorder item. A workorder must have at /// last one workorder item and the UI should ensure the /// user can not even attempt to delete it. /// /// public void Remove(WorkorderItem obj) { EnsureAtLeastOneWorkorderItem(); List.Remove(obj); } /// /// Remove WorkorderItem by string of id value /// Will throw an exception if attempt to delete /// the last workorder item. A workorder must have at /// last one workorder item and the UI should ensure the /// user can not even attempt to delete it. /// /// public void Remove(string WorkorderItemID) { System.Guid sid = new System.Guid(WorkorderItemID); Remove(sid); } /// /// Remove by Guid value of ID /// Will throw an exception if attempt to delete /// the last workorder item. A workorder must have at /// last one workorder item and the UI should ensure the /// user can not even attempt to delete it. /// /// public void Remove(Guid WorkorderItemID) { EnsureAtLeastOneWorkorderItem(); WorkorderItem widelete = null; foreach (WorkorderItem child in List) { if (child.ID == WorkorderItemID) { widelete = child; break; } } if (widelete != null) Remove(widelete); } /// /// Add a new WorkorderItem to the collection /// /// public WorkorderItem Add(Workorder obj) { WorkorderItem child=WorkorderItem.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 (WorkorderItem child in List) { if(RO==false) RO=AyaBizUtils.Right("Object.WorkorderItem")<(int)SecurityLevelTypes.ReadWrite; child.SetReadOnly(RO); } } #endregion #region Contains /// /// Check if item in collection /// /// public bool Contains(WorkorderItem obj) { foreach (WorkorderItem 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 WorkorderItemID) { System.Guid sid = new System.Guid(WorkorderItemID); foreach (WorkorderItem child in List) { if(child.ID==sid) return true; } return false; } /// /// Check if item in collection by ID value /// /// public bool Contains(Guid WorkorderItemID) { foreach (WorkorderItem child in List) { if(child.ID==WorkorderItemID) return true; } return false; } /// /// Check if item in deleted collection /// /// public bool ContainsDeleted(WorkorderItem obj) { foreach (WorkorderItem 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 WorkorderItemID) { System.Guid sid = new System.Guid(WorkorderItemID); foreach (WorkorderItem child in deletedList) { if(child.ID==sid) return true; } return false; } #endregion #region Static methods /// /// NewItems /// /// internal static WorkorderItems NewItems() { return new WorkorderItems(); } /// /// GetItems /// internal static WorkorderItems GetItems(SafeDataReader dr, Workorder obj) { WorkorderItems col = new WorkorderItems(); col.Fetch(dr,obj.WorkorderType, obj); return col; } #endregion #region DAL DATA ACCESS /// /// Fetch children /// private void Fetch(SafeDataReader dr, WorkorderTypes type, Workorder obj) { while(dr.Read()) { List.Add(WorkorderItem.GetItem(dr, type, obj)); } } /// /// Update children /// /// /// internal void Update(Workorder obj,IDbTransaction tr) { //update (thus deleting) any deleted child objects foreach (WorkorderItem child in deletedList) { if(!child.mMoved) child.Update(obj,tr); } //Now that they are deleted remove them from memory deletedList.Clear(); foreach (WorkorderItem child in List) { child.Update(obj,tr); } } #region Shared delete method /// /// Given a workorder ID /// /// retrieves all workorder item id values for that /// workorder, then calls the child and grandchild Shared static direct /// delete methods to clear out those collections, then finally /// removes all workorder items of that workorder ID /// /// Called by Workorder delete /// /// ID of parent workorder item /// Database transaction from ascendant item internal static void DeleteItems(Guid WorkorderID,IDbTransaction transaction) { //This one is more tricky, here we need to iterate through all the matching workorderitem records //and call the workorderitem static delete method SafeDataReader dr = null; System.Collections.ArrayList al = new System.Collections.ArrayList(); try { dr=DBUtil.GetReaderFromSQLString("SELECT aID " + "FROM aWorkorderItem " + "WHERE aWorkorderID=@ID",WorkorderID,transaction); while(dr.Read()) { al.Add(dr.GetGuid("aID")); } dr.Close(); foreach(object o in al) WorkorderItem.DeleteItems((Guid)o,transaction); } finally { if(dr!=null) dr.Close(); } } #endregion #endregion }//end WorkorderItems }//end namespace GZTW.AyaNova.BLL