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)

Adding an Event to the Google Calendar:

JAVA:
  1. try{
  2.   //get connection
  3.   URL feedUrl = new URL("http://www.google.com/calendar/feeds/ACCOUNTNAME/private/full");
  4.   GoogleService myService = new GoogleService("cl", str_service);
  5.   myService.setUserCredentials(ACCOUNTNAME, ACCOUNTPASSWORD);
  6.                        
  7.   //create event here
  8.   URL postUrl = new URL("http://www.google.com/calendar/feeds/"+str_email+"/private/full");
  9.   EventEntry myEntry = new EventEntry();
  10.   myEntry.setTitle(new PlainTextConstruct(request.getParameter("event_name")));
  11.   myEntry.setContent(new PlainTextConstruct(request.getParameter("event_desc")));
  12.  
  13.   //these are your date values for the calendar   
  14.   DateTime startTime = DateTime.parseDateTime("2006-09-14T15:00:00-08:00");
  15.   DateTime endTime = DateTime.parseDateTime("2006-09-14T17:00:00-08:00");
  16.                        
  17.   When eventTimes = new When();
  18.   eventTimes.setStartTime(startTime);
  19.   eventTimes.setEndTime(endTime);
  20.   myEntry.addTime(eventTimes);
  21.            
  22.   Where eventWhere = new Where();
  23.   eventWhere.setValueString(request.getParameter("event_location"));
  24.   myEntry.addLocation(eventWhere);
  25.            
  26.   // Send the request and receive the response:
  27.   EventEntry insertedEntry = (EventEntry)myService.insert(postUrl, myEntry);
  28.   str_msg="Event Added Successfully.";
  29. }
  30. catch(Exception ex){
  31.     System.out.println("google ad event ex:"+ex);
  32.     str_msg="Error adding Google Calendar Event:"+ex;
  33. }

Getting a Date Range of Events from the Google Calendar:

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.

JAVA:
  1. try{
  2.     //get connection
  3.     URL feedUrl = new URL("http://www.google.com/calendar/feeds/ACCOUNTNAME/private/full");
  4.     CalendarQuery myQuery = new CalendarQuery(feedUrl);
  5.                                
  6.     String str_start="2006-09-14T00:00:00";
  7.     String str_end="2006-09-21T23:59:59";
  8.                                
  9.     myQuery.setMinimumStartTime(DateTime.parseDateTime(str_start));
  10.     myQuery.setMaximumStartTime(DateTime.parseDateTime(str_end));
  11.     myQuery.addCustomParameter(new Query.CustomParameter("orderby", "starttime"));
  12.     myQuery.addCustomParameter(new Query.CustomParameter("sortorder", "ascending"));
  13.     myQuery.addCustomParameter(new Query.CustomParameter("singleevents", "true"));
  14.                                
  15.     CalendarService myService = new CalendarService(str_service);
  16.     myService.setUserCredentials(ACCOUNTNAME, ACCOUNTPASS);
  17.                                
  18.     // Send the request and receive the response:
  19.     Feed resultFeed = (Feed)myService.query(myQuery, Feed.class);
  20.                                
  21.     new EventFeed().declareExtensions(myService.getExtensionProfile());
  22.             EventFeed calFeed = (EventFeed) myService.query(myQuery,EventFeed.class);
  23.                            
  24.              EventEntry calEntry = null;
  25.            
  26.             if (calFeed.getEntries().size()> 0) {
  27.                          java.util.List lTimes = null;
  28.                          java.util.List lLocs = null;
  29.                                    
  30.                 for (int n=0; n<calFeed.getEntries().size();n++){
  31.                       calEntry = (EventEntry) calFeed.getEntries().get(n);
  32.                       
  33.                                //grabs description for event
  34.                                TextContent tc = (TextContent)calEntry.getContent();
  35.               PlainTextConstruct ptc = (PlainTextConstruct)tc.getContent();
  36.                                        
  37.              calEntry.getTitle().getPlainText();
  38.                               lTimes=calEntry.getTimes();
  39.                      When when =null;
  40.                
  41.                                for (java.util.Iterator iterator1 = lTimes.iterator();iterator1.hasNext();) {
  42.                       when = (When)iterator1.next();
  43.             strTempStart=when.getStartTime().toUiString();
  44.             strTempStart=strTempStart.substring(strTempStart.indexOf(" "),strTempStart.length());
  45.                                        
  46.                     strTempEnd=when.getEndTime().toUiString();
  47.                     strTempEnd=strTempEnd.substring(strTempEnd.indexOf(" "),strTempEnd.length());
  48.             strTempEnd=convertTime(strTempEnd);
  49.               }
  50.                            
  51.                       lLocs=calEntry.getLocations();
  52.                       for (java.util.Iterator iterator1 = lLocs.iterator();iterator1.hasNext();) {
  53.                       Where where = (Where)iterator1.next();
  54.             where.getValueString();
  55.               }
  56.                            }
  57.     }
  58.     catch(Exception ex){
  59.         System.out.println("google view events ex:"+ex);
  60.         str_msg="Error viewing Team Google Calendar:"+ex;
  61.     }

Editing a Google Calendar Event:

JAVA:
  1. //get connection
  2. CalendarService calendarService = new CalendarService(str_service);
  3. calendarService.setUserCredentials(ACCOUNTNAME, ACCOUNTPASSWORD);
  4.  
  5. //call calEntry.getSelfLink().getHref() to get the url
  6. //example: http://www.google.com/calendar/feeds/ACCOUNTNAME/private/full/d1gn05stk3175uthp30s3et3is 
  7. URL entryUrl = new URL(str_event_url);
  8.            
  9. // Mark the feed as an Event feed:
  10. final ExtensionProfile profile = calendarService.getExtensionProfile();
  11. new EventFeed().declareExtensions(profile);
  12. final EventFeed feed = (EventFeed)calendarService.getFeed(entryUrl, EventFeed.class);
  13.             
  14. EventEntry retrievedEntry = (EventEntry)calendarService.getEntry(entryUrl, EventEntry.class);
  15. retrievedEntry.setTitle(new PlainTextConstruct(request.getParameter("event_name")));
  16. retrievedEntry.setContent(new PlainTextConstruct(request.getParameter("event_desc")));
  17.  
  18. //same date format as adding       
  19. DateTime startTime = DateTime.parseDateTime(str_start_time);
  20. DateTime endTime = DateTime.parseDateTime(str_end_time);
  21.            
  22. java.util.List lTimes=retrievedEntry.getTimes();
  23. When when =null;
  24. for (java.util.Iterator iterator1 = lTimes.iterator();iterator1.hasNext();) {
  25.       when = (When)iterator1.next();
  26.       when.setStartTime(startTime);
  27.       when.setEndTime(endTime);
  28. }
  29.            
  30. java.util.List lLocs=retrievedEntry.getLocations();
  31. for (java.util.Iterator iterator1 = lLocs.iterator();iterator1.hasNext();) {
  32.               Where where = (Where)iterator1.next();
  33.      where.setValueString(request.getParameter("event_location"));
  34. }
  35.            
  36. // Update the info
  37. // call retrievedEntry.getEditLink().getHref() to get the edit url like this: http://www.google.com/calendar/feeds/info%40webscope.ca/private/full/d1gn05stk3175uthp30s3et3is/63294552679
  38. URL editUrl = new URL(str_edit_event_url);
  39. EventEntry updatedEntry = (EventEntry)calendarService.update(editUrl, retrievedEntry);
  40. 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!

YourTeam

Cheers,
Adrian