Exemplo 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);
        }
Exemplo 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));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Saves Calendar Event to ExigoWebContext
        /// </summary>
        /// <param name="model">Calendar Event to Save.</param>
        /// <returns>Calendar Event</returns>
        public static CalendarEvent SaveCalendarEvent(CalendarEvent model)
        {
            // Default the SQL Command to Updating an Existing Event
            var sql = @"
                        UPDATE ExigoWebContext.CalendarEvents 
                        SET
                            CalendarID = @calendarID, 
                            CalendarEventTypeID = @calendarEventTypeID, 
                            CalendarEventPrivacyTypeID = @calendarEventPrivacyTypeID, 
                            CreatedBy = @createdBy, CreatedDate = @createdDate, 
                            Description = @description, 
                            Start = @start, 
                            [End] = @end, 
                            StartTimezone = @startTimezone, 
                            EndTimezone = @endTimezone, 
                            IsAllDay = @isAllDay, 
                            Title = @title, 
                            Address1 = @address1, 
                            Address2 = @address2, 
                            City = @city, 
                            State = @state, 
                            Zip = @zip, 
                            Country = @country, 
                            CalendarSpeakerID = @speakersID, 
                            Phone = @phone, 
                            Flyer = @flyer, 
                            Cost = @cost, 
                            ConferenceNumber = @conferenceNumber, 
                            ConferencePIN = @conferencePIN, 
                            Url = @url, 
                            RecurrenceException = @recurrenceException, 
                            RecurrenceID = @recurrenceID, 
                            RecurrenceRule = @recurrenceRule
                        WHERE
                            CalendarEventID = @calendarEventID
                    ";

            // If the Calendar Event has no ID then we need to create one
            if (model.CalendarEventID == Guid.Empty)
            {
                // Create a New Calendar Event ID
                model.CalendarEventID = Guid.NewGuid();

                // Change the SQL Command to Saving an Event
                sql = @"
                        INSERT INTO ExigoWebContext.CalendarEvents
                            (CalendarEventID, CalendarID, CalendarEventTypeID, CalendarEventPrivacyTypeID, CreatedBy, CreatedDate, Description, Start, [End], StartTimezone, EndTimezone, IsAllDay, Title, Address1, Address2, City, State, Zip, Country, CalendarSpeakerID, Phone, Flyer, Cost, ConferenceNumber, ConferencePIN, Url, RecurrenceException, RecurrenceID, RecurrenceRule)
                        VALUES 
                            (@calendarEventID, @calendarID, @calendarEventTypeID, @calendarEventPrivacyTypeID, @createdBy, @createdDate, @description, @start, @end, @startTimezone, @endTimezone, @isAllDay, @title, @address1, @address2, @city, @state, @zip, @country, @speakersID, @phone, @flyer, @cost, @conferenceNumber, @conferencePIN, @url, @recurrenceException, @recurrenceID, @recurrenceRule)
                    ";
            }

            // Establish a SQL Connection
            using (var ctx = ExigoDAL.Sql())
            {
                // Execute the SQL Command
                ctx.Execute(sql, new
                {
                    calendarEventID            = model.CalendarEventID,
                    calendarID                 = model.CalendarID,
                    calendarEventTypeID        = model.CalendarEventTypeID,
                    calendarEventPrivacyTypeID = model.CalendarEventPrivacyTypeID,
                    createdBy           = model.CreatedBy,
                    createdDate         = model.CreatedDate,
                    description         = model.Description,
                    start               = model.Start,
                    end                 = model.End,
                    startTimezone       = model.StartTimezone,
                    endTimezone         = model.EndTimezone,
                    isAllDay            = model.IsAllDay,
                    title               = model.Title,
                    address1            = model.Location.Address1,
                    address2            = model.Location.Address2,
                    city                = model.Location.City,
                    state               = model.Location.State,
                    zip                 = model.Location.Zip,
                    country             = model.Location.Country,
                    speakersID          = model.SpeakerID,
                    phone               = model.Phone,
                    flyer               = model.Flyer,
                    cost                = model.Cost,
                    conferenceNumber    = model.ConferenceNumber,
                    conferencePIN       = model.ConferencePIN,
                    url                 = model.Url,
                    recurrenceException = model.RecurrenceException,
                    recurrenceID        = model.RecurrenceID,
                    recurrenceRule      = model.RecurrenceRule
                });
            }

            // Save Tags if applicable
            if (model.Tags.Any())
            {
                SaveCalendarEventTags(model.CalendarEventID, model.Tags);
            }

            return(model);
        }