Example #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);
        }
Example #2
0
        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);
            }
        }
Example #3
0
        public static ExigoService.CalendarEvent GetCalendarEvent(Guid calendarEventID)
        {
            // Get the requested calendar event
            var context       = Exigo.ODataCalendars();
            var calendarEvent = context.CalendarEvents.Expand("CalendarEventType")
                                .Where(c => c.CalendarEventID == calendarEventID)
                                .FirstOrDefault();

            return((ExigoService.CalendarEvent)calendarEvent);
        }
Example #4
0
        public static IEnumerable <ExigoService.CalendarEventType> GetCalendarEventTypes()
        {
            // Get all calendar event types
            var context            = Exigo.ODataCalendars();
            var calendarEventTypes = context.CalendarEventTypes.ToList();

            foreach (var calendarEventType in calendarEventTypes)
            {
                yield return((ExigoService.CalendarEventType)calendarEventType);
            }
        }
Example #5
0
        public static ExigoService.CalendarEvent CopyCalendarEvent(Guid calendarEventID, int customerID, bool copyRecurrenceSettings = true)
        {
            // Get the event
            var context       = Exigo.ODataCalendars();
            var calendarEvent = context.CalendarEvents
                                .Where(c => c.CalendarEventID == calendarEventID)
                                .FirstOrDefault();

            if (calendarEvent == null)
            {
                return(null);
            }


            // Create a clone of the event
            var copiedCalendarEvent = GlobalUtilities.Extend(calendarEvent, new CalendarContext.CalendarEvent());


            // Fetch the calendars
            var calendar = GetCalendars(new GetCalendarsRequest
            {
                CustomerID = customerID,
                IncludeCalendarSubscriptions = false
            }).FirstOrDefault();

            if (calendar == null)
            {
                return(null);
            }


            // Change some settings to give the event new ownership
            copiedCalendarEvent.CalendarID      = calendar.CalendarID;
            copiedCalendarEvent.CalendarEventID = Guid.Empty;
            copiedCalendarEvent.IsPrivateCopy   = true;

            // Clear out the recurring settings if desired
            if (!copyRecurrenceSettings)
            {
                copiedCalendarEvent.CalendarEventRecurrenceTypeID        = null;
                copiedCalendarEvent.CalendarEventRecurrenceInterval      = null;
                copiedCalendarEvent.CalendarEventRecurrenceMaxInstances  = null;
                copiedCalendarEvent.CalendarEventRecurrenceEndDate       = null;
                copiedCalendarEvent.WeeklyCalendarEventRecurrenceDays    = null;
                copiedCalendarEvent.MonthlyCalendarEventRecurrenceTypeID = null;
            }


            return((ExigoService.CalendarEvent)copiedCalendarEvent);
        }
Example #6
0
        public static void UnsubscribeFromCustomerCalendar(int customerID, Guid calendarID)
        {
            // Grab the customer's existing subscription and delete it if it exists
            var context = Exigo.ODataCalendars();
            var existingSubscription = context.CalendarSubscriptions
                                       .Where(c => c.CustomerID == customerID)
                                       .Where(c => c.CalendarID == calendarID)
                                       .FirstOrDefault();

            if (existingSubscription != null)
            {
                context.DeleteObject(existingSubscription);
                context.SaveChanges();
            }
        }
Example #7
0
        public static IEnumerable <ExigoService.Calendar> GetCustomerCalendarSubscriptions(int customerID)
        {
            var context   = Exigo.ODataCalendars();
            var calendars = context.CalendarSubscriptions
                            .Where(c => c.CustomerID == customerID)
                            .Select(c => new
            {
                c.Calendar
            })
                            .ToList();

            foreach (var calendar in calendars)
            {
                yield return((ExigoService.Calendar)calendar.Calendar);
            }
        }
Example #8
0
        public static IEnumerable <ExigoService.Calendar> GetCalendars(GetCalendarsRequest request)
        {
            var context = Exigo.ODataCalendars();
            var query   = context.Calendars.AsQueryable();

            // Get a customer's calendars if applicable
            if (request.CustomerID != null)
            {
                query = query.Where(c => c.CustomerID == (int)request.CustomerID);

                // Include the customer's calendar subscriptions if applicable
                if (request.IncludeCalendarSubscriptions)
                {
                    var calendarSubscriptions    = GetCustomerCalendarSubscriptions((int)request.CustomerID);
                    var calendarSubscriptionsIDs = calendarSubscriptions.Select(c => c.CalendarID).ToList();

                    if (calendarSubscriptionsIDs.Count > 0)
                    {
                        query = query.Where(calendarSubscriptionsIDs.ToOrExpression <CalendarContext.Calendar, Guid>("CalendarID"));
                    }
                }
            }

            // Include any additional requested calendars
            if (request.CalendarIDs != null && request.CalendarIDs.Count() > 0)
            {
                query = query.Where(request.CalendarIDs.ToList().ToOrExpression <CalendarContext.Calendar, Guid>("CalendarID"));
            }


            // Get the calendars
            var calendars = query.ToList();


            // If we asked for a specific customer's calendars, and none of the calendars belong to that customer, create a default calendar and add it to the collection.
            if (request.CustomerID != null && calendars.Where(c => c.CustomerID == (int)request.CustomerID).Count() == 0)
            {
                yield return(CreateDefaultCalendar((int)request.CustomerID));
            }


            // Return the calendars
            foreach (var calendar in calendars)
            {
                yield return((ExigoService.Calendar)calendar);
            }
        }
Example #9
0
        public static void DeleteCalendarEvent(Guid calendarEventID, int customerID)
        {
            // Get the event
            var context       = Exigo.ODataCalendars();
            var calendarEvent = context.CalendarEvents
                                .Where(c => c.Calendar.CustomerID == customerID)
                                .Where(c => c.CalendarEventID == calendarEventID)
                                .FirstOrDefault();

            if (calendarEvent == null)
            {
                return;
            }

            // Delete the event
            context.DeleteObject(calendarEvent);
            context.SaveChanges();
        }
Example #10
0
        public static void SubscribeToCustomerCalendar(int customerID, Guid calendarID)
        {
            // Check to ensure that the requested calendar exists
            var calendar = GetCalendars(new GetCalendarsRequest
            {
                CalendarIDs = new List <Guid>()
                {
                    calendarID
                }
            }).FirstOrDefault();

            if (calendar == null)
            {
                return;
            }

            // Next, ensure the customer is not subscribing to their own calendar
            if (calendar.CustomerID == customerID)
            {
                return;
            }

            // Next, check to ensure the provided customer is not already subscribed to this calendar
            var existingSubscription = GetCustomerCalendarSubscriptions(customerID)
                                       .Where(c => c.CalendarID == calendarID)
                                       .FirstOrDefault();

            if (existingSubscription != null)
            {
                return;
            }

            // If we got here, we can go ahead and subscribe
            var context         = Exigo.ODataCalendars();
            var newSubscription = new CalendarContext.CalendarSubscription();

            newSubscription.CustomerID = customerID;
            newSubscription.CalendarID = calendarID;

            context.AddToCalendarSubscriptions(newSubscription);
            context.SaveChanges();

            ClearCache("GetCalendars", customerID);
        }
Example #11
0
        public static ExigoService.Calendar CreateCalendar(CreateCalendarRequest request)
        {
            // Create the calendar
            var calendar = new CalendarContext.Calendar();

            calendar.CalendarID     = Guid.NewGuid();
            calendar.CustomerID     = request.CustomerID;
            calendar.Description    = request.Description;
            calendar.CalendarTypeID = 1;
            calendar.CreatedDate    = DateTime.Now;

            // Save the calendar
            var context = Exigo.ODataCalendars();

            context.AddToCalendars(calendar);
            //TODO: Calendar: Commented out because of error -- context.SaveChanges();

            // Return the new calendar
            return((ExigoService.Calendar)calendar);
        }
Example #12
0
        public static IEnumerable <ExigoService.Calendar> GetCalendars(GetCalendarsRequest request)
        {
            var context = Exigo.ODataCalendars();
            var corporateCalendarContext = Exigo.OData();
            var calendars = new List <Calendar>();
            var cacheKey  = "GetCalendars/{0}/{1}".FormatWith(request.CustomerID ?? 0, request.IncludeCalendarSubscriptions);
            var cache     = (HttpContext.Current != null) ? HttpContext.Current.Cache : null;

            // Check the cache to see if we've already made this call recently
            if (cache == null || cache[cacheKey] == null)
            {
                GlobalUtilities.RunAsyncTasks(

                    // Add the customer's personal calendars
                    () =>
                {
                    //if (request.CustomerID != null)
                    if (false)
                    {
                        var apiCalendars = context.Calendars
                                           .Where(c => c.CustomerID == (int)request.CustomerID)
                                           .ToList();
                        foreach (var apiCalendar in apiCalendars)
                        {
                            calendars.Add((ExigoService.Calendar)apiCalendar);
                        }
                    }
                },

                    // Include the customer's calendar subscriptions if applicable
                    () =>
                {
                    if (request.CustomerID != null && request.IncludeCalendarSubscriptions)
                    {
                        var calendarSubscriptions    = GetCustomerCalendarSubscriptions((int)request.CustomerID);
                        var calendarSubscriptionsIDs = calendarSubscriptions.Select(c => c.CalendarID).ToList();

                        if (calendarSubscriptionsIDs.Count > 0)
                        {
                            var apiCalendars = context.Calendars
                                               .Where(calendarSubscriptionsIDs.ToOrExpression <CalendarContext.Calendar, Guid>("CalendarID"))
                                               .ToList();
                            foreach (var apiCalendar in apiCalendars)
                            {
                                calendars.Add((ExigoService.Calendar)apiCalendar);
                            }
                        }
                    }
                },

                    // Include any additional requested calendars
                    () =>
                {
                    if (request.CalendarIDs != null && request.CalendarIDs.Count() > 0)
                    {
                        var apiCalendars = context.Calendars
                                           .Where(request.CalendarIDs.ToList().ToOrExpression <CalendarContext.Calendar, Guid>("CalendarID"))
                                           .ToList();
                        foreach (var apiCalendar in apiCalendars)
                        {
                            calendars.Add((ExigoService.Calendar)apiCalendar);
                        }
                    }
                }
                    );


                // If we asked for a specific customer's calendars, and none of the calendars belong to that customer, create a default calendar and add it to the collection.
                if (request.CustomerID != null && calendars.Where(c => c.CustomerID == (int)request.CustomerID).Count() == 0)
                {
                    var defaultCalendar = CreateDefaultCalendar((int)request.CustomerID);
                    calendars.Add(defaultCalendar);
                }

                if (cache != null)
                {
                    cache.Insert(cacheKey, calendars,
                                 null,
                                 DateTime.Now.AddMinutes(5),
                                 Cache.NoSlidingExpiration,
                                 CacheItemPriority.Normal,
                                 null);
                }
            }
            else
            {
                calendars = (List <ExigoService.Calendar>)cache[cacheKey];
            }


            // Return the calendars
            foreach (var calendar in calendars)
            {
                yield return(calendar);
            }
        }