private void FormToModel() { // // Weekly // var days = new[] { new { Checked = this.WeeklyDaysChecked[0].Value, DayOfWeek = DayOfWeek.Monday }, new { Checked = this.WeeklyDaysChecked[1].Value, DayOfWeek = DayOfWeek.Tuesday }, new { Checked = this.WeeklyDaysChecked[2].Value, DayOfWeek = DayOfWeek.Wednesday }, new { Checked = this.WeeklyDaysChecked[3].Value, DayOfWeek = DayOfWeek.Thursday }, new { Checked = this.WeeklyDaysChecked[4].Value, DayOfWeek = DayOfWeek.Friday }, new { Checked = this.WeeklyDaysChecked[5].Value, DayOfWeek = DayOfWeek.Saturday }, new { Checked = this.WeeklyDaysChecked[6].Value, DayOfWeek = DayOfWeek.Sunday }, }; // Remove any saved days. this.Schedule.OccursAtDaysOfWeek.Clear(); // Re-add only the currently selected days. foreach (var day in days) { if (!day.Checked) { continue; } var obj = new Models.PlanScheduleDayOfWeek { Schedule = this.Schedule, DayOfWeek = day.DayOfWeek }; this.Schedule.OccursAtDaysOfWeek.Add(obj); } }
private Trigger[] BuildTriggers(Models.ISchedulablePlan plan) { List <Trigger> triggers = new List <Trigger>(); Models.PlanSchedule schedule = plan.Schedule; switch (schedule.ScheduleType) { case Models.ScheduleTypeEnum.RUN_MANUALLY: { break; } case Models.ScheduleTypeEnum.SPECIFIC: { DateTime?optional = schedule.OccursSpecificallyAt; if (!optional.HasValue) { break; } DateTime whenToStart = optional.Value; Trigger tr = Trigger.CreateTrigger(TaskTriggerType.Time); // When to start? tr.StartBoundary = whenToStart.ToLocalTime(); triggers.Add(tr); break; } case Models.ScheduleTypeEnum.RECURRING: { if (!schedule.RecurrencyFrequencyType.HasValue) { break; } Trigger tr = null; switch (schedule.RecurrencyFrequencyType.Value) { case Models.FrequencyTypeEnum.DAILY: { tr = Trigger.CreateTrigger(TaskTriggerType.Daily); if (schedule.IsRecurrencyDailyFrequencySpecific) { // Repetition - Occurs every day tr.Repetition.Interval = TimeSpan.FromDays(1); } break; } case Models.FrequencyTypeEnum.WEEKLY: { if (schedule.OccursAtDaysOfWeek == null || schedule.OccursAtDaysOfWeek.Count == 0) { break; } tr = Trigger.CreateTrigger(TaskTriggerType.Weekly); WeeklyTrigger wt = tr as WeeklyTrigger; // IMPORTANT: The default constructed `WeeklyTrigger` sets Sunday. wt.DaysOfWeek = 0; Models.PlanScheduleDayOfWeek matchDay = null; matchDay = schedule.OccursAtDaysOfWeek.SingleOrDefault(p => p.DayOfWeek == DayOfWeek.Monday); if (matchDay != null) { wt.DaysOfWeek |= DaysOfTheWeek.Monday; } matchDay = schedule.OccursAtDaysOfWeek.SingleOrDefault(p => p.DayOfWeek == DayOfWeek.Tuesday); if (matchDay != null) { wt.DaysOfWeek |= DaysOfTheWeek.Tuesday; } matchDay = schedule.OccursAtDaysOfWeek.SingleOrDefault(p => p.DayOfWeek == DayOfWeek.Wednesday); if (matchDay != null) { wt.DaysOfWeek |= DaysOfTheWeek.Wednesday; } matchDay = schedule.OccursAtDaysOfWeek.SingleOrDefault(p => p.DayOfWeek == DayOfWeek.Thursday); if (matchDay != null) { wt.DaysOfWeek |= DaysOfTheWeek.Thursday; } matchDay = schedule.OccursAtDaysOfWeek.SingleOrDefault(p => p.DayOfWeek == DayOfWeek.Friday); if (matchDay != null) { wt.DaysOfWeek |= DaysOfTheWeek.Friday; } matchDay = schedule.OccursAtDaysOfWeek.SingleOrDefault(p => p.DayOfWeek == DayOfWeek.Saturday); if (matchDay != null) { wt.DaysOfWeek |= DaysOfTheWeek.Saturday; } matchDay = schedule.OccursAtDaysOfWeek.SingleOrDefault(p => p.DayOfWeek == DayOfWeek.Sunday); if (matchDay != null) { wt.DaysOfWeek |= DaysOfTheWeek.Sunday; } break; } case Models.FrequencyTypeEnum.MONTHLY: { if (!schedule.MonthlyOccurrenceType.HasValue || !schedule.OccursMonthlyAtDayOfWeek.HasValue) { break; } tr = Trigger.CreateTrigger(TaskTriggerType.MonthlyDOW); MonthlyDOWTrigger mt = tr as MonthlyDOWTrigger; switch (schedule.MonthlyOccurrenceType.Value) { case Models.MonthlyOccurrenceTypeEnum.FIRST: mt.WeeksOfMonth = WhichWeek.FirstWeek; break; case Models.MonthlyOccurrenceTypeEnum.SECOND: mt.WeeksOfMonth = WhichWeek.SecondWeek; break; case Models.MonthlyOccurrenceTypeEnum.THIRD: mt.WeeksOfMonth = WhichWeek.ThirdWeek; break; case Models.MonthlyOccurrenceTypeEnum.FOURTH: mt.WeeksOfMonth = WhichWeek.FourthWeek; break; case Models.MonthlyOccurrenceTypeEnum.PENULTIMATE: mt.WeeksOfMonth = WhichWeek.ThirdWeek; break; case Models.MonthlyOccurrenceTypeEnum.LAST: mt.WeeksOfMonth = WhichWeek.LastWeek; break; } switch (schedule.OccursMonthlyAtDayOfWeek.Value) { case DayOfWeek.Monday: mt.DaysOfWeek = DaysOfTheWeek.Monday; break; case DayOfWeek.Tuesday: mt.DaysOfWeek = DaysOfTheWeek.Tuesday; break; case DayOfWeek.Wednesday: mt.DaysOfWeek = DaysOfTheWeek.Wednesday; break; case DayOfWeek.Thursday: mt.DaysOfWeek = DaysOfTheWeek.Thursday; break; case DayOfWeek.Friday: mt.DaysOfWeek = DaysOfTheWeek.Friday; break; case DayOfWeek.Saturday: mt.DaysOfWeek = DaysOfTheWeek.Saturday; break; case DayOfWeek.Sunday: mt.DaysOfWeek = DaysOfTheWeek.Sunday; break; } break; } case Models.FrequencyTypeEnum.DAY_OF_MONTH: { if (!schedule.OccursAtDayOfMonth.HasValue) { break; } tr = Trigger.CreateTrigger(TaskTriggerType.Monthly); MonthlyTrigger mt = tr as MonthlyTrigger; // // TODO: What happens if the specified day is >=29 and we are in February? // mt.DaysOfMonth = new int[] { schedule.OccursAtDayOfMonth.Value }; break; } } if (tr == null) { break; } // When to start? DateTime now = DateTime.UtcNow; if (schedule.IsRecurrencyDailyFrequencySpecific) { TimeSpan?optional = schedule.RecurrencySpecificallyAtTime; if (!optional.HasValue) { break; } TimeSpan time = optional.Value; tr.StartBoundary = new DateTime(now.Year, now.Month, now.Day, time.Hours, time.Minutes, time.Seconds); } else { tr.StartBoundary = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0); // Start of day. } // Repetition - Occurs every interval if (!schedule.IsRecurrencyDailyFrequencySpecific) { switch (schedule.RecurrencyTimeUnit.Value) { case Models.TimeUnitEnum.HOURS: tr.Repetition.Interval = TimeSpan.FromHours(schedule.RecurrencyTimeInterval.Value); break; case Models.TimeUnitEnum.MINUTES: tr.Repetition.Interval = TimeSpan.FromMinutes(schedule.RecurrencyTimeInterval.Value); break; } } // Window limits if (!schedule.IsRecurrencyDailyFrequencySpecific) { if (schedule.RecurrencyWindowStartsAtTime.HasValue && schedule.RecurrencyWindowEndsAtTime.HasValue) { tr.Repetition.StopAtDurationEnd = false; TimeSpan window = schedule.RecurrencyWindowEndsAtTime.Value - schedule.RecurrencyWindowStartsAtTime.Value; tr.Repetition.Duration = window; //tr.ExecutionTimeLimit = window; } } triggers.Add(tr); break; } } if (triggers.Count == 0) { Warn("No task was created for {0}", BuildTaskName(plan)); } return(triggers.ToArray()); }