📄 gcalendar.java
字号:
}
return createDerbyCalEvent(updatedEntry);
}
/**
* Delete an event
*
* @param editURLString
* The string representation of the edit URL
*/
public void deleteEvent(final String editURLString) throws Exception {
final URL editURL = new URL(editURLString);
// Send the request. ry;
try {
AccessController.doPrivileged(
new PrivilegedExceptionAction<Object>() {
public Object run() throws Exception {
service.delete(editURL);
return null;
}
}
);
} catch (PrivilegedActionException e) {
handleException(e.getException(),
"Unable to delete event with edit URL " + editURLString, null);
}
}
/**
* Get the list of events. You would want to
* use this, for example, if you are trying to get the calendar when
* the application first starts up.
*
* @return a HashMap of events for the calendar, where the hash id
* is the id of the event
*/
public Collection<CalEvent> getEvents() throws Exception {
CalendarEventFeed resultFeed = null;
// Do this in a privileged block, as it connects to the Internet
try {
resultFeed = AccessController.doPrivileged(
new PrivilegedExceptionAction<CalendarEventFeed>() {
public CalendarEventFeed run() throws Exception {
CalendarQuery myQuery = new CalendarQuery(feedUrl);
myQuery.setMinimumStartTime(DateTime.parseDateTime(
startDay + "T00:00:00" + gmtOffset));
myQuery.setMaximumStartTime(DateTime.parseDateTime(
endDay + "T23:59:59" + gmtOffset));
// Send the request and receive the response:
return service.query(myQuery, CalendarEventFeed.class);
}
}
);
} catch (PrivilegedActionException e) {
handleException(e.getException(), "Unable to get entries from " +
"Google Calendar", null);
}
// Build a list of DerbyCal events based on these entries
ArrayList<CalEvent> calentries = new ArrayList<CalEvent>();
// debugging
// System.out.println("Entries from Google Calendar:");
for ( EventEntry entry : resultFeed.getEntries() ) {
calentries.add(createDerbyCalEvent(entry));
}
return calentries;
}
public void clearCalendar() throws Exception {
// Get the latest list of entries
Collection<CalEvent> events = getEvents();
// Delete the entries one at a time
for ( CalEvent event : events ) {
deleteEvent(event.getEditURL());
}
}
/**
* Create a Google EventEntry from an event object
*/
private EventEntry createEventEntry(CalEvent event) throws Exception {
EventEntry entry = new EventEntry();
entry.setTitle(new PlainTextConstruct(event.getTitle()));
entry.setId(event.getId());
Person author = new Person(AUTHOR_NAME, null, AUTHOR_EMAIL);
entry.getAuthors().add(author);
DateTime startTime = DateTime.parseDateTime(event.getDate() + "T00:00:00" +
gmtOffset);
startTime.setDateOnly(true);
// An all day event has an end time of the next day. Need to
// calculate that using the wonderful Java date APIs...
String endTimeString = getNextDay(event.getDate());
DateTime endTime = DateTime.parseDateTime(endTimeString + "" +
"T00:00:00" + gmtOffset);
endTime.setDateOnly(true);
When eventTimes = new When();
eventTimes.setStartTime(startTime);
eventTimes.setEndTime(endTime);
entry.addTime(eventTimes);
return entry;
}
/**
* Create a DerbyCalEvent from an EventEntry
*/
private CalEvent createDerbyCalEvent(EventEntry entry) throws Exception {
String title = entry.getTitle().getPlainText();
// Simplifying assumption: all entries are day-long events.
// No more, no less
List<When> times = entry.getTimes();
String start = times.get(0).getStartTime().toString();
String editURL = entry.getEditLink().getHref();
CalEvent event = new CalEvent(entry.getId(), start, title,
editURL, entry.getVersionId());
return event;
}
/**
* Handle an exception received when invoking a method on Google Calendar
*
* @param e
* The exception that was encountered. Obtain this by calling
* getException() on the PrivilegedActionException
*
* @param message
* This is the message specific to the invocation that got the exception,
* such as "unable to update event". This is prepended upon the details
* provided by this method
*
* @param entry
* The event entry associated with this message, if available. Otherwise
* set this to null.
*
* @throws Exception
* When the exception encountered should result in an exception being thrown
* to the caller. This is almost always the case.
*/
private void handleException(Exception e, String message, EventEntry entry)
throws Exception {
String entryInfo = "";
String id;
String title;
String date;
if ( entry != null ) {
id = parseId(entry.getId());
title = entry.getTitle().getPlainText();
List<When> times = entry.getTimes();
date = times.get(0).getStartTime().toString();
entryInfo = date + ": " + title;
}
if ( e instanceof java.net.NoRouteToHostException ) {
throw new NetworkDownException(e);
}
if ( e instanceof ResourceNotFoundException ) {
message += ": the event " + entryInfo + " does not appear " +
"to exist. Perhaps somebody else deleted it?";
}
else if ( e instanceof ServiceException ) {
ServiceException se = (ServiceException)e;
// Let's see if this is a conflict where we are trying to
// update an entry that has been modified. The error code
// is 400, which is not helpful, it's supposed to be 403,
// "conflict", but oh well...
String responseBody = se.getResponseBody();
if ( responseBody != null &&
responseBody.contains("Sequence ids from event") &&
responseBody.contains("do not match")) {
message += ": unable to update event " +
entryInfo + ", it appears someone else has updated it." +
" The event will be updated to contain the latest " +
" version from Google Calendar.";
}
else {
message += ": exception from Google Calendar. HTTP error code " +
"is " + se.getHttpErrorCodeOverride() + "; response body is: " +
se.getResponseBody();
}
} else {
message += ": please see next exception for details.";
}
throw new Exception(message, e);
}
/**
* Strip off the unique id of the entry from the full Google
* Calendar id, which includes the Feed URL
*/
private String parseId(String id) {
if ( id == null ) {
return null;
}
return id.substring(id.lastIndexOf("/") + 1, id.length());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -