Thu 21 Sep 2006
Using the Java Client Library with Google Calendar API - PART 2
Posted by anadeau under Developer Wing , Coding Tips & Info[5] Comments
Hey Everyone,
Time for another post. I know it's been awhile but I've been pretty heavy into the Google API stuff. Honestly was kinda tricky to work with a first but I finally might be onto it... yeah, I'm just a bit of a slow learner I guess
In my last post I briefly mentioned using the Java Client Library. I've now got some code examples that might help some people who might have bumped into some of the same issues I was having (mostly grabbing an individual event from the Google Calendar). We are working on building a free online application called YourTeam. It's going to combine a community for Hockey Coaches, Players and Parents and also a Private Team area for teams to stay informed and have fun! If you'd like to read all about it and how excited we are about the new beta release about to come out, click here to read my brothers post!
Here is the code that I was pulling my hair out for a few days over. These examples use the client library and are Java Server Pages. Enjoy! I hope they help. You will have to replace a few variables to get it working with your own code (ie: ACCOUNTNAME is your Google account name)
-
try{
-
//get connection
-
GoogleService myService = new GoogleService("cl", str_service);
-
myService.setUserCredentials(ACCOUNTNAME, ACCOUNTPASSWORD);
-
-
//create event here
-
EventEntry myEntry = new EventEntry();
-
myEntry.setTitle(new PlainTextConstruct(request.getParameter("event_name")));
-
myEntry.setContent(new PlainTextConstruct(request.getParameter("event_desc")));
-
-
//these are your date values for the calendar
-
DateTime startTime = DateTime.parseDateTime("2006-09-14T15:00:00-08:00");
-
DateTime endTime = DateTime.parseDateTime("2006-09-14T17:00:00-08:00");
-
-
When eventTimes = new When();
-
eventTimes.setStartTime(startTime);
-
eventTimes.setEndTime(endTime);
-
myEntry.addTime(eventTimes);
-
-
Where eventWhere = new Where();
-
eventWhere.setValueString(request.getParameter("event_location"));
-
myEntry.addLocation(eventWhere);
-
-
// Send the request and receive the response:
-
EventEntry insertedEntry = (EventEntry)myService.insert(postUrl, myEntry);
-
str_msg="Event Added Successfully.";
-
}
-
str_msg="Error adding Google Calendar Event:"+ex;
-
}
Note: This code might not compile, I tried to take out a lot of code to shorten it but it should be a good starting point.
-
try{
-
//get connection
-
CalendarQuery myQuery = new CalendarQuery(feedUrl);
-
-
String str_start="2006-09-14T00:00:00";
-
String str_end="2006-09-21T23:59:59";
-
-
myQuery.setMinimumStartTime(DateTime.parseDateTime(str_start));
-
myQuery.setMaximumStartTime(DateTime.parseDateTime(str_end));
-
myQuery.addCustomParameter(new Query.CustomParameter("orderby", "starttime"));
-
myQuery.addCustomParameter(new Query.CustomParameter("sortorder", "ascending"));
-
myQuery.addCustomParameter(new Query.CustomParameter("singleevents", "true"));
-
-
CalendarService myService = new CalendarService(str_service);
-
myService.setUserCredentials(ACCOUNTNAME, ACCOUNTPASS);
-
-
// Send the request and receive the response:
-
Feed resultFeed = (Feed)myService.query(myQuery, Feed.class);
-
-
new EventFeed().declareExtensions(myService.getExtensionProfile());
-
EventFeed calFeed = (EventFeed) myService.query(myQuery,EventFeed.class);
-
-
EventEntry calEntry = null;
-
-
if (calFeed.getEntries().size()> 0) {
-
-
for (int n=0; n<calFeed.getEntries().size();n++){
-
calEntry = (EventEntry) calFeed.getEntries().get(n);
-
-
//grabs description for event
-
TextContent tc = (TextContent)calEntry.getContent();
-
PlainTextConstruct ptc = (PlainTextConstruct)tc.getContent();
-
-
calEntry.getTitle().getPlainText();
-
lTimes=calEntry.getTimes();
-
When when =null;
-
-
when = (When)iterator1.next();
-
strTempStart=when.getStartTime().toUiString();
-
strTempStart=strTempStart.substring(strTempStart.indexOf(" "),strTempStart.length());
-
-
strTempEnd=when.getEndTime().toUiString();
-
strTempEnd=strTempEnd.substring(strTempEnd.indexOf(" "),strTempEnd.length());
-
strTempEnd=convertTime(strTempEnd);
-
}
-
-
lLocs=calEntry.getLocations();
-
Where where = (Where)iterator1.next();
-
where.getValueString();
-
}
-
}
-
}
-
str_msg="Error viewing Team Google Calendar:"+ex;
-
}
-
//get connection
-
CalendarService calendarService = new CalendarService(str_service);
-
calendarService.setUserCredentials(ACCOUNTNAME, ACCOUNTPASSWORD);
-
-
//call calEntry.getSelfLink().getHref() to get the url
-
//example: http://www.google.com/calendar/feeds/ACCOUNTNAME/private/full/d1gn05stk3175uthp30s3et3is
-
-
// Mark the feed as an Event feed:
-
final ExtensionProfile profile = calendarService.getExtensionProfile();
-
new EventFeed().declareExtensions(profile);
-
final EventFeed feed = (EventFeed)calendarService.getFeed(entryUrl, EventFeed.class);
-
-
EventEntry retrievedEntry = (EventEntry)calendarService.getEntry(entryUrl, EventEntry.class);
-
retrievedEntry.setTitle(new PlainTextConstruct(request.getParameter("event_name")));
-
retrievedEntry.setContent(new PlainTextConstruct(request.getParameter("event_desc")));
-
-
//same date format as adding
-
DateTime startTime = DateTime.parseDateTime(str_start_time);
-
DateTime endTime = DateTime.parseDateTime(str_end_time);
-
-
When when =null;
-
when = (When)iterator1.next();
-
when.setStartTime(startTime);
-
when.setEndTime(endTime);
-
}
-
-
Where where = (Where)iterator1.next();
-
where.setValueString(request.getParameter("event_location"));
-
}
-
-
// Update the info
-
// call retrievedEntry.getEditLink().getHref() to get the edit url like this: http://www.google.com/calendar/feeds/info%40webscope.ca/private/full/d1gn05stk3175uthp30s3et3is/63294552679
-
EventEntry updatedEntry = (EventEntry)calendarService.update(editUrl, retrievedEntry);
-
str_msg="Event Updated Successfully.";
So, hopefully that code can help someone out there. It might be a bit messy and long to look at but if you grab it and use it along with the Tutorials from Google I hope it can start working. Up next for me will be deleting Google Calendar Events from our webapp (YourTeam) and then hopefully some other enhancements!
So, if anyone has any questions about the information above let me know and I'll try my best
and if anyone knows any hockey coaches, players or fans out there that are looking for a great, fun team software package send them to YourTeam!

Cheers,
Adrian

