using System; using System.Windows; using System.ComponentModel; using System.Runtime.CompilerServices; using sharpknife.ViewModel; using System.Reflection; using sharpknife.Data; using System.Globalization; using sharpknife.Commands; using System.Linq; using System.Diagnostics; using System.Data.Objects.DataClasses; namespace sharpknife { public partial class ScheduledCommand { public enum RecurrenceCodes { Minute = 2, Day = 5, Week = 10, Month = 15 } public RecurrenceCodes Recurrence { get { return (RecurrenceCodes)RecurrenceCode; } set { RecurrenceCode = (int)value; OnPropertyChanged("Recurrence"); } } private void UpdateRecurrence() { switch ((RecurrenceCodes)RecurrenceCode) { case RecurrenceCodes.Minute: SchedulationRule.Rule = new MinuteRule(); break; case RecurrenceCodes.Day: SchedulationRule.Rule = new DayRule(); break; case RecurrenceCodes.Week: SchedulationRule.Rule = new WeekRule(); break; case RecurrenceCodes.Month: SchedulationRule.Rule = new MonthRule(); break; } } public enum StatusCodes { Enabled = 0, Disabled = 1 } public StatusCodes Status { get { return (StatusCodes)StatusCode; } set { StatusCode = (int)value; OnPropertyChanged("Status"); OnPropertyChanged("IsEnabled"); } } public bool IsEnabled { get { return Status == StatusCodes.Enabled; } } public SchedulationRules SchedulationRule { get; set; } public Command Command { get; set; } public static Command CreateCommand(string commandName) { return (Command)Assembly.GetExecutingAssembly().CreateInstance(commandName); } public static Command CreateCommand(string commandName, object[] args) { return (Command)Assembly.GetExecutingAssembly().CreateInstance( commandName, true, BindingFlags.CreateInstance, null, args, CultureInfo.InvariantCulture, null); } public ScheduledCommand() { Status = StatusCodes.Enabled; SchedulationRule = new SchedulationRules(); NextExecution = DateTime.Now; this.PropertyChanged += new PropertyChangedEventHandler(ScheduledCommand_PropertyChanged); } void ScheduledCommand_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "RecurrenceCode") { UpdateRecurrence(); } else if (e.PropertyName == "StartAtHH") { SchedulationRule.StartAtHH = StartAtHH.HasValue ? StartAtHH.Value : 0; } else if (e.PropertyName == "StartAtMM") { SchedulationRule.StartAtMM = StartAtMM.HasValue ? StartAtMM.Value : 0; } else if (e.PropertyName == "RecurEvery") { if (SchedulationRule.Rule != null) { SchedulationRule.Rule.RecurEvery = RecurEvery.HasValue ? RecurEvery.Value : 0; } } } public void Save() { using (databaseEntities context = new databaseEntities()) { ScheduledCommand command = context.ScheduledCommand.SingleOrDefault(c => c.ScheduledCommandId == this.ScheduledCommandId); if (command == null) { ScheduledCommandId = Guid.NewGuid(); CreatedOn = DateTime.Now; ModifiedOn = DateTime.Now; context.ScheduledCommand.AddObject((ScheduledCommand)this); } else { context.ScheduledCommand.ApplyCurrentValues(this); } context.SaveChanges(); } } } }