-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalendar.java
More file actions
252 lines (195 loc) · 7.93 KB
/
Copy pathCalendar.java
File metadata and controls
252 lines (195 loc) · 7.93 KB
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
package javaxt.azure.graph;
import java.util.*;
import javaxt.json.*;
//******************************************************************************
//** Calendar
//******************************************************************************
/**
* Used to encapsulate a Calendar and Events
*
******************************************************************************/
public class Calendar extends Node {
private final String userID;
//**************************************************************************
//** Constructor
//**************************************************************************
public Calendar(JSONObject json, String userID, Connection conn){
super(json, conn);
this.userID = userID;
}
//**************************************************************************
//** isDefault
//**************************************************************************
public boolean isDefault(){
return get("isDefaultCalendar").toBoolean();
}
//**************************************************************************
//** getEvents
//**************************************************************************
public ArrayList<Event> getEvents(Integer limit) throws Exception {
ArrayList<Event> events = new ArrayList<>();
LinkedHashMap<String, String> params = new LinkedHashMap<>();
if (limit!=null) params.put("$top", limit+"");
JSONObject json = conn.getResponse(getURL(params));
for (JSONValue r : json.get("value").toJSONArray()){
events.add(new Event(r.toJSONObject(), conn));
}
return events;
}
//**************************************************************************
//** getEvents
//**************************************************************************
public ArrayList<Event> getEvents(javaxt.utils.Date startDate,
javaxt.utils.Date endDate) throws Exception {
LinkedHashMap<String, String> params = new LinkedHashMap<>();
//Create date filter
params.put("$filter", "start/dateTime ge '" + startDate.toISOString() +
"' and end/dateTime le '" + endDate.toISOString() + "'");
//Add order by
params.put("$orderby", "start/dateTime asc");
return getEvents(params);
}
//**************************************************************************
//** getEvents
//**************************************************************************
private ArrayList<Event> getEvents(LinkedHashMap<String, String> params) throws Exception {
ArrayList<Event> events = new ArrayList<>();
//Get calendar events
params.put("$count", "true");
JSONObject json = conn.getResponse(getURL(params));
Integer count = json.get("@odata.count").toInteger();
JSONArray records = json.get("value").toJSONArray();
for (JSONValue r : records){
events.add(new Event(r.toJSONObject(), conn));
}
if (events.isEmpty()) return events;
//Get more calendar events as needed
while (events.size()<count){
params.put("$skip", events.size()+"");
json = conn.getResponse(getURL(params));
for (JSONValue r : json.get("value").toJSONArray()){
events.add(new Event(r.toJSONObject(), conn));
}
}
return events;
}
//**************************************************************************
//** getEvent
//**************************************************************************
public Event getEvent(String id) throws Exception {
String url = getURL() + "/" + id;
return new Event(conn.getResponse(url), conn);
}
//**************************************************************************
//** saveEvent
//**************************************************************************
public void saveEvent(Event event) throws Exception {
String url = getURL();
if (event.getID()==null){
conn.getResponse(url, event.toJson(), "POST");
}
else{
url += "/" + event.getID();
JSONObject json = new JSONObject();
for (String k : updatableKeys){
json.set(k, event.get(k));
}
conn.getResponse(url, json, "PATCH");
}
}
//**************************************************************************
//** deleteEvent
//**************************************************************************
public void deleteEvent(Event event) throws Exception {
deleteEvent(event.getID());
}
//**************************************************************************
//** deleteEvent
//**************************************************************************
public void deleteEvent(String id) throws Exception {
if (id==null) return;
String url = getURL() + "/" + id;
conn.getResponse(url, null, "DELETE");
}
private static String[] updatableKeys = new String[]{
"subject", "body", "categories", "start", "end", "location"
};
//**************************************************************************
//** Event Class
//**************************************************************************
/** Used to represent a calendar event
*/
public class Event extends Node {
public Event(JSONObject json, Connection conn){
super(json, conn);
}
public String getSubject(){
return get("subject").toString();
}
public void setSubject(String subject){
set("subject", subject);
}
public void set(String key, Object val){
for (String k : updatableKeys){
if (key.equalsIgnoreCase(k)){
super.set(k, val);
break;
}
}
}
public javaxt.utils.Date getStartDate(){
return getDate("start").clone();
}
public void setStartDate(javaxt.utils.Date date){
int duration = (int) getEndDate().compareTo(getStartDate(), "minutes");
javaxt.utils.Date endDate = date.clone().add(duration, "minutes");
setDate("start", date);
setDate("end", endDate);
}
public javaxt.utils.Date getEndDate(){
return getDate("end").clone();
}
public void setEndDate(javaxt.utils.Date date){
setDate("end", date);
}
private javaxt.utils.Date getDate(String key){
try{
String dt = get(key).get("dateTime").toString();
String tz = get(key).get("timeZone").toString();
javaxt.utils.Date d = new javaxt.utils.Date(dt);
d.setTimeZone(tz, true);
return d;
}
catch(Exception e){
return null;
}
}
private void setDate(String key, javaxt.utils.Date date){
JSONObject json = get(key).toJSONObject();
json.set("dateTime", date.toISOString());
json.set("timeZone", "UTC");
}
}
//**************************************************************************
//** getURL
//**************************************************************************
private String getURL(){
return getURL(null);
}
private String getURL(LinkedHashMap<String, String> params){
String calendarID = getID();
String url = "/users/" + userID + "/calendars/" + calendarID + "/events";
if (params!=null && !params.isEmpty()){
//Update url
url += "?";
Iterator<String> it = params.keySet().iterator();
while (it.hasNext()){
String key = it.next();
String val = params.get(key);
val = val.replace(" ", "%20");
url += "&" + key + "=" + val;
}
}
return url;
}
}