public WorkItem CreateNonRecurringWorkItem(Outlook.AppointmentItem item) { var workItem = new WorkItem(); workItem.Subject = item.Subject; workItem.Location = item.Location; workItem.Body = item.Body; workItem.OutlookEntryId = item.EntryID; workItem.StartDateTime = item.Start; workItem.EndDateTime = item.End; workItem.AllDayEvent = item.AllDayEvent; workItem.Duration = item.Duration / 60; workItem.WorkItemType = WorkItemType.Appointment; workItem.Reminder = Reminder.Driving; workItem.Origin = this.CurrentUser.BaseLocation; workItem.isRecurring = false; workItem.CreatedByUserId = this.CurrentUser.UserId; workItem.UpdatedByUserId = this.CurrentUser.UserId; return workItem; }
/// <summary> /// Imports the items in Outlook calendar folder of current user /// </summary> public ServiceResult<OutlookItemImportServiceResult> ImportOutlookCalendarItems() { Outlook.Application outlookApp = null; Outlook.NameSpace mapiNamespace = null; Outlook.MAPIFolder CalendarFolder = null; Outlook.Items outlookCalendarItems = null; var messages = new List<Message>(); var countOfFailedReminderSchedules = 0; // try to initialize Outlook API and log on try { outlookApp = new Outlook.Application(); mapiNamespace = outlookApp.GetNamespace("MAPI"); CalendarFolder = mapiNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar); mapiNamespace.Logon(Missing.Value, Missing.Value, true, true); } catch (Exception ex) { messages.Add(new Message() { Text = string.Format("Neparedzēta kļūda. ", ex.Message), Severity = MessageSeverity.Error }); } //build a filter from user inputs String filter = String.Format("([Start] >= '{0:g}') AND ([End] <= '{1:g}')", IntervalStart, IntervalEnd); //get the filtered Outlook items including the recurring items and sort them to expand the recurrences automatically outlookCalendarItems = CalendarFolder.Items.Restrict(filter); outlookCalendarItems.Sort("[Start]"); outlookCalendarItems.IncludeRecurrences = true; //if there are no items in the calendar folder within the specified interval, return and notify the user if (outlookCalendarItems.GetFirst() == null) { var message = "Norādītajā laika periodā Jūsu Outlook kalendārā netika atrasts neviens uzdevums"; return new ServiceResult<OutlookItemImportServiceResult>() { Data = OutlookItemImportServiceResult.NotImported, Messages = new Message[] { new Message() { Text = message, Severity = MessageSeverity.Info } } }; } //iterate through each returned Outlook calendar item and process it foreach (Outlook.AppointmentItem item in outlookCalendarItems) { var existingWorkItem = this.dataContext.WorkItems.Where(o => o.OutlookEntryId != null && o.OutlookEntryId == item.EntryID).FirstOrDefault(); //if item is not recurring, create a new work item or update an exisiting one if (!item.IsRecurring) { if (existingWorkItem == null) { var workItem = CreateNonRecurringWorkItem(item); this.dataContext.WorkItems.Add(workItem); countOfFailedReminderSchedules += ScheduleReminder(workItem); } else { if (existingWorkItem.UpdatedAt <= item.LastModificationTime) { UpdateNonRecurringWorkItem(existingWorkItem, item); countOfFailedReminderSchedules += ScheduleReminder(existingWorkItem); } } this.dataContext.SaveChanges(); } else if (item.IsRecurring) { Outlook.RecurrencePattern recurrencePattern = item.GetRecurrencePattern(); RecurrenceType recurrenceType = 0; //Get all the exceptions in the series of this recurring item, e.g. items which belong to a series of occurrences, but whose properties have been changed List<Outlook.Exception> exceptions = new List<Outlook.Exception>(); foreach (Outlook.Exception exception in recurrencePattern.Exceptions) { exceptions.Add(exception); } int recurTypeId = (int)recurrencePattern.RecurrenceType; bool hasEndDate; if (recurrencePattern.NoEndDate == true) hasEndDate = false; else hasEndDate = true; //determine the recurrence type of the item switch (recurTypeId) { case 0: recurrenceType = RecurrenceType.Daily; break; case 1: recurrenceType = RecurrenceType.Weekly; break; case 2: recurrenceType = RecurrenceType.Monthly; break; case 3: recurrenceType = RecurrenceType.MonthNth; break; case 4: recurrenceType = RecurrenceType.Yearly; break; case 6: recurrenceType = RecurrenceType.YearNth; break; default: break; } if (existingWorkItem == null) { //Create a new work item that will act as a parent for all of its recurring items var workItem = new WorkItem(); //if recurrence pattern has end date we save it, //else we assume the end date is the end of the year to avoid large data sets if (hasEndDate == true) workItem.EndDateTime = recurrencePattern.PatternEndDate; else workItem.EndDateTime = new DateTime(DateTime.Now.Year, 12, 31); workItem.Subject = item.Parent.Subject; workItem.Location = item.Parent.Location; workItem.Body = item.Parent.Body; workItem.OutlookEntryId = item.Parent.EntryID; workItem.StartDateTime = recurrencePattern.PatternStartDate; workItem.Duration = item.Parent.Duration / 60; workItem.WorkItemType = WorkItemType.Appointment; workItem.isRecurring = true; //add the recurrence pattern workItem.RecurrencePattern = new WIRecurrencePattern { Interval = recurrencePattern.Interval, DayOfWeekMask = (DayOfWeekMask)Enum.ToObject(typeof(DayOfWeekMask), recurrencePattern.DayOfWeekMask), DayOfMonth = recurrencePattern.DayOfMonth, MonthOfYear = (MonthOfYear)Enum.ToObject(typeof(MonthOfYear), recurrencePattern.MonthOfYear), Instance = (Instance)Enum.ToObject(typeof(Instance), recurrencePattern.Instance) }; //add the recurring item workItem.RecurringItems = new List<RecurringItem>(); workItem.RecurringItems.Add(new RecurringItem { OriginalDate = item.Start, Start = item.Start, End = item.End, Duration = (item.End - item.Start).TotalHours, Origin = this.CurrentUser.BaseLocation, Subject = item.Subject, Body = item.Body, Location = item.Location, UpdatedAt = DateTime.Now }); countOfFailedReminderSchedules += ScheduleReminder(workItem, workItem.RecurringItems.FirstOrDefault()); workItem.RecurrenceType = recurrenceType; workItem.CreatedByUserId = this.CurrentUser.UserId; workItem.UpdatedByUserId = this.CurrentUser.UserId; this.dataContext.WorkItems.Add(workItem); this.dataContext.SaveChanges(); } else { //Check if recurrence pattern has not changed var existingPattern = existingWorkItem.RecurrencePattern; int mismatch = 0; if (existingPattern.Interval != recurrencePattern.Interval) mismatch = 1; if ((int)existingPattern.DayOfWeekMask != (int)recurrencePattern.DayOfWeekMask) mismatch = 1; if ((int)existingPattern.Instance != recurrencePattern.Instance) mismatch = 1; if (existingPattern.DayOfMonth != recurrencePattern.DayOfMonth) mismatch = 1; if ((int)existingPattern.MonthOfYear != recurrencePattern.MonthOfYear) mismatch = 1; if (mismatch == 1) { //if the pattern has changed delete all of the old recurring items, save the new pattern and asociate it with the work item foreach (var recurringItem in existingWorkItem.RecurringItems.ToList()) { this.dataContext.RecurringItems.Remove(recurringItem); var jobId = this.scheduler.GetJobId(existingWorkItem, recurringItem); scheduler.RemoveReminder(jobId); } this.dataContext.WIRecurrencePatterns.Remove(existingPattern); existingWorkItem.RecurrencePattern = new WIRecurrencePattern { Interval = recurrencePattern.Interval, DayOfWeekMask = (DayOfWeekMask)Enum.ToObject(typeof(DayOfWeekMask), recurrencePattern.MonthOfYear), DayOfMonth = recurrencePattern.DayOfMonth, MonthOfYear = (MonthOfYear)Enum.ToObject(typeof(MonthOfYear), recurrencePattern.MonthOfYear), Instance = (Instance)Enum.ToObject(typeof(Instance), recurrencePattern.MonthOfYear) }; } else { //if pattern hasn`t changed maybe the time span of the pattern has changed, if so, update the datetime values and remove unnecessary recurring items if (recurrencePattern.PatternStartDate != existingWorkItem.StartDateTime || recurrencePattern.PatternEndDate != existingWorkItem.EndDateTime) { foreach (var recurringItem in existingWorkItem.RecurringItems .Where(o => o.Start < recurrencePattern.PatternStartDate || o.End > recurrencePattern.PatternEndDate) .ToList()) { this.dataContext.RecurringItems.Remove(recurringItem); var jobId = this.scheduler.GetJobId(existingWorkItem, recurringItem); scheduler.RemoveReminder(jobId); } existingWorkItem.StartDateTime = recurrencePattern.PatternStartDate; existingWorkItem.StartDateTime = recurrencePattern.PatternEndDate; } } // we only need to look at the exceptions that are not deleted var nonDeletedException = exceptions.Where(o => o.Deleted == true).ToList(); var exception = nonDeletedException.Find(o => o.AppointmentItem.Start == item.Start); if (exception != null) { var existingRecurringItem = this.dataContext.RecurringItems.Where(o => o.OriginalDate == exception.OriginalDate).FirstOrDefault(); if (existingRecurringItem != null) { UpdateRecurringItem(existingRecurringItem, item); countOfFailedReminderSchedules += ScheduleReminder(existingRecurringItem.WorkItem, existingRecurringItem); } else { existingWorkItem.RecurringItems.Add(new RecurringItem { OriginalDate = item.Start, Exception = true, Start = item.Start, End = item.End, Subject = item.Subject, Body = item.Body, Location = item.Location, UpdatedAt = DateTime.Now }); countOfFailedReminderSchedules += ScheduleReminder(existingWorkItem, existingWorkItem.RecurringItems.FirstOrDefault()); } } else { var existingRecurringItem = existingWorkItem.RecurringItems.Where(o => o.OriginalDate == item.Start).FirstOrDefault(); if (existingRecurringItem == null) { existingWorkItem.RecurringItems.Add(new RecurringItem { OriginalDate = item.Start, Exception = false, Start = item.Start, End = item.End, Subject = item.Subject, Body = item.Body, Location = item.Location, UpdatedAt = DateTime.Now }); countOfFailedReminderSchedules += ScheduleReminder(existingWorkItem, existingWorkItem.RecurringItems.FirstOrDefault()); } else { UpdateRecurringItem(existingRecurringItem, item); countOfFailedReminderSchedules += ScheduleReminder(existingRecurringItem.WorkItem, existingRecurringItem); } } this.dataContext.SaveChanges(); } } } //Log off mapiNamespace.Logoff(); if (countOfFailedReminderSchedules > 0) { messages.Add(new Message() { Text = string.Format("{0} no importētajiem uzdevumiem netika ieplānoti atgādinājumi, jo šajā procesā rādās kļūdas", countOfFailedReminderSchedules.ToString()), Severity = MessageSeverity.Warning }); } var result = new ServiceResult<OutlookItemImportServiceResult>(); result.Messages = messages.ToArray(); if (messages.Any(m => m.Severity == MessageSeverity.Error)) result.Data = OutlookItemImportServiceResult.Error; else if (messages.Any(m => m.Severity == MessageSeverity.Warning)) result.Data = OutlookItemImportServiceResult.OkWithWarnings; else result.Data = OutlookItemImportServiceResult.Ok; return result; }
public void UpdateNonRecurringWorkItem(WorkItem existingWorkItem, Outlook.AppointmentItem item) { existingWorkItem.Subject = item.Subject; existingWorkItem.Location = item.Location; existingWorkItem.Body = item.Body; existingWorkItem.StartDateTime = item.Start; existingWorkItem.EndDateTime = item.End; existingWorkItem.AllDayEvent = item.AllDayEvent; existingWorkItem.Duration = item.Duration / 60; existingWorkItem.UpdatedAt = DateTime.Now; existingWorkItem.UpdatedByUserId = this.CurrentUser.UserId; }
public string GetJobId(WorkItem workItem) { return workItem.CreatedByUserId.ToString() + "@" + workItem.CreatedAt.ToString(); }
public int ScheduleReminder(WorkItem workItem, RecurringItem recurringItem) { var message = ""; try { scheduler.SetRecurringItemReminderData(scheduler, workItem, recurringItem); var reminderServiceResult = scheduler.ScheduleReminder(); message = scheduler.HandleReminderServiceResult(reminderServiceResult); } catch (Exception ex) { message = "Atgādinājuma ieplānošana beigusies ar kļūdu! " + ex.Message; } if (message.Length != 0) { return 1; } else return 0; }
public string GetJobId(WorkItem workItem, RecurringItem recurringItem) { return "recurringItem" + recurringItem.Id.ToString() + "by" + workItem.CreatedByUserId.ToString() + "@" + workItem.CreatedAt.ToString(); }
public void SetWorkItemReminderData(ReminderScheduler scheduler, WorkItem workItem) { scheduler.Id = workItem.CreatedByUserId.ToString() + "@" + workItem.CreatedAt.ToString(); scheduler.WorkItemType = workItem.WorkItemType; scheduler.Reminder = workItem.Reminder; scheduler.StartTime = workItem.StartDateTime; scheduler.EndTime = workItem.EndDateTime; scheduler.Duration = workItem.Duration; scheduler.Origin = workItem.Origin; scheduler.Location = workItem.Location; scheduler.Subject = workItem.Subject; scheduler.MailTo = workItem.CreatedBy.Email; //scheduler.Url = Url.Action("Details", "WorkItem", new { id = workItem.Id }, Request.Url.Scheme); }
/// <summary> /// Creates recurring items from a given reccurence pattern and time period /// </summary> /// <param name="workItem">The work item</param> /// <param name="model">The model</param> private void CreateOccurrences(WorkItem workItem, WorkItemDataInputModel model) { var occurrenceService = new OccurrenceService(); var occurrenceDates = occurrenceService.GetOccurrenceDates(workItem); workItem.RecurringItems = new List<RecurringItem>(); foreach (var date in occurrenceDates) { var originalDate = new DateTime(date.Year, date.Month, date.Day, model.RecurringItemStart.Value.Hour, model.RecurringItemStart.Value.Minute, model.RecurringItemStart.Value.Second); if (!this.dataContext.RecurringItems.Any(r => r.OriginalDate == originalDate)) { workItem.RecurringItems.Add(new RecurringItem { Start = new DateTime(date.Year, date.Month, date.Day, model.RecurringItemStart.Value.Hour, model.RecurringItemStart.Value.Minute, model.RecurringItemStart.Value.Second), End = new DateTime(date.Year, date.Month, date.Day, model.RecurringItemEnd.Value.Hour, model.RecurringItemEnd.Value.Minute, model.RecurringItemEnd.Value.Second), OriginalDate = originalDate, Exception = false, Subject = workItem.Subject, Body = workItem.Body, Location = workItem.Location, UpdatedAt = DateTime.Now }); } } }
/// <summary> /// Removes all of the recurring items for a work item /// </summary> /// <param name="workItem">The work item</param> private void RemoveRecurringItems(WorkItem workItem) { foreach (var recurringItem in workItem.RecurringItems.ToList()) { this.dataContext.RecurringItems.Remove(recurringItem); } }
private void AddCategories(WorkItemDataInputModel model, WorkItem workItem) { foreach (var categoryId in model.SelectedCategoryIds) { var category = this.dataContext.Categories.Find(categoryId); workItem.Categories.Add(category); } }
/// <summary> /// Method gets the occurrence dates of an event, given the recurrence type and pattern /// </summary> /// <param name="recurrenceType">The recurrence type</param> /// <param name="recurrencePattern">The recurrence pattern</param> /// <returns>A list of datetime objects</returns> public List<DateTime> GetOccurrenceDates(WorkItem workItem) { var occurrenceDates = new List<DateTime>(); var recurrencePattern = workItem.RecurrencePattern; DateTime start = (DateTime)workItem.StartDateTime; DateTime end = workItem.EndDateTime; int interval = recurrencePattern.Interval; int dayOfMonth = recurrencePattern.DayOfMonth; int instance = (int)recurrencePattern.Instance; int monthOfYear = (int)recurrencePattern.MonthOfYear; if (workItem.RecurrenceType == RecurrenceType.Daily) { for (DateTime cur = start; cur <= end; cur = cur.AddDays(interval)) { occurrenceDates.Add(cur); } return occurrenceDates; } else if (workItem.RecurrenceType == RecurrenceType.Weekly) { var daysOfWeek = GetRecurringDaysOfWeek((int)recurrencePattern.DayOfWeekMask); foreach (DayOfWeek dayOfWeek in daysOfWeek) { DateTime next = GetNextWeekday(dayOfWeek, start); for (DateTime cur = next; cur <= end; cur = cur.AddDays(interval * 7)) { occurrenceDates.Add(cur); } } return occurrenceDates; } else if (workItem.RecurrenceType == RecurrenceType.Monthly) { int counter = 0; DateTime month = new DateTime(start.Year, start.Month, 1); DateTime currentDate = month; for (DateTime cur = month; currentDate <= end; cur = month.AddMonths(interval * counter)) { var day = dayOfMonth; if (dayOfMonth > DateTime.DaysInMonth(cur.Year, cur.Month)) { day = DateTime.DaysInMonth(cur.Year, cur.Month); } currentDate = new DateTime(cur.Year, cur.Month, day); occurrenceDates.Add(currentDate); counter++; } return occurrenceDates; } else if (workItem.RecurrenceType == RecurrenceType.MonthNth) { var dayOfWeek = GetRecurringDaysOfWeek((int)recurrencePattern.DayOfWeekMask); DateTime NthWeekdayDayInMonth = GetNthWeekdayDateInMonth(start, instance, dayOfWeek); if (NthWeekdayDayInMonth < start) { NthWeekdayDayInMonth = GetNthWeekdayDateInMonth(start = start.AddMonths(1), instance, dayOfWeek); } for (DateTime cur = NthWeekdayDayInMonth; cur <= end; cur = GetNthWeekdayDateInMonth(cur = cur.AddMonths(interval), instance, dayOfWeek)) { occurrenceDates.Add(cur); } return occurrenceDates; } else if (workItem.RecurrenceType == RecurrenceType.Yearly) { int counter = 0; DateTime month = new DateTime(start.Year, start.Month, 1); DateTime currentDate = month; for (DateTime cur = month; currentDate <= end; cur = month.AddYears(interval * counter)) { var day = dayOfMonth; if (dayOfMonth > DateTime.DaysInMonth(cur.Year, cur.Month)) { day = DateTime.DaysInMonth(cur.Year, cur.Month); } currentDate = new DateTime(cur.Year, cur.Month, day); occurrenceDates.Add(currentDate); counter++; } return occurrenceDates; } else { var dayOfWeek = GetRecurringDaysOfWeek((int)recurrencePattern.DayOfWeekMask); DateTime NthWeekdayDayInMonth = GetNthWeekdayDateInMonth(start, instance, dayOfWeek); if (NthWeekdayDayInMonth < start) { NthWeekdayDayInMonth = GetNthWeekdayDateInMonth(start = start.AddYears(1), instance, dayOfWeek); } for (DateTime cur = NthWeekdayDayInMonth; cur <= end; cur = GetNthWeekdayDateInMonth(cur = cur.AddYears(interval), instance, dayOfWeek)) { occurrenceDates.Add(cur); } return occurrenceDates; } }
internal void UpdateWorkItem(WorkItem workItem) { workItem.Subject = this.Subject; workItem.Location = this.Location; workItem.Body = this.Body; workItem.Duration = this.Duration; workItem.Priority = this.Priority; workItem.WorkItemType = this.WorkItemType; if (this.WorkItemType == WorkItemType.Task) { workItem.isRecurring = false; workItem.StartDateTime = null; workItem.EndDateTime = this.DueDate; } else { workItem.AllDayEvent = this.AllDayEvent; workItem.StartDateTime = this.StartDate; workItem.isRecurring = this.isRecurring; workItem.EndDateTime = this.EndDate; } }
//take the values from the model and populate a WorkItem class with them to save the work item to the database public WorkItem TransformToWorkItem() { var workItem = new WorkItem(); workItem.Subject = this.Subject; workItem.Location = this.Location; workItem.Body = this.Body; workItem.Priority = this.Priority; workItem.Reminder = this.Reminder; workItem.Origin = this.Origin; workItem.WorkItemType = this.WorkItemType; if (this.WorkItemType == WorkItemType.Task) { workItem.Duration = this.Duration; workItem.StartDateTime = null; workItem.EndDateTime = this.DueDate; } else { workItem.StartDateTime = this.StartDate; workItem.EndDateTime = this.EndDate; workItem.AllDayEvent = this.AllDayEvent; workItem.isRecurring = this.isRecurring; workItem.Duration = (this.EndDate - this.StartDate.Value).TotalHours; } return workItem; }
/// <summary> /// Sets the model values from an incoming work item /// </summary> /// <param name="workItem">The work item</param> public WorkItemDataInputModel(WorkItem workItem) { this.Categories = new List<Category>(); this.SelectedCategoryIds = new List<int>(); SetInitialValues(); this.WorkItemId = workItem.Id; this.Subject = workItem.Subject; this.Location = workItem.Location; this.Body = workItem.Body; this.Priority = workItem.Priority; this.Reminder = workItem.Reminder; this.Origin = workItem.Origin; this.WorkItemType = workItem.WorkItemType; if (workItem.WorkItemType == WorkItemType.Task) { this.DueDate = workItem.EndDateTime; this.StartDate = DateTime.Now; this.EndDate = DateTime.Now; } else if (workItem.WorkItemType == WorkItemType.Appointment) { this.AllDayEvent = workItem.AllDayEvent; this.StartDate = workItem.StartDateTime; this.EndDate = workItem.EndDateTime; this.DueDate = DateTime.Now; } this.Duration = workItem.Duration; this.isRecurring = workItem.isRecurring; // if work item is recurring we only want the specific recurrence type fields to be filled with values if (workItem.isRecurring) { var recurrencePattern = workItem.RecurrencePattern; this.RecurrenceType = (RecurrenceType)workItem.RecurrenceType; this.RecurringItemStart = workItem.RecurringItems.FirstOrDefault(o => o.Exception == false).Start; this.RecurringItemEnd = workItem.RecurringItems.FirstOrDefault(o => o.Exception == false).End; switch (workItem.RecurrenceType) { case RecurrenceType.Daily: this.DailyInterval = recurrencePattern.Interval; break; case RecurrenceType.Weekly: this.WeeklyInterval = recurrencePattern.Interval; var mask = (int)recurrencePattern.DayOfWeekMask; this.WeekDays = (DaysOfWeek)Enum.ToObject(typeof(DaysOfWeek), mask); break; case RecurrenceType.Monthly: this.MonthlyInterval = recurrencePattern.Interval; this.DayOfMonth = recurrencePattern.DayOfMonth; break; case RecurrenceType.MonthNth: this.MonthNthInterval = recurrencePattern.Interval; this.MonthInstance = recurrencePattern.Instance; this.MonthDayOfWeekMask = recurrencePattern.DayOfWeekMask; break; case RecurrenceType.Yearly: this.YearlyInterval = recurrencePattern.Interval; this.DayOfMonthForYear = recurrencePattern.DayOfMonth; break; case RecurrenceType.YearNth: this.YearNthInterval = recurrencePattern.Interval; this.YearInstance = recurrencePattern.Instance; this.YearDayOfWeekMask = recurrencePattern.DayOfWeekMask; break; } } }