/////////////////////////////////////////////////////////// // EventWindowSet.cs // Implementation of Class EventWindowSet // Class type: Utility (biz object layer) // Created on: 07-Oct-2005 // Object design: John // Coded: John 07-Oct-2005 /////////////////////////////////////////////////////////// using System; using System.Collections; namespace GZTW.AyaNova.BLL { #pragma warning disable 1591 /// /// Manages a set of event windows for a week /// Serializable for easy saving to database / file etc /// Easily confirms if a given date/time is within an allowable /// window or not /// [Serializable] public class EventWindowSet { //Any time or day is allowed //this allows a quicker short circuit //comparison private bool _AnyTime; private bool _IsDirty; //An array for each day of the week's events private EventWindow[] _Events; /// /// An event window structure /// this is added to an array of structures to cover a week /// of day windows /// [Serializable] public struct EventWindow { internal bool _AnyTimeOfDay; internal bool _Active; //Only time portion is used internal DateTime _StartTime; internal DateTime _EndTime; public bool AnyTimeOfDay{get{return _AnyTimeOfDay;}} public bool Active{get{return _Active;}} public DateTime StartTime{get{return _StartTime;}} public DateTime EndTime{get{return _EndTime;}} } public EventWindowSet() { _Events=new EventWindow[7]; System.DateTime dtStartDefault=new DateTime(1968,3,12,9,0,0); System.DateTime dtEndDefault=new DateTime(1968,3,12,17,0,0); //Initialize //0==sunday 6==Saturday for(int x=0;x<7;x++) { _Events[x]._StartTime=dtStartDefault; _Events[x]._EndTime=dtEndDefault; _Events[x]._AnyTimeOfDay=false; _Events[x]._Active=true; } _AnyTime=true; _IsDirty=false; } public bool IsValid { get { return true; } } public bool IsDirty { get { return _IsDirty; } } public bool AnyTime { get { return _AnyTime; } set { _AnyTime=value; _IsDirty=true; } } public EventWindow[] Events { get { return _Events; } } /// /// Sets an event for day indicated. /// /// int value of DayOfWeek enum (0=Sunday...6=Saturday) /// true=deliver any time of day if active /// True=day is enabled for deliver, false=no delivery on this day /// Starting time window for delivery (time only is used date is irrelevant) /// Ending time window for delivery (time only is used date is irrelevant) public void SetEvent(int Day, bool AnyTimeOfDay, bool Active, DateTime StartTime, DateTime EndTime) { _IsDirty=true; _Events[Day]._AnyTimeOfDay=AnyTimeOfDay; _Events[Day]._Active=Active; _Events[Day]._StartTime=StartTime; _Events[Day]._EndTime=EndTime; } /// /// Returns true if a given date/time is within /// an event window. /// /// False if not. /// /// /// public bool InEventWindow(System.DateTime dtValue) { //Short circuit tests... if(this._AnyTime) return true; int nDay=(int)dtValue.DayOfWeek; //Not an active day anyway? if(!_Events[nDay]._Active) return false; //Any time of day ok? if(_Events[nDay]._AnyTimeOfDay) return true; //ok, it's not a simple one, so let's compare times... double dbCompareValue=dtValue.TimeOfDay.TotalMinutes; //Is it in the window? if(dbCompareValue >= _Events[nDay]._StartTime.TimeOfDay.TotalMinutes && dbCompareValue <= _Events[nDay]._EndTime.TimeOfDay.TotalMinutes) return true; //Guess not return false; } //------------------------------------------------------ }// End of class #pragma warning restore 1591 }//End of namespace