/////////////////////////////////////////////////////////// // xContacts.cs // Implementation of Class xContacts // CSLA type: Editable child collection // Created on: 07-Jun-2004 8:41:18 AM // Object design: Joyce // Coded: John 29-July-2004 /////////////////////////////////////////////////////////// using System; using System.Data; using CSLA.Data; using CSLA; using System.Text; using System.Collections; namespace GZTW.AyaNova.BLL { /// /// DEPRECATED V4.0 /// [Serializable] public class xContacts : BusinessCollectionBase { #region Constructor //Private constructor prevents direction instantiation private xContacts() { //Child MarkAsChild(); AllowSort=false; AllowFind=true; AllowEdit=true; AllowNew=true; AllowRemove=true; } #endregion #region Business properties and methods /// /// Retrieve xContact by index /// /// Index internal xContact this[int Item] { get { return (xContact) List[Item]; } } /// /// Retrieve xContact by string containing Guid value /// internal xContact this[string ContactID] { get { System.Guid sid = new System.Guid(ContactID); foreach (xContact child in List) { if(child.ID==sid) return child; } return null; } } /// /// Retrieve xContact by Guid value /// internal xContact this[Guid ContactGUID] { get { foreach (xContact child in List) { if(child.ID==ContactGUID) return child; } return null; } } /// /// Remove xContact by passing it in /// /// internal void Remove(xContact obj) { List.Remove(obj); EnsureThereIsADefaultContact(); } /// /// Remove xContact by string of id value /// /// public void Remove(string ContactID) { System.Guid sid = new System.Guid(ContactID); Remove(sid); } /// /// Remove by Guid value of ID /// /// public void Remove(Guid ContactID) { xContact delete = null; foreach (xContact child in List) { if (child.ID == ContactID) { delete = child; break; } } if (delete != null) Remove(delete); } /// /// Add new /// /// /// /// internal xContact Add(RootObjectTypes RootObjectType, Guid RootObjectID) { xContact child=xContact.NewItem( RootObjectType, RootObjectID); List.Add(child); EnsureThereIsADefaultContact(); return child; } /// /// Ensures one contact is set to default /// private void EnsureThereIsADefaultContact() { if(List.Count==0) return; if(List.Count==1) { ((xContact)List[0]).internalPrimaryContact = true; return; } foreach (xContact child in List) { if(child.PrimaryContact==true) return; } //set contact 0 to primary since nothing else //took care of it ((xContact)List[0]).internalPrimaryContact = true; } /// /// Checks if grandchildren are dirty /// /// public bool GrandChildIsDirty() { foreach (xContact child in List) { if(child.Phones.IsDirty==true) return true; } return false; } /// /// Checks if grandchildren are valid /// /// public bool GrandChildIsValid() { foreach (xContact child in List) { if(child.Phones.IsValid==false) return false; } return true; } /// /// Iterates through collection and sets the default contact id /// to true for to contact id passed in /// /// Sets any other contacts if they are primarycontact=true to false /// /// public void SetPrimaryContact(Guid ContactID) { foreach(xContact child in List) { //Only change them if necessary otherwise if(child.ID==ContactID) child.internalPrimaryContact = true; else child.internalPrimaryContact = false; } } /// /// Iterates through collection and gets the default contact /// internal xContact GetPrimaryContact() { foreach(xContact child in List) { if(child.PrimaryContact) return child; } return null; } /// /// Returns a string containing the primary contact's /// default contact info /// (i.e. Email address and all phones collection) /// public string GetPrimaryContactDefaultContactInfo() { xContact c = GetPrimaryContact(); if(c==null) return ""; StringBuilder b = new StringBuilder(); //List all phone records, ensuring that default //phone record is at top of list followed by all others xContactPhone cpDefault=c.Phones.GetDefaultPhone(); if(cpDefault!=null) { b.Append(AyaBizUtils.SS("",cpDefault.FullPhoneRecord,"\r\n")); foreach(xContactPhone cp in c.Phones) { if(cp!=cpDefault) { b.Append(AyaBizUtils.SS("",cp.FullPhoneRecord,"\r\n")); } } } b.Append(AyaBizUtils.SS(LocalizedTextTable.GetLocalizedTextDirect("Contact.Label.EmailAddress")+": ",c.EmailAddress,"\r\n")); return b.ToString(); } #endregion #region Contains /// /// Check if item in collection /// /// internal bool Contains(xContact obj) { foreach (xContact 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 ContactID) { System.Guid sid = new System.Guid(ContactID); foreach (xContact child in List) { if(child.ID==sid) return true; } return false; } /// /// Check if item in deleted collection /// /// internal bool ContainsDeleted(xContact obj) { foreach (xContact 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 ContactID) { System.Guid sid = new System.Guid(ContactID); foreach (xContact child in deletedList) { if(child.ID==sid) return true; } return false; } #endregion #region Static methods /// /// NewItems /// /// internal static xContacts NewItems() { return new xContacts(); } internal static xContacts GetItems(SafeDataReader dr, RootObjectTypes RootObjectType) { xContacts col = new xContacts(); col.Fetch(dr,RootObjectType); return col; } #endregion #region DAL DATA ACCESS private void Fetch(SafeDataReader dr, RootObjectTypes RootObjectType) { while(dr.Read()) { List.Add(xContact.GetItem(dr,RootObjectType)); } } /// /// Update /// /// /// /// internal void Update(Guid RootObjectID, RootObjectTypes RootObjectType, IDbTransaction tr) { //update (thus deleting) any deleted child objects foreach (xContact child in deletedList) { child.Update( RootObjectID, RootObjectType, tr); } //Now that they are deleted remove them from memory deletedList.Clear(); //Changed: 20-June-2006 - Added this line for insurance //as it was necessary for the contactphones collection EnsureThereIsADefaultContact(); foreach (xContact child in List) { child.Update( RootObjectID, RootObjectType,tr); } } #region Shared delete method /// /// Given a RootObject ID /// /// retrieves all xContact id values for that /// RootObject, then calls the child and grandchild Shared static direct /// delete methods to clear out those collections, then finally /// removes all xContacts for that RootObject /// /// Called by various rootobjects with contacts delete methods /// /// ID of parent RootObject item /// Database transaction from ascendant item internal static void DeleteItems(Guid RootObjectID,IDbTransaction transaction) { SafeDataReader dr = null; ArrayList al=new ArrayList(); try { dr=DBUtil.GetReaderFromSQLString("SELECT aID " + "FROM aContact " + "WHERE aRootObjectID=@ID",RootObjectID,transaction); while(dr.Read()) { al.Add(dr.GetGuid("aID")); } dr.Close(); foreach(object o in al) xContact.DeleteItems((Guid)o,transaction); } finally { if(dr!=null) dr.Close(); } } #endregion #endregion }//end xContacts }//end namespace GZTW.AyaNova.BLL