Esempio n. 1
0
        public static ExigoService.CalendarEvent SaveCalendarEvent(ExigoService.CalendarEvent model)
        {
            // Create our context
            var context = Exigo.ODataCalendars();

            // Convert our model to the event
            var odataCalendarEvent = (CalendarContext.CalendarEvent)model;

            // Check to see if we are creating a new event, or changing an existing
            if (odataCalendarEvent.CalendarEventID == Guid.Empty)
            {
                odataCalendarEvent.CalendarEventID = Guid.NewGuid();
                context.AddToCalendarEvents(odataCalendarEvent);
            }
            else
            {
                context.AttachTo("CalendarEvents", odataCalendarEvent);
                context.UpdateObject(odataCalendarEvent);
            }

            // Save the event
            context.SaveChanges();

            // Set the new calendar event ID
            model.CalendarEventID = odataCalendarEvent.CalendarEventID;

            return(model);
        }
Esempio n. 2
0
        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));
        }