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 =