Accessing Google Calendar from Oracle ADF Application – Displaying Events in Gantt Chart

2

 Google Calendar (http://www.google.com/calendar) is one of the many services offered by Google. It allows users to manage a personal or shared agenda on line, from the comfort of their own browser. In an easy to use web interface, agenda entries can be created and edited. Google Calendar can also send notifcations for events, as email, SMS or popup.

googlecalendaringantt002.jpg

One of the nice things about Google Calendar is the fact that it offers an API: a programmtic interface that we can access to retrieve information about Calendar Entries, and also to create, update and delete entries or their details. This means for example that we can integrate the Calendar into our own applications, presenting Agenda entries in any format we desire.

In this article, I will describe how I linked up an Oracle ADF Web Application – created in JDeveloper 11g Technical Preview 2 (I had an issue getting it to run properly in TP3) – with Google Calendar and used the ADF Rich Faces Data Visualization component Gantt Chart for an alternative presentation of the Calendar entries. ....
Note: eventually, when the Gantt Chart component can be used to manipulate tasks as well as just present them, we should be able to extend this application and manipulate the Google Calendar data from it as well.

The steps to take:

  1. download the Google Calendar API Java Client API
  2. create a new JDeveloper application and project; add Google Calendar API libraries to it
  3. create a class GoogleAgendaManager that accesses Google Calendar to return the entries for a specific Calendar; implement a main method that displays the agenda entries so we can easily test this class
  4. create classes EventManager and Event; the first one can be used as a Business Service (interface) to return collections of the second; these can be acquired using GoogleAgendaManager but could also come from somewhere else
  5. create an ADF DataControl for class EventManager

At this point, we can start the Web part of the application, the ADF Faces application that leverages this Data Control and indirectly Google Calendar. Note that the Web Application needs to know nothing about Google Calendar.

  1. create a Web project; add ADF Faces and Data Visualization tag libraries
  2. create a new JSF page
  3. drag EventManager DataControl to this page; drop it as a Gantt Chart (schedule); configure the Gantt Chart data properties
  4. run the page

And we have done it! The browser displays the ADF Faces Gantt Chart component with the Calendar Entries retrieved from Google Calendar:

Here is our ADF Faces application:

And here the original overview in the Google Calendar Web View

 

There is a lot we can do to enhance, tune, refine the presentation of the agenda entries in the ADF application. This article is primarily meant as a proof of the concept.

Step by Step getting it to work

1. Download the Google Calendar API Java Client API

Go to http://code.google.com/apis/calendar/developers_guide_java.html , read the instructions and download the libraries at http://code.google.com/p/gdata-java-client/downloads/list (for example the file  gdata.java-1.15.2.zip). Extract the zip-file to a temporary directory.

2. Create a new JDeveloper application and project; add Google Calendar API libraries to it

Create a new application, for example GoogleCalendarWebFrontend with an empty project – for example CalendarClientAndService. Create a lib directory under this project and copy the jar files in subdirectory googleCalendar\gdata\java\lib to this lib directory. Next add these jar-files, or at least these:

to the project.

3. Create a class GoogleAgendaManager that accesses Google Calendar to return the entries for a specific Calendar; implement a main method that displays the agenda entries so we can easily test this class

A very simple implementation of this class that retrieves all entries from a private calendar – note that you have to provide your own Google account's username and password – looks like this:

package nl.amis.googleCalendar;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.calendar.CalendarEventFeed;
import com.google.gdata.data.extensions.When;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import nl.amis.events.Event;
public class CalendarManager {
    public CalendarManager() {
    }
    public void showEvents() throws AuthenticationException,
                                    MalformedURLException, IOException,
                                    ServiceException {
        // Create a CalenderService and authenticate
        CalendarService myService = new CalendarService("AMIS-GoogleCalendarManager-1");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myService.setUserCredentials(&quot;<a href="mailto:your.account@gmail.com">your.account@gmail.com</a>&quot;, &quot;yoursecretpassword&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Send the request and print the response
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; URL feedUrl = new URL(&quot;<a href="http://www.google.com/calendar/feeds/your.account@gmail.com/private/full">http://www.google.com/calendar/feeds/your.account@gmail.com/private/full</a>&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CalendarEventFeed resultFeed = myService.getFeed(feedUrl, CalendarEventFeed.class);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; resultFeed.getEntries().size(); i++) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CalendarEventEntry entry = resultFeed.getEntries().get(i);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(&quot;\t&quot; + entry.getTitle().getPlainText());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; List&lt;When&gt; times= entry.getTimes();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (When when:times) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(when.getStartTime()+&quot; - &quot;+when.getEndTime() );
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; public static void main(String[] args) throws AuthenticationException,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MalformedURLException,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IOException,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ServiceException {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CalendarManager calendarManager = new CalendarManager();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; calendarManager.showEvents();
&nbsp;&nbsp;&nbsp; }
}

When I run this class, the output looks like:

4. Create classes EventManager and Event; the first one can be used as a Business Service (interface) to return collections of the second; these can be acquired using GoogleAgendaManager but could also come from somewhere else

EventManager: (I cheated a little on the exception catching for brevity)

package nl.amis.events;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import nl.amis.googleCalendar.CalendarManager;
public class EventManager {
&nbsp;&nbsp;&nbsp; public EventManager() {
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; public List&lt;Event&gt; getEvents() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CalendarManager calendarManager = new CalendarManager();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; List&lt;Event&gt; events = new ArrayList();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; events = calendarManager.retrieveEvents();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Collections.sort(events);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return events;
&nbsp;&nbsp;&nbsp; }
}

and Event:

package nl.amis.events;
import java.util.Date;
public class Event implements Comparable {
&nbsp;&nbsp;&nbsp; String title;
&nbsp;&nbsp;&nbsp; Date startTime;
&nbsp;&nbsp;&nbsp; Date endTime;
&nbsp;&nbsp;&nbsp; String id;
&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; public Event() {
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; public void setTitle(String title) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.title = title;
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; public String getTitle() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return title;
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; public void setStartTime(Date startTime) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.startTime = startTime;
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; public Date getStartTime() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return startTime;
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; public void setEndTime(Date endTime) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.endTime = endTime;
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; public Date getEndTime() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return endTime;
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; public void setId(String id) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.id = id;
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; public String getId() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return id;
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; public int compareTo(Object o) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return startTime.compareTo( ((Event)o).getStartTime());&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; }
}
&nbsp;

I also add the following method to the CalendarManager class, in order to make a list Events available to the event manager:

&nbsp;&nbsp;&nbsp; public List&lt;Event&gt; retrieveEvents() throws AuthenticationException,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MalformedURLException, IOException,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ServiceException {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Create a CalenderService and authenticate
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CalendarService myService = new CalendarService(&quot;AMIS-GoogleCalendarManager-1&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myService.setUserCredentials(&quot;<a href="mailto:your.account@gmail.com">your.account@gmail.com</a>&quot;, &quot;yoursecretpassword&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; URL feedUrl = new URL(&quot;<a href="http://www.google.com/calendar/feeds/your.account@gmail.com/private/full">http://www.google.com/calendar/feeds/your.account@gmail.com/private/full</a>&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CalendarEventFeed resultFeed = myService.getFeed(feedUrl, CalendarEventFeed.class);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; List&lt;Event&gt; events = new ArrayList();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; resultFeed.getEntries().size(); i++) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CalendarEventEntry entry = resultFeed.getEntries().get(i);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Event event = new Event();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; event.setId(i);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; event.setTitle(entry.getTitle().getPlainText());&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; List&lt;When&gt; times= entry.getTimes();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (When when:times) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; event.setStartTime( new Date( when.getStartTime().getValue()));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; event.setEndTime( new Date( when.getEndTime().getValue()));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; events.add(event);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }// for
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return events;
&nbsp;&nbsp;&nbsp; }

5. Create an ADF DataControl for class EventManager

The effect is the creation of a number of XML files that together define the EventManager Data Control

The Web Application

1. Create a Web project; add ADF Faces and Data Visualization tag libraries

2. Create a new JSF page – for example GoogleCalendarGantt

3. Drag EventManager DataControl to this page; drop it as a Gantt Chart (schedule); configure the Gantt Chart data properties

Edit the Task Settings for the Gantt Chart (note we do not deal with Subtasks, Split Tasks and Dependent Tasks for the moment).

4. Run the page

Note: when I tried to do the same thing in JDeveloper 11g TP3, I ran into issues: after deploying the application, it seemed that the Google Calendar   API Service could not be reached (debugging showed that everything went as planned right up until the moment the Google service was accessed). I have asked for help on the JDeveloper 11g TP Forum, since I do not know what is causing this issue.

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

2 Comments

  1. Bert Van Aerschot on

    Hi,

    Thx for the interesting article…
    I just wanted to let you know that the example works fine in TP3 …

    no problems whatsoever

    Kind regards

    Bert