611 lines
14 KiB
C#
611 lines
14 KiB
C#
///////////////////////////////////////////////////////////
|
|
// 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
|
|
|
|
/// <summary>
|
|
/// Notification delivery setting
|
|
/// </summary>
|
|
[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
|
|
|
|
/// <summary>
|
|
/// Private constructor to prevent direct instantiation
|
|
/// </summary>
|
|
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
|
|
|
|
/// <summary>
|
|
/// Get internal id number
|
|
/// </summary>
|
|
public Guid ID
|
|
{
|
|
get
|
|
{
|
|
return mID;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get created date
|
|
///
|
|
///
|
|
/// </summary>
|
|
public string Created
|
|
{
|
|
get
|
|
{
|
|
return mCreated.ToString();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get modified date
|
|
///
|
|
///
|
|
/// </summary>
|
|
public string Modified
|
|
{
|
|
get
|
|
{
|
|
return mModified.ToString();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get user record ID of person who created this record
|
|
///
|
|
///
|
|
/// </summary>
|
|
public Guid Creator
|
|
{
|
|
get
|
|
{
|
|
return mCreator;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get user ID of person who modified this record
|
|
///
|
|
///
|
|
/// </summary>
|
|
public Guid Modifier
|
|
{
|
|
get
|
|
{
|
|
return mModifier;
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Descriptive Name of delivery method
|
|
/// </summary>
|
|
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();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// AyaNova User ID of notification delivery subscriber
|
|
///
|
|
/// </summary>
|
|
internal Guid UserID
|
|
{
|
|
get
|
|
{
|
|
return mUserID;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Physical delivery method from built in choices of NotifyDeliveryMethods
|
|
/// </summary>
|
|
public NotifyDeliveryMethods DeliveryMethod
|
|
{
|
|
get
|
|
{
|
|
return mDeliveryMethod;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if(mDeliveryMethod!=value)
|
|
{
|
|
mDeliveryMethod = value;
|
|
MarkDirty();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Address for delivery
|
|
/// could be an SMS email address, internet mail address etc
|
|
/// Not used by all delivery methods
|
|
/// </summary>
|
|
public string Address
|
|
{
|
|
get
|
|
{
|
|
return mAddress;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if(mAddress!=value)
|
|
{
|
|
//TODO: Bizrule - no greater than 512 characters
|
|
mAddress = value;
|
|
MarkDirty();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Desired format for message that is sent to user via method chosen
|
|
/// </summary>
|
|
public NotifyDeliveryMessageFormats MessageFormat
|
|
{
|
|
get
|
|
{
|
|
return mMessageFormat;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if(mMessageFormat!=value)
|
|
{
|
|
mMessageFormat = value;
|
|
MarkDirty();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Maximum characters after which message sent to user
|
|
/// is cut off
|
|
///
|
|
/// A value of zero indicates message should *not* be truncated at all
|
|
/// </summary>
|
|
public int MaxCharacters
|
|
{
|
|
get
|
|
{
|
|
return mMaxCharacters;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if(mMaxCharacters!=value)
|
|
{
|
|
mMaxCharacters = value;
|
|
MarkDirty();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Event windows - object containing
|
|
/// information about when delivery can be made
|
|
/// for this notification delivery setting
|
|
/// </summary>
|
|
public EventWindowSet EventWindows
|
|
{
|
|
get
|
|
{
|
|
return this.mEventWindowSet;
|
|
}
|
|
}
|
|
|
|
/// <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.Notification")
|
|
)
|
|
);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region System.object overrides
|
|
|
|
public override string ToString()
|
|
{
|
|
return "NotifyDeliverySetting" + mID.ToString();
|
|
}
|
|
|
|
///
|
|
/// <param name="obj"></param>
|
|
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
|
|
|
|
|
|
/// <summary>
|
|
/// Create new NotifyDeliverySetting
|
|
/// </summary>
|
|
/// <returns>NotifyDeliverySetting</returns>
|
|
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")));
|
|
|
|
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fetch existing NotifyDeliverySetting
|
|
/// </summary>
|
|
/// <returns>NotifyDeliverySetting</returns>
|
|
/// <param name="dr">Datareader</param>
|
|
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
|
|
|
|
/// <summary>
|
|
/// Populate this object from the values in the datareader passed to it
|
|
/// </summary>
|
|
/// <param name="dr"></param>
|
|
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
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="tr"></param>
|
|
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 |