Friday, May 16, 2008

Another way to use Groovy in the Enterprise

Another great way to incorporate Groovy into your enterprise Java projects is Groovy SQL. GSql makes JDBC access so easy and Groovy's seamless Java integration and dynamic nature provides for some interesting ways to take advantage of it.

In this example we'll build a utility class that will use convention to turn an SQL select statement into a List of data transfer objects.

Our goal here is to take an SQL statement and a Class, execute that statement and use the results to populate instances of the Class. We will assume the convention of camel case for object properties and underscores for database fields, so that a field called LAST_NAME would be mapped to a property called lastName. We will also assume that the select statement will only return fields that have a corresponding property in the Class.

So let's see what this might look like:

static Collection getData(String query, Class clazz, Connection con){
def field = ''
def result = []
def sql = new Sql(con)
sql.rows(query).each {row ->
def dto = clazz.newInstance()
row.keySet().each{
field = toCamelCase(it)
dto."${field}" = row."${it}"
}
result << dto
}
result
}


After defining a few variables, we create our groovy.sql.Sql object using the constructor that takes a Connection. This will allow us to use Connections provided by an application server such as WebLogic or WebSphere. We could easily change this method to take advantage of the other constructors.

Next we'll call the rows method of the Sql class, which will execute the SQL statement and return an ArrayList of GroovyRowResults. Now we'll iterate through this ArrayList and create a new instance of our DTO class for each row. GroovyRowResult implements the Map interface so we can loop through its keys with row.keySet().each. This will give us each of the fields in that row and we will turn them into property names with a call to the toCamelCase() method (which we'll look at shortly). Notice how we take advantage of GStrings to dynamically access properties of our DTO as well as keys of the row map. After we assign each of the fields in the row to properties of the DTO instance we add it to the result List. When the loop is done we just return the List. Obviously, more can be done to make this more robust and more flexible, but it's pretty amazing how easy it was to build a Groovy class that can replace tons of repetitive Java code.

Let's take a look at how we could use this class from a Java project:

...
Connection con = getConnectionFromPool();
String query = "SELECT FIRST_NAME, LAST_NAME, EMAIL FROM MEMBER";
Collection members = GroovyData.getData(query, MemberDTO.class, con);
...

That's all there is to it.

Now let's take a quick look at the toCamelCase() method we mentioned above. This doesn't directly have anything to do with the purpose of this post, but I am hoping that someone might see this and offer a groovier way to do the same thing. Basically the goal is to take a database field name such as LAST_NAME and turn it into a camelCase string like lastName. Here's how I did it:

static String toCamelCase(String val){
def sb = new StringBuffer()
Boolean newWord = false
val.toLowerCase().each{
if (it == '_')
newWord = true
else{
if (newWord){
newWord = false
sb << it.toUpperCase()
}
else
sb << it
}
}
sb.toString()
}

I was sure there was a groovier way to do this and, sure enough, Andres Almiray came up with this awesome improvement:

static String toCamelCase(String val){
def result = val.split("_").collect { word ->
word[0].toUpperCase() + word[1..-1].toLowerCase()
}.join("")
result[0].toLowerCase() + result[1..-1]
}

Thanks Andres! And if that wasn't groovy enough, Ted Naleid came up with this cool mix of regular expressions and meta-programming. Good stuff Ted!

def camelCaseConverter(orig) {
orig.toLowerCase().replaceAll(/_(\w)?/) { wholeMatch, firstLetter ->
firstLetter?.toUpperCase() ?: ""
}
}

String.metaClass.toCamelCase = {->
camelCaseConverter(delegate)
}

Thursday, May 1, 2008

Groovy and Grails at JavaOne

It's less than a week until JavaOne 2008! It is going to be an exciting week packed full of brain stretching content. There will be technical sessions, birds of a feather sessions, panels, keynotes, etc. on just about everything having anything to do with Java. But most importantly, there will be a bunch of Groovy and Grails related sessions. Starting with Jeff Brown's Grails presentation at Community One on Monday, followed by the G2One / NoFluffJustStuff gala Monday evening at the W Hotel.

Then for the rest of the week a groovy search in the Content Catalog turned up the following list:

ID

Session Title

Speaker(s)

Date / Time

Room

TS-5771

The Duke and the Elephant: PHP Meets Java™ Technology--the Best of Both Worlds

Rob Nicholson, IBM

Tuesday
05/06/2008
10:50 AM -11:50 AM

Esplanade 301

TS-5764

Grails in Depth

Guillaume Laforge, G2One, Inc., Graeme Rocher, G2One Inc

Tuesday
05/06/2008
6:00 PM -7:00 PM

Esplanade 307-310

BOF-5613

Jersey: RESTful Web Services Made Easy

Jakub Podlesak, Sun Microsystems
Paul Sandoz, Sun Microsystems, Inc.


Tuesday
05/06/2008
8:30 PM -9:20 PM

Esplanade 307-310

PAN-5435

The Script Bowl: A Rapid-Fire Comparison of Scripting Languages

Guillaume Laforge, G2One, Inc., Charles Nutter, Sun Microsystems, Inc., Jorge Ortiz, Stanford, Raghavan Srinivas, Sun Microsystems, Inc.,
Frank Wierzbicki, Sun Microsystems


Wednesday
05/07/2008
9:30 AM -10:30 AM

Gateway 104

TS-5572


Groovy, the Red Pill: Metaprogramming--How to Blow the Mind of Developers on the Java™ Platform

Scott Davis, Davisworld Consulting, Inc.

Wednesday
05/07/2008
9:30 AM -10:30 AM

North Mtg-121/122/124/125

TS-5815

Going Mobile with JavaFX™ Script Technology, Groovy, and Google Android

Mike Mannion, Canoo Engineering AG

Wednesday
05/07/2008
10:50 AM -11:50 AM

Esplanade 301

TS-6050

Comparing JRuby and Groovy

Neal Ford, ThoughtWorks Inc.

Wednesday
05/07/2008
1:30 PM -2:30 PM

North Mtg-121/122/124/125

TS-5274

Groovy on a Cloud: Testing Java™ Platform, Enterprise Edition (Java EE Platform) Applications on Amazon EC2

Chris Richardson, Chris Richardson Consulting, Inc.

Wednesday
05/07/2008
4:10 PM -5:10 PM

Gateway 104

TS-6213

Boldly Go Where the Java™ Programming Language Has Never Gone Before

Geert Bevin, Terracotta

Wednesday
05/07/2008
4:10 PM -5:10 PM

North Mtg-121/122/124/125

BOF-5102

Cooking Your Own Groovy Builder: A Step Forward into Domain-Specific Languages

Andres Almiray, Oracle, Ixchel Ruiz, Clemente Camara y Asociados

Wednesday
05/07/2008
8:30 PM -9:20 PM

Gateway 104

BOF-5110

Extending Groovy’s Swing User Interface in Builder to Build Richer Applications

Danno Ferrin, Intelligent Software Solutions, Inc., James Williams, Spatial Networks

Wednesday
05/07/2008
8:30 PM -9:20 PM

Hall E 133

TS-5793

Groovy and Grails: Changing the Landscape of Java™ Platform, Enterprise Edition (Java EE Platform) Patterns

Guillaume Laforge, G2One, Inc., Graeme Rocher, G2One Inc

Thursday
05/08/2008
9:30 AM -10:30 AM

North Mtg-121/122/124/125

TS-5693

Writing Your Own JSR-Compliant, Domain-Specific Scripting Language

John Colosi, VeriSign, Inc.; David Smith, VeriSign Inc.

Thursday
05/08/2008
1:30 PM -2:30 PM

Esplanade 301

TS-6457


Choosing Your Java™ Technology-Based Web Framework: A Comparison

Richard Pack, Hyperic; Stacey Schneider, Hyperic

Thursday
05/08/2008
2:50 PM -3:50 PM

Esplanade 307-310

TS-6606

Patterns for Integrating Java™ and JavaScript™ Technology: Tales from the Front Lines

David Caldwell, David P. Caldwell, Inc.

Thursday
05/08/2008
2:50 PM -3:50 PM

North Mtg-121/122/124/125

BOF-5101

Boosting Your Testing Productivity with Groovy

Andres Almiray, Oracle;, James Williams, Spatial Networks

Thursday
05/08/2008
7:30 PM -8:20 PM

Gateway 102/103

TS-5098

Building Rich Applications with Groovy's SwingBuilder

Andres Almiray, Oracle;, Danno Ferrin, Intelligent Software Solutions, Inc.

Friday
05/09/2008
12:10 PM -1:10 PM

Esplanade 305

TS-6050

Comparing JRuby and Groovy

Neal Ford, ThoughtWorks Inc.

Friday
05/09/2008
1:30 PM -2:30 PM

Esplanade 303

TS-6039

Jython - Implementing Dynamic Language Features for the Java™ Platform Ecosystem

Jim Baker, Zyasoft, Tobias Ivarsson, Neo Technology

Friday
05/09/2008
2:50 PM -3:50 PM

Esplanade 305



Well, if you read this far and if you happen to be wandering around the Pavillion on Tuesday afternoon at 3:30, I will be giving a mini-talk on Java User Groups at the java.net Community Corner. (That way I can say that I gave a presentation at JavaOne).

Monday, April 14, 2008

NoFluffJustStuff JUG Events

I was just at nofluffjuststuff.com and I noticed this impressive list of Java User Group events coming up:

Apr. 15 - Omaha, NE - Eclipse Plugin Development at OJUG - Omaha Java Users Group
Apr. 21 - Durham, NC - Groovin Builds Gant Get Any Easier at Triangle Java Users Group
Apr. 24 - New York, NY - Give It a REST at New York Java Sig
Apr. 30 - Houston, TX - Design Patterns in Dynamic Languages at Houston Java Users Group
May. 14 - Dallas, TX - 10 Ways to Improve Your Code at JavaMUG
May. 15 - Salt Lake City, UT - Thorough Introduction to Groovy at Utah Java Users Group
Jun. 11 - Calgary, AB - Core Groovy at Calgary Java Users Group
Jun. 11 - Dallas, Texas - Grails - Agile Web 2.0 The Easy Way at JavaMUG

As the leader of a JUG (Capital Java User Group - Madison, WI) I appreciate all the support that the folks at NoFluffJustStuff provide to the Java community. As a Groovy / Grails developer, I also appreciate the way that they are helping to usher in the next generation of Java!

Thursday, April 10, 2008

Google App Engine - Vote for Groovy

If you haven't heard about the Google App Engine then you should check it out: http://code.google.com/appengine. If you have heard about it then you know that it initially only supports apps written in Python. Google does plan to support other languages and one of the first ones requested was Groovy! In fact the first issue on their issue list is "Add Java and Groovy support".

You can add your support by going to their issue list (http://code.google.com/p/googleappengine/issues/list) and giving that issue a star. You can also add a comment but it's the number of stars that moves an issue to the top of the list. Even though the Java/Groovy request is the first one entered (id = 1) it is currently ranking 5th in stars. Slightly behind Ruby and a good deal behind Perl. This is a great opportunity to be a squeaky wheel. So head on over to the list and give Groovy a star!

Tuesday, April 8, 2008

Programming Groovy has shipped!

I just got an email from the Pragmatic Bookstore announcing that my copy of Programming Groovy, by Venkat Subramaniam, has shipped! I have just about finished reading the PDF version but I am still looking forward to having the paper copy in my hands.
I'll try to post a review soon, but I'll give you the short version now:
It's a great book and if you have any interest in Groovy you should buy it right away!

Thursday, April 3, 2008

Groovy and Grails at ApacheCon

If you've been watching the mailing lists you probably already saw this, but if not: A couple of very Groovy guys, Paul King and Andres Almiray, have submitted presentations for ApacheCon US 2008. To support these proposed sessions and help spread some Groovy goodness, register on the ApacheCon website: http://apachecon.com/html/login.html

You can find more info and a list of proposed sessions in this Nabble thread:

http://www.nabble.com/Groovy-at-ApacheCon-US-2008-to16450918.html


Wednesday, April 2, 2008

Searchable Plugin: Just use it!

Warning: If you are one of the clever folks who has already discovered the searchable plugin then there's nothing new for you here. You might want to move along to Glen Smith's latest post about hit highlighting with Searchable. If you haven't looked at Searchable yet, you need to. So, either go directly to grails.org/Searchable+Plugin or read on if you need more convincing.

I've been working with Grails for over a year, all the while hearing great things about the Searchable plugin. So far, I have only had the need for simple searches so I just never bothered to look at Searchable. That was really dumb! Like, "why bother learning how to use a hammer, this rock is working fine".


So, just the other day, I had a search that was going from two potential fields to three or four. So, I decided to take a look at the Searchable plugin and see how "hard" it would be to get it integrated. Well, as with most things in the Groovy / Grails world, I had it up an running almost before I had finished reading the wiki page. It is so easy and so powerful that I can't see myself building any kind of search without it.

Here's a simple scenario for an example: We have a Company class that has half a dozen fields that users might want to search on and they want the search form to be at the top of the list page.

First we install the plugin like so:

> grails install-plugin searchable

Then add the following line to Company.groovy:
    static searchable = true
It doesn't matter where you add this line to your Groovy class, but I like to put all static references at the top.

Next we'll add a search action to our controller:


def search = {
def companyList
if (params.query)
companyList = Company.search(params.query).results
else
companyList = Company.list()
render(view:'list', model:[companyList : companyList])
}
Just like GORM adds list(), find(), save(), etc. to our domain class, Searchable added search(), which takes a query string and optionally a map where you can add options like max, offset, sort, etc. It returns a searchResult which contains a results property which is an ArrayList. So, our companyList can be assigned either our search results or the result of a call to list().
There are other methods added to our domain and many more things that can be done with Searchable. You can read more about it at: grails.org/Searchable+Plugin+-+Searching

Finally we add a search bar to our list.gsp:

< div class="nav">
< g:form action="search" method="post">
Search: < input type="text" name="query">
< input value="Search" type="submit">
< /g:form>
< /div>


Now our users can search for companies by any of the properties using one or more search words and the result will show up in our standard list page. Searchable also supports complex queries which you can read all about at grails.org/Searchable+Plugin+-+Searching#SearchablePlugin-Searching-StringQueries

There's more that we could do here such as pagination, but I'll leave that as an exercise for the reader. How hard could it be?