public static FreeBusyResponse executeFreeBusyQuery(string myEmail, DateTime limitDate, CalendarService service)
        {
            FreeBusyRequest f = new FreeBusyRequest();
            FreeBusyRequestItem fbri = new FreeBusyRequestItem();
            fbri.Id = myEmail;
            f.Items = new List<FreeBusyRequestItem>();
            f.Items.Add(fbri);
            f.TimeZone = "Europe/London";
            f.TimeMin = DateTime.Today.AddDays(1);
            f.TimeMax = limitDate;

            // Attempt to get free busy data.
            FreebusyResource.QueryRequest fbquery = service.Freebusy.Query(f);

            // This call fails with global bad request
            FreeBusyResponse fbresponse = fbquery.Execute();

            Console.WriteLine("FreeBusy Query Response");
            return fbresponse;
        }
Beispiel #2
0
        static FreeBusyResponse LoadRequest(string account)
        {
            FreeBusyRequestItem calendarRequested = new FreeBusyRequestItem();

            var today = DateTime.Now.ToString("yyyy-MM-dd");

            FreeBusyRequest requestBody = new FreeBusyRequest {
                TimeMin  = Convert.ToDateTime(today + "T00:00:00.000Z"),
                TimeMax  = Convert.ToDateTime(today + "T23:59:59.000Z"),
                Items    = new List <FreeBusyRequestItem>(),
                TimeZone = "Europe/Rome"
            };

            calendarRequested.Id = account;
            requestBody.Items.Add(calendarRequested);

            FreebusyResource.QueryRequest request = _service.Freebusy.Query(requestBody);
            return(request.Execute());
        }
Beispiel #3
0
        public List <Appointment> FreeBusyCheck(DateTime startDate, DateTime endDate)
        {
            FreeBusyRequest request = new FreeBusyRequest();

            request.TimeMin = startDate;
            request.TimeMax = endDate;
            var items = new List <FreeBusyRequestItem>();
            var item  = new FreeBusyRequestItem();

            item.Id   = calendarId;
            item.ETag = "";
            items.Add(item);
            request.Items = items;
            var appointments = GetAppointments(startDate, endDate);

            FreebusyResource.QueryRequest testRequest = service.Freebusy.Query(request);
            var responseObject = testRequest.Execute();

            if (responseObject.Calendars.ContainsKey(calendarId))
            {
                if (responseObject.Calendars[calendarId].Busy.Count > 0)
                {
                    foreach (var busyItem in responseObject.Calendars[calendarId].Busy)
                    {
                        var busyStart = busyItem.Start;
                        var busyEnd   = busyItem.End;

                        // gets all the available date times between busy start and end date
                        var starts = appointments.Where(a => a.StartDate >= busyStart).ToList();
                        var ends   = starts.Where(a => a.StartDate < busyEnd).ToList();

                        foreach (var appointment in ends)
                        {
                            appointments[appointments.IndexOf(appointment)].IsBusy = true;
                        }
                    }
                }
            }
            return(appointments);
        }
Beispiel #4
0
        public CalendarEventList GetComparingEvents(DateTime timeMin, DateTime timeMax, List<String> CalendarIDs, String ViewFormat,bool primaryEmail)
        {

             allFree = new List<FreeTime>();
         finalFree = new List<FreeTime>();
         FinalallFree= new Dictionary<FreeTime, int>();
         FinalallFreeConnection = new Dictionary<FreeTime, String>();
         FreeBusy = new Dictionary<FreeTime, string>();
      FinalFreeBusy = new Dictionary<FreeTime, string>();
      primary = primaryEmail;
            if(primary)
                firstEmail = CalendarIDs[0].Substring(0, CalendarIDs[0].IndexOf("."));

            //Console.WriteLine("fffffffffffff "+ firstEmail);


            log.Debug("Try to compare  events from other Google Calendars ");
            CalendarEventList calendarEvents = new CalendarEventList();

            Dictionary<string, List<FreeTime>> personAndFreeTimes = new Dictionary<string, List<FreeTime>>();
            List<String> noEvents = new List<string>();

            

            try
            {
                foreach (String calendarID in CalendarIDs)
                {

                    List<FreeTime> freeTimeList = new List<FreeTime>();
                    if (timeMax.DayOfWeek == 0)
                        timeMax = timeMax.AddDays(-2);

                    if (timeMax.DayOfWeek == DayOfWeek.Saturday)
                        timeMax = timeMax.AddDays(-1);

                    if (timeMin.DayOfWeek == DayOfWeek.Saturday)
                        timeMax = timeMax.AddDays(+2);

                    if (timeMin.DayOfWeek == DayOfWeek.Sunday)
                        timeMax = timeMax.AddDays(+1);





                    EventsResource.ListRequest events = service.Events.List(calendarID);

                    events.SingleEvents = true;
                    events.MaxResults = 2500;
                    events.TimeMin = timeMin;
                    events.TimeMax = timeMax;
                    Events eventList = events.Execute();






                    FreeBusyRequest request = new FreeBusyRequest();
                    List<FreeBusyRequestItem> requestItems = new List<FreeBusyRequestItem>();
                    request.TimeMin = timeMin;
                    request.TimeMax = timeMax;
                    FreeBusyRequestItem r = new FreeBusyRequestItem();
                                                          

                    r.Id = calendarID;

                    request.Items = new List<FreeBusyRequestItem>();
                    request.Items.Add(r);

                    FreebusyResource.QueryRequest TestRequest = service.Freebusy.Query(request);
                    var responseOBJ = TestRequest.Execute();
                    int count = responseOBJ.Calendars[calendarID].Busy.Count;
                    

                    if (count == 0)
                    {
                        FreeTime free = new FreeTime();

                        free.setStart(new DateTime(timeMax.Year, timeMax.Month, timeMax.Day, 9, 0, 0));
                        free.setEnd(new DateTime(timeMax.Year, timeMax.Month, timeMax.Day, 5, 0, 0).AddHours(12));
                        free.setEmail(calendarID);
                        freeTimeList.Add(free);
                        //Console.WriteLine(calendarID + " " + free.Start + " " + free.End);

                    }


                    if (count != 0)
                    {


                       // Console.WriteLine(timeMax.Date + "date max ");
                        //Console.WriteLine(timeMin.Date + "date min ");
                       // Console.WriteLine(DateTime.Today + "fff");
                        DateTime d = timeMin.Date;
                        Boolean found = false;
                        

                        if (d.DayOfWeek == DayOfWeek.Sunday)
                            d = d.AddDays(1);

                      //  Console.WriteLine("first     "+ d.Date);
                        if (timeMax.Date.CompareTo(timeMin.Date) != 0 && d.DayOfWeek != DayOfWeek.Saturday && d.DayOfWeek != DayOfWeek.Sunday)
                        {

                            //Console.WriteLine("second    " + d.Date);
                        while (d <= timeMax)
                        {
                          found = false;
                            foreach (TimePeriod n in responseOBJ.Calendars[calendarID].Busy)
                            { 
                                //Console.WriteLine("third    " + d.Date);
                                //Console.WriteLine(d.CompareTo(n.Start.Value.Date) + " d: " + d.Date + "n :  " + n.Start.Value.Date); 
                                if (d.CompareTo(n.Start.Value.Date) == 0)
                                {
                                    found = true;
                                    break;
                                }
                            }

                           // Console.WriteLine("fourth    " + d.Date + " " + found);
                            if (!found)
                            {
                                //Console.WriteLine("not found    " + d.Date);
                                FreeTime free = new FreeTime();

                                free.setStart(new DateTime(d.Year, d.Month, d.Day, 9, 0, 0));
                                free.setEnd(new DateTime(d.Year, d.Month, d.Day, 5, 0, 0).AddHours(12));
                                free.setEmail(calendarID);
                                freeTimeList.Add(free);
                               // Console.WriteLine("adding to list    " + free.Start + " " + free.End);
                            }
                            d= d.AddDays(+1);
                            
                            if (d.DayOfWeek == DayOfWeek.Saturday)
                                d = d.AddDays(+2);
                            
                        }
                        }


                        int i = 0;
                        int j = 0;
                        Boolean singleevent = false;
                       // Console.WriteLine("the count " + count);
                        if (count == 1) singleevent = true;
                        while (i < count && count != 0)
                        {
                            FreeTime free = new FreeTime();
                            Boolean startatNine = false;
                            Boolean endFive = false;

                            DateTime starttime = (responseOBJ.Calendars[calendarID].Busy[i].Start).Value;
                            DateTime endtime = (responseOBJ.Calendars[calendarID].Busy[i].End).Value;


                           // Console.WriteLine(" lets see " + starttime + " " + endtime);

                            DateTime startworkhours = new DateTime(starttime.Year, starttime.Month, starttime.Day, 9, 0, 0);
                            DateTime endworkhours = new DateTime(starttime.Year, starttime.Month, starttime.Day, 5, 0, 0).AddHours(12);


                            if (i == 0 && singleevent == false)
                            {
                                if (j == 0 && (DateTime.Compare(startworkhours, starttime) != 0))
                                {
                                    free = subtractFromNine(starttime, endtime, i);
                                    free.setEmail(calendarID);

                                    if ((DateTime.Compare(starttime.Date, ((DateTime)responseOBJ.Calendars[calendarID].Busy[i + 1].Start).Date) < 0))
                                    {

                                        i--;
                                        j++;
                                    }

                                }



                                else if (startworkhours.Equals(starttime) || j > 0)
                                {
                                    //Console.WriteLine("urghhhhh" + DateTime.Compare(starttime.Date, ((DateTime)responseOBJ.Calendars[calendarID].Busy[i + 1].Start).Date));

                                    if (startworkhours.Equals(starttime) && j == 0)
                                    {

                                       // Console.WriteLine("inside second if ");
                                        startatNine = true;
                                        if (DateTime.Compare(starttime.Date, ((DateTime)responseOBJ.Calendars[calendarID].Busy[i + 1].Start).Date) < 0)
                                        {
                                          // Console.WriteLine("::::::::::::::::::::::::::::::::::::::::::::::::::::");
                                            j++;
                                            i--;

                                        }


                                    }

                                    else if ((DateTime.Compare(starttime.Date, ((DateTime)responseOBJ.Calendars[calendarID].Busy[i + 1].Start).Date) < 0) && j > 0)
                                    {
                                        //Console.WriteLine("inside first if ");
                                        free = subtractFromFive(starttime, endtime, i);
                                        free.setEmail(calendarID);
                                        j = 0;
                                    }

                                }

                            }




                            else if (i + 1 == count && singleevent == false)
                            {
                                if (j == 0)
                                {
                                   //Console.WriteLine(" else if (i + 1 == count && !(endworkhours.equals(endtime)) && singleevent == false) j==0 ");
                                    DateTime tempstarttime = (responseOBJ.Calendars[calendarID].Busy[i - 1].Start).Value;
                                    DateTime tempendtime = (responseOBJ.Calendars[calendarID].Busy[i - 1].End).Value;

                                    if ((DateTime.Compare(starttime.Date, tempendtime.Date) == 0))
                                    {
                                        //Console.WriteLine(" entered if ");
                                        TimeSpan interval = starttime - tempendtime;
                                        free.setStart(starttime - interval);
                                        //Console.WriteLine(free.Start);
                                        free.setEnd(starttime);
                                        free.setEmail(calendarID);

                                  }

                                    else
                                    {
                                        free = subtractFromNine(starttime, endtime, i);
                                        
                                    }

                                    if (!(endworkhours.Equals(endtime)))
                                    {
                                        j++;
                                        i--;
                                    }

                                }
                                else
                                {
                                    if (!(endworkhours.Equals(endtime)))
                                    {
                                        free = subtractFromFive(starttime, endtime, i);
                                        j = 0;
                                    }
                                    else endFive = true;


                                }


                            }
                            else if (singleevent)
                            {
                                //Console.WriteLine("inside single event " + j + " " + starttime + " " + startworkhours);
                                if (j == 0 && !(DateTime.Compare(starttime, startworkhours) == 0))
                                {
                                    free = subtractFromNine(starttime, endtime, i);


                                    if (!(DateTime.Compare(endtime, endworkhours) == 0))
                                    {
                                        j++;
                                        i--;
                                    }

                                }


                                else
                                {
                                    if (!(DateTime.Compare(endtime, endworkhours) == 0))
                                    {
                                        free = subtractFromFive(starttime, endtime, i);
                                        free.setEmail(calendarID);
                                        j = 0;
                                    }
                                    else endFive = true;

                                }


                            }

                            else
                            {

                               //Console.WriteLine("yaaaaaaaaaaaaaaarab");
                                DateTime tempstarttime = (responseOBJ.Calendars[calendarID].Busy[i - 1].Start).Value;
                                DateTime tempendtime = (responseOBJ.Calendars[calendarID].Busy[i - 1].End).Value;
                                if (j == 0)
                                {
                                    if ((DateTime.Compare(tempendtime.Date, starttime.Date) == 0))
                                    {
                                        //Console.WriteLine("entered the first");
                                        TimeSpan interval = starttime - tempendtime;
                                        free.setStart(starttime - interval);
                                        free.setEnd(starttime);
                                        free.setEmail(calendarID);


                                    }

                                    else
                                    {
                                        //Console.WriteLine("entered the second");
                                        if (startworkhours.Equals(starttime))
                                        {
                                           // Console.WriteLine("statrt at nine");
                                            startatNine = true;
                                        }
                                        else
                                        {
                                            free = subtractFromNine(starttime, endtime, i);
                                        }

                                    }

                                    if ((DateTime.Compare(starttime.Date, ((DateTime)(responseOBJ.Calendars[calendarID].Busy[i + 1].Start).Value).Date) < 0))
                                    {
                                       //Console.WriteLine("entered the comparason");
                                        i--;
                                        j++;



                                    }

                                }
                                else
                                {
                                    //Console.WriteLine("supposed " + endworkhours.Equals(endtime));
                                    if (!(endworkhours.Equals(endtime)))
                                    {
                                        free = subtractFromFive(starttime, endtime, i);
                                        free.setEmail(calendarID);
                                        j = 0;
                                    }
                                    else { endFive = true; j = 0; }

                                }



                            }
                            i++;


                            if (!startatNine && !endFive)
                            {
                                //Console.WriteLine("befor adding " + free.Start + "  " + free.End);
                                freeTimeList.Add(free);
                               //  Console.WriteLine(calendarID + " " + free.Start + " " + free.End);

                            }

                            //Console.WriteLine("after adding to list");

                        }//end while
                    }//end if (count !=0 )

                   
                    personAndFreeTimes.Add(calendarID.Substring(0, calendarID.IndexOf(".")), freeTimeList);

                    //foreach (String k in personAndFreeTimes.Keys)
                    //{
                    //    foreach (FreeTime f in personAndFreeTimes[k])
                    //        //Console.WriteLine(k + " " + f.Start + " " + f.End);
                    //}

                    freeTimeList.Sort();

                   

                    }//end foreach


                numberOfEmails = personAndFreeTimes.Count;
                Stack myStack = new Stack();
                foreach (String key in personAndFreeTimes.Keys)
                {


                    myStack.Push(personAndFreeTimes[key]);
                }

                //  -----------------------------------  All People View   --------------------------------------------------   //

                if (ViewFormat == "All People")
                {

                 
                    Stack s1 = new Stack();
                    Stack s2 = new Stack();
                    foreach (String key in personAndFreeTimes.Keys)
                    {
                        // string result = noEvents.Find(delegate(String  i) { return i == key; });
                        //  if(result!=null)
                        KeyValuePair<List<FreeTime>, String> pair = new KeyValuePair<List<FreeTime>, String>(personAndFreeTimes[key], key);

                        s1.Push(pair);
                        s2.Push(pair);
                        //Console.WriteLine("pair " + pair.Key.ToString() + " " + pair.Value);
                    }



                    while (s1.Count != 0)
                    {
                        KeyValuePair<List<FreeTime>, String> sp1 = (KeyValuePair<List<FreeTime>, String>)s1.Pop();
                        while (s2.Count != 0)
                        {
                            KeyValuePair<List<FreeTime>, String> sp2 = (KeyValuePair<List<FreeTime>, String>)s2.Pop();
                            if (!(sp1.Value.Equals(sp2.Value)))
                                FindMutual2(sp1, sp2);
                        }
                        s2 = s1;
                    }



                    FinalMutual2();

                    if (personAndFreeTimes.Count > 2)
                    {

                        FinalMutual3();
                        FinalMutual3();
                    }
                    //Console.WriteLine(" back to main");
                    // FinalMutual4();
                    calendarEvents = ConvertToMutualCalendarEvents(FinalFreeBusy);

                }//end if All People view

                //  -----------------------------------     --------------------------------------------------   //


                //  -----------------------------------  "Some People" View   --------------------------------------------------   //

                if (ViewFormat == "Some People")
                {

                    Stack s1 = new Stack();
                    Stack s2 = new Stack();
                    foreach (String key in personAndFreeTimes.Keys)
                    {
                        
                        KeyValuePair<List<FreeTime>, String> pair = new KeyValuePair<List<FreeTime>, String>(personAndFreeTimes[key], key);

                        s1.Push(pair);
                        s2.Push(pair);
                        //Console.WriteLine("pair " + pair.Key.ToString() + " " + pair.Value);
                    }



                    while (s1.Count != 0)
                    {
                        KeyValuePair<List<FreeTime>, String> sp1 = (KeyValuePair<List<FreeTime>, String>)s1.Pop();
                        while (s2.Count != 0)
                        {
                            KeyValuePair<List<FreeTime>, String> sp2 = (KeyValuePair<List<FreeTime>, String>)s2.Pop();
                            if (!(sp1.Value.Equals(sp2.Value)))
                                FindMutual2(sp1, sp2);
                        }
                        s2 = s1;
                    }



                    FinalMutual2();

                    // if (personAndFreeTimes.Count > 2)
                    int num = 1;
                    while( num<personAndFreeTimes.Count)
                    {
                        FinalMutual3();
                        num++;
                    }
                    FinalMutual3();
                    
                    calendarEvents = ConvertToFreeBusyCalendarEvents(FinalFreeBusy);
                } //end free busy view 

                //Console.WriteLine("before sort");
                calendarEvents.SortByDate();

                log.Debug("Successfully compared  events from other Google Calendars");
            }// end try


            catch (Exception ex)
            {
                log.Error("Failed to compare  events from other  Google Calendars with error:", ex);
            }

            
            return calendarEvents;

        }