+ (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;
Integer batchid = 0;
Method methodType = Method.ALERT; //Method.EMAIL;
Reminder reminder = new Reminder();
reminder.setDays( REMINDER_DAYS );
reminder.setMethod(methodType);
CalendarEventFeed batchRequest = new CalendarEventFeed();
// Copy contacts with title and birthday and check if event exists
for (ContactEntry contact : conFeed.getEntries()) {
if ( (contact.hasName()) && (contact.hasBirthday()) ) {
exists = false;
update = false;
CalendarEventEntry entry = new CalendarEventEntry();
SimpleDateFormat sdf = new SimpleDateFormat( DATE_FORMAT_GCON_PARSE_YMD );
Date date;
String datePattern;
try {
sdf.applyPattern( DATE_FORMAT_GCON_PARSE_YMD );
date = sdf.parse(contact.getBirthday().getWhen());
datePattern = DATE_FORMAT_GCAL_TEXT_DMY;
} catch (ParseException e) {
sdf.applyPattern( DATE_FORMAT_GCON_PARSE_MD );
date = sdf.parse(contact.getBirthday().getWhen());
datePattern = DATE_FORMAT_GCAL_TEXT_DM;
}
for ( CalendarEventEntry event : eventFeed.getEntries() ) {
if ( event.getTitle().getPlainText().contains( contact.getName().getFullName().getValue() ) ) {
// found event for given contact
// check date
sdf.applyPattern( DATE_FORMAT_GCAL_SET_EVENT );
if ( event.getRecurrence().getValue().contains( sdf.format(date) )) {
//if ( event.getRecurrence().getValue().contains( recurData.substring(0, 25) )) {
// same date - nothing todo
//DEBUG: System.out.println("\tcontact and event have same date: " + contact.getName().getFullName().getValue() + " " + event.getTitle().getPlainText() );
System.out.println("\tcontact and event have same date: " + contact.getName().getFullName().getValue() + " " + event.getTitle().getPlainText() );
exists = true;
}
else {
// date not correct - update date
//DEBUG: System.out.println("\tcontact and event have not the same date: " + contact.getName().getFullName().getValue() + " " + contact.getBirthday().getWhen() + " " + event.getTitle().getPlainText() /* + " " + event.getRecurrence().getValue() */ );
System.out.println("\tcontact and event have not the same date: " + contact.getName().getFullName().getValue() + " " + contact.getBirthday().getWhen() );
update = true;
entry = event;
}
}
}
if ( exists == false ) {
// no event for given contact - add event
sdf.applyPattern( datePattern );
entry.setTitle( new PlainTextConstruct( contact.getName().getFullName().getValue() + " " + sdf.format(date) ) );
entry.setContent( new PlainTextConstruct( "Birthday Celebration " + contact.getName().getFullName().getValue() + " (" + sdf.format(date) + ")") );
sdf.applyPattern( DATE_FORMAT_GCAL_SET_EVENT );
String recurData = "DTSTART;VALUE=DATE:" + sdf.format(date) + "\n"
+ "DTEND;VALUE=DATE:" + sdf.format(date) + "\n"
+ "RRULE:FREQ=YEARLY";
Recurrence recur = new Recurrence();
recur.setValue(recurData);
entry.setRecurrence(recur);
entry.getReminder().add(reminder);
batchid++;
BatchUtils.setBatchId(entry, batchid.toString());
if ( update == true ) {
System.out.println("\tUpd contact: " + contact.getName().getFullName().getValue());
BatchUtils.setBatchOperationType(entry, BatchOperationType.UPDATE);
} else {
System.out.println("\tAdd contact: " + contact.getName().getFullName().getValue());
BatchUtils.setBatchOperationType(entry, BatchOperationType.INSERT);
}
batchRequest.getEntries().add(entry);
}
}
}
// Get the batch link URL and send the batch request there.
if ( batchRequest.getEntries().isEmpty() ) {
System.out.println("No Batch Request");
}else {
System.out.println("Send Calendar Batch Request");
Link batchLink = eventFeed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM);
//CalendarEventFeed batchResponse =
calService.batch(new URL(batchLink.getHref()), batchRequest);
}
/* //DEBUG:
// Print all events
// Get Number of calendar entries