private static void AddSequence(DateTime start, EventCommandContext context)
        {
            DateTime tmp = start;

             while (true)
             {
            if (
               string.Compare(tmp.DayOfWeek.ToString(), context.Schedule.DaysOfWeek.ToString(), true) ==
               0)
            {
               if (context.Schedule.Sequence != Sequence.None &&
                   context.Schedule.Sequence != Sequence.First)
               {
                  tmp = tmp.AddDays(((int) context.Schedule.Sequence) * 7);
               }

               if (tmp >= Utilities.StringToDate(context.Schedule.StartDate))
               {
                  AddEvent(tmp, context);
               }

               break;
            }

            tmp = tmp.AddDays(1);
             }
        }
        protected override void Execute(EventCommandContext context)
        {
            if (context.EventList == null || context.Event == null)
             {
            Log.Error("Calendar Module: command - cannot find calendar", null);
            return;
             }

             if (string.IsNullOrEmpty(context.Event.EndDate))
             {
            context.Event.EndDate = context.Event.StartDate;
             }

             if (string.IsNullOrEmpty(context.Event.EndTime))
             {
            context.Event.EndTime = Utilities.GetDefaultEndTime(context.Event.StartTime);
             }

             if (context.Schedule == null)
             {
            Create(context.Event, context.EventList, context.Branch);
             }
             else
             {
            CalendarActions.CreateSchedule(context.Event, context.Schedule, context.Options);
             }
        }
        private static void AddYearly(DateTime start, EventCommandContext context)
        {
            if (context.Event == null || context.Schedule == null ||
             start > Utilities.StringToDate(context.Schedule.EndDate) ||
             context.Schedule.Frequency == 0 || context.EventList == null)
             {
            return;
             }

             try
             {
            var tmp = new DateTime(start.Year, (int) context.Schedule.Month,
                                   context.Schedule.Frequency);

            if (tmp >= Utilities.StringToDate(context.Schedule.StartDate) &&
                tmp <= Utilities.StringToDate(context.Schedule.EndDate))
            {
               AddEvent(tmp, context);
            }
             }
             catch
             {
            Log.Warn("Calendar: cannot add a yearly recurrence event:" + context.Event.Name, context);
             }

             AddYearly(start.AddYears(1), context);
        }
 public static void CreateRecurrence(Event ev, Schedule schedule, Options options)
 {
     Message message = Message.Parse(ev,
                                  "recurrence:" +
                                  schedule.Recurrence.ToString().ToLower());
      var context = new EventCommandContext(ev, schedule, options);
      Dispatcher.Dispatch(message, context);
 }
        protected static void AddEvent(DateTime date, EventCommandContext context)
        {
            if ((date > Utilities.StringToDate(context.Schedule.EndDate)) || date < Utilities.StringToDate(context.Schedule.StartDate))
                {
                    return;
                }

                context.Event.StartDate = Utilities.NormalizeDate(date);
                context.Event.EndDate = Utilities.NormalizeDate(date);
                context.Event.ScheduleID = context.Schedule.ID;
                context.EventList.AddEvent(context.Event, context.Branch);
        }
        private static void addWeekly(EventCommandContext context, DateTime start)
        {
            if (context.Event != null && context.Schedule != null && start <= Utilities.StringToDate(context.Schedule.EndDate) &&
             context.Schedule.Frequency > 0 && context.EventList != null)
             {
            addByDayOfWeek(start, context);

            start = start.AddDays(context.Schedule.Frequency * 7);

            addWeekly(context, start);
             }
        }
        private static void addDaily(DateTime start, EventCommandContext context)
        {
            if (((context.Event != null) && (context.Schedule != null)) &&
             (((start <= Utilities.StringToDate(context.Schedule.EndDate)) &&
               (context.Schedule.Frequency != 0)) && (context.EventList != null)))
             {
            context.Event.StartDate = Utilities.NormalizeDate(start);
            context.Event.EndDate = Utilities.NormalizeDate(start);
            context.Event.ScheduleID = context.Schedule.ID;

            context.EventList.AddEvent(context.Event, StaticSettings.EventBranch);

            addDaily(start.AddDays(context.Schedule.Frequency), context);
             }
        }
        private static void addByDayOfWeek(DateTime start, EventCommandContext context)
        {
            int number = 0;
             foreach (DaysCollection value in Enum.GetValues(typeof(DaysCollection)))
             {
            if (value != 0) // 0 is 'None' in DaysCollection enum
            {
                ++number;

                if ((value & context.Schedule.DaysOfWeek) == value)
               {
                  AddEvent(start.AddDays(number), context);
               }
            }
             }
        }
        protected override void Execute(EventCommandContext context)
        {
            if (context.Schedule != null && context.EventListManager != null)
             {
            if (context.Schedule.IsNew)
            {
               context.Schedule.StartDate = context.Event.StartDate;
               context.Schedule.EndDate = context.Event.EndDate;

               Create(context.Schedule, context.Event, context.EventListManager);
            }

            CalendarActions.CreateRecurrence(context.Event, context.Schedule, context.Options);

            DeleteEmptyScheduler(context.Schedule);
             }
        }
        private static void AddMonthly(DateTime start, EventCommandContext context)
        {
            if (context.Event == null || context.Schedule == null ||
             start > Utilities.StringToDate(context.Schedule.EndDate) ||
             context.Schedule.Frequency == 0 || context.EventList == null)
             {
            return;
             }

             switch (context.Schedule.Sequence)
             {
            case Sequence.First:
            case Sequence.Second:
            case Sequence.Third:
            case Sequence.Fourth:
               AddSequence(start, context);
               break;

            case Sequence.Last:

               DateTime tmp = start.AddMonths(1);
               tmp = tmp.Subtract(TimeSpan.FromDays(1));

               while (true)
               {
                  if (
                     string.Compare(tmp.DayOfWeek.ToString(), context.Schedule.DaysOfWeek.ToString(),
                                    true) == 0)
                  {
                     AddEvent(tmp, context);
                     break;
                  }

                  tmp = tmp.Subtract(TimeSpan.FromDays(1));
               }

               break;
            default:
               break;
             }

             AddMonthly(start.AddMonths(context.Schedule.Frequency), context);
        }
 protected override void Execute(EventCommandContext context)
 {
     addDaily(Utilities.StringToDate(context.Schedule.StartDate), context);
 }
 protected override void Execute(EventCommandContext context)
 {
     AddMonthly(Utilities.FirstMonthDay(Utilities.StringToDate(context.Schedule.StartDate)),
             context);
 }
 public static void CreateSchedule(Event ev, Schedule schedule, Options options)
 {
     Message message = Message.Parse(ev, "schedule:create");
      var context = new EventCommandContext(ev, schedule, options);
      Dispatcher.Dispatch(message, context);
 }
 public static void CreateEvent(Event ev, Schedule schedule, Options options, BranchItem branch)
 {
     Message message = Message.Parse(ev, "event:create");
      var calendarContext = new EventCommandContext(ev, schedule, options, branch);
      Dispatcher.Dispatch(message, calendarContext);
 }
 protected abstract void Execute(EventCommandContext context);
 protected override void Execute(EventCommandContext context)
 {
     addWeekly(context, Utilities.FirstCalendarDay(Utilities.StringToDate(context.Schedule.StartDate)));
 }