/////////////////////////////////////////////////////////// // WorkorderItemLoans.cs // Implementation of Class WorkorderItemLoans // CSLA type: Editable Grandchild collection // Created on: 12-Nov-2005 // Object design: Joyce/John // Coded: John 12-Nov-2005 /////////////////////////////////////////////////////////// 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 WorkorderItemLoans : BusinessCollectionBase { #region Constructor //Private constructor prevents direction instantiation private WorkorderItemLoans() { //Child MarkAsChild(); AllowSort=false; AllowFind=true; AllowEdit=true; AllowNew=true; AllowRemove=true; } #endregion #region Business properties and methods /// /// Retrieve WorkorderItemLoan by index /// /// Index public WorkorderItemLoan this[int Item] { get { return (WorkorderItemLoan) List[Item]; } } /// /// Retrieve WorkorderItemLoan by string containing Guid value /// public WorkorderItemLoan this[string WorkorderItemLoanID] { get { System.Guid sid = new System.Guid(WorkorderItemLoanID); foreach (WorkorderItemLoan child in List) { if(child.ID==sid) return child; } return null; } } //case 1975 /// /// Retrieve WorkorderItemLoan by Guid value /// public WorkorderItemLoan this[Guid WorkorderItemLoanID] { get { foreach (WorkorderItemLoan child in List) { if (child.ID == WorkorderItemLoanID) return child; } return null; } } /// /// Remove WorkorderItemLoan by passing it in /// /// public void Remove(WorkorderItemLoan obj) { List.Remove(obj); } /// /// Remove WorkorderItemLoan by string of id value /// /// public void Remove(string WorkorderItemLoanID) { System.Guid sid = new System.Guid(WorkorderItemLoanID); Remove(sid); } /// /// Remove by Guid value of ID /// /// public void Remove(Guid WorkorderItemLoanID) { WorkorderItemLoan delete = null; foreach (WorkorderItemLoan child in List) { if (child.ID == WorkorderItemLoanID) { delete = child; break; } } if (delete != null) Remove(delete); } /// /// Add a new WorkorderItemLoan to the collection /// /// public WorkorderItemLoan Add(WorkorderItem obj) { WorkorderItemLoan child=WorkorderItemLoan.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 (WorkorderItemLoan child in List) { if(RO==false) RO=AyaBizUtils.Right("Object.WorkorderItemLoan")<(int)SecurityLevelTypes.ReadWrite; child.SetReadOnly(RO); } } #endregion #region Contains /// /// Check if item in collection /// /// public bool Contains(WorkorderItemLoan obj) { foreach (WorkorderItemLoan 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 WorkorderItemLoanID) { System.Guid sid = new System.Guid(WorkorderItemLoanID); foreach (WorkorderItemLoan child in List) { if(child.ID==sid) return true; } return false; } /// /// Check if item in deleted collection /// /// public bool ContainsDeleted(WorkorderItemLoan obj) { foreach (WorkorderItemLoan 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 WorkorderItemLoanID) { System.Guid sid = new System.Guid(WorkorderItemLoanID); foreach (WorkorderItemLoan child in deletedList) { if(child.ID==sid) return true; } return false; } #endregion #region Static methods /// /// NewItems /// /// internal static WorkorderItemLoans NewItems() { return new WorkorderItemLoans(); } /// /// Get items - Grandchild style /// /// /// internal static WorkorderItemLoans GetItems(WorkorderItem obj) { WorkorderItemLoans col = new WorkorderItemLoans(); //case 1317 if (AyaBizUtils.Right(RootObjectTypes.WorkorderItemLoan) > (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 aWorkorderItemLoan " + "WHERE aWorkorderItemID=@ID",obj.ID); while(dr.Read()) { List.Add(WorkorderItemLoan.GetItem(dr,obj)); } } finally { if(dr!=null) dr.Close(); } } /// /// Update children /// /// /// internal void Update(WorkorderItem obj,IDbTransaction tr) { //update (thus deleting) any deleted child objects foreach (WorkorderItemLoan child in deletedList) { child.Update(obj,tr); } //Now that they are deleted remove them from memory deletedList.Clear(); foreach (WorkorderItemLoan child in List) { child.Update(obj,tr); } } #region Shared delete method /// /// 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) { //Delete all items indexed keywords *and* reset LoanItem currentworkorderItemLoan for any //workorderitemloan under indicated workorderitem SafeDataReader dr = null; System.Collections.ArrayList al= new System.Collections.ArrayList(); try { dr=DBUtil.GetReaderFromSQLString("SELECT aID " + "FROM aWorkorderItemLoan " + "WHERE aWorkorderItemID=@ID",WorkorderItemID,transaction); while(dr.Read()) { al.Add(dr.GetGuid("aID")); } dr.Close(); foreach(object o in al) { DBUtil.RemoveKeywords(transaction,RootObjectTypes.WorkorderItemLoan,(Guid)o); //Un-Flag any loan item with aCurrentWorkorderItemLoan = this id DBCommandWrapper cmLoanItemUpdate = DBUtil.GetCommandFromSQL( "UPDATE aLoanItem SET aCurrentWorkorderItemLoan=@GUIDEMPTY " + "WHERE aCurrentWorkorderItemLoan=@CurrentWOILoanID" ); cmLoanItemUpdate.AddInParameter("@CurrentWOILoanID",DbType.Guid, (Guid)o); cmLoanItemUpdate.AddInParameter("@GUIDEMPTY",DbType.Guid, Guid.Empty); DBUtil.DB.ExecuteNonQuery(cmLoanItemUpdate, transaction); } } finally { if(dr!=null) dr.Close(); } // DBCommandWrapper cmDeleteKeywords = DBUtil.GetCommandFromSQL( // "DELETE aSearchKey FROM aSearchKey INNER JOIN aWorkorderItemLoan " + // "ON aSearchKey.aSourceObjectID = aWorkorderItemLoan.aID WHERE " + // "(aWorkorderItemLoan.aWorkorderItemID = @ID);"); // cmDeleteKeywords.AddInParameter("@ID",DbType.Guid,WorkorderItemID); // DBUtil.DB.ExecuteNonQuery(cmDeleteKeywords,transaction); DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM aWorkorderItemLoan WHERE aWorkorderItemID=@ID;"); cmDelete.AddInParameter("@ID",DbType.Guid,WorkorderItemID); DBUtil.DB.ExecuteNonQuery(cmDelete, transaction); } #endregion #endregion }//end WorkorderItemLoans }//end namespace GZTW.AyaNova.BLL