/////////////////////////////////////////////////////////// // xContactPhones.cs // Implementation of Class xContactPhones // CSLA type: Editable Grandchild collection // Created on: 07-Jun-2004 8:41:18 AM // Object design: Joyce // Coded: John 27-July-2004 /////////////////////////////////////////////////////////// using System; using System.Data; using CSLA.Data; using CSLA; using GZTW.Data; namespace GZTW.AyaNova.BLL { /// /// DEPRECATED v4.0 /// [Serializable] public class xContactPhones : BusinessCollectionBase { #region Constructor //Private constructor prevents direction instantiation private xContactPhones() { //Child MarkAsChild(); AllowSort=false; AllowFind=true; AllowEdit=true; AllowNew=true; AllowRemove=true; } #endregion #region Business properties and methods /// /// Retrieve xContactPhone by index /// /// Index public xContactPhone this[int Item] { get { return (xContactPhone) List[Item]; } } /// /// Retrieve xContactPhone by string containing Guid value /// public xContactPhone this[string ContactPhoneID] { get { System.Guid sid = new System.Guid(ContactPhoneID); foreach (xContactPhone child in List) { if(child.ID==sid) return child; } return null; } } /// /// Retrieve xContactPhone by Guid value /// public xContactPhone this[Guid ContactPhoneGUID] { get { foreach (xContactPhone child in List) { if(child.ID==ContactPhoneGUID) return child; } return null; } } /// /// Retrieve xContactPhone by type /// public xContactPhone this[ContactPhoneTypes ptype] { get { //First see if the default is a match, if so return it //this way the most important one of type is guaranteed to be the returned one xContactPhone cp=GetDefaultPhone(); if (cp != null && cp.ContactPhoneType == ptype) return cp; foreach (xContactPhone child in List) { if (child.ContactPhoneType == ptype) return child; } return null; } } /// /// Remove xContactPhone by passing it in /// /// public void Remove(xContactPhone obj) { List.Remove(obj); EnsureThereIsADefaultPhone(); } /// /// Remove xContactPhone by string of id value /// /// public void Remove(string ContactPhoneID) { System.Guid sid = new System.Guid(ContactPhoneID); Remove(sid); } /// /// Remove by Guid value of ID /// /// public void Remove(Guid ContactPhoneID) { xContactPhone delete = null; foreach (xContactPhone child in List) { if (child.ID == ContactPhoneID) { delete = child; break; } } if (delete != null) Remove(delete); } /// /// Add a new xContactPhone to the collection /// /// internal xContactPhone Add(xContact obj) { xContactPhone child=xContactPhone.NewItem(obj); List.Add(child); EnsureThereIsADefaultPhone(); return child; } /// /// Sets passed in phone id default to true /// all others to false /// /// public void SetPhoneDefault(Guid ContactPhoneID) { // Guid cid = new Guid(ContactID); foreach(xContactPhone child in List) { //Only change them if necessary otherwise if(child.ID==ContactPhoneID) child.internalPhoneDefault = true; else child.internalPhoneDefault = false; } } /// /// Iterates through collection and gets the default phone record /// public xContactPhone GetDefaultPhone() { foreach(xContactPhone child in List) { if(child.PhoneDefault) return child; } return null; } /// /// Iterates through collection and gets the fax number or an empty string if none found /// public string GetFax() { foreach(xContactPhone child in List) { if(child.ContactPhoneType==ContactPhoneTypes.Fax) return child.NumberOnlyFullPhoneRecord; } return ""; } /// /// Iterates through collection and gets the phone number that is not a /// default number or a fax number if none found then an empty string is returned /// public string GetAltPhone() { foreach(xContactPhone child in List) { if(!child.PhoneDefault && child.ContactPhoneType!=ContactPhoneTypes.Fax) return child.NumberOnlyFullPhoneRecord; } return ""; } /// /// Ensures one contact is set to default /// private void EnsureThereIsADefaultPhone() { if(List.Count==0) return; if(List.Count==1) { ((xContactPhone)List[0]).internalPhoneDefault = true; return; } foreach (xContactPhone child in List) { if(child.PhoneDefault==true) return; } //set phone 0 to default since nothing else //took care of it ((xContactPhone)List[0]).internalPhoneDefault = true; } #endregion #region Contains /// /// Check if item in collection /// /// public bool Contains(xContactPhone obj) { foreach (xContactPhone 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 ContactPhoneID) { System.Guid sid = new System.Guid(ContactPhoneID); foreach (xContactPhone child in List) { if(child.ID==sid) return true; } return false; } /// /// Check if item in deleted collection /// /// public bool ContainsDeleted(xContactPhone obj) { foreach (xContactPhone 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 ContactPhoneID) { System.Guid sid = new System.Guid(ContactPhoneID); foreach (xContactPhone child in deletedList) { if(child.ID==sid) return true; } return false; } #endregion #region Static methods /// /// NewItems /// /// internal static xContactPhones NewItems() { return new xContactPhones(); } /// /// Get items - Grandchild style /// /// /// internal static xContactPhones GetItems(xContact obj) { xContactPhones col = new xContactPhones(); col.Fetch(obj); return col; } #endregion #region DAL DATA ACCESS /// /// Fetch children - grandchild style /// /// private void Fetch(xContact obj) { SafeDataReader dr = null; try { dr=DBUtil.GetReaderFromSQLString("SELECT aContactPhone.* " + "FROM aContactPhone WHERE aContactID=@ID",obj.ID); while(dr.Read()) { List.Add(xContactPhone.GetItem(dr,obj.RootObjectType)); } } finally { if(dr!=null) dr.Close(); } } /// /// Update children /// /// /// internal void Update(xContact obj,IDbTransaction tr) { //update (thus deleting) any deleted child objects foreach (xContactPhone child in deletedList) { child.Update(obj,tr); } //Now that they are deleted remove them from memory deletedList.Clear(); //Changed: 20-June-2006 - Added this line to cover //scenario where in a new record with multiple contactphones //the default is deleted just before saving. EnsureThereIsADefaultPhone(); foreach (xContactPhone child in List) { child.Update(obj,tr); } } #region Shared delete method /// /// Given a xContact ID deletes all items for that ID /// /// Called by xContact delete /// /// ID of parent xContact item /// Database transaction from ascendant item internal static void DeleteItems(Guid ContactID,IDbTransaction transaction) { System.Collections.ArrayList al=new System.Collections.ArrayList(); SafeDataReader dr = null; try { dr=DBUtil.GetReaderFromSQLString("SELECT aID " + "FROM aContactPhone " + "WHERE aContactID=@ID",ContactID,transaction); while(dr.Read()) { al.Add(dr.GetGuid("aID")); } dr.Close(); foreach(object o in al) DBUtil.RemoveKeywords(transaction, RootObjectTypes.ContactPhone,(Guid)o); } finally { if(dr!=null) dr.Close(); } //Delete objects themselves DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM aContactPhone WHERE aContactID=@ID;"); cmDelete.AddInParameter("@ID",DbType.Guid,ContactID); DBUtil.DB.ExecuteNonQuery(cmDelete, transaction); } #endregion #endregion }//end xContactPhones }//end namespace GZTW.AyaNova.BLL