This repository has been archived by the owner on May 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
CalendarSnippets.java
424 lines (379 loc) · 16 KB
/
CalendarSnippets.java
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
* Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license. See full license at the bottom of this file.
*/
package com.microsoft.office365.snippetapp.Snippets;
import android.util.Patterns;
import com.microsoft.outlookservices.Attendee;
import com.microsoft.outlookservices.BodyType;
import com.microsoft.outlookservices.DayOfWeek;
import com.microsoft.outlookservices.EmailAddress;
import com.microsoft.outlookservices.Event;
import com.microsoft.outlookservices.ItemBody;
import com.microsoft.outlookservices.PatternedRecurrence;
import com.microsoft.outlookservices.RecurrencePattern;
import com.microsoft.outlookservices.RecurrencePatternType;
import com.microsoft.outlookservices.RecurrenceRange;
import com.microsoft.outlookservices.RecurrenceRangeType;
import com.microsoft.outlookservices.ResponseStatus;
import com.microsoft.outlookservices.ResponseType;
import com.microsoft.outlookservices.odata.OutlookClient;
import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ExecutionException;
import java.util.regex.Matcher;
public class CalendarSnippets {
OutlookClient mCalendarClient;
public CalendarSnippets(OutlookClient mailClient) {
mCalendarClient = mailClient;
}
/**
* Return a list of calendar events from the default calendar and ordered by the
* Start field. The range of events to be returned includes events from
* 1 week prior to current date through 1 week into the future. 10 events
* are returned per page.
* <p/>
* This snippet only selects the Subject, Start, and End fields
* to reduce network traffic.
*
* @return List. A list of the {@link com.microsoft.outlookservices.Event} objects
*/
public List<Event> getO365Events() throws ExecutionException, InterruptedException {
int EVENT_RANGE_START = 1;
int EVENT_RANGE_END = 1;
int PAGE_SIZE = 10;
String SORT_COLUMN = "Start";
java.util.Calendar dateStart = java.util.Calendar.getInstance();
//Set the date range of the calendar view to retrieve
dateStart.add(Calendar.WEEK_OF_MONTH, -EVENT_RANGE_START);
java.util.Calendar dateEnd = java.util.Calendar.getInstance();
dateEnd.add(Calendar.WEEK_OF_MONTH, EVENT_RANGE_END);
return mCalendarClient
.getMe()
.getCalendarView()
.addParameter("startdatetime", dateStart)
.addParameter("enddatetime", dateEnd)
.top(PAGE_SIZE)
.select("Subject,Start,End")
.orderBy(SORT_COLUMN)
.read()
.get();
}
/**
* Removes an event specified by the id
* are returned per page
*
* @param eventId The id of the event to be removed
*/
public void deleteCalendarEvent(String eventId)
throws ExecutionException
, InterruptedException {
mCalendarClient
.getMe()
.getEvents()
.getById(eventId).delete().get();
}
/**
* Creates an event
*
* @param subject The subject of the event
* @param itemBodyHtml The body of the event as HTML
* @param startDate The start date of the event
* @param endDate The end date of the event
* @param attendeeAddresses A list of attendee email addresses
* @return String The id of the created event
*/
public String createCalendarEvent(
String subject
, String itemBodyHtml
, java.util.Calendar startDate
, Calendar endDate
, List<String> attendeeAddresses)
throws ExecutionException
, InterruptedException {
Event newEvent = new Event();
newEvent.setSubject(subject);
//Create an event item body with HTML formatted text
ItemBody itemBody = new ItemBody();
itemBody.setContent(itemBodyHtml);
itemBody.setContentType(BodyType.HTML);
//Fill new calendar event with body, start date, all day flag
//and end date
newEvent.setBody(itemBody);
newEvent.setStart(startDate);
newEvent.setIsAllDay(false);
newEvent.setEnd(endDate);
Matcher matcher;
List<Attendee> attendeeList = new ArrayList<>();
for (String attendeeAddress : attendeeAddresses) {
// Add mail to address if mailToString is an email address
matcher = Patterns.EMAIL_ADDRESS.matcher(attendeeAddress);
if (matcher.matches()) {
EmailAddress emailAddress = new EmailAddress();
emailAddress.setAddress(attendeeAddress);
Attendee attendee = new Attendee();
attendee.setEmailAddress(emailAddress);
attendeeList.add(attendee);
}
}
newEvent.setAttendees(attendeeList);
return mCalendarClient
.getMe()
.getEvents()
.select("ID")
.add(newEvent).get().getId();
}
/**
* Creates a recurring event. This snippet will create an event that recurs
* every Tuesday and Thursday from 1PM to 2PM. You can modify this snippet
* to work with other recurrence patterns.
*
* @param subject The subject of the event
* @param itemBodyHtml The body of the event as HTML
* @param attendees A list of attendee email addresses
* @return String The id of the created event
*/
public String createRecurringCalendarEvent(
String subject
, String itemBodyHtml
, List<String> attendees)
throws ExecutionException
, InterruptedException {
//Create a new Office 365 Event object
Event newEvent = new Event();
newEvent.setSubject(subject);
ItemBody itemBody = new ItemBody();
itemBody.setContent(itemBodyHtml);
itemBody.setContentType(BodyType.HTML);
newEvent.setBody(itemBody);
//Set the attendee list
List<Attendee> attendeeList = convertEmailStringsToAttendees(attendees);
newEvent.setAttendees(attendeeList);
//Set start date to the next occurring Tuesday
DateTime startDate = DateTime.now();
if (startDate.getDayOfWeek() < DateTimeConstants.TUESDAY) {
startDate = startDate.dayOfWeek().setCopy(DateTimeConstants.TUESDAY);
} else {
startDate = startDate.plusWeeks(1);
startDate = startDate.dayOfWeek().setCopy(DateTimeConstants.TUESDAY);
}
//Set start time to 1 PM
startDate = startDate.hourOfDay().setCopy(13)
.withMinuteOfHour(0)
.withSecondOfMinute(0)
.withMillisOfSecond(0);
//Set end time to 2 PM
DateTime endDate = startDate.hourOfDay().setCopy(14);
//Set start and end time on the new Event (next Tuesday 1-2PM)
newEvent.setStart(startDate.toCalendar(Locale.getDefault()));
newEvent.setIsAllDay(false);
newEvent.setEnd(endDate.toCalendar(Locale.getDefault()));
//Configure the recurrence pattern for the new event
//In this case the meeting will occur every Tuesday and Thursday from 1PM to 2PM
RecurrencePattern recurrencePattern = new RecurrencePattern();
List<DayOfWeek> daysMeetingRecursOn = new ArrayList();
daysMeetingRecursOn.add(DayOfWeek.Tuesday);
daysMeetingRecursOn.add(DayOfWeek.Thursday);
recurrencePattern.setType(RecurrencePatternType.Weekly);
recurrencePattern.setDaysOfWeek(daysMeetingRecursOn);
recurrencePattern.setInterval(1); //recurs every week
//Create a recurring range. In this case the range does not end
//and the event occurs every Tuesday and Thursday forever.
RecurrenceRange recurrenceRange = new RecurrenceRange();
recurrenceRange.setType(RecurrenceRangeType.NoEnd);
recurrenceRange.setStartDate(startDate.toCalendar(Locale.getDefault()));
//Create a pattern of recurrence. It contains the recurrence pattern
//and recurrence range created previously.
PatternedRecurrence patternedRecurrence = new PatternedRecurrence();
patternedRecurrence.setPattern(recurrencePattern);
patternedRecurrence.setRange(recurrenceRange);
//Finally pass the patterned recurrence to the new Event object.
newEvent.setRecurrence(patternedRecurrence);
//Create the event and return the id
return mCalendarClient
.getMe()
.getEvents()
.select("ID")
.add(newEvent).get().getId();
}
/**
* Updates the subject, body, start date, end date, or attendees of an event
*
* @param subject The subject of the event
* @param allDay true if event spans a work day
* @param itemBodyHtml The body of the event as HTML
* @param startDate The start date of the event
* @param endDate The end date of the event
* @param attendees A list of attendee email addresses
* @return String The id of the created event
*/
public Event updateCalendarEvent(
String eventId
, String subject
, boolean allDay
, String itemBodyHtml
, java.util.Calendar startDate
, Calendar endDate
, List<String> attendees) throws ExecutionException, InterruptedException {
Event calendarEvent = getCalendarEvent(eventId);
calendarEvent.setSubject(subject);
if (itemBodyHtml != null && itemBodyHtml.length() > 0) {
ItemBody itemBody = new ItemBody();
itemBody.setContent(itemBodyHtml);
itemBody.setContentType(BodyType.HTML);
calendarEvent.setBody(itemBody);
}
if (startDate != null) {
calendarEvent.setStart(startDate);
}
if (endDate != null) {
calendarEvent.setEnd(endDate);
}
calendarEvent.setIsAllDay(allDay);
if (attendees != null && attendees.size() > 0) {
//clear attendee list and set with new list
calendarEvent.setAttendees(null);
List<Attendee> attendeeList = convertEmailStringsToAttendees(attendees);
calendarEvent.setAttendees(attendeeList);
}
return mCalendarClient
.getMe()
.getEvents()
.getById(eventId)
.select("Subject")
.update(calendarEvent)
.get();
}
/**
* Create a calendar event based off a full Event object passed to this method.
*
* @return the same Event object updated with the ID from the server.
*/
public Event createCalendarEvent(Event eventToCreate) throws ExecutionException, InterruptedException {
return mCalendarClient
.getMe()
.getEvents()
.select("ID")
.add(eventToCreate).get();
}
/**
* Gets the invitation status of a given attendee for a given event
*
* @param eventId The id of the event to be removed
* @param myEmailAddress The email address of the attendee whose status is of interest
*/
public ResponseType getEventAttendeeStatus(String eventId, String myEmailAddress)
throws ExecutionException, InterruptedException {
for (Attendee attendee : getCalendarEvent(eventId).getAttendees()) {
String attendeeEmail = attendee.getEmailAddress().getAddress();
if (attendeeEmail.equalsIgnoreCase(myEmailAddress)) {
return attendee.getStatus().getResponse();
}
}
return null;
}
/**
* Responds to an event invitation on behalf of the specified attendee
*
* @param eventId The id of the event to be removed
* @param myEmailAddress The email address of the attendee whose status is of interest
* @param response The user's response to the event invitation
*/
public Event respondToCalendarEventInvite(
String eventId,
String myEmailAddress,
ResponseType response)
throws ExecutionException, InterruptedException {
//Get the event
Event calendarEvent = getCalendarEvent(eventId);
if (calendarEvent == null || calendarEvent.getAttendees() == null)
return null;
//find the correct attendee and set the response status for that attendee
for (Attendee attendee : calendarEvent.getAttendees()) {
String attendeeEmailAddress = attendee.getEmailAddress().getAddress();
if (attendeeEmailAddress.equalsIgnoreCase(myEmailAddress)) {
ResponseStatus inviteResponse = new ResponseStatus();
inviteResponse.setResponse(response);
attendee.setStatus(inviteResponse);
break;
}
}
//Update calendar event with response status and return the updated event
return mCalendarClient
.getMe()
.getEvents()
.select("ID")
.getById(eventId)
.update(calendarEvent)
.get();
}
/**
* Gets the event object of the event specified by an event Id
*
* @param eventId The id of the event to be responded to
* @return Event The event of interest
*/
public Event getCalendarEvent(String eventId) throws ExecutionException, InterruptedException {
return mCalendarClient
.getMe()
.getEvents()
.getById(eventId)
.select("ID,Attendees")
.read()
.get();
}
/**
* Local helper method that converts an list of email strings into
* a list of attendees.
*
* @param emails A list of email strings
* @return A list of Attendees
*/
private List<Attendee> convertEmailStringsToAttendees(List<String> emails) {
Matcher matcher;
List<Attendee> attendeeList = new ArrayList<>();
for (String email : emails) {
// Add email to attendee list if email string is a valid email address
matcher = Patterns.EMAIL_ADDRESS.matcher(email);
if (matcher.matches()) {
EmailAddress emailAddress = new EmailAddress();
emailAddress.setAddress(email);
Attendee attendee = new Attendee();
attendee.setEmailAddress(emailAddress);
attendeeList.add(attendee);
}
}
return attendeeList;
}
}
// *********************************************************
//
// O365-Android-Snippets, https://github.com/OfficeDev/O365-Android-Snippets
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// *********************************************************