Files
ayanova7/source/bizobjects/AyaLib/GZTW.AyaNova.BLL/xContact.cs
2018-06-29 19:47:36 +00:00

766 lines
19 KiB
C#

///////////////////////////////////////////////////////////
// xContact.cs
// Implementation of Class xContact
// CSLA type: Editable Child
// Created on: 07-Jun-2004 8:41:17 AM
// Object design: Joyce
// Coded: John 14-July-2004
///////////////////////////////////////////////////////////
using System;
using System.Data;
using CSLA.Data;
using GZTW.Data;
using CSLA;
using System.Threading;
using CSLA.Security;
//using log4net;
namespace GZTW.AyaNova.BLL {
/// <summary>
/// DEPRECATED V4.0
/// xContact item
/// </summary>
[Serializable]
internal class xContact : BusinessBase {
//CustomFields
// Create a logger for use in this class
//private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#region Attributes
private bool bReadOnly;
private Guid mID;
private Guid mRootObjectID;
private RootObjectTypes mRootObjectType;
private Guid mCreator;
private Guid mModifier;
private SmartDate mCreated;
private SmartDate mModified;
private bool mPrimaryContact;
private string mEmailAddress="";
private Guid mContactTitleID;
private string mFirstName="";
private string mLastName="";
private string mJobTitle="";
private string mDescription="";
private xContactPhones mContactPhones;
#endregion
#region Constructor
private xContact()
{
//Child object
MarkAsChild();
//Set to read / write initially so that properties
//can be set
bReadOnly=false;
//pre-"break" critical elements
//to ensure they are set by parent
//before saving this child object
RootObjectType=0;
RootObjectID=System.Guid.Empty;
//New ID
mID = Guid.NewGuid();
mContactPhones=xContactPhones.NewItems();
//Set record history to defaults
mCreated = new SmartDate(DBUtil.CurrentWorkingDateTime);
mModified=new SmartDate();
mCreator=Guid.Empty;
mModifier=Guid.Empty;
}
#endregion
#region Business properties
/// <summary>
/// Get internal id number Read only property because it's set internally, not
/// externally
/// </summary>
public Guid ID
{
get
{
return mID;
}
}
/// <summary>
/// xContact's phone number collection
/// </summary>
public xContactPhones Phones
{
get
{
return mContactPhones;
}
}
/// <summary>
/// RootObject type
/// </summary>
public RootObjectTypes RootObjectType
{
get
{
return mRootObjectType;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mRootObjectType!=value)
{
mRootObjectType = value;
MarkDirty();
}
}
}
}
/// <summary>
/// ID of root object this xContact belongs to
/// </summary>
public Guid RootObjectID
{
get
{
return mRootObjectID;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mRootObjectID!=value)
{
mRootObjectID = value;
BrokenRules.Assert("RootObjectIDRequired",
"Error.Object.RequiredFieldEmpty,Common.Label.RootObject","RootObjectID",value==System.Guid.Empty);
MarkDirty();
}
}
}
}
/// <summary>
/// User ID who created this xContact record
/// </summary>
public Guid Creator
{
get
{
return mCreator;
}
}
/// <summary>
/// Date this xContact was created
/// </summary>
public string Created
{
get
{
return mCreated.ToString();
}
}
/// <summary>
/// User ID who last modified this record
/// </summary>
public Guid Modifier
{
get
{
return mModifier;
}
}
/// <summary>
/// Last date this xContact was modified
/// </summary>
public string Modified
{
get
{
return mModified.ToString();
}
}
/// <summary>
/// Title for this contact (i.e Mr, Mrs etc)
/// </summary>
public Guid ContactTitleID
{
get
{
return mContactTitleID;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mContactTitleID!=value)
{
mContactTitleID = value;
MarkDirty();
}
}
}
}
/// <summary>
/// If true, this is the primary contact for this address object If false, then not
/// First contact entered is set to true for this address Additional contacts
/// entered are then set to false automatically User has ability to change true and
/// false
/// </summary>
public bool PrimaryContact
{
get
{
return mPrimaryContact;
}
//Case 124
set
{
if (bReadOnly)
ThrowSetError();
else
{
if (mPrimaryContact != value)
{
mPrimaryContact = value;
MarkDirty();
}
}
}
}
internal bool internalPrimaryContact
{
set
{
if (bReadOnly)
ThrowSetError();
else
{
if (mPrimaryContact != value)
{
mPrimaryContact = value;
MarkDirty();
}
}
}
}
/// <summary>
/// Email address of contact - hyperlink to email program
/// </summary>
public string EmailAddress
{
get
{
return mEmailAddress;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mEmailAddress!=value)
{
mEmailAddress = value;
MarkDirty();
}
}
}
}
public string FirstName
{
get
{
return mFirstName;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mFirstName!=value)
{
mFirstName = value;
MarkDirty();
}
}
}
}
public string LastName
{
get
{
return mLastName;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mLastName!=value)
{
mLastName = value;
MarkDirty();
}
}
}
}
public string Description
{
get
{
return mDescription;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mDescription!=value)
{
mDescription = value;
MarkDirty();
}
}
}
}
/// <summary>
/// Job title for this contact (i.e sales manager, office staff, etc)
/// </summary>
public string JobTitle
{
get
{
return mJobTitle;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mJobTitle!=value)
{
mJobTitle = value;
MarkDirty();
}
}
}
}
/// <summary>
/// Throw an error when a read only user
/// tries to set a property
/// (this should normally never be called unless someone is using the developer api since the UI
/// should prevent it from happening initially)
/// </summary>
private void ThrowSetError()
{
throw new System.Security.SecurityException
(
string.Format
(
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToChange"),
LocalizedTextTable.GetLocalizedTextDirect("O.xContact")
)
);
}
#endregion
#region System.object overrides
public override string ToString()
{
return "Contact" + mID.ToString();
}
///
/// <param name="obj"></param>
public override bool Equals(Object obj)
{
if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false;
xContact c=(xContact)obj;
return (mID==c.ID);
}
public override int GetHashCode()
{
return ("Contact" + mID.ToString()).GetHashCode();
}
#endregion
#region Searching
/// <summary>
/// DEPRECATED V4.X
/// </summary>
/// <param name="ID"></param>
/// <param name="searchTerms"></param>
/// <returns></returns>
public static SearchResult GetSearchResult(Guid ID, string[]searchTerms)
{
return new SearchResult();
}
#endregion
#region Static methods
/// <summary>
/// Create new xContact
/// Sets RootObjectID, RootObjectType
/// </summary>
internal static xContact NewItem(RootObjectTypes RootObjectType, Guid RootObjectID)
{
if(AyaBizUtils.Right(RootObjectType)>(int)SecurityLevelTypes.ReadOnly)
{
xContact c= new xContact();
c.RootObjectID=RootObjectID;
c.RootObjectType=RootObjectType;
return c;
}
else
throw new System.Security.SecurityException(
string.Format(
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToCreate"),
LocalizedTextTable.GetLocalizedTextDirect("O.xContact")));
}
internal static xContact GetItem(SafeDataReader dr,RootObjectTypes RootObjectType)
{
if(AyaBizUtils.Right(RootObjectType)>(int)SecurityLevelTypes.NoAccess)
{
xContact child = new xContact();
child.Fetch(dr,RootObjectType);
return child;
}
else
throw new System.Security.SecurityException(
string.Format(
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToRetrieve"),
LocalizedTextTable.GetLocalizedTextDirect("O.xContact")));
}
#endregion
#region DAL DATA ACCESS
#region custom concurrency check
//Changed: 20-June-2006, not required, this object has an ID field and buggy to boot
// private static void CheckSafeToUpdate(System.DateTime LastUpdated, RootObjectTypes RootObjectType,bool PrimaryContact, Guid RootObjectID, IDbTransaction tr)
// {
// DBCommandWrapper dbCommandWrapper = DBUtil.DB.GetSqlStringCommandWrapper(
// "SELECT aModified, aModifier FROM aContact WHERE (aRootObjectID " +
// "= @RootObjectID) AND " +
// "(aRootObjectType = @RootObjectType) AND (aPrimaryContact=@aPRIMARYCONTACT);"
// );
// dbCommandWrapper.AddInParameter("@RootObjectID",DbType.Guid,RootObjectID);
// dbCommandWrapper.AddInParameter("@RootObjectType",DbType.Int16,(int)RootObjectType);
// dbCommandWrapper.AddInParameter("@aPRIMARYCONTACT",DbType.Boolean,PrimaryContact);
// SafeDataReader r = new SafeDataReader(DBUtil.DB.ExecuteReader(dbCommandWrapper,tr));
// //changed: 20-June-2006 Noticed this isn't explicitly closing the data reader
// //added the try and catch block so could put that in the finally block
// try
// {
// if(r.Read())
// {
// if(!DBUtil.DatesAreEqualish(DBUtil.ToUTC(LastUpdated),r.GetSmartDate("aModified").Date))
// {
// Guid gModifier=r.GetGuid("aModifier");
// r.Close();
// dbCommandWrapper.Command.Parameters.Clear();
// dbCommandWrapper.AddInParameter("@ID",DbType.Guid,gModifier);
// dbCommandWrapper.Command.CommandText = "SELECT aFirstName, aLastName FROM aUser WHERE aID = @ID";
// r = new SafeDataReader(DBUtil.DB.ExecuteReader(dbCommandWrapper,tr));
// if (r.Read())
// {
// string sUser = r.GetString("aFirstName") + " " + r.GetString("aLastName");
// r.Close();
// throw new System.Exception(string.Format(LocalizedTextTable.GetLocalizedTextDirect("Error.DB.RecordModifiedExternally"), "xContact", sUser));
// }
// }
// //else
// //{
// // //de nada
// // r.Close();
// // return;
// //}
// }
// }
// catch
// {
// throw;
// }
// finally
// {
// if (r != null) r.Close();
// }
//}
#endregion
#region Update
/// <summary>
/// Update object to database
/// </summary>
/// <param name="RootObjectType">RootOjbect type</param>
/// <param name="RootObjectID">GUID of root object</param>
/// <param name="tr">Parent object's sql transaction object</param>
internal void Update( Guid RootObjectID,RootObjectTypes RootObjectType, IDbTransaction tr)
{
//Phones is a grandchild collection
//it must be called to update regardless of whether
//the contact record is dirty or not, otherwise it would never get saved
//Phone collection
//ONLY update if this is not a new contact record
//otherwise there will be a foreign key violation if
//contact is not saved first
if(!IsNew)
mContactPhones.Update(this,tr);
//No need to update if there is nothing changed
if(!this.IsDirty) return;
//Changed: 20-June-2006 to ensure primary contact at fetch time is the one being
//compared against from the db rather than as it is now which could have been modified
//by user resulting in comparing against the wrong db record
//// If not a new record, check if record was modified
////by another user since original retrieval:
//if (!IsNew)//call custom checksafetoupdate method for an
// CheckSafeToUpdate(this.mModified.Date, RootObjectType, this.mPrimaryContactInDB, RootObjectID, tr);
if (!IsNew)
DBUtil.CheckSafeToUpdateInsideTransaction(this.mModified.Date, this.mID, "ACONTACT", tr);
#region Delete
if(IsDeleted)
{
if(!IsNew)
{
//Use shared delete method to remove this record and all
//grandchildren
DeleteItems(this.mID,tr);
}
MarkNew();
return;
}
#endregion
#region Add / Update
//get modification time temporarily, if update succeeds then
//set to this time
System.DateTime dtModified = DBUtil.CurrentWorkingDateTime;
//convert empty strings to null
if(this.mDescription=="") this.mDescription=null;
DBCommandWrapper cm = null;
if(IsNew)//Add or update?
cm=DBUtil.GetCommandFromSQL(
"INSERT INTO aContact (aRootObjectID,aRootObjectType, aPrimaryContact, aID, aEmailAddress, aContactTitleID, " +
"aFirstName, aLastName, aJobTitle, aDescription, aCreated,aModified,aCreator,aModifier) " +
"VALUES (@RootObjectID,@RootObjectType,@PrimaryContact,@ID,@EmailAddress,@ContactTitleID, " +
"@FirstName,@LastName,@JobTitle,@Description,@Created,@Modified,@CurrentUserID,@CurrentUserID)"
);
else
cm=DBUtil.GetCommandFromSQL(
"UPDATE aContact SET aRootObjectID=@RootObjectID, aRootObjectType=@RootObjectType, " +
"aPrimaryContact=@PrimaryContact, " +
"aID=@ID, aEmailAddress=@EmailAddress, " +
"aContactTitleID=@ContactTitleID, aFirstName=@FirstName, " +
"aLastName=@LastName, aJobTitle=@JobTitle, " +
"aDescription=@Description, aModifier=@CurrentUserID, " +
"aModified=@Modified WHERE aID=@ID"
);
cm.AddInParameter("@ID",DbType.Guid,this.mID);
cm.AddInParameter("@RootObjectID",DbType.Guid, RootObjectID);
cm.AddInParameter("@RootObjectType",DbType.Int16, (int)RootObjectType);
cm.AddInParameter("@CurrentUserID",DbType.Guid, CurrentUserID);
cm.AddInParameter("@Created",DbType.DateTime, DBUtil.ToUTC(mCreated.Date));
cm.AddInParameter("@Modified",DbType.DateTime, DBUtil.ToUTC(dtModified));
cm.AddInParameter("@ContactTitleID",DbType.Guid,mContactTitleID);
cm.AddInParameter("@EmailAddress", DbType.String, mEmailAddress);
cm.AddInParameter("@FirstName", DbType.String, mFirstName);
cm.AddInParameter("@JobTitle", DbType.String, mJobTitle);
cm.AddInParameter("@LastName", DbType.String, mLastName);
cm.AddInParameter("@Description", DbType.String, mDescription);
cm.AddInParameter("@PrimaryContact",DbType.Boolean, mPrimaryContact);
DBUtil.DB.ExecuteNonQuery(cm, tr);
//If new then update contacts otherwise
//it's already been handled above
if(IsNew)
mContactPhones.Update(this,tr);
//Process keywords
DBUtil.ProcessKeywords(tr,this.mID,RootObjectTypes.Contact,IsNew,AyaBizUtils.Break(false,
mEmailAddress,mFirstName,mJobTitle,mLastName,mDescription));
MarkOld();//db is now synched with object
//Successful update so
//change modification time to match
this.mModified.Date=dtModified;
#endregion
}
#endregion
#region Fetch
private void Fetch(SafeDataReader dr,RootObjectTypes RootObjectType)
{
//Standard items
mCreated=DBUtil.ToLocal(dr.GetSmartDate("aCreated"));
mCreator=dr.GetGuid("aCreator");
mModified=DBUtil.ToLocal(dr.GetSmartDate("aModified"));
mModifier=dr.GetGuid("aModifier");
//xContact specific items
mID=dr.GetGuid("aID");
mRootObjectID= dr.GetGuid("aRootObjectID");
mRootObjectType=(RootObjectTypes)dr.GetInt16("aRootObjectType");
mContactTitleID = dr.GetGuid("aContactTitleID");
mEmailAddress=dr.GetString("aEmailAddress");
mFirstName=dr.GetString("aFirstName");
mJobTitle=dr.GetString("aJobTitle");
mLastName=dr.GetString("aLastName");
mDescription=dr.GetString("aDescription");
mPrimaryContact=dr.GetBoolean("aPrimaryContact");
//Populate child phones collection
//Since xContactPhones is a grandchild collection
//will be loaded by the xContactPhones collection
//object who will do the database call based on
//the ID of this object which is passed via the object
//reference
mContactPhones=xContactPhones.GetItems(this);
//Get access rights level
bReadOnly=AyaBizUtils.Right(RootObjectType)<(int)SecurityLevelTypes.ReadWrite;
MarkOld();
}
#endregion
#region Shared Delete method One stop delete shop
/// <summary>
/// Called by collection and by the Update method in this object
/// to remove all grandchild collections and xContact item itself
/// </summary>
/// <param name="ContactID"></param>
/// <param name="transaction"></param>
internal static void DeleteItems(Guid ContactID, IDbTransaction transaction)
{
xContactPhones.DeleteItems(ContactID,transaction);
DBUtil.RemoveKeywords(transaction,RootObjectTypes.Contact,ContactID);
DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM aContact WHERE aID=@ID;");
cmDelete.AddInParameter("@ID",DbType.Guid,ContactID);
DBUtil.DB.ExecuteNonQuery(cmDelete, transaction);
}
#endregion
#endregion
}//end xContact
}//end namespace GZTW.AyaNova.BLL