public static IEnumerable <ExigoService.CalendarEvent> GetCalendarEvents(GetCalendarEventsRequest request) { // Get the customer's calendars var calendars = GetCalendars(request); var calendarIDs = calendars.Select(c => c.CalendarID).ToList(); // Create the collection of events var calendarEvents = new List <ExigoService.CalendarEvent>(); // First, grab the single events that fall between those dates var context = Exigo.ODataCalendars(); var singleEvents = context.CalendarEvents.Expand("CalendarEventType") .Where(calendarIDs.ToOrExpression <CalendarContext.CalendarEvent, Guid>("CalendarID")) .Where(c => c.CalendarEventRecurrenceTypeID == null) .Where(c => c.StartDate >= request.UtcStartDate && c.StartDate <= request.UtcEndDate) .Where(c => c.EndDate >= request.UtcStartDate && c.EndDate <= request.UtcEndDate) .ToList(); // Convert the single events to our model foreach (var singleEvent in singleEvents) { var modelSingleEvent = (ExigoService.CalendarEvent)singleEvent; calendarEvents.Add(modelSingleEvent); } // Next, grab the recurring events var recurringEvents = context.CalendarEvents.Expand("CalendarEventType") .Where(calendarIDs.ToOrExpression <CalendarContext.CalendarEvent, Guid>("CalendarID")) .Where(c => c.CalendarEventRecurrenceTypeID != null) .Where(c => c.StartDate <= request.UtcEndDate) .ToList(); foreach (var recurringEvent in recurringEvents) { var instances = new List <ExigoService.CalendarEvent>(); var modelRecurringEvent = (ExigoService.CalendarEvent)recurringEvent; instances = GetCalendarRecurringEventInstances(instances, modelRecurringEvent, request).ToList(); foreach (var instance in instances) { calendarEvents.Add(instance); } } // Order and return the events in the collection calendarEvents = calendarEvents.OrderBy(c => c.StartDate).ToList(); foreach (var calendarEvent in calendarEvents) { yield return(calendarEvent); } }
private static IEnumerable<ExigoService.CalendarEvent> GetCalendarRecurringEventInstances(List<ExigoService.CalendarEvent> instances, ExigoService.CalendarEvent recurringEvent, GetCalendarEventsRequest request, int instanceAttempts = 0) { // Set some variables for easier access if (recurringEvent.CalendarEventRecurrenceTypeID == null) return instances; var recurrenceTypeID = (int)recurringEvent.CalendarEventRecurrenceTypeID; var recurrenceInterval = (int)recurringEvent.CalendarEventRecurrenceInterval; // Before we do anything, validate that our start date is valid based on its recurrence settings // Weekly recurrence validations if (recurrenceTypeID == 2) { var indexInList = recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek.IndexOf(recurringEvent.StartDate.DayOfWeek); // If the start date's day of week doesn't match any of the available days in the week, // we need to make an adjustment to the start date and restart the method. if (indexInList == -1) { instanceAttempts--; var daysToAdjust = 0; foreach (var day in recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek) { if ((int)day > (int)recurringEvent.StartDate.DayOfWeek) { daysToAdjust = (int)day - (int)recurringEvent.StartDate.DayOfWeek; recurringEvent.StartDate = recurringEvent.StartDate.AddDays(daysToAdjust); recurringEvent.EndDate = recurringEvent.EndDate.AddDays(daysToAdjust); break; } } // If we didn't find a day in the week that follows the current day, advance to the first available day in the next week if (daysToAdjust == 0) { daysToAdjust = 7 - ((int)recurringEvent.StartDate.DayOfWeek - (int)recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek.First()); recurringEvent.StartDate = recurringEvent.StartDate.AddDays(daysToAdjust); recurringEvent.EndDate = recurringEvent.EndDate.AddDays(daysToAdjust); } return GetCalendarRecurringEventInstances(instances, recurringEvent, request, instanceAttempts); } } // Add an instance of this recurring event if applicable if (recurringEvent.StartDate >= request.StartDate && recurringEvent.StartDate < request.EndDate) { // Create and add the instance var instance = GlobalUtilities.Extend(recurringEvent, new ExigoService.CalendarEvent()); instance.IsRecurringEventInstance = true; instances.Add(instance); } // Increment the amount of instances attempted instanceAttempts++; // If we have the maximum number of instances, stop creating instances and return the collection if (recurringEvent.CalendarEventRecurrenceMaxInstances != null && instanceAttempts >= (int)recurringEvent.CalendarEventRecurrenceMaxInstances) { return instances; } // Increment the period of time based on the recurrence type. switch (recurrenceTypeID) { // Daily case 1: recurringEvent.StartDate = recurringEvent.StartDate.AddDays(1 * recurrenceInterval); recurringEvent.EndDate = recurringEvent.EndDate.AddDays(1 * recurrenceInterval); break; // Weekly case 2: // Determine if we are repeating this event more than once per week. // If this event only occurs once per week, just add another week. if (recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek.Count == 1) { recurringEvent.StartDate = recurringEvent.StartDate.AddDays(7 * recurrenceInterval); recurringEvent.EndDate = recurringEvent.EndDate.AddDays(7 * recurrenceInterval); } // If this event occurs more than once in a week, let's ensure that we advance the dates appropriately. else { var indexInList = recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek.IndexOf(recurringEvent.UtcStartDate.DayOfWeek); // If we are on the last day in the list, skip ahead to the front of the list var daysToAdvance = 0; if (indexInList == (recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek.Count - 1)) { daysToAdvance = (7 - ((int)recurringEvent.StartDate.DayOfWeek - (int)recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek.First())) + (7 * (recurrenceInterval - 1)); } // Otherwise, add the number of days between the current start date and the next day in the week. else { daysToAdvance = ((int)recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek[indexInList + 1]) - ((int)recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek[indexInList]); } recurringEvent.StartDate = recurringEvent.StartDate.AddDays(daysToAdvance); recurringEvent.EndDate = recurringEvent.EndDate.AddDays(daysToAdvance); } break; // Monthly case 3: var monthlyRecurringTypeID = (int)recurringEvent.MonthlyCalendarEventRecurrenceTypeID; switch (monthlyRecurringTypeID) { // Day of the month case 1: recurringEvent.StartDate = recurringEvent.StartDate.AddMonths(1 * recurrenceInterval); recurringEvent.EndDate = recurringEvent.EndDate.AddMonths(1 * recurrenceInterval); break; // Day of the week (ie. second Tuesday of the month) case 2: // Determine which week and day the current start date is in var dayOfWeek = recurringEvent.StartDate.DayOfWeek; var nthWeek = 1; var checkingDate = recurringEvent.StartDate.BeginningOfMonth().Next(dayOfWeek); while (recurringEvent.StartDate != checkingDate) { checkingDate.AddDays(7); nthWeek++; } var nextMonth = recurringEvent.StartDate.AddMonths(1 * recurrenceInterval).BeginningOfMonth(); var timeDifference = recurringEvent.EndDate.Subtract(recurringEvent.StartDate); recurringEvent.StartDate = GlobalUtilities.GetNthWeekofMonth(nextMonth, nthWeek, dayOfWeek); recurringEvent.EndDate = recurringEvent.StartDate.Add(timeDifference); break; } break; // Yearly case 4: recurringEvent.StartDate = recurringEvent.StartDate.AddYears(1 * recurrenceInterval); recurringEvent.EndDate = recurringEvent.EndDate.AddYears(1 * recurrenceInterval); break; } // If we have exceeded the maximum recurrence end date, stop creating instances and return the collection if (recurringEvent.CalendarEventRecurrenceEndDate != null && (DateTime)recurringEvent.CalendarEventRecurrenceEndDate < recurringEvent.StartDate) { return instances; } // If our new start date has exceeded the range of the original request, stop creating instances and return the collection if (recurringEvent.StartDate > request.EndDate) { return instances; } return GetCalendarRecurringEventInstances(instances, recurringEvent, request, instanceAttempts); }
public static IEnumerable<ExigoService.CalendarEvent> GetCalendarEvents(GetCalendarEventsRequest request) { // Get the customer's calendars var calendars = GetCalendars(request); var calendarIDs = calendars.Select(c => c.CalendarID).ToList(); // Create the collection of events var calendarEvents = new List<ExigoService.CalendarEvent>(); // First, grab the single events that fall between those dates var context = Exigo.ODataCalendars(); var singleEvents = context.CalendarEvents.Expand("CalendarEventType") .Where(calendarIDs.ToOrExpression<CalendarContext.CalendarEvent, Guid>("CalendarID")) .Where(c => c.CalendarEventRecurrenceTypeID == null) .Where(c => c.StartDate >= request.UtcStartDate && c.StartDate <= request.UtcEndDate) .Where(c => c.EndDate >= request.UtcStartDate && c.EndDate <= request.UtcEndDate) .ToList(); // Convert the single events to our model foreach (var singleEvent in singleEvents) { var modelSingleEvent = (ExigoService.CalendarEvent)singleEvent; calendarEvents.Add(modelSingleEvent); } // Next, grab the recurring events var recurringEvents = context.CalendarEvents.Expand("CalendarEventType") .Where(calendarIDs.ToOrExpression<CalendarContext.CalendarEvent, Guid>("CalendarID")) .Where(c => c.CalendarEventRecurrenceTypeID != null) .Where(c => c.StartDate <= request.UtcEndDate) .ToList(); foreach (var recurringEvent in recurringEvents) { var instances = new List<ExigoService.CalendarEvent>(); var modelRecurringEvent = (ExigoService.CalendarEvent)recurringEvent; instances = GetCalendarRecurringEventInstances(instances, modelRecurringEvent, request).ToList(); foreach (var instance in instances) { calendarEvents.Add(instance); } } // Order and return the events in the collection calendarEvents = calendarEvents.OrderBy(c => c.StartDate).ToList(); foreach (var calendarEvent in calendarEvents) { yield return calendarEvent; } }
private static IEnumerable <ExigoService.CalendarEvent> GetCalendarRecurringEventInstances(List <ExigoService.CalendarEvent> instances, ExigoService.CalendarEvent recurringEvent, GetCalendarEventsRequest request, int instanceAttempts = 0) { // Set some variables for easier access if (recurringEvent.CalendarEventRecurrenceTypeID == null) { return(instances); } var recurrenceTypeID = (int)recurringEvent.CalendarEventRecurrenceTypeID; var recurrenceInterval = (int)recurringEvent.CalendarEventRecurrenceInterval; // Before we do anything, validate that our start date is valid based on its recurrence settings // Weekly recurrence validations if (recurrenceTypeID == 2) { var indexInList = recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek.IndexOf(recurringEvent.StartDate.DayOfWeek); // If the start date's day of week doesn't match any of the available days in the week, // we need to make an adjustment to the start date and restart the method. if (indexInList == -1) { instanceAttempts--; var daysToAdjust = 0; foreach (var day in recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek) { if ((int)day > (int)recurringEvent.StartDate.DayOfWeek) { daysToAdjust = (int)day - (int)recurringEvent.StartDate.DayOfWeek; recurringEvent.StartDate = recurringEvent.StartDate.AddDays(daysToAdjust); recurringEvent.EndDate = recurringEvent.EndDate.AddDays(daysToAdjust); break; } } // If we didn't find a day in the week that follows the current day, advance to the first available day in the next week if (daysToAdjust == 0) { daysToAdjust = 7 - ((int)recurringEvent.StartDate.DayOfWeek - (int)recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek.First()); recurringEvent.StartDate = recurringEvent.StartDate.AddDays(daysToAdjust); recurringEvent.EndDate = recurringEvent.EndDate.AddDays(daysToAdjust); } return(GetCalendarRecurringEventInstances(instances, recurringEvent, request, instanceAttempts)); } } // Add an instance of this recurring event if applicable if (recurringEvent.StartDate >= request.StartDate && recurringEvent.StartDate < request.EndDate) { // Create and add the instance var instance = GlobalUtilities.Extend(recurringEvent, new ExigoService.CalendarEvent()); instance.IsRecurringEventInstance = true; instances.Add(instance); } // Increment the amount of instances attempted instanceAttempts++; // If we have the maximum number of instances, stop creating instances and return the collection if (recurringEvent.CalendarEventRecurrenceMaxInstances != null && instanceAttempts >= (int)recurringEvent.CalendarEventRecurrenceMaxInstances) { return(instances); } // Increment the period of time based on the recurrence type. switch (recurrenceTypeID) { // Daily case 1: recurringEvent.StartDate = recurringEvent.StartDate.AddDays(1 * recurrenceInterval); recurringEvent.EndDate = recurringEvent.EndDate.AddDays(1 * recurrenceInterval); break; // Weekly case 2: // Determine if we are repeating this event more than once per week. // If this event only occurs once per week, just add another week. if (recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek.Count == 1) { recurringEvent.StartDate = recurringEvent.StartDate.AddDays(7 * recurrenceInterval); recurringEvent.EndDate = recurringEvent.EndDate.AddDays(7 * recurrenceInterval); } // If this event occurs more than once in a week, let's ensure that we advance the dates appropriately. else { var indexInList = recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek.IndexOf(recurringEvent.UtcStartDate.DayOfWeek); // If we are on the last day in the list, skip ahead to the front of the list var daysToAdvance = 0; if (indexInList == (recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek.Count - 1)) { daysToAdvance = (7 - ((int)recurringEvent.StartDate.DayOfWeek - (int)recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek.First())) + (7 * (recurrenceInterval - 1)); } // Otherwise, add the number of days between the current start date and the next day in the week. else { daysToAdvance = ((int)recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek[indexInList + 1]) - ((int)recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek[indexInList]); } recurringEvent.StartDate = recurringEvent.StartDate.AddDays(daysToAdvance); recurringEvent.EndDate = recurringEvent.EndDate.AddDays(daysToAdvance); } break; // Monthly case 3: var monthlyRecurringTypeID = (int)recurringEvent.MonthlyCalendarEventRecurrenceTypeID; switch (monthlyRecurringTypeID) { // Day of the month case 1: recurringEvent.StartDate = recurringEvent.StartDate.AddMonths(1 * recurrenceInterval); recurringEvent.EndDate = recurringEvent.EndDate.AddMonths(1 * recurrenceInterval); break; // Day of the week (ie. second Tuesday of the month) case 2: // Determine which week and day the current start date is in var dayOfWeek = recurringEvent.StartDate.DayOfWeek; var nthWeek = 1; var checkingDate = recurringEvent.StartDate.BeginningOfMonth().Next(dayOfWeek); while (recurringEvent.StartDate != checkingDate) { checkingDate.AddDays(7); nthWeek++; } var nextMonth = recurringEvent.StartDate.AddMonths(1 * recurrenceInterval).BeginningOfMonth(); var timeDifference = recurringEvent.EndDate.Subtract(recurringEvent.StartDate); recurringEvent.StartDate = GlobalUtilities.GetNthWeekofMonth(nextMonth, nthWeek, dayOfWeek); recurringEvent.EndDate = recurringEvent.StartDate.Add(timeDifference); break; } break; // Yearly case 4: recurringEvent.StartDate = recurringEvent.StartDate.AddYears(1 * recurrenceInterval); recurringEvent.EndDate = recurringEvent.EndDate.AddYears(1 * recurrenceInterval); break; } // If we have exceeded the maximum recurrence end date, stop creating instances and return the collection if (recurringEvent.CalendarEventRecurrenceEndDate != null && (DateTime)recurringEvent.CalendarEventRecurrenceEndDate < recurringEvent.StartDate) { return(instances); } // If our new start date has exceeded the range of the original request, stop creating instances and return the collection if (recurringEvent.StartDate > request.EndDate) { return(instances); } return(GetCalendarRecurringEventInstances(instances, recurringEvent, request, instanceAttempts)); }
/// <summary> /// Retrieves Enumerable of Calendar Event from the ExigoWebContext /// </summary> /// <param name="request">The request used to identify which Calendar Events to retrieve.</param> /// <returns>Enumerable of Calendar Events</returns> public static IEnumerable <CalendarEvent> GetCalendarEvents(GetCalendarEventsRequest request) { // Create the collection of events var calendarEvents = new List <CalendarEvent>(); // Get a List of Calendars using the request passed in var calendarIDs = GetCalendars(request).Select(cal => cal.CalendarID).ToList(); // Logic to get single Event data, if Event ID is provided var calendarIDSQL = (calendarIDs.Count > 0) ? "WHERE ce.CalendarID IN @calendarIDs" : ""; var singleEventSQL = (request.EventID != Guid.Empty) ? ((calendarIDs.Count > 0) ? " AND" : " WHERE") + " ce.CalendarEventID = '{0}'".FormatWith(request.EventID) : ""; var whereClause = calendarIDSQL + singleEventSQL; // Set the SQL Command string sql = @" SELECT [CalendarEventID], [CalendarID], [CalendarEventPrivacyTypeID], [CreatedBy], [CreatedDate], [Description], [Start], [End], [StartTimezone], [EndTimezone], [IsAllDay], [Title], [Phone], [Flyer], [Cost], [ConferenceNumber], [ConferencePIN], [Url], [RecurrenceException], [RecurrenceID], [RecurrenceRule], [SpeakerName], SpeakerID = [CalendarSpeakerID], [CalendarEventTypeID], [Address1], [Address2], [City], [State], [Country], [Zip] FROM ExigoWebContext.CalendarEvents ce " + whereClause + @" SELECT * FROM ExigoWebContext.CalendarEventTags cet INNER JOIN ExigoWebContext.Tags t ON t.TagID = cet.TagID "; // Establish a SQL Connection using (var ctx = ExigoDAL.Sql()) { // Exceute the SQL Command using (var query = ctx.QueryMultiple(sql, new { calendarIDs })) { // Get each Calendar Event and Set the Address and Tags calendarEvents = query.Read <CalendarEvent, Address, CalendarEvent>((calendarevent, address) => { calendarevent.Location = address ?? new Address(); calendarevent.Tags = new List <string>(); return(calendarevent); }, "Address1").ToList(); // If we got any Events back if (calendarEvents.Any()) { // Read the Tags part of the Command var tags = query.Read <CalendarEventTag, Tag, CalendarEventTag>((cet, tag) => { cet.Tag = tag; return(cet); }, "TagID").ToList(); // Go through each Calendar Event returned and set the Tags to the appropriate Tags foreach (var calEvent in calendarEvents) { calEvent.Tags = tags.Where(t => t.CalendarEventID == calEvent.CalendarEventID && t.Tag.Name != "").ToList().Select(t => t.Tag.Name).ToList(); } } } } calendarEvents = calendarEvents.Where(c => c.CreatedBy == request.CustomerID || c.CalendarEventPrivacyTypeID == 1).ToList(); // Return the Calendar Events return(calendarEvents); }