Thursday, August 17, 2006

grails 101

urgh. i'm not sure if im still a little drunk or my hangover's kickin in, or both. either way, im not exactly in sync at the moment, so the following may not make sense - bare with me. all i can remember england won four nil and o'neills pours a great pint of guinness.

recently i've been playing with groovy on the grails. groovy is a jsr standard scripting language which means it runs on the jvm, like java does. this means you can create classes in groovy and they will compile and run as java classes do, and hence run in a servlet container, jee container, and any other container that runs on a jvm.

grails is in it's infancy, but the idea is it is groovy's competitor to ruby on rails. rails is much further down the line, but the major benefit of both rails and grails is they allow you to develop a new application much faster than it would be possible if you were hand coding your application. from what i can see the two major reasons why this is possibly are one, much of the code is generated for you, and two, they implement the theory of convention over configuration.

so lets say you want to create a simple book management application, you want to develop a persistent book repository, and a method of managing it, where you can list all books, look at a single book, create new books, edit existing books, and delete unwanted books. all you need to do from a coding perspective is create a simple groovy pojo for your book (which is much less code than a java pojo and is essentially your model) and note your desired database connection details. now the grails console will generate the controller, the views, and will use the built-in framework to handle the http conversations, generate the database if needed, manage the o/r mapping and the application logic.

this is all because convention is used. basically if you created a string member in your book model class called author, a book table will be created with a column named author. then a hibernate mapping will be dynamically generated to map your book model class to your book table, and will use a string property with the member and column being named author. similar generations, dynamic or not, are handled a the front end.

i do really suggest having a play where you will find it is this simple (if you find my explanation simple that is).

groovy provide an eclipse plugin which will allow you to easily edit groovy files in the eclipse ide. grails doesn't really provide a plugin, but do have a page letting you know how to use grails in eclipse.

in addition to the tutorials on the website, i've also found these two great tutorials. i would initially want to use grails to provide me with crud management operations on oracle database tables via a website for my existing projects, which is why i find the following so useful, you may be in a different situation. jason rudolph has put together a comprehensive tutorial called hoisting grails to your legacy db. and grall at oracle has provided us with an oracle specific grails tutorial.

i have found a couple of sticking pointing with grails so far. my first one is i have not figured out whether grails has the ability to cope with databases with composite ids. another huge advantage with grails is if the convention method doesn't suit your needs then you can override it with configuration. so if you have a legacy database table that doesn't have an primary key column called id then you can create a hibernate mapping file for this table. however when i have tried to do this and use the composite id configuration methods i have got stuck. this may just be due to my novice level in groovy, and there may well be a way.

the other sticking point i came across was lob handling, of which there was none. again, when mapping from a legacy database you can point to a clob or blob column, however, grails didn't know how to handle this and exceptions were thrown all over the place. so after a conversation with graeme rocher, the lead in the grails project, at the grails user forum, i raised the issue at the grails jira site but being impatient decided to start attacking the problem myself.

i checked out the project from cvs, and made an amendment or two and added some additional classes, and it gave me the ability to list, read, and delete clob files, which i'm convinced would work for blob files too but i hadn't got around to testing. the only problem was it wouldn't allow me to create or edit blog files. You can see from the forum discussion linked above that this issue is still pending, as this was a week or two ago and i got stuck and moved on, but i do intend to return. the point i got to was that it seemed to me that transaction synchronisation was switched off, when it defaults to being turned on, and the exceptions were asking me to switch it on to allow lob persistence. unfortunately i couldn't find where in the code this was - but maybe i am wrong, hopefully i will find out !

so just a last couple of links you may find useful. here is guillaume laforge's blog who seems to be an active member of the forum and has a very good grails intensive blog, and finally for those of us based in london here is the groovy and grails london user group who has recently been out for a few beers, even though i couldn't make it along this time - maybe next.

happy grail hunting....


Post a Comment

Links to this post:

Create a Link

<< Home