Archive for the 'groovy' Category

Dec 02 2008

a groovy google calendar

Published by jfrank under groovy

I pushed some buttons and opened up part of my svn repository to share the code from my earlier example:

https://www.joshuafrankamp.com/svn/incubator/simpleGroovyCalendar/src/

I read up on groovy’s site, about google data support but I actually couldn’t find the referenced code anywhere… I’d like to learn more about ‘use’ and other aop/mixin techniques.

No responses yet

Nov 29 2008

groovy code needs viagra (it keeps shrinking)

Published by jfrank under groovy

The other day I was talking to some coworkers about a groovy method that I had refactored. It was one that I had pulled the guts out because I needed two forms of the same data. So I added getDaySizeList which contains most of the logic, but I still needed to get a rollup sum from getDuration for another part of the application.

I knew groovy collections supported an “each” function that takes a closure. Groovy syntax allows for omission of parentheses when there is at least one argument, so it can look like this:

myCollection.each{ closure } not myCollection.each({ closure })

it would also allow you do things like:

myCollection.add object  (look no parens!)

which creeps me out right now, so I’m not going to think about it.

I digress… Here is my method, that I thought was a short implementation:

int getDuration(){
	int hours = 0;
	getDaySizeList().each{dayHourMap -> hours += dayHourMap.hours}
	return hours
}

I was excited, no visible loop, just something to do “each” for the members of the collection, drilling down into the hours property of the map in each collection item, summing them into a waiting “hours” int, then returning it. Slick.

Barney then replied that I should have (duh) used the sum closure-taking builtin in groovy… which turns the above method into this:

int getDuration(){
	return getDaySizeList().sum{it.hours}
}

“it” is the default single argument to the closure.  You can see I only kept the end of the closure, to the right of the former +=.

Magic.

One response so far

Nov 28 2008

Vacation ala Google Calendar

Published by jfrank under groovy

I’ve been building a railo-hibernate-groovy fto (flex time off) vacation app in my “for fun” time at work and home. The goal is to get an application that can replace the arcane paper based system that my company currently uses.

I have most of the front end of the app done, but I needed a good calendar view to show the events. I decided I would use google calendars, so I wrote a little calender wrapper. Here is a snippet of the test driver class. GoogleCalendar is mine, the *Entry items are part of the api.

This is groovy code, not java…

//omitting calendar name assumes the default users calendar
GoogleCalendar gc1 = new GoogleCalendar(username,password)
// adds an event 3 days in duration starting 3 days from now, with title and desc here
CalendarEventEntry entry = gc1.insertEvent("DELETE ME FTO","FTO",new Date()+3,new Date()+6)
gc1.deleteEvent(entry)

println("List of users owned calendars:")
for (CalendarEntry cal : gc1.getCalendars()) {
	println("\t" + cal.getTitle().getPlainText());
}

//this demonstrates the mixed constructor that is allowed in the groovy wrapper.
//i define a calendar name, and it looks it up and sets that as the calendar to work with
GoogleCalendar gc2 = new GoogleCalendar(username,password,"Web Services Team")
//adds an event 3 days in duration starting 2 days from now, with title and desc here on the WST calendar
gc2.insertEvent("Joshua FTO","FTO",new Date()+2,new Date()+5)

Such fun.

This will allow me to have a nice visual display of everyone’s scheduled vacation calendar, and have an easy way to share it with other apps/users such as our wiki.

No responses yet