-
Notifications
You must be signed in to change notification settings - Fork 392
/
SyncTokenSample.java
144 lines (129 loc) · 5.14 KB
/
SyncTokenSample.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
/*
* Copyright (c) 2014 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.
*/
package com.google.api.services.samples.calendar.sync;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.util.DateTime;
import com.google.api.client.util.store.DataStore;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.CalendarScopes;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.Events;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
/**
* A sample that demonstrates how to efficiently sync
* <a href="https://developers.google.com/google-apps/calendar/v3/reference/">Calendar resource</a>
* using sync tokens.
*
* @author ekoleda+devrel@google.com (Eric Koleda)
*/
public class SyncTokenSample {
/**
* The key in the sync settings datastore that holds the current sync token.
*/
private static final String SYNC_TOKEN_KEY = "syncToken";
/**
* Global instance of the Calendar client.
*/
private static Calendar client;
/**
* Global instance of the event datastore.
*/
private static DataStore<String> eventDataStore;
/**
* Global instance of the sync settings datastore.
*/
private static DataStore<String> syncSettingsDataStore;
public static void main(String[] args) {
try {
List<String> scopes = Lists.newArrayList(CalendarScopes.CALENDAR_READONLY);
client = Utils.createCalendarClient(scopes);
eventDataStore = Utils.getDataStoreFactory().getDataStore("EventStore");
syncSettingsDataStore = Utils.getDataStoreFactory().getDataStore("SyncSettings");
run();
} catch (Throwable t) {
t.printStackTrace();
System.exit(1);
}
}
/**
* Syncs events from the user's primary calendar to a local datastore. A full sync is performed on
* the first run, with incremental syncs on subsequent runs.
*/
private static void run() throws IOException {
// Construct the {@link Calendar.Events.List} request, but don't execute it yet.
Calendar.Events.List request = client.events().list("primary");
// Load the sync token stored from the last execution, if any.
String syncToken = syncSettingsDataStore.get(SYNC_TOKEN_KEY);
if (syncToken == null) {
System.out.println("Performing full sync.");
// Set the filters you want to use during the full sync. Sync tokens aren't compatible with
// most filters, but you may want to limit your full sync to only a certain date range.
// In this example we are only syncing events up to a year old.
Date -1);
request.setTimeMin(new DateTime(oneYearAgo, TimeZone.getTimeZone("UTC")));
} else {
System.out.println("Performing incremental sync.");
request.setSyncToken(syncToken);
}
// Retrieve the events, one page at a time.
String pageToken = null;
Events events = null;
do {
request.setPageToken(pageToken);
try {
events = request.execute();
} catch (GoogleJsonResponseException e) {
if (e.getStatusCode() == 410) {
// A 410 status code, "Gone", indicates that the sync token is invalid.
System.out.println("Invalid sync token, clearing event store and re-syncing.");
syncSettingsDataStore.delete(SYNC_TOKEN_KEY);
eventDataStore.clear();
run();
} else {
throw e;
}
}
List<Event> items = events.getItems();
if (items.size() == 0) {
System.out.println("No new events to sync.");
} else {
for (Event event : items) {
syncEvent(event);
}
}
pageToken = events.getNextPageToken();
} while (pageToken != null);
// Store the sync token from the last request to be used during the next execution.
syncSettingsDataStore.set(SYNC_TOKEN_KEY, events.getNextSyncToken());
System.out.println("Sync complete.");
}
/**
* Sync an individual event. In this example we simply store it's string represenation to a file
* system data store.
*/
private static void syncEvent(Event event) throws IOException {
if ("cancelled".equals(event.getStatus()) && eventDataStore.containsKey(event.getId())) {
eventDataStore.delete(event.getId());
System.out.println(String.format("Deleting event: ID=%s", event.getId()));
} else {
eventDataStore.set(event.getId(), event.toString());
System.out.println(
String.format("Syncing event: ID=%s, Name=%s", event.getId(), event.getSummary()));
}
}
}