Ejemplo n.º 1
0
        /// <summary>
        /// Assigns free busy times to the exchange users that are passed into the method
        /// </summary>
        public virtual void GetCalendarInfoForUsers(ExchangeUserDict users, DateTimeRange window)
        {
            // Perform  appointment lookup in parallel with FB lookup
            using (AppointmentLookupFuture future =
                       new AppointmentLookupFuture(this, users, window))
            {
                Dictionary <ExchangeUser, FreeBusy> freeBusyBlocks =
                    freebusy.LookupFreeBusyTimes(users, window);

                foreach (ExchangeUser user in users.Values)
                {
                    // If the server gave us no data for a user, that's
                    // fine.  We just skip the user.
                    if (!freeBusyBlocks.ContainsKey(user))
                    {
                        continue;
                    }

                    /* Retrieve the free busy blocks */
                    FreeBusy freeBusy = freeBusyBlocks[user];

                    user.AccessLevel = GCalAccessLevel.ReadAccess;

                    List <Appointment> appointments = future.getResult(user);

                    MergeFreeBusyWithAppointments(
                        user,
                        freeBusy,
                        appointments,
                        window);
                }
            }
        }
        public void TestFreeBusyAppointmentMerge()
        {
            FreeBusy fb = createFreeBusy(_user);

            this.MergeFreeBusyWithAppointments(_user, fb, _appointments, _window);

            Assert.AreEqual(5, _user.BusyTimes.Count);
            Assert.AreEqual(7, _appointments.Count);
            Assert.AreEqual(5, fb.All.Count);

            DateTime start = fb.All[0].Start;
            DateTime end   = fb.All[0].End;

            Assert.AreEqual(start, _user.BusyTimes[start].StartDate);
            Assert.AreEqual(end, _user.BusyTimes[start].EndDate);
            Assert.AreEqual(1, _user.BusyTimes[start].Appointments.Count);
            Assert.AreEqual(start, _user.BusyTimes[start].Appointments[0].StartDate);
            Assert.AreEqual(end, _user.BusyTimes[start].Appointments[0].EndDate);
            Assert.IsFalse(_user.BusyTimes[start].Appointments[0].AllDayEvent);

            start = fb.All[1].Start;
            end   = fb.All[1].End;

            Assert.AreEqual(start, _user.BusyTimes[start].StartDate);
            Assert.AreEqual(end, _user.BusyTimes[start].EndDate);
            Assert.AreEqual(2, _user.BusyTimes[start].Appointments.Count);
            Assert.AreEqual(start, _user.BusyTimes[start].Appointments[0].StartDate);
            Assert.AreEqual(end, _user.BusyTimes[start].Appointments[1].EndDate);
            Assert.IsFalse(_user.BusyTimes[start].Appointments[0].AllDayEvent);
            Assert.IsFalse(_user.BusyTimes[start].Appointments[1].AllDayEvent);

            start = fb.All[2].Start;
            end   = fb.All[2].End;

            Assert.AreEqual(start, _user.BusyTimes[start].StartDate);
            Assert.AreEqual(end, _user.BusyTimes[start].EndDate);
            Assert.AreEqual(0, _user.BusyTimes[start].Appointments.Count);

            start = fb.All[3].Start;
            end   = fb.All[3].End;

            Assert.AreEqual(start, _user.BusyTimes[start].StartDate);
            Assert.AreEqual(end, _user.BusyTimes[start].EndDate);
            Assert.AreEqual(1, _user.BusyTimes[start].Appointments.Count);
            Assert.AreEqual(start, _user.BusyTimes[start].Appointments[0].StartDate);
            Assert.AreEqual(end, _user.BusyTimes[start].Appointments[0].EndDate);
            Assert.IsTrue(_user.BusyTimes[start].Appointments[0].AllDayEvent);

            start = fb.All[4].Start;
            end   = fb.All[4].End;

            Assert.AreEqual(start, _user.BusyTimes[start].StartDate);
            Assert.AreEqual(end, _user.BusyTimes[start].EndDate);
            Assert.AreEqual(2, _user.BusyTimes[start].Appointments.Count);
            Assert.AreEqual(start, _user.BusyTimes[start].Appointments[0].StartDate);
            Assert.AreEqual(start.AddHours(2), _user.BusyTimes[start].Appointments[0].EndDate);

            //dump(_user.BusyTimes);
        }
 private static void ClearFreeBusy(
     FreeBusy freeBusy)
 {
     freeBusy.All.Clear();
     freeBusy.Busy.Clear();
     freeBusy.Tentative.Clear();
     freeBusy.OutOfOffice.Clear();
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Determines whether the specified <see cref="Cronofy.FreeBusy"/> is
 /// equal to the current <see cref="Cronofy.FreeBusy"/>.
 /// </summary>
 /// <param name="other">
 /// The <see cref="Cronofy.FreeBusy"/> to compare with the current
 /// <see cref="Cronofy.FreeBusy"/>.
 /// </param>
 /// <returns>
 /// <c>true</c> if the specified <see cref="Cronofy.FreeBusy"/> is equal
 /// to the current <see cref="Cronofy.FreeBusy"/>; otherwise,
 /// <c>false</c>.
 /// </returns>
 public bool Equals(FreeBusy other)
 {
     return(other != null &&
            this.CalendarId == other.CalendarId &&
            object.Equals(this.Start, other.Start) &&
            object.Equals(this.End, other.End) &&
            object.Equals(this.FreeBusyStatus, other.FreeBusyStatus));
 }
        private FreeBusy createFreeBusy(ExchangeUser user)
        {
            FreeBusy result = new FreeBusy();

            result.User = user;

            result.All  = _freeBusy.GetRange(0, _freeBusy.Count);
            result.Busy = _freeBusy.GetRange(0, _freeBusy.Count);
            return(result);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Converst Exchange raster free busy string to FreeBusy
        /// </summary>
        /// <param name="baseTime">The start date of the free busy</param>
        /// <param name="freeBusyInterval">The granularity of the free busy in minutes</param>
        /// <param name="freeBusyRaster">The raster to parse</param>
        /// <param name="freeBusy">The free busy result</param>
        /// <returns>void</returns>
        public static void ParseRasterFreeBusy(
            DateTime baseTime,
            int freeBusyInterval,
            string freeBusyRaster,
            FreeBusy freeBusy)
        {
            BusyStatus oldState = BusyStatus.Free;
            int        startRun = 0;
            int        idx      = 0;

            if (string.IsNullOrEmpty(freeBusyRaster))
            {
                return;
            }

            log.DebugFormat("Parsing the raster {0}", freeBusyRaster);

            foreach (char current in freeBusyRaster)
            {
                BusyStatus newState = ConvertRasterToFreeBusy(current);

                if (newState != oldState)
                {
                    RecordFreeBusyInterval(baseTime,
                                           oldState,
                                           freeBusyInterval,
                                           startRun,
                                           idx,
                                           freeBusy);

                    oldState = newState;
                    startRun = idx;
                }

                idx++;
            }

            RecordFreeBusyInterval(baseTime,
                                   oldState,
                                   freeBusyInterval,
                                   startRun,
                                   idx,
                                   freeBusy);

            log.DebugFormat("Parsed the raster into {0} busy and {1} tentative ranges",
                            freeBusy.Busy.Count,
                            freeBusy.Tentative.Count);
        }
Ejemplo n.º 7
0
        public virtual ICalendarComponent Build(string objectName)
        {
            ICalendarComponent c;
            var name = objectName.ToUpper();

            switch (name)
            {
            case Components.Alarm:
                c = new Alarm();
                break;

            case EventStatus.Name:
                c = new CalendarEvent();
                break;

            case Components.Freebusy:
                c = new FreeBusy();
                break;

            case JournalStatus.Name:
                c = new Journal();
                break;

            case Components.Timezone:
                c = new VTimeZone();
                break;

            case TodoStatus.Name:
                c = new Todo();
                break;

            case Components.Calendar:
                c = new Calendar();
                break;

            case Components.Daylight:
            case Components.Standard:
                c = new VTimeZoneInfo();
                break;

            default:
                c = new CalendarComponent();
                break;
            }
            c.Name = name;
            return(c);
        }
Ejemplo n.º 8
0
        public virtual ICalendarComponent Build(string objectName)
        {
            ICalendarComponent c = null;
            var name             = objectName.ToUpper();

            switch (name)
            {
            case Components.Alarm:
                c = new Alarm();
                break;

            case Components.Event:
                c = new CalendarEvent();
                break;

            case Components.Freebusy:
                c = new FreeBusy();
                break;

            case Components.Journal:
                c = new Journal();
                break;

            case Components.Timezone:
                c = new VTimeZone();
                break;

            case Components.Todo:
                c = new Todo();
                break;

            case Components.Calendar:
                c = new Calendar();
                break;

            default:
                c = new CalendarComponent();
                break;
            }
            c.Name = name;
            return(c);
        }
Ejemplo n.º 9
0
        private static void RecordFreeBusyInterval(
            DateTime baseTime,
            BusyStatus state,
            int freeBusyInterval,
            int start,
            int end,
            FreeBusy freeBusy)
        {
            DateTime      eventStart = baseTime.AddMinutes(start * freeBusyInterval);
            DateTime      eventEnd   = baseTime.AddMinutes(end * freeBusyInterval);
            DateTimeRange range      = new DateTimeRange(eventStart, eventEnd);

            // Handle the state
            switch (state)
            {
            default:
            case BusyStatus.Free:
                // We don't record these
                break;

            case BusyStatus.Busy:
                // Busy is recorded in busy and all
                freeBusy.All.Add(range);
                freeBusy.Busy.Add(range);
                break;

            case BusyStatus.Tentative:
                freeBusy.Tentative.Add(range);
                break;

            case BusyStatus.OutOfOffice:
                // OOO is recorded in out of office and all
                freeBusy.All.Add(range);
                freeBusy.OutOfOffice.Add(range);
                break;
            }
        }
        public void TestParseRasterFreeBusy()
        {
            DateTime startDate = new DateTime(2008, 05, 1, 10, 0, 0, DateTimeKind.Utc);
            DateTime date1     = new DateTime(2008, 05, 1, 10, 15, 0, DateTimeKind.Utc);
            DateTime date2     = new DateTime(2008, 05, 1, 10, 30, 0, DateTimeKind.Utc);
            DateTime date3     = new DateTime(2008, 05, 1, 10, 45, 0, DateTimeKind.Utc);
            DateTime date4     = new DateTime(2008, 05, 1, 11, 0, 0, DateTimeKind.Utc);
            DateTime date5     = new DateTime(2008, 05, 1, 11, 15, 0, DateTimeKind.Utc);
            FreeBusy freeBusy  = new FreeBusy();

            FreeBusyConverter.ParseRasterFreeBusy(startDate, 15, "1", freeBusy);
            Assert.AreEqual(freeBusy.All.Count, 0);
            Assert.AreEqual(freeBusy.Busy.Count, 0);
            Assert.AreEqual(freeBusy.OutOfOffice.Count, 0);
            Assert.AreEqual(freeBusy.Tentative.Count, 1);
            Assert.AreEqual(freeBusy.Tentative[0].Start, startDate);
            Assert.AreEqual(freeBusy.Tentative[0].End, date1);
            ClearFreeBusy(freeBusy);

            FreeBusyConverter.ParseRasterFreeBusy(startDate, 15, "2", freeBusy);
            Assert.AreEqual(freeBusy.All.Count, 1);
            Assert.AreEqual(freeBusy.Busy.Count, 1);
            Assert.AreEqual(freeBusy.OutOfOffice.Count, 0);
            Assert.AreEqual(freeBusy.Tentative.Count, 0);
            Assert.AreEqual(freeBusy.All[0].Start, startDate);
            Assert.AreEqual(freeBusy.All[0].End, date1);
            Assert.AreEqual(freeBusy.Busy[0].Start, startDate);
            Assert.AreEqual(freeBusy.Busy[0].End, date1);
            ClearFreeBusy(freeBusy);

            FreeBusyConverter.ParseRasterFreeBusy(startDate, 15, "3", freeBusy);
            Assert.AreEqual(freeBusy.All.Count, 1);
            Assert.AreEqual(freeBusy.Busy.Count, 0);
            Assert.AreEqual(freeBusy.OutOfOffice.Count, 1);
            Assert.AreEqual(freeBusy.Tentative.Count, 0);
            Assert.AreEqual(freeBusy.All[0].Start, startDate);
            Assert.AreEqual(freeBusy.All[0].End, date1);
            Assert.AreEqual(freeBusy.OutOfOffice[0].Start, startDate);
            Assert.AreEqual(freeBusy.OutOfOffice[0].End, date1);
            ClearFreeBusy(freeBusy);

            FreeBusyConverter.ParseRasterFreeBusy(startDate, 15, "4", freeBusy);
            Assert.AreEqual(freeBusy.All.Count, 0);
            Assert.AreEqual(freeBusy.Busy.Count, 0);
            Assert.AreEqual(freeBusy.OutOfOffice.Count, 0);
            Assert.AreEqual(freeBusy.Tentative.Count, 0);
            ClearFreeBusy(freeBusy);

            FreeBusyConverter.ParseRasterFreeBusy(startDate, 15, "11", freeBusy);
            Assert.AreEqual(freeBusy.All.Count, 0);
            Assert.AreEqual(freeBusy.Busy.Count, 0);
            Assert.AreEqual(freeBusy.OutOfOffice.Count, 0);
            Assert.AreEqual(freeBusy.Tentative.Count, 1);
            Assert.AreEqual(freeBusy.Tentative[0].Start, startDate);
            Assert.AreEqual(freeBusy.Tentative[0].End, date2);
            ClearFreeBusy(freeBusy);

            FreeBusyConverter.ParseRasterFreeBusy(startDate, 15, "22", freeBusy);
            Assert.AreEqual(freeBusy.All.Count, 1);
            Assert.AreEqual(freeBusy.Busy.Count, 1);
            Assert.AreEqual(freeBusy.OutOfOffice.Count, 0);
            Assert.AreEqual(freeBusy.Tentative.Count, 0);
            Assert.AreEqual(freeBusy.All[0].Start, startDate);
            Assert.AreEqual(freeBusy.All[0].End, date2);
            Assert.AreEqual(freeBusy.Busy[0].Start, startDate);
            Assert.AreEqual(freeBusy.Busy[0].End, date2);
            ClearFreeBusy(freeBusy);

            FreeBusyConverter.ParseRasterFreeBusy(startDate, 15, "33", freeBusy);
            Assert.AreEqual(freeBusy.All.Count, 1);
            Assert.AreEqual(freeBusy.Busy.Count, 0);
            Assert.AreEqual(freeBusy.OutOfOffice.Count, 1);
            Assert.AreEqual(freeBusy.Tentative.Count, 0);
            Assert.AreEqual(freeBusy.All[0].Start, startDate);
            Assert.AreEqual(freeBusy.All[0].End, date2);
            Assert.AreEqual(freeBusy.OutOfOffice[0].Start, startDate);
            Assert.AreEqual(freeBusy.OutOfOffice[0].End, date2);
            ClearFreeBusy(freeBusy);

            FreeBusyConverter.ParseRasterFreeBusy(startDate, 15, "44", freeBusy);
            Assert.AreEqual(freeBusy.All.Count, 0);
            Assert.AreEqual(freeBusy.Busy.Count, 0);
            Assert.AreEqual(freeBusy.OutOfOffice.Count, 0);
            Assert.AreEqual(freeBusy.Tentative.Count, 0);
            ClearFreeBusy(freeBusy);

            FreeBusyConverter.ParseRasterFreeBusy(startDate, 15, "0114", freeBusy);
            Assert.AreEqual(freeBusy.All.Count, 0);
            Assert.AreEqual(freeBusy.Busy.Count, 0);
            Assert.AreEqual(freeBusy.OutOfOffice.Count, 0);
            Assert.AreEqual(freeBusy.Tentative.Count, 1);
            Assert.AreEqual(freeBusy.Tentative[0].Start, date1);
            Assert.AreEqual(freeBusy.Tentative[0].End, date3);
            ClearFreeBusy(freeBusy);

            FreeBusyConverter.ParseRasterFreeBusy(startDate, 15, "0224", freeBusy);
            Assert.AreEqual(freeBusy.All.Count, 1);
            Assert.AreEqual(freeBusy.Busy.Count, 1);
            Assert.AreEqual(freeBusy.OutOfOffice.Count, 0);
            Assert.AreEqual(freeBusy.Tentative.Count, 0);
            Assert.AreEqual(freeBusy.All[0].Start, date1);
            Assert.AreEqual(freeBusy.All[0].End, date3);
            Assert.AreEqual(freeBusy.Busy[0].Start, date1);
            Assert.AreEqual(freeBusy.Busy[0].End, date3);
            ClearFreeBusy(freeBusy);

            FreeBusyConverter.ParseRasterFreeBusy(startDate, 15, "0334", freeBusy);
            Assert.AreEqual(freeBusy.All.Count, 1);
            Assert.AreEqual(freeBusy.Busy.Count, 0);
            Assert.AreEqual(freeBusy.OutOfOffice.Count, 1);
            Assert.AreEqual(freeBusy.Tentative.Count, 0);
            Assert.AreEqual(freeBusy.All[0].Start, date1);
            Assert.AreEqual(freeBusy.All[0].End, date3);
            Assert.AreEqual(freeBusy.OutOfOffice[0].Start, date1);
            Assert.AreEqual(freeBusy.OutOfOffice[0].End, date3);
            ClearFreeBusy(freeBusy);

            FreeBusyConverter.ParseRasterFreeBusy(startDate, 15, "0440", freeBusy);
            Assert.AreEqual(freeBusy.All.Count, 0);
            Assert.AreEqual(freeBusy.Busy.Count, 0);
            Assert.AreEqual(freeBusy.OutOfOffice.Count, 0);
            Assert.AreEqual(freeBusy.Tentative.Count, 0);
            ClearFreeBusy(freeBusy);

            FreeBusyConverter.ParseRasterFreeBusy(startDate, 15, "40312", freeBusy);
            Assert.AreEqual(freeBusy.All.Count, 2);
            Assert.AreEqual(freeBusy.Busy.Count, 1);
            Assert.AreEqual(freeBusy.OutOfOffice.Count, 1);
            Assert.AreEqual(freeBusy.Tentative.Count, 1);
            Assert.AreEqual(freeBusy.All[0].Start, date2);
            Assert.AreEqual(freeBusy.All[0].End, date3);
            Assert.AreEqual(freeBusy.All[1].Start, date4);
            Assert.AreEqual(freeBusy.All[1].End, date5);
            Assert.AreEqual(freeBusy.Busy[0].Start, date4);
            Assert.AreEqual(freeBusy.Busy[0].End, date5);
            Assert.AreEqual(freeBusy.Tentative[0].Start, date3);
            Assert.AreEqual(freeBusy.Tentative[0].End, date4);
            Assert.AreEqual(freeBusy.OutOfOffice[0].Start, date2);
            Assert.AreEqual(freeBusy.OutOfOffice[0].End, date3);
            ClearFreeBusy(freeBusy);
        }
Ejemplo n.º 11
0
 /// <summary>
 /// Determines whether the specified <see cref="Cronofy.FreeBusy"/> is
 /// equal to the current <see cref="Cronofy.FreeBusy"/>.
 /// </summary>
 /// <param name="other">
 /// The <see cref="Cronofy.FreeBusy"/> to compare with the current
 /// <see cref="Cronofy.FreeBusy"/>.
 /// </param>
 /// <returns>
 /// <c>true</c> if the specified <see cref="Cronofy.FreeBusy"/> is equal
 /// to the current <see cref="Cronofy.FreeBusy"/>; otherwise,
 /// <c>false</c>.
 /// </returns>
 public bool Equals(FreeBusy other)
 {
     return other != null
         && this.CalendarId == other.CalendarId
         && object.Equals(this.Start, other.Start)
         && object.Equals(this.End, other.End)
         && object.Equals(this.FreeBusyStatus, other.FreeBusyStatus);
 }
Ejemplo n.º 12
0
        /// <summary>
        /// Combines the free busy and appointment blocks supplied to the exchange user object
        /// If no appointments are supplied the user will still have free busy time blocks assigned
        /// to them, with a null appointment assigned to the free busy time.
        /// </summary>
        /// <param name="exchangeUser">Exchange users to apply freeBusy and appointments</param>
        /// <param name="freeBusy">The collection of FreeBusy blocks to assign to exchangeUser</param>
        /// <param name="appointments">The collection of appointment blocks to assign to exchangeUser</param>
        /// <param name="window">Window to merge for</param>
        protected void MergeFreeBusyWithAppointments(
            ExchangeUser exchangeUser,
            FreeBusy freeBusy,
            List <Appointment> appointments,
            DateTimeRange window)
        {
            using (BlockTimer bt = new BlockTimer("MergeFreeBusyWithAppointments"))
            {
                IntervalTree <FreeBusyTimeBlock> busyIntervals =
                    new IntervalTree <FreeBusyTimeBlock>();
                FreeBusyCollection busyTimes       = new FreeBusyCollection();
                int appointmentsCount              = 0;
                List <DateTimeRange> combinedTimes =
                    FreeBusyConverter.MergeFreeBusyLists(freeBusy.All, freeBusy.Tentative);

                /* Add the date ranges from each collection in the FreeBusy object */
                ConvertFreeBusyToBlocks(window,
                                        combinedTimes,
                                        busyTimes,
                                        busyIntervals);

                if (appointments != null && appointments.Count > 0)
                {
                    appointmentsCount = appointments.Count;
                    foreach (Appointment appt in appointments)
                    {
                        log.DebugFormat("Appt \"{0}\" {1} {2} response = {3} status = {4} busy = {5}",
                                        appt.Subject,
                                        appt.Range,
                                        appt.StartDate.Kind,
                                        appt.ResponseStatus,
                                        appt.MeetingStatus,
                                        appt.BusyStatus);

                        if (appt.BusyStatus == BusyStatus.Free)
                        {
                            continue;
                        }

                        DateTimeRange            range  = new DateTimeRange(appt.StartDate, appt.EndDate);
                        List <FreeBusyTimeBlock> result =
                            busyIntervals.FindAll(range, IntervalTreeMatch.Overlap);

                        log.DebugFormat("Found {0} ranges overlap {1}", result.Count, range);

                        foreach (FreeBusyTimeBlock block in result)
                        {
                            log.DebugFormat("Adding \"{0}\" to FB {1} {2}",
                                            appt.Subject,
                                            block.Range,
                                            block.StartDate.Kind);
                            block.Appointments.Add(appt);
                        }

                        busyTimes.Appointments.Add(appt);
                    }
                }

                foreach (FreeBusyTimeBlock block in busyTimes.Values)
                {
                    block.Appointments.Sort(CompareAppointmentsByRanges);
                }

                log.InfoFormat("Merge Result of {0} + Appointment {1} -> {2}",
                               combinedTimes.Count,
                               appointmentsCount,
                               busyTimes.Count);

                /* Assign the data structure to the exchange user */
                exchangeUser.BusyTimes = busyTimes;
            }
        }