1: public class CalendarUtil {
2: private final static Logger logger = Logger.getLogger(CalendarUtil.class.getName());
3: private String username;
4: private String password;
5: String calendarName;6: private URL calendarUrl;
7: 8: private CalendarService service;
9: 10: // The base URL for a user's calendar metafeed (needs a username appended).
11: private static final String METAFEED_URL_BASE =
12: "http://www.google.com/calendar/feeds/";
13: 14: private static final String CALENDAR_URL_SUFFIX = "/private/full";
15: 16: public void init() {
17: service = new CalendarService("DR-Appt-App-1");
18: try {
19: service.setUserCredentials(username, password);20: } catch (AuthenticationException e) {
21: throw new RuntimeException("Could not authenticate to Google Calendar with username: " + username, e);
22: } 23: String calendarId = null;24: try {
25: calendarId = getCalendarId();26: } catch (IOException e) {
27: throw new RuntimeException("Error caught trying to connect to calendar: ", e);
28: } catch (ServiceException e) {
29: throw new RuntimeException("Error caught trying to connect to calendar: ", e);
30: }31: try {
32: calendarUrl = new URL(METAFEED_URL_BASE + calendarId + CALENDAR_URL_SUFFIX);
33: } catch (MalformedURLException e) {
34: throw new RuntimeException("Bad URL: " + calendarUrl.toString(), e);
35: }36: if (logger.isDebugEnabled()) {
37: logger.debug("Url is " + calendarUrl.toString());
38: } 39: } 40: 41: 42: //get id of correct calendar
43: private String getCalendarId() throws IOException, ServiceException {
44: String calendarId = null;45: URL metafeedUrl = new URL(METAFEED_URL_BASE + username);
46: if (logger.isDebugEnabled()) {
47: logger.debug("Url is " + metafeedUrl.toString());
48: }49: CalendarFeed resultFeed = service.getFeed(metafeedUrl, CalendarFeed.class);
50: for (int i = 0; i < resultFeed.getEntries().size(); i++) {
51: CalendarEntry entry = resultFeed.getEntries().get(i);52: if (calendarName.equalsIgnoreCase(entry.getTitle().getPlainText())) {
53: calendarId = new File(entry.getId()).getName();
54: break;
55: } 56: }57: return calendarId;
58: } 59: Using the above class, you now can access the non primary calendar simply using the syntax:
calendarUtil.getService().insert(…)
or any other service function.