📄 gcalendar.java
字号:
/* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You 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. */import java.util.*;import java.util.concurrent.*;import java.net.URL;import java.security.*;import java.text.SimpleDateFormat;import com.google.gdata.client.calendar.*;import com.google.gdata.data.extensions.*;import com.google.gdata.data.*;import com.google.gdata.data.calendar.*;import com.google.gdata.util.*;import com.google.gdata.client.*;/** * This class provides a simple abstraction for interacting with the * Google Calendar Data API. It runs as a separate thread so that requests * can be posted asynchronously. * * All requests are shipped through the requestQueue. The run method * reads from the request queue and ships the requests to Google Calendar. * Results are then fed back on the response queue. */public class GCalendar { private static String GOOGLE_CAL_URL_PREFIX = "http://www.google.com/calendar/feeds/"; private static String AUTHOR_NAME="David Van Couvering"; private static String AUTHOR_EMAIL="david.vancouvering@gmail.com"; private String calendarId; private URL feedUrl; private EventFeed feed; private CalendarService service; private String gmtOffset; private String startDay; private String endDay; /** * Creates a new instance of GCalendarService * * @param calendarId * The special token that identifies the calendar. * @see DerbyCalendarApplet.connect() for a full description of this. * * @param user * Your Google Calendar account name, e.g. david.vancouvering@gmail.com * * @param password * Your Google Calendar password * * * @param startDay * the starting day for the calendar, in the format <yyyy>-<mm>-<dd> * * @param endDay * the starting day for the calendar, in the format <yyyy>-<mm>-<dd> */ public GCalendar(String calendarId, String gmtOffset, final String user, final String password, String startDay, String endDay) throws Exception { this.calendarId = calendarId; this.gmtOffset = gmtOffset; this.startDay = startDay; this.endDay = endDay; // We're accessing a read-write feed, and we want full information this.feedUrl = new URL(GOOGLE_CAL_URL_PREFIX + calendarId + "/private/full"); try { AccessController.doPrivileged( new PrivilegedExceptionAction<Object>() { public Object run() throws Exception { service = new CalendarService("DerbyCalendar Demo"); service.setUserCredentials(user, password); // Send the request and receive the response: feed = service.getFeed(feedUrl, EventFeed.class); return null; } } ); } catch (PrivilegedActionException e) { handleException(e.getException(), "Unable to get Google Calendar feed", null); } } /** * Add an event to the Google Calendar * * @param event * The event to add * * @return * The event that was added, modified as needed by what was * returned from Google Calendar. In particular, the id has * been updated to be the id provided by Google Calendar. */ public CalEvent addEvent(String date, String title) throws Exception { EventEntry newEntry = new EventEntry(); newEntry.setTitle(new PlainTextConstruct(title)); Person author = new Person(AUTHOR_NAME, null, AUTHOR_EMAIL); newEntry.getAuthors().add(author); DateTime startTime = DateTime.parseDateTime(date + "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(date); DateTime endTime = DateTime.parseDateTime(endTimeString + "" + "T00:00:00" + gmtOffset); endTime.setDateOnly(true); When eventTimes = new When(); eventTimes.setStartTime(startTime); eventTimes.setEndTime(endTime); newEntry.addTime(eventTimes); // Need a 'final' version of the entry so it can be used // inside the privileged block. final EventEntry finalEntry = newEntry; // Send the request. The response contains the final version of the // entry. In particular, we need to store the edit URL for future // updates EventEntry addedEntry = null; try { addedEntry = AccessController.doPrivileged( new PrivilegedExceptionAction<EventEntry>() { public EventEntry run() throws Exception { return service.insert(feedUrl, finalEntry); } } ); } catch (PrivilegedActionException e) { handleException(e.getException(), "Unable to add event", finalEntry); } return new CalEvent(addedEntry.getId(), date, title, addedEntry.getEditLink().getHref(), addedEntry.getVersionId()); } /** * Get the next day given a day string of the format <yyyy>-<mm>-<dd> */ private String getNextDay(String day) throws Exception { // See how simple it is to add a day to a date? :) // Thanks be to Google for finding this code... java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd"); Date date = sdf.parse(day); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.DATE, 1); return sdf.format(cal.getTime()); } /** * Update an event * * @param id * The id of the event to be updated * * @param title * The new title for the event - that's the only thing we * allow to be changed at this time -- keeping things simple. * * @return * A new instance of an event, with fields updated as necessary * from Google (e.g. version id has changed) */ public CalEvent updateEvent(CalEvent event) throws Exception { final EventEntry entry = createEventEntry(event); final URL editURL = new URL(event.getEditURL()); // Send the request. EventEntry updatedEntry = null; try { updatedEntry = AccessController.doPrivileged( new PrivilegedExceptionAction<EventEntry>() { public EventEntry run() throws Exception { return service.update(editURL, entry); } } ); } catch (PrivilegedActionException e) { handleException(e.getException(), "Unable to update event", entry);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -