package com.casamind.adware.server.proxy;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;
import com.google.appengine.api.utils.SystemProperty;
import com.google.gdata.client.batch.BatchInterruptedException;
import com.google.gdata.client.calendar.CalendarQuery;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.Link;
import com.google.gdata.data.batch.BatchOperationType;
import com.google.gdata.data.batch.BatchStatus;
import com.google.gdata.data.batch.BatchUtils;
import com.google.gdata.data.calendar.CalendarEntry;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.calendar.CalendarEventFeed;
import com.google.gdata.data.calendar.CalendarFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
public class GDataCalendarProxy {
private static final Logger log = Logger.getLogger(GDataCalendarProxy.class.getName());
private long gdataThreadSleep;
private long gdataConnectTimeout;
private CalendarService service;
private URL calendarFeedUri;
private CalendarFeed calendarFeed;
private CalendarEventFeed batchFeed;
public GDataCalendarProxy(String calendarFeedUrl, long gdataThreadSleep, long gdataConnectTimeout) {
}
public GDataCalendarProxy(String calendarFeedUrl, String login, String password, long gdataThreadSleep, long gdataConnectTimeout) {
this.service = new CalendarService("Avicena-v1");
this.gdataThreadSleep = gdataThreadSleep;
this.gdataConnectTimeout = gdataConnectTimeout;
try {
if (isDevMod()) {
// Ignore the servlet parameter
login("avicena.dev@gmail.com", "mofiapke1;");
} else {
log.info("GData API Login : \n\t" + login + "\n\t" + password);
login(login, password);
}
this.calendarFeedUri = new URL(calendarFeedUrl);
this.calendarFeed = service.getFeed(calendarFeedUri, CalendarFeed.class);
this.batchFeed = service.getFeed(calendarFeedUri, CalendarEventFeed.class);
} catch (AuthenticationException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void login(String user, String pass) throws AuthenticationException {
this.service.setUserCredentials(user, pass);
this.service.setConnectTimeout((int) gdataConnectTimeout);
this.service.setReadTimeout((int) gdataConnectTimeout);
}
public CalendarEntry findCalendarEntry(String title) throws ServiceException {
List<CalendarEntry> entries = calendarFeed.getEntries();
for (CalendarEntry entry : entries) {
if (entry.getTitle().getPlainText().compareTo(title) == 0) {
return entry;
}
}
return null;
}
public CalendarEventFeed getEventsRangeFeed(Date startDate, Date endDate) throws IOException, ServiceException {
SimpleDateFormat logFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
CalendarQuery query = new CalendarQuery(calendarFeedUri);
query.setMinimumStartTime(DateTime.parseDateTime(logFormatter.format(startDate)));
query.setMaximumStartTime(DateTime.parseDateTime(logFormatter.format(endDate)));
return service.query(query, CalendarEventFeed.class);
}
public void batchEntries(List<CalendarEventEntry> entries, BatchOperationType batchOperationType) throws BatchInterruptedException, MalformedURLException, IOException, ServiceException {
if (entries == null || entries.size() == 0) {
return;
}
// Create an batch entry to delete an existing event.
CalendarEventFeed batchRequest = new CalendarEventFeed();
for (CalendarEventEntry entry : entries) {
BatchUtils.setBatchId(entry, entry.getTitle().getPlainText());
BatchUtils.setBatchOperationType(entry, batchOperationType);
batchRequest.getEntries().add(entry);
}
// Get the batch link URL and send the batch request there.
Link batchLink = batchFeed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM);
CalendarEventFeed batchResponse = service.batch(new URL(batchLink.getHref()), batchRequest);
// Ensure that all the operations were successful.
boolean isSuccess = true;
for (CalendarEventEntry entry : batchResponse.getEntries()) {
String batchId = BatchUtils.getBatchId(entry);
if (!BatchUtils.isSuccess(entry)) {
isSuccess = false;
BatchStatus status = BatchUtils.getBatchStatus(entry);
log.warning("\n" + batchId + " failed (" + status.getReason() + ") " + status.getContent());
}
}
if (isSuccess) {
log.info("Successfully executed all events via batch request.");
}
}
/**
* @return true if the app is in dev mode, false otherwise.
* @param request
*/
public boolean isDevMod() {
return SystemProperty.environment.value() != SystemProperty.Environment.Value.Production;
}
}