File: BookThemAll.cs

package info (click to toggle)
gdata-sharp 2.2.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 12,092 kB
  • sloc: cs: 67,781; xml: 38,234; python: 163; makefile: 149; sh: 27
file content (148 lines) | stat: -rw-r--r-- 5,456 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
// Copyright 2011 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

using System;
using System.Text;
using System.Collections;
using Google.GData.Apps;
using Google.GData.Client;
using Google.GData.Calendar;
using Google.GData.Extensions;
using Google.GData.Extensions.Apps;

namespace BookThemAll
{
    public class NoAvailableSlotWithinLimitException : Exception
    {
    }

    public class BookThemAllSample
    {
        protected AppsService apps;
        protected CalendarService calendar;
        protected static string feedUrl = "https://www.google.com/calendar/feeds/{0}/private/full";
        private string admin;

        public BookThemAllSample(string domain, string admin, string password)
        {
            apps = new AppsService(domain, admin, password);
            calendar = new CalendarService("BookThemAll");
            calendar.setUserCredentials(admin, password);
            this.admin = admin;
        }

        public AtomEntryCollection RetrieveAllGroupMembers(string group)
        {
            return apps.Groups.RetrieveAllMembers(group).Entries;
        }

        public bool IsUserAvailable(string user, DateTime datetime)
        {
            EventQuery query = new EventQuery(string.Format(feedUrl, user));
            query.StartTime = datetime;      
            query.EndTime = datetime + new System.TimeSpan(0, 0, 30, 0);
            EventFeed feed = calendar.Query(query);
            return feed.Entries.Count == 0;
        }

        public bool AreUsersAvailable(AtomEntryCollection users, DateTime datetime)
        {
            foreach (AppsExtendedEntry entry in users)
            {
                string user = entry.getPropertyValueByName("memberId");
                bool isAvailable = IsUserAvailable(user, datetime);
                if (!isAvailable)
                {
                    return false;
                }
            }
            return true;
        }

        public DateTime FindNextAvailableTimeSlot(AtomEntryCollection users, DateTime limit)
        {
            DateTime nextHour = NextHour();
            while (nextHour < limit)
            {
                if (AreUsersAvailable(users, nextHour))
                {
                    return nextHour;
                }
                nextHour += new System.TimeSpan(0, 0, 30, 0);
            }
            throw new NoAvailableSlotWithinLimitException();
        }

        public DateTime NextHour()
        {
            DateTime now = DateTime.Now;
            return now + new System.TimeSpan(0, 0, 59-now.Minute, 59-now.Second, 1000-now.Millisecond);
        }

        public EventEntry CreateEvent(string title, DateTime date, AtomEntryCollection users)
        {
            EventEntry entry = new EventEntry();
            entry.Title.Text = title;
            entry.Content.Content = title + "Content";
            When eventTime = new When(date, date + new System.TimeSpan(0, 0, 30, 0));
            entry.Times.Add(eventTime);
            foreach (AppsExtendedEntry user in users)
            {
                string member = user.getPropertyValueByName("memberId");
                Who who = new Who();
                who.Email = member;
                who.Rel = Who.RelType.EVENT_ATTENDEE;
                entry.Participants.Add(who);
            }
            Uri postUri = new Uri(string.Format(feedUrl, admin));
            return calendar.Insert(postUri, entry);
        }

        public DateTime Run(string group)
        {
            DateTime now = DateTime.Now;
            AtomEntryCollection users =  RetrieveAllGroupMembers(group);
            DateTime nextSlot = FindNextAvailableTimeSlot(users, now + new System.TimeSpan(30, 0, 0, 0));
            CreateEvent("Meeting", nextSlot, users);
            return nextSlot;
        }

        static void Main(string[] args)
        {
            if (args.Length != 4)
            {
                Console.WriteLine("Usage: BookThemAll <domain> <admin> <password> <group>");
            }
            else
            {
                // Workaround Certificate validation errors
                System.Net.ServicePointManager.ServerCertificateValidationCallback +=
                  delegate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                           System.Security.Cryptography.X509Certificates.X509Chain chain,
                           System.Net.Security.SslPolicyErrors sslPolicyErrors)
                  {
                    return true;
                  };
                string domain = args[0];
                string admin = args[1];
                string password = args[2];
                string group = args[3];
                BookThemAllSample bookthemall = new BookThemAllSample(domain, admin, password);
                Console.WriteLine("Event created at: {0}", bookthemall.Run(group));
            }
        }
    }
}