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

1374 lines
36 KiB
C#

///////////////////////////////////////////////////////////
// ScheduleMarker.cs
// Implementation of Class ScheduleMarker
// CSLA type: Editable Root
// Created on: 07-Jun-2004 8:41:36 AM
// Object design: Joyce
// Coded: John 30-Jul-2004
///////////////////////////////////////////////////////////
using System;
using System.Data;
using CSLA.Data;
using GZTW.Data;
using CSLA;
using System.Threading;
using CSLA.Security;
using System.ComponentModel;
using System.Collections;
namespace GZTW.AyaNova.BLL
{
/// <summary>
/// A schedulemarker is used to make an entry in the calendar for items that are not workorder item scheduled users.
/// Users make schedule markers to indicate time off, holidays or to remind them to follow up on root objects using the follow up feature etc.
/// </summary>
[Serializable]
public class ScheduleMarker : BusinessBase {
#region Attributes
private bool bReadOnly;
private Guid mID;
private string mName=null;
private SmartDate mCreated;
private SmartDate mModified;
private Guid mCreator;
private Guid mModifier;
private string mNotes="";
private SmartDate mStartDate;
private SmartDate mStopDate;
private Guid mSourceID;
private ScheduleMarkerSourceTypes mScheduleMarkerSourceType;
private int mARGB;
private Guid mFollowID;
private RootObjectTypes mFollowType;
private bool mCompleted;//case 1968
#endregion
#region Constructor
/// <summary>
/// Private constructor to prevent direct instantiation
/// </summary>
private ScheduleMarker()
{
//Set to read / write initially so that properties
//can be set
bReadOnly=false;
//New ID
mID = Guid.NewGuid();
//pre-break the rule
Name="";
//Notes="";
mStartDate = new SmartDate(DBUtil.CurrentWorkingDateTime);//case 1967 set starting date so can't save with no date
mStopDate = new SmartDate(DBUtil.CurrentWorkingDateTime.AddHours(1));//case 1967 starting default
//mRangeStartDate=new SmartDate();
//mRangeStopDate=new SmartDate();
//Set record history to defaults
mCreated = new SmartDate(DBUtil.CurrentWorkingDateTime);
mModified=new SmartDate();
mCreator=Guid.Empty;
mModifier=Guid.Empty;
//default is for global source ID to prevent
//saving without one
//normally this will be set by caller but
//for global it won't since there is no id
//so use built in id
mSourceID = ScheduleMarkerGlobalSourceID;
//Added:31-Oct-2006
mScheduleMarkerSourceType = ScheduleMarkerSourceTypes.Global;
mFollowType = RootObjectTypes.Nothing;
mCompleted = false;//case 1968
}
#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>
/// 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>
/// Set/get Schedulemarker name
/// </summary>
public string Name
{
get
{
return mName;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mName!=value)
{
mName = value;
BrokenRules.Assert("NameRequired","Error.Object.RequiredFieldEmpty,ScheduleMarker.Label.Name","Name",value.Length==0);
BrokenRules.Assert("NameLength",
"Error.Object.FieldLengthExceeded255,ScheduleMarker.Label.Name","Name",value.Length>255);
MarkDirty();
}
}
}
}
/// <summary>
/// Detailed notes to display about schedule marker item
/// </summary>
public string Notes
{
get
{
return mNotes;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mNotes!=value)
{
mNotes = value;
MarkDirty();
}
}
}
}
/// <summary>
/// Start date of this schedulemarker
/// </summary>
public object StartDate
{
get
{
return mStartDate.DBValue;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if (!AyaBizUtils.SmartDateEquals(mStartDate, value)) //Case 298
{
mStartDate.DBValue = value;
//not empty Start date is not valid without stop date
BrokenRules.Assert("StopDateRequired","Error.Object.RequiredFieldEmpty,ScheduleMarker.Label.StopDate",
"StartDate",!mStartDate.IsEmpty && mStopDate.IsEmpty);
//Is stop date valid now?
BrokenRules.Assert("StartDateRequired","Error.Object.RequiredFieldEmpty,ScheduleMarker.Label.StartDate",
"StopDate",!mStopDate.IsEmpty && mStartDate.IsEmpty);
if(!mStopDate.IsEmpty&&!mStartDate.IsEmpty)
{
BrokenRules.Assert("StartDateBeforeStopDate","Error.Object.StartDateAfterEndDate",
"StartDate",mStartDate.CompareTo(mStopDate)>-1);
}
MarkDirty();
}
}
}
}
/// <summary>
/// Start date of this schedulemarker
/// as Date/Time object for use outside of UI binding
/// </summary>
public DateTime dtStartDate
{
get
{
return mStartDate.Date;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mStartDate.Date!=value)
{
mStartDate.Date = value;
//not empty Start date is not valid without stop date
BrokenRules.Assert("StopDateRequired","Error.Object.RequiredFieldEmpty,ScheduleMarker.Label.StopDate","StartDate",!mStartDate.IsEmpty && mStopDate.IsEmpty);
//Is stop date valid now?
BrokenRules.Assert("StartDateRequired","Error.Object.RequiredFieldEmpty,ScheduleMarker.Label.StartDate","StopDate",!mStopDate.IsEmpty && mStartDate.IsEmpty);
if(!mStopDate.IsEmpty&&!mStartDate.IsEmpty)
{
BrokenRules.Assert("StartDateBeforeStopDate","Error.Object.StartDateAfterEndDate",
"StartDate",mStartDate.CompareTo(mStopDate)>-1);
}
MarkDirty();
}
}
}
}
/// <summary>
/// Stop date of this scheulde marker
/// </summary>
public object StopDate
{
get
{
return mStopDate.DBValue;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if (!AyaBizUtils.SmartDateEquals(mStopDate, value)) //Case 298
{
mStopDate.DBValue = value;
//Not empty Stop date is not valid without start date
BrokenRules.Assert("StartDateRequired","Error.Object.RequiredFieldEmpty,ScheduleMarker.Label.StartDate","StopDate",!mStopDate.IsEmpty && mStartDate.IsEmpty);
//Rule needs to be checked here even though it's to do with start date because it can be broken
//there but can only be unbroken here so it needs to be checked in both places...
//Is start date valid now?
BrokenRules.Assert("StopDateRequired","Error.Object.RequiredFieldEmpty,ScheduleMarker.Label.StopDate","StartDate",!mStartDate.IsEmpty && mStopDate.IsEmpty);
if(!mStopDate.IsEmpty&&!mStartDate.IsEmpty)
{
BrokenRules.Assert("StartDateBeforeStopDate","Error.Object.StartDateAfterEndDate",
"StartDate",mStartDate.CompareTo(mStopDate)>-1);
}
MarkDirty();
}
}
}
}
/// <summary>
/// Stop date of this scheulde marker as DateTime
/// for use outside of form binding
/// </summary>
public DateTime dtStopDate
{
get
{
return mStopDate.Date;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mStopDate.Date!=value)
{
mStopDate.Date = value;
//Not empty Stop date is not valid without start date
BrokenRules.Assert("StartDateRequired","Error.Object.RequiredFieldEmpty,ScheduleMarker.Label.StartDate",
"StopDate",!mStopDate.IsEmpty && mStartDate.IsEmpty);
//Rule needs to be checked here even though it's to do with start date because it can be broken
//there but can only be unbroken here so it needs to be checked in both places...
//Is start date valid now?
BrokenRules.Assert("StopDateRequired","Error.Object.RequiredFieldEmpty,ScheduleMarker.Label.StopDate",
"StartDate",!mStartDate.IsEmpty && mStopDate.IsEmpty);
if(!mStopDate.IsEmpty&&!mStartDate.IsEmpty)
{
BrokenRules.Assert("StartDateBeforeStopDate","Error.Object.StartDateAfterEndDate",
"StartDate",mStartDate.CompareTo(mStopDate)>-1);
}
MarkDirty();
}
}
}
}
/// <summary>
/// ARGB color for display in UI
/// </summary>
public int ARGB
{
get
{
return mARGB;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mARGB!=value)
{
mARGB = value;
MarkDirty();
}
}
}
}
/// <summary>
/// ID of source, whether global, a specific region or a specific user
/// This is who or whom is meant to view this record.
/// </summary>
public Guid SourceID
{
get
{
return mSourceID;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mSourceID!=value)
{
mSourceID = value;
MarkDirty();
}
}
}
}
/// <summary>
/// Source that the schedulemarker applies to whether a global schedulemarker, a
/// selected region or an indivudal user
/// </summary>
public ScheduleMarkerSourceTypes ScheduleMarkerSourceType
{
get
{
return mScheduleMarkerSourceType;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mScheduleMarkerSourceType!=value)
{
mScheduleMarkerSourceType = value;
MarkDirty();
}
}
}
}
//case 1968
/// <summary>
/// Get /set completed status of ScheduleMarker / Follow-up
/// </summary>
public bool Completed
{
get
{
return mCompleted;
}
set
{
if (bReadOnly)
ThrowSetError();
else
{
if (mCompleted != value)
{
mCompleted = value;
MarkDirty();
}
}
}
}
/// <summary>
/// Returns an arraylist containing the
/// scheduleable user Guid ID values of
/// all users affected by / included in this
/// schedulemarker.
///
/// So, for example, if it's one user, only that
/// user will be in the arraylist, however if it's
/// a schedulemarker for a dispatchzone then all Guid's for
/// active scheduleable users in that dispatchzone
/// will be in the arraylist.
/// </summary>
public ArrayList ScheduleableUsersMarkerAppliesTo
{
get
{
ArrayList al=new ArrayList();
//Fetch a scheduleable user list if this marker applies to more than one user
UserListScheduleable uls = null;
if(this.mScheduleMarkerSourceType!=ScheduleMarkerSourceTypes.User)
uls = UserListScheduleable.GetList();
switch(this.mScheduleMarkerSourceType)
{
#region Single user?
case ScheduleMarkerSourceTypes.User:
{
al.Add(this.mSourceID);
break;
}
#endregion
#region Region?
case ScheduleMarkerSourceTypes.Regional:
{
//Loop through all active scheduleable users
//if the region ID matches the marker region ID then
//add to list
foreach(UserListScheduleable.UserListScheduleableInfo ui in uls)
{
if(ui.Active && ui.RegionID==this.mSourceID)
{
al.Add(ui.ID);
}
}
break;
}
#endregion
#region DispatchZone?
case ScheduleMarkerSourceTypes.DispatchZone:
{
//Loop through all active scheduleable users
//if the Dispatch zone ID matches the marker zone ID then
//add to list
foreach(UserListScheduleable.UserListScheduleableInfo ui in uls)
{
if(ui.Active && ui.DispatchZoneID==this.mSourceID)
{
al.Add(ui.ID);
}
}
break;
}
#endregion
#region Scheduleable user group?
case ScheduleMarkerSourceTypes.ScheduleableUserGroup:
{
ScheduleableUserGroupUsersList ScheduleMarkerGroup=ScheduleableUserGroupUsersList.GetList(this.mSourceID);
//Loop through all active scheduleable users
//if they are in the ScheduleableUserGroup then
//add to list
foreach(UserListScheduleable.UserListScheduleableInfo ui in uls)
{
if(ui.Active && ScheduleMarkerGroup.Contains(ui.ID))
{
al.Add(ui.ID);
}
}
break;
}
#endregion
#region Global?
case ScheduleMarkerSourceTypes.Global:
{
//Loop through all active scheduleable users
//add to list
foreach(UserListScheduleable.UserListScheduleableInfo ui in uls)
{
if(ui.Active)
{
al.Add(ui.ID);
}
}
break;
}
#endregion
}//end of switch statement
return al;
}
}
/// <summary>
/// Follow up object type
/// This is the type of object that is being followed up on
/// </summary>
public RootObjectTypes FollowType
{
get
{
return mFollowType;
}
set
{
if (bReadOnly)
ThrowSetError();
else
{
if (mFollowType != value)
{
mFollowType = value;
MarkDirty();
}
}
}
}
/// <summary>
/// Follow up object ID
/// ID of the object that is intended to be followed up on.
/// </summary>
public Guid FollowID
{
get
{
return mFollowID;
}
set
{
if (bReadOnly)
ThrowSetError();
else
{
if (mFollowID != value)
{
mFollowID = value;
MarkDirty();
}
}
}
}
/// <summary>
/// Indicates if item can be duplicated or not
/// Item can be duplicated if the current user
/// has write rights to this item and this item
/// is not dirty or new and IsValid
/// </summary>
public bool CanDuplicate
{//added case 1975
get
{
if (!AyaBizUtils.CanWrite(RootObjectTypes.ScheduleMarker)) return false;
if (IsDirty || IsNew || (!IsValid)) return false;
return true;
}
}
/// <summary>
/// Generates a duplicate of this item
/// and returns it.
/// </summary>
/// <returns></returns>
public ScheduleMarker Duplicate()
{//added for case 1975
ScheduleMarker dest = ScheduleMarker.NewItem();
dest.Name = DBUtil.CurrentWorkingDateTime.ToString();
dest.ARGB = ARGB;
dest.dtStartDate = dtStartDate;
dest.dtStopDate = dtStopDate;
dest.FollowID = FollowID;
dest.FollowType = FollowType;
dest.ScheduleMarkerSourceType = ScheduleMarkerSourceType;
dest.SourceID = SourceID;
dest.Notes = Notes;
return dest;
}
/// <summary>
/// Indicates if item is a follow up vs a standard
/// schedule marker
/// </summary>
public bool IsFollowUp
{//added case 1975
get
{
return this.mFollowID != Guid.Empty;
}
}
/// <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.ScheduleMarker")
)
);
}
#endregion
#region System.Object overrides
/// <summary>
///
/// </summary>
/// <returns></returns>
public override string ToString()
{
return "ScheduleMarker" + mID.ToString();
}
/// <summary>
///
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(Object obj)
{
if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false;
ScheduleMarker c=(ScheduleMarker)obj;
return mID==c.mID;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return ("ScheduleMarker"+mID).GetHashCode();
}
#endregion
#region Searching
/// <summary>
/// Returns a search result object based on search terms
/// for the ID specified
/// </summary>
/// <param name="ID"></param>
/// <param name="searchTerms"></param>
/// <returns></returns>
public static SearchResult GetSearchResult(Guid ID, string[]searchTerms)
{
if(AyaBizUtils.Right("Object.ScheduleMarker")<(int)SecurityLevelTypes.ReadOnly)
return new SearchResult();
SearchResult sr=new SearchResult();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
SafeDataReader dr = null;
try
{
dr=DBUtil.GetReaderFromSQLString(
"SELECT aSourceID, aScheduleMarkerSourceType, aCreated, aModified, aCreator, aModifier, aName, " +
"aNotes FROM aScheduleMarker WHERE (aID = @ID)"
,ID);
if(!dr.Read())
return new SearchResult();//DBUtil.ThrowFetchError("SearchResult for ScheduleMarkerID: " + ID.ToString());
//Case 58 weed out markers that are regional and not in your region
Guid SourceID = dr.GetGuid("aSourceID");
ScheduleMarkerSourceTypes ScheduleMarkerSourceType = (ScheduleMarkerSourceTypes)dr.GetInt16("aScheduleMarkerSourceType");
if (ScheduleMarkerSourceType == ScheduleMarkerSourceTypes.Regional)
{
if (!AyaBizUtils.InYourRegion(SourceID)) return new SearchResult();//case 58
}
sr.Description=dr.GetString("aName");
sb.Append(sr.Description);
sb.Append(" ");
sb.Append(dr.GetString("aNotes"));
sr.Created=DBUtil.ToLocal(dr.GetSmartDate("aCreated"));
sr.Modified=DBUtil.ToLocal(dr.GetSmartDate("aModified"));
sr.Creator=dr.GetGuid("aCreator");
sr.Modifier=dr.GetGuid("aModifier");
}
finally
{
if(dr!=null) dr.Close();
}
//Formulate results
ExtractAndRank er = new ExtractAndRank();
er.Process(sb.ToString().Trim(),searchTerms);
sr.Extract=er.Extract;
sr.Rank=er.Ranking;
sr.AncestorRootObjectID=ID;
sr.AncestorRootObjectType=RootObjectTypes.ScheduleMarker;
return sr;
}
#endregion
#region Static methods
/// <summary>
/// Create new ScheduleMarker
/// </summary>
public static ScheduleMarker NewItem()
{
if(AyaBizUtils.Right("Object.ScheduleMarker")>(int)SecurityLevelTypes.ReadOnly)
return new ScheduleMarker();
else
throw new System.Security.SecurityException(
string.Format(
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToCreate"),
LocalizedTextTable.GetLocalizedTextDirect("O.ScheduleMarker")));
}
/// <summary>
/// Fetch existing ScheduleMarker
/// </summary>
/// <param name="_ID">Client Guid</param>
public static ScheduleMarker GetItem(Guid _ID)
{
//case 1967 support new item in grid list
if (_ID == AyaBizUtils.NewObjectGuid)
return NewItem();
if(AyaBizUtils.Right("Object.ScheduleMarker")>(int)SecurityLevelTypes.NoAccess)
return (ScheduleMarker)DataPortal.Fetch(new Criteria(_ID));
else
throw new System.Security.SecurityException(
string.Format(
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToRetrieve"),
LocalizedTextTable.GetLocalizedTextDirect("O.ScheduleMarker")));
}
/// <summary>
/// Delete ScheduleMarker (if clients assigned to this group, should be notified)
/// </summary>
/// <param name="_ID">Client GUID</param>
public static void DeleteItem(Guid _ID)
{
if(AyaBizUtils.Right("Object.ScheduleMarker")>(int)SecurityLevelTypes.ReadWrite)
DataPortal.Delete(new Criteria(_ID));
else
throw new System.Security.SecurityException(
string.Format(
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToDelete"),
LocalizedTextTable.GetLocalizedTextDirect("O.ScheduleMarker")));
}
/// <summary>
/// Guid used for Global sources
/// </summary>
public static Guid ScheduleMarkerGlobalSourceID
{
get
{
return new Guid("{96AB6A7D-CAD7-4786-8911-127C0C4C1175}");
}
}
/// <summary>
/// Retrieve internal ID from name.
///
/// </summary>
/// <param name="Name">Text value</param>
/// <returns>Guid ID value or Guid.Empty if no match</returns>
public static Guid GetIDFromName(string Name)
{
return GuidFetcher.GetItem("ASCHEDULEMARKER", "ANAME", Name);
}
#endregion
#region Shared Notification Message Processor
internal static NotifyMessage GetNotificationMessage(NotifyMessageRequestData d)
{
ScheduleMarker s=ScheduleMarker.GetItem(d.RootObjectID);
string sEventDescription="";
switch (d.EventType)
{
case (int)ScheduleMarkerEvent.Created:
sEventDescription = LocalizedTextTable.GetLocalizedTextDirect("ScheduleMarker.Label.Event.Created", d.Language);
break;
case (int)ScheduleMarkerEvent.PendingAlert:
sEventDescription = LocalizedTextTable.GetLocalizedTextDirect("ScheduleMarker.Label.Event.PendingAlert", d.Language);
break;
}
string sMessage=sEventDescription;
string sSubject=sEventDescription;
NotifyMessage nm=null;
//case 1415
string sStartDate = "";
string sStopDate = "";
if (d.TimeZoneOffset != 0)
{
sStartDate = s.dtStartDate.ToUniversalTime().AddHours(d.TimeZoneOffset).ToString();
sStopDate = s.dtStopDate.ToUniversalTime().AddHours(d.TimeZoneOffset).ToString();
}
else
{
sStartDate = s.dtStartDate.ToString();
sStopDate = s.dtStopDate.ToString();
}
if (d.Format == NotifyDeliveryMessageFormats.Brief)
{
sMessage += ":" + s.Name + " " + sStartDate + " - " + sStopDate;
if (d.MaxCharacters > 0 && sMessage.Length > d.MaxCharacters)
nm = new NotifyMessage("", sMessage.Substring(0, d.MaxCharacters));
else
nm=new NotifyMessage("", sMessage);
}
else
{
sSubject+=": " + s.Name;
sMessage+=": " + s.Name +"\r\n" +
LocalizedTextTable.GetLocalizedTextDirect("ScheduleMarker.Label.StartDate", d.Language) + ": " + sStartDate + "\r\n" +
LocalizedTextTable.GetLocalizedTextDirect("ScheduleMarker.Label.StopDate", d.Language) + ": " + sStopDate + "\r\n" +
LocalizedTextTable.GetLocalizedTextDirect("ScheduleMarker.Label.Notes", d.Language) + ": " + s.Notes;
nm=new NotifyMessage(sSubject,sMessage);
}
return nm;
}
#endregion
#region DAL DATA ACCESS
#region Fetch
///
/// <param name="Criteria"></param>
protected override void DataPortal_Fetch(object Criteria)
{
//set to false to load items initially
bReadOnly=false;
Criteria crit = (Criteria)Criteria;
SafeDataReader dr = null;
try
{
dr=DBUtil.GetReaderFromSQLString("SELECT * FROM aScheduleMarker WHERE aID=@ID;",crit.ID);
if(!dr.Read())
DBUtil.ThrowFetchError("ScheduleMarker ID: " + crit.ID.ToString());
//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");
//ScheduleMarker fields
//Important: use property not internal field
//so that initial broken rule is unbroken on fetch
Name=dr.GetString("aName");
mNotes=dr.GetString("aNotes");
mStartDate=DBUtil.ToLocal(dr.GetSmartDate("aStartDate"));
mStopDate=DBUtil.ToLocal(dr.GetSmartDate("aStopDate"));
mSourceID=dr.GetGuid("aSourceID");
mScheduleMarkerSourceType=(ScheduleMarkerSourceTypes)dr.GetInt16("aScheduleMarkerSourceType");
mARGB=dr.GetInt32("AARGB");
mFollowID = dr.GetGuid("aFollowID");
mFollowType = (RootObjectTypes)dr.GetInt16("aFollowType");
//case 1968
mCompleted = dr.GetBoolean("ACOMPLETED");
}
finally
{
if(dr!=null) dr.Close();
}
MarkOld();
//Get access rights level
bReadOnly=AyaBizUtils.Right("Object.ScheduleMarker")<(int)SecurityLevelTypes.ReadWrite;
}
#endregion fetch
#region Update
/// <summary>
/// Called by DataPortal to delete/add/update data into the database
/// </summary>
protected override void DataPortal_Update()
{
// If not a new record, check if record was modified
//by another user since original retrieval:
if(!IsNew)
DBUtil.CheckSafeToUpdate(this.mModified.Date,this.mID,"aScheduleMarker");
#region Delete
if(IsDeleted)
{
if(!IsNew)
{
//Delete object and child objects
DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM aScheduleMarker WHERE aID = @ID;");
cmDelete.AddInParameter("@ID",DbType.Guid,this.mID);
using (IDbConnection connection = DBUtil.DB.GetConnection())
{
connection.Open();
IDbTransaction transaction = connection.BeginTransaction();
try
{
DBUtil.DB.ExecuteNonQuery(cmDelete, transaction);
DBUtil.RemoveKeywords(transaction,RootObjectTypes.ScheduleMarker,this.mID);
// Commit the transaction
transaction.Commit();
}
catch
{
// Rollback transaction
transaction.Rollback();
throw;
}
finally
{
connection.Close();
}
}
//Remove any events for this object
if (AyaBizUtils.GlobalSettings.UseNotification)//Case 510
{
NotifyEvent.RemoveAllEventsForObject(this.mID);
}
//-----------------------------
}
MarkNew();
return;
}
#endregion
#region Add / Update
//case 3185
if (mStartDate.IsEmpty && mStopDate.IsEmpty)
{
mStartDate = new SmartDate(DBUtil.CurrentWorkingDateTime);
mStopDate = new SmartDate(DBUtil.CurrentWorkingDateTime.AddHours(1));
}
//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 aScheduleMarker (aID, aName, aNotes, aStartDate, " +
"aStopDate, aScheduleMarkerSourceType, aSourceID, " +
"AARGB, aCreated,aModified,aCreator,aModifier, aFollowID, aFollowType, ACOMPLETED) VALUES (@ID,@Name, " +
"@Notes,@StartDate,@StopDate,@ScheduleMarkerSourceType, " +
"@SourceID,@ARGB,@Created,@Modified,@CurrentUserID,@CurrentUserID, @FollowID, @FollowType, @COMPLETED)"
);
else
cm=DBUtil.GetCommandFromSQL(
"UPDATE aScheduleMarker SET aID=@ID, aName=@Name, aNotes=@Notes, " +
"aStartDate=@StartDate, aStopDate=@StopDate, " +
"aScheduleMarkerSourceType=@ScheduleMarkerSourceType, " +
"aSourceID=@SourceID, AARGB=@ARGB, " +
"aModifier=@CurrentUserID, aModified=@Modified, " +
"aFollowID=@FollowID, aFollowType=@FollowType, " +
"ACOMPLETED=@COMPLETED " +
"WHERE " +
"aID=@ID"
);
//ScheduleMarker specific fields
cm.AddInParameter("@ID",DbType.Guid,mID);
cm.AddLargeStringInParameter("@Notes",mNotes);
cm.AddInParameter("@Name",DbType.String, mName);
cm.AddInParameter("@StartDate",DbType.DateTime,DBUtil.ToUTC(mStartDate).DBValue);
cm.AddInParameter("@StopDate",DbType.DateTime,DBUtil.ToUTC(mStopDate).DBValue);
cm.AddInParameter("@SourceID",DbType.Guid,mSourceID);
cm.AddInParameter("@ScheduleMarkerSourceType",DbType.Int16,(int)mScheduleMarkerSourceType);
cm.AddInParameter("@ARGB",DbType.Int32,mARGB);
cm.AddInParameter("@FollowID", DbType.Guid, mFollowID);
cm.AddInParameter("@FollowType", DbType.Int16, (int)mFollowType);
//case 1968
cm.AddInParameter("@COMPLETED", DbType.Boolean, mCompleted);
//Standard fields
cm.AddInParameter("@CurrentUserID",DbType.Guid, CurrentUserID);
cm.AddInParameter("@Created",DbType.DateTime, DBUtil.ToUTC(mCreated).DBValue);
cm.AddInParameter("@Modified",DbType.DateTime, DBUtil.ToUTC(dtModified));
using (IDbConnection connection = DBUtil.DB.GetConnection())
{
connection.Open();
IDbTransaction transaction = connection.BeginTransaction();
try
{
DBUtil.DB.ExecuteNonQuery(cm, transaction);
//Process keywords
DBUtil.ProcessKeywords(transaction,this.mID,RootObjectTypes.ScheduleMarker,IsNew,AyaBizUtils.Break(false,
mName,mNotes));
MarkOld();//db is now synched with object
// Commit the transaction
transaction.Commit();
}
catch
{
// Rollback transaction
transaction.Rollback();
throw;
}
finally
{
connection.Close();
}
//Successful update so
//change modification time to match
this.mModified.Date=dtModified;
//Process events as necessary
if (AyaBizUtils.GlobalSettings.UseNotification)//Case 510
{
//Since a slight change in a schedulemarker could affect and
//change quite a few users, it's safest to simply wipe any
//existing events if this is an update for this object
//and re-do them entirely
if(!this.IsNew) NotifyEvent.RemoveAllEventsForObject(this.mID);
//Let's just be sure at least one person is interested in this event
//this is duplicated in the NotifyEvent.addorupdateevent code
//but worth it to nip in the bud here if not necessary
//since part of this code needs to fetch a lot of data and Process it
bool InterestInCreation = NotifyEventOfInterestFetcher.IsEventInteresting(RootObjectTypes.ScheduleMarker, (int)ScheduleMarkerEvent.Created, Guid.Empty);
bool InterestInPendingAlert = NotifyEventOfInterestFetcher.IsEventInteresting(RootObjectTypes.ScheduleMarker, (int)ScheduleMarkerEvent.PendingAlert, Guid.Empty);
if(InterestInCreation || InterestInPendingAlert)
{
//Add a notify event for each user this sched marker applies to
ArrayList al=ScheduleableUsersMarkerAppliesTo;
for(int x=0;x<al.Count;x++)
{
if(InterestInCreation)
{
//Created event
NotifyEvent.AddOrUpdateEvent(RootObjectTypes.ScheduleMarker,this.mID,(int)ScheduleMarkerEvent.Created,
(Guid)al[x],new SmartDate(),Guid.Empty);
}
if(InterestInPendingAlert)
{
//PendingAlert event
NotifyEvent.AddOrUpdateEvent(RootObjectTypes.ScheduleMarker,this.mID,(int)ScheduleMarkerEvent.PendingAlert,
(Guid)al[x],this.mStartDate,Guid.Empty);
}
}
}
}
}
#endregion
}
#endregion update
#region Delete
/// <summary>
/// Remove a ScheduleMarker record from the database
/// </summary>
/// <param name="Criteria"></param>
protected override void DataPortal_Delete(object Criteria)
{
Criteria crit = (Criteria)Criteria;
//Delete object and child objects
DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM aScheduleMarker WHERE aID = @ID;");
cmDelete.AddInParameter("@ID",DbType.Guid,crit.ID);
using (IDbConnection connection = DBUtil.DB.GetConnection())
{
connection.Open();
IDbTransaction transaction = connection.BeginTransaction();
try
{
DBUtil.DB.ExecuteNonQuery(cmDelete, transaction);
DBUtil.RemoveKeywords(transaction,RootObjectTypes.ScheduleMarker,crit.ID);
// Commit the transaction
transaction.Commit();
}
catch
{
// Rollback transaction
transaction.Rollback();
throw;
}
finally
{
connection.Close();
}
//Remove any events for this object
if (AyaBizUtils.GlobalSettings.UseNotification)//Case 510
{
NotifyEvent.RemoveAllEventsForObject(crit.ID);
}
}
}
#endregion delete
#endregion
#region Override IsValid / IsDirty
//Override base class version if there are child objects
/*
public override bool IsValid
{
get
{
return base.IsValid && ChildItem.IsValid;
}
}
public override bool IsDirty
{
get
{
return base.IsDirty || ChildItem.IsDirty;
}
}
*/
#endregion
#region criteria
/// <summary>
/// Criteria for identifying existing object
/// </summary>
[Serializable]
private class Criteria
{
public Guid ID;
public Criteria(Guid _ID)
{
ID=_ID;
}
}
#endregion
}//end ScheduleMarker
#region Notification events
#pragma warning disable 1591
public enum ScheduleMarkerEvent : int
{
[Description("LT:ScheduleMarker.Label.Event.Created")]
Created = 1,
[Description("LT:ScheduleMarker.Label.Event.PendingAlert")]
PendingAlert = 256
}//end events
#pragma warning restore 1591
#endregion
}//end namespace GZTW.AyaNova.BLL