// The URL for all contacts of the specified user
URL conUrl = new URL( CONTACTS_FEED_URL );
// Get Number of contacts
Query conQuery = new Query(conUrl);
conQuery.setMaxResults(1);
ContactFeed conFeed = myService.query(conQuery, ContactFeed.class);
if (conFeed.getTotalResults() == 0) {
System.out.println("No Contacts");
return;
} else {
// Set Number of contacts for query
conQuery.setMaxResults(conFeed.getTotalResults());
conFeed = myService.query(conQuery, ContactFeed.class);
}
/* //DEBUG:
System.out.println("All contacts:");
for (ContactEntry contact : conFeed.getEntries()) {
System.out.println("\tContact: "
+ (contact.hasName() ? contact.getName().getFullName().getValue() : "null")
+ (contact.hasBirthday() ? "\t" + contact.getBirthday().getValue() : ""));
}
*/
// Create CalendarService and authenticate using ClientLogin
CalendarService calService = new CalendarService(APP_NAME);
calService.setProtocolVersion(CalendarService.Versions.V2_1);
calService.setUserCredentials(userName, userPassword);
// The URL for the own calendars feed of the specified user
URL calUrl = new URL ( CALENDAR_FEED_URL );
// Get Number of calendars
Query calQuery = new Query(calUrl);
calQuery.setMaxResults(1);
CalendarFeed calFeed = calService.query(calQuery, CalendarFeed.class);
if (calFeed.getTotalResults() == 0) {
System.out.println("No Calendars");
} else {
// Set Number of calendars for query
calQuery.setMaxResults(calFeed.getTotalResults());
calFeed = calService.query(calQuery, CalendarFeed.class);
}
// Search calendar
System.out.println("Calendar to use for birthdays");
for (CalendarEntry entry : calFeed.getEntries()) {
if ( entry.getTitle().getPlainText().equals( CALENDAR_NAME ) ) {
calEntry = entry;
}
}
if (calEntry == null) {
// No calendar found - Add a birthday calendar
calEntry = new CalendarEntry();
calEntry.setTitle(new PlainTextConstruct( CALENDAR_NAME ));
calEntry.setSummary(new PlainTextConstruct( CALENDAR_SUMMARY ));
calEntry.setTimeZone(calFeed.getEntries().get(0).getTimeZone());
calEntry.setHidden(HiddenProperty.FALSE);
calEntry.setColor(new ColorProperty( CALENDAR_COLOR ));
calEntry.setSelected(SelectedProperty.TRUE);
//calEntry.addLocation(new Where("", "", "Oakland"));
//calEntry.addLocation(calFeed.getEntries().get(0).getLocations().);
// Insert the calendar
calService.insert(calUrl, calEntry);
System.out.println("\tCreated: " + calEntry.getTitle().getPlainText());
//Thread.sleep(5000);
calQuery.setMaxResults(calFeed.getTotalResults()+1);
calFeed = calService.query(calQuery, CalendarFeed.class);
for (CalendarEntry entry : calFeed.getEntries()) {
if ( entry.getTitle().getPlainText().equals( CALENDAR_NAME ) ) {
calEntry = entry;
}
}
}
else {
System.out.println("\tFound: " + ( (calEntry != null) ? calEntry.getTitle().getPlainText() : "null") );
}
// Get all events
URL eventUrl = new URL ( calEntry.getLink(Link.Rel.ALTERNATE, Link.Type.ATOM).getHref() );
// Get Number of events
Query eventQuery = new Query(eventUrl);
eventQuery.setMaxResults(1);
CalendarEventFeed eventFeed = calService.query(eventQuery, CalendarEventFeed.class);
if (eventFeed.getTotalResults() == 0) {
System.out.println("No Events");
} else {
// Set Number of events for query
eventQuery.setMaxResults(eventFeed.getTotalResults());
eventFeed = calService.query(eventQuery, CalendarEventFeed.class);
}
Boolean exists = false;
Boolean update = false;