/* * CalendarDataHandler * * Version 1.0 * * 20070325 * * Author: Jenny Nordgren */ /** * CalendarDataHandler sets up a thread that handles the connection to the google API and uploads any * events passed to the thread. */ import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.*; import java.io.*; import com.google.gdata.client.*; import com.google.gdata.client.calendar.*; import com.google.gdata.data.*; import com.google.gdata.data.calendar.*; import com.google.gdata.data.extensions.*; import com.google.gdata.util.*; import java.net.URL; import java.util.ArrayList; import java.util.Iterator; import java.util.Date; import java.util.Calendar; public class CalendarDataHandler implements Runnable { private Thread t = new Thread(this); private GCalHelper appWindow; private ArrayList events; private String username = ""; private String password = ""; /** * This constructor takes the mother app window, a list of events to be uploaded, and the user credentials for * authenticating with the calendar service. */ public CalendarDataHandler(GCalHelper appWindow, ArrayListModel events, String username, String password) { this.appWindow = appWindow; this.events = events; this.username = username; this.password = password; t.start(); } /** * This is the implementation of the Runnable Interface. A connection is set up, the user is authenticated and * events are added to the calendar feed. A list of the events that were uploaded are reported back to the * main thread. */ public void run() { try { // URL-object for handling connection URL feedUrl = new URL("http://www.google.com/calendar/feeds/" + username + "/private/full"); CalendarService myService = new CalendarService("Nordgren-GCalHelper-0.1"); myService.setUserCredentials(username, password); //ArrayList for recording which events get uploaded so that they can be reported to the main data model ArrayList updatedEvents = new ArrayList(); //ArrayList with the events in the main data collection that are already uploaded, to avoid duplicate uploads ArrayList uploaded = appWindow.getUploadedEvents(); Iterator myIterator = events.iterator(); while (myIterator.hasNext()) { CalendarEventEntry e = (CalendarEventEntry)myIterator.next(); if(!uploaded.contains(e)) { CalendarEventEntry insertedEntry = (CalendarEventEntry)myService.insert(feedUrl, e); updatedEvents.add(e); } } //Report back what was uploaded appWindow.recordUploadedEvents(updatedEvents); } //Report any trouble to the user catch (MalformedURLException e1) { appWindow.setMessageLabel("URL Exception");} catch (ServiceException e4) {appWindow.setMessageLabel("Service problem! Wrong user details or connection problems."); } catch (IOException e2) { appWindow.setMessageLabel("No internet connection!");} } }