Files
2018-06-29 19:47:36 +00:00

443 lines
10 KiB
C#

///////////////////////////////////////////////////////////
// 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
{
/// <summary>
/// DEPRECATED v4.0
/// </summary>
[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
/// <summary>
/// Retrieve xContactPhone by index
/// </summary>
/// <param name="Item">Index</param>
public xContactPhone this[int Item]
{
get
{
return (xContactPhone) List[Item];
}
}
/// <summary>
/// Retrieve xContactPhone by string containing Guid value
/// </summary>
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;
}
}
/// <summary>
/// Retrieve xContactPhone by Guid value
/// </summary>
public xContactPhone this[Guid ContactPhoneGUID]
{
get
{
foreach (xContactPhone child in List)
{
if(child.ID==ContactPhoneGUID)
return child;
}
return null;
}
}
/// <summary>
/// Retrieve xContactPhone by type
/// </summary>
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;
}
}
/// <summary>
/// Remove xContactPhone by passing it in
/// </summary>
/// <param name="obj"></param>
public void Remove(xContactPhone obj)
{
List.Remove(obj);
EnsureThereIsADefaultPhone();
}
/// <summary>
/// Remove xContactPhone by string of id value
/// </summary>
/// <param name="ContactPhoneID"></param>
public void Remove(string ContactPhoneID)
{
System.Guid sid = new System.Guid(ContactPhoneID);
Remove(sid);
}
/// <summary>
/// Remove by Guid value of ID
/// </summary>
/// <param name="ContactPhoneID"></param>
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);
}
/// <summary>
/// Add a new xContactPhone to the collection
/// </summary>
/// <param name="obj"></param>
internal xContactPhone Add(xContact obj)
{
xContactPhone child=xContactPhone.NewItem(obj);
List.Add(child);
EnsureThereIsADefaultPhone();
return child;
}
/// <summary>
/// Sets passed in phone id default to true
/// all others to false
/// </summary>
/// <param name="ContactPhoneID"></param>
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;
}
}
/// <summary>
/// Iterates through collection and gets the default phone record
/// </summary>
public xContactPhone GetDefaultPhone()
{
foreach(xContactPhone child in List)
{
if(child.PhoneDefault)
return child;
}
return null;
}
/// <summary>
/// Iterates through collection and gets the fax number or an empty string if none found
/// </summary>
public string GetFax()
{
foreach(xContactPhone child in List)
{
if(child.ContactPhoneType==ContactPhoneTypes.Fax)
return child.NumberOnlyFullPhoneRecord;
}
return "";
}
/// <summary>
/// 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
/// </summary>
public string GetAltPhone()
{
foreach(xContactPhone child in List)
{
if(!child.PhoneDefault && child.ContactPhoneType!=ContactPhoneTypes.Fax)
return child.NumberOnlyFullPhoneRecord;
}
return "";
}
/// <summary>
/// Ensures one contact is set to default
/// </summary>
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
/// <summary>
/// Check if item in collection
/// </summary>
/// <param name="obj"></param>
public bool Contains(xContactPhone obj)
{
foreach (xContactPhone 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="ContactPhoneID"></param>
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;
}
/// <summary>
/// Check if item in deleted collection
/// </summary>
/// <param name="obj"></param>
public bool ContainsDeleted(xContactPhone obj)
{
foreach (xContactPhone 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="ContactPhoneID"></param>
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
/// <summary>
/// NewItems
/// </summary>
/// <returns></returns>
internal static xContactPhones NewItems()
{
return new xContactPhones();
}
/// <summary>
/// Get items - Grandchild style
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
internal static xContactPhones GetItems(xContact obj)
{
xContactPhones col = new xContactPhones();
col.Fetch(obj);
return col;
}
#endregion
#region DAL DATA ACCESS
/// <summary>
/// Fetch children - grandchild style
/// </summary>
/// <param name="obj"></param>
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();
}
}
/// <summary>
/// Update children
/// </summary>
/// <param name="obj"></param>
/// <param name="tr"></param>
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
/// <summary>
/// Given a xContact ID deletes all items for that ID
///
/// Called by xContact delete
/// </summary>
/// <param name="ContactID">ID of parent xContact item</param>
/// <param name="transaction">Database transaction from ascendant item</param>
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