///////////////////////////////////////////////////////////
// NotifyDeliverySetting.cs
// Implementation of Class NotifyDeliverySetting
// CSLA type: Editable Child
// Created on: 06-Oct-2005
// Object design: John
// Coded: John 06-Oct-2005
///////////////////////////////////////////////////////////
using System;
using System.Data;
using CSLA.Data;
using GZTW.Data;
using CSLA;
using System.Threading;
using CSLA.Security;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace GZTW.AyaNova.BLL
{
#pragma warning disable 1591
///
/// Notification delivery setting
///
[Serializable]
public class NotifyDeliverySetting : BusinessBase
{
#region Attributes
private bool bReadOnly;
private Guid mID;
private SmartDate mCreated;
private SmartDate mModified;
private Guid mCreator;
private Guid mModifier;
private string mName=null;
private Guid mUserID;
private NotifyDeliveryMethods mDeliveryMethod;
private string mAddress="";
private NotifyDeliveryMessageFormats mMessageFormat;
private int mMaxCharacters=0;
private EventWindowSet mEventWindowSet;
#endregion
#region Constructor
///
/// Private constructor to prevent direct instantiation
///
private NotifyDeliverySetting()
{
//Set as child object
MarkAsChild();
//Set to read / write initially so that properties
//can be set
bReadOnly=false;
//New ID
mID = Guid.NewGuid();
//pre-break the rule
Name="";
//set some defaults so that user won't see an empty
//selection in UI right off the bat
mDeliveryMethod=NotifyDeliveryMethods.Memo;
mMessageFormat=NotifyDeliveryMessageFormats.Full;
mUserID=User.CurrentThreadUserID;
//Set record history to defaults
mCreated = new SmartDate(DBUtil.CurrentWorkingDateTime);
mModified=new SmartDate();
mCreator=Guid.Empty;
mModifier=Guid.Empty;
mEventWindowSet=null;
}
#endregion
#region Business properties
///
/// Get internal id number
///
public Guid ID
{
get
{
return mID;
}
}
///
/// Get created date
///
///
///
public string Created
{
get
{
return mCreated.ToString();
}
}
///
/// Get modified date
///
///
///
public string Modified
{
get
{
return mModified.ToString();
}
}
///
/// Get user record ID of person who created this record
///
///
///
public Guid Creator
{
get
{
return mCreator;
}
}
///
/// Get user ID of person who modified this record
///
///
///
public Guid Modifier
{
get
{
return mModifier;
}
}
///
/// Descriptive Name of delivery method
///
public string Name
{
get
{
return mName;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mName!=value)
{
mName = value;
BrokenRules.Assert("NameRequired",
"Error.Object.RequiredFieldEmpty,NotifyDeliverySetting.Label.Name","Name",value.Length==0);
BrokenRules.Assert("NameLength",
"Error.Object.FieldLengthExceeded255,NotifyDeliverySetting.Label.Name","Name",value.Length>255);
MarkDirty();
}
}
}
}
///
/// AyaNova User ID of notification delivery subscriber
///
///
internal Guid UserID
{
get
{
return mUserID;
}
}
///
/// Physical delivery method from built in choices of NotifyDeliveryMethods
///
public NotifyDeliveryMethods DeliveryMethod
{
get
{
return mDeliveryMethod;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mDeliveryMethod!=value)
{
mDeliveryMethod = value;
MarkDirty();
}
}
}
}
///
/// Address for delivery
/// could be an SMS email address, internet mail address etc
/// Not used by all delivery methods
///
public string Address
{
get
{
return mAddress;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mAddress!=value)
{
//TODO: Bizrule - no greater than 512 characters
mAddress = value;
MarkDirty();
}
}
}
}
///
/// Desired format for message that is sent to user via method chosen
///
public NotifyDeliveryMessageFormats MessageFormat
{
get
{
return mMessageFormat;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mMessageFormat!=value)
{
mMessageFormat = value;
MarkDirty();
}
}
}
}
///
/// Maximum characters after which message sent to user
/// is cut off
///
/// A value of zero indicates message should *not* be truncated at all
///
public int MaxCharacters
{
get
{
return mMaxCharacters;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mMaxCharacters!=value)
{
mMaxCharacters = value;
MarkDirty();
}
}
}
}
///
/// Event windows - object containing
/// information about when delivery can be made
/// for this notification delivery setting
///
public EventWindowSet EventWindows
{
get
{
return this.mEventWindowSet;
}
}
///
/// 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)
///
private void ThrowSetError()
{
throw new System.Security.SecurityException
(
string.Format
(
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToChange"),
LocalizedTextTable.GetLocalizedTextDirect("O.Notification")
)
);
}
#endregion
#region System.object overrides
public override string ToString()
{
return "NotifyDeliverySetting" + mID.ToString();
}
///
///
public override bool Equals(Object obj)
{
if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false;
NotifyDeliverySetting c=(NotifyDeliverySetting)obj;
return mID==c.mID;
}
public override int GetHashCode()
{
return ("NotifyDeliverySetting"+mID).GetHashCode();
}
#endregion
#region Static methods
///
/// Create new NotifyDeliverySetting
///
/// NotifyDeliverySetting
internal static NotifyDeliverySetting NewItem(Guid UserID)
{
if(AyaBizUtils.Right("Object.Notification")>(int)SecurityLevelTypes.ReadOnly)
{
NotifyDeliverySetting c = new NotifyDeliverySetting();
c.mEventWindowSet=new EventWindowSet();
c.mUserID=UserID;
return c;
}
else
throw new System.Security.SecurityException(
string.Format(
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToCreate"),
LocalizedTextTable.GetLocalizedTextDirect("O.Notification")));
}
///
/// Fetch existing NotifyDeliverySetting
///
/// NotifyDeliverySetting
/// Datareader
internal static NotifyDeliverySetting GetItem(SafeDataReader dr)
{
if(AyaBizUtils.Right("Object.Notification")>(int)SecurityLevelTypes.NoAccess || AyaBizUtils.IsGenerator)
{
NotifyDeliverySetting child = new NotifyDeliverySetting();
child.Fetch(dr);
return child;
}
else
throw new System.Security.SecurityException(
string.Format(
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToRetrieve"),
LocalizedTextTable.GetLocalizedTextDirect("O.Notification")));
}
#endregion
#region DAL DATA ACCESS
#region Fetch
///
/// Populate this object from the values in the datareader passed to it
///
///
private void Fetch(SafeDataReader dr)
{
//Standard fields
mID=dr.GetGuid("aID");
mCreated=DBUtil.ToLocal(dr.GetSmartDate("aCreated"));
mModified=DBUtil.ToLocal(dr.GetSmartDate("aModified"));
mCreator=dr.GetGuid("aCreator");
mModifier=dr.GetGuid("aModifier");
//NotifyDeliverySetting fields
mUserID=dr.GetGuid("aUserID");
mDeliveryMethod=(NotifyDeliveryMethods)dr.GetInt16("aNotifyDeliveryMethod");
mAddress=dr.GetString("AADDRESS");
mMessageFormat=(NotifyDeliveryMessageFormats)dr.GetInt16("aMessageFormat");
mMaxCharacters=dr.GetInt32("aMaxCharacters");
//Important: use property not internal field
//so that initial broken rule is unbroken on fetch
Name=dr.GetString("aName");
//Deserialize the eventmanager object
//Get the layout size
int nEventWindowSize=dr.GetInt32("aEventWindowSetSize");
//Is there anything to load?
if(nEventWindowSize>0)
{
BinaryFormatter bformatter=new BinaryFormatter();
bformatter.AssemblyFormat=System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
byte[] EventWindowData=new Byte[nEventWindowSize];
//retrieve the bytes
dr.GetBytes("aEventWindowSet",0,EventWindowData,0,nEventWindowSize);
MemoryStream mstream=new MemoryStream(EventWindowData);
this.mEventWindowSet=(EventWindowSet)bformatter.Deserialize(mstream);
}
else
{
//Nothing saved so make it fresh
this.mEventWindowSet=new EventWindowSet();
}
//Get access rights level
bReadOnly=AyaBizUtils.Right("Object.Notification")<(int)SecurityLevelTypes.ReadWrite;
MarkOld();
}
#endregion fetch
#region Update
///
///
///
///
internal void Update(IDbTransaction tr)
{
//No need to update if there is nothing changed
if(!this.IsDirty) return;
// If not a new record, check if record was modified
//by another user since original retrieval:
if(!IsNew)
DBUtil.CheckSafeToUpdateInsideTransaction(this.mModified.Date,this.mID,"aNotifyDeliverySetting", tr);
#region Delete
if(IsDeleted)
{
if(!IsNew)
{
//Delete object and child objects
DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM aNotifyDeliverySetting WHERE aID = @ID;");
cmDelete.AddInParameter("@ID",DbType.Guid,this.mID);
DBUtil.DB.ExecuteNonQuery(cmDelete, tr);
//-----------------------------
}
MarkNew();
return;
}
#endregion
#region Add / Update
//get modification time temporarily, if update succeeds then
//set to this time
System.DateTime dtModified = DBUtil.CurrentWorkingDateTime;
DBCommandWrapper cm = null;
if(IsNew)//Add or update?
cm=DBUtil.GetCommandFromSQL(
"INSERT INTO aNotifyDeliverySetting (aID, aName, aUserID, " +
"aNotifyDeliveryMethod,AADDRESS,aMessageFormat,aMaxCharacters, " +
"aEventWindowSetSize, aEventWindowSet,aCreated,aModified,aCreator,aModifier) VALUES " +
"(@ID,@Name,@UserID,@DeliveryMethod,@Address,@MessageFormat,@MaxCharacters, " +
"@EventWindowSetSize,@EventWindowSet,@Created,@Modified,@CurrentUserID,@CurrentUserID)"
);
else
cm=DBUtil.GetCommandFromSQL(
"UPDATE aNotifyDeliverySetting SET " +
"aID=@ID, aName=@Name, aUserID=@UserID, aNotifyDeliveryMethod=@DeliveryMethod, " +
"AADDRESS=@Address, aMessageFormat=@MessageFormat, aMaxCharacters=@MaxCharacters, " +
"aEventWindowSetSize=@EventWindowSetSize,aEventWindowSet=@EventWindowSet, aModifier=@CurrentUserID, aModified=@Modified WHERE aID=@ID"
);
//NotifyDeliverySetting fields
cm.AddInParameter("@Name",DbType.String,mName);
cm.AddInParameter("@UserID",DbType.Guid,mUserID);
cm.AddInParameter("@DeliveryMethod",DbType.Int16,(int)this.mDeliveryMethod);
cm.AddInParameter("@Address",DbType.String,mAddress);
cm.AddInParameter("@MessageFormat",DbType.Int16,(int)mMessageFormat);
cm.AddInParameter("@MaxCharacters",DbType.Int32,mMaxCharacters);
//Event window object serialize:
MemoryStream ms=new MemoryStream();
BinaryFormatter b=new BinaryFormatter();
b.AssemblyFormat=System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
b.Serialize(ms,mEventWindowSet);
cm.AddInParameter("@EventWindowSetSize",DbType.Int32,ms.Length);
cm.AddInParameter("@EventWindowSet",DbType.Object,ms.GetBuffer());
//Standard fields
cm.AddInParameter("@ID",DbType.Guid,this.mID);
cm.AddInParameter("@CurrentUserID",DbType.Guid, CurrentUserID);
cm.AddInParameter("@Created",DbType.DateTime, DBUtil.ToUTC(mCreated.Date));
cm.AddInParameter("@Modified",DbType.DateTime, DBUtil.ToUTC(dtModified));
DBUtil.DB.ExecuteNonQuery(cm, tr);
MarkOld();//db is now synched with object
//Successful update so
//change modification time to match
this.mModified.Date=dtModified;
#endregion
}
#endregion update
#endregion
#region Override IsValid / IsDirty
//Override base class version if there are child objects
public override bool IsValid
{
get
{
return base.IsValid && mEventWindowSet.IsValid ;
}
}
public override bool IsDirty
{
get
{
return base.IsDirty || mEventWindowSet.IsDirty ;
}
}
//
#endregion
}//end NotifyDeliverySetting
#pragma warning restore 1591
}//end namespace GZTW.AyaNova.BLL