440 lines
9.7 KiB
C#
440 lines
9.7 KiB
C#
///////////////////////////////////////////////////////////
|
|
// 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
|
|
{
|
|
/// <summary>
|
|
/// DEPRECATED V4.0
|
|
/// </summary>
|
|
[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
|
|
/// <summary>
|
|
/// Retrieve xContact by index
|
|
/// </summary>
|
|
/// <param name="Item">Index</param>
|
|
internal xContact this[int Item]
|
|
{
|
|
get
|
|
{
|
|
return (xContact) List[Item];
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve xContact by string containing Guid value
|
|
/// </summary>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve xContact by Guid value
|
|
/// </summary>
|
|
internal xContact this[Guid ContactGUID]
|
|
{
|
|
get
|
|
{
|
|
|
|
foreach (xContact child in List)
|
|
{
|
|
if(child.ID==ContactGUID)
|
|
return child;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove xContact by passing it in
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
internal void Remove(xContact obj)
|
|
{
|
|
List.Remove(obj);
|
|
EnsureThereIsADefaultContact();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove xContact by string of id value
|
|
/// </summary>
|
|
/// <param name="ContactID"></param>
|
|
public void Remove(string ContactID)
|
|
{
|
|
System.Guid sid = new System.Guid(ContactID);
|
|
Remove(sid);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove by Guid value of ID
|
|
/// </summary>
|
|
/// <param name="ContactID"></param>
|
|
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);
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add new
|
|
/// </summary>
|
|
/// <param name="RootObjectType"></param>
|
|
/// <param name="RootObjectID"></param>
|
|
/// <returns></returns>
|
|
internal xContact Add(RootObjectTypes RootObjectType, Guid RootObjectID)
|
|
{
|
|
xContact child=xContact.NewItem( RootObjectType, RootObjectID);
|
|
List.Add(child);
|
|
EnsureThereIsADefaultContact();
|
|
return child;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ensures one contact is set to default
|
|
/// </summary>
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks if grandchildren are dirty
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool GrandChildIsDirty()
|
|
{
|
|
foreach (xContact child in List)
|
|
{
|
|
if(child.Phones.IsDirty==true)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks if grandchildren are valid
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool GrandChildIsValid()
|
|
{
|
|
foreach (xContact child in List)
|
|
{
|
|
if(child.Phones.IsValid==false)
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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
|
|
/// </summary>
|
|
/// <param name="ContactID"></param>
|
|
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;
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Iterates through collection and gets the default contact
|
|
/// </summary>
|
|
internal xContact GetPrimaryContact()
|
|
{
|
|
foreach(xContact child in List)
|
|
{
|
|
if(child.PrimaryContact)
|
|
return child;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a string containing the primary contact's
|
|
/// default contact info
|
|
/// (i.e. Email address and all phones collection)
|
|
/// </summary>
|
|
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
|
|
|
|
/// <summary>
|
|
/// Check if item in collection
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
internal bool Contains(xContact obj)
|
|
{
|
|
foreach (xContact child in List)
|
|
{
|
|
if(child.Equals(obj)) return true;
|
|
}
|
|
return false;
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if item in collection by string of ID value
|
|
/// </summary>
|
|
/// <param name="ContactID"></param>
|
|
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;
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if item in deleted collection
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
internal bool ContainsDeleted(xContact obj)
|
|
{
|
|
foreach (xContact child in deletedList)
|
|
{
|
|
if(child.Equals(obj)) return true;
|
|
}
|
|
return false;
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if item in deleted collection by string of ID value
|
|
/// </summary>
|
|
/// <param name="ContactID"></param>
|
|
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
|
|
|
|
/// <summary>
|
|
/// NewItems
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
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));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update
|
|
/// </summary>
|
|
/// <param name="RootObjectID"></param>
|
|
/// <param name="RootObjectType"></param>
|
|
/// <param name="tr"></param>
|
|
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
|
|
/// <summary>
|
|
/// 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
|
|
/// </summary>
|
|
/// <param name="RootObjectID">ID of parent RootObject item</param>
|
|
/// <param name="transaction">Database transaction from ascendant item</param>
|
|
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 |