Grails Scaffolding CRUD Templates for Google App Engine / Java

Update : This code has been merged into the Grails 1.1.1. release of the App Engine Plugin, please see the Spring Source Blog for a screencast and how to use this.

I spent a few hours trying to understand how JDO works on the app engine yesterday. I had some experience tinkering with the Grails templating system, and thought it would be a neat experiment to port the Templates into JDO / Persistence Manager. Here is a full working CRUD template for Grails based on JDO. The intent here is to rewrite the controllers to use JDO while retaining complete compatibility of the views.

SHORT VERSION

For those of you who know what you’re doing, you can download the zip file here : http://tomaslin.googlepages.com/templates.zip

Invoke grails install-templates, and replace the src/templates directory with the contents of the zip.

From then on, you would proceed as with a normal Grails project. Invoke grails create-domain-class com.fu.Panda to create your domain object, and type Grails generate-all com.fu.Panda to generate the CRUD functionality for it.

Note 1: Datanucleous doesn’t seem to like unpackaged domain classes, so please try to use fully qualified package names ( com.fu.Panda as opposed to just Panda )

Note 2: You need to enable sessions for page redirects to work in the app engine. See the section titled ENABLE SESSIONS.

RIDICULOUSLY DETAILED VERSION

Here are some step by step instructions of getting this to work.

INSTALL GRAILS 1.1.1, and the APP ENGINE PLUGIN

Graeme Rocher has outlined the steps needed to install the app-engine plugin in detail, so I won’t rehash those steps. Get Grails 1.1.1 and follow steps 1-8 here:  http://www.grails.org/plugin/app-engine

There is a step 9, which I needed to do in order to deploy properly. This is mentioned in the comments but is put here for completeness.

  1. call grails app-engine deploy
  2. you will get an error message
  3. download the appengine sdk from here
  4. unzip this into a directory ( for me /Users/tomaslin/appengine )
  5. your grails directory should now have a file called deploy, copy this into a new directory temp and unzip it.
  6. Remove the deploy file.
  7. call /Users/tomaslin/appengine/bin/appcfg.sh deploy temp
  8. Google will prompt you for credential. Wait a long time and you are now set up to use the app engine.

2. INSTALL THE TEMPLATES

  1. Call grails create-app MyApp
  2. Go into MyApp directory, call grails install-templates
  3. delete the src/templates directory and replace it with the goodness found in here : http://tomaslin.googlepages.com/templates.zip
    Create your domain class by invoking grails create-domain-class com.test.sample
  4. Open grails-app/domains/com/test/sample
  5. You will see that there is a JDO enabled domain class here. We use the id as the primary key here, but you can also use other values, please see the JDO reference on the type of keys here

The domain class has a dummy property, you would add more here by changing the name, example

@Persistent
String name

The @Persistent annotation is important. It tells datanucleus to persist this field in the DB.

After you have finished editing your domain class, simply call grails generate-all, which will generate the appropriate controllers and views.

You can now launch your application with grails app-engine.

However, you will notice that when you try to save, you will get a session exception. We talk about this next.

ENABLE SESSIONS

By default, the Google App Engine has sessions turned off. However, whenever Grails calls an action redirect in the controller, it requires you to handle these sessions. You would normally modify the appengine-web.xml file to simply enable this. However, in the current implementation of the Grails App Engine plugin, this is a little obscure.

The App Engine plugin writes out the appengine-web.xml on application deployment, so we need to hack this process to include turning on of the sessions, adding

    <sessions-enabled>true</sessions-enabled>

to our generated appengine-web.xml file.

The path to this plugin directory, for me, is /Users/tomaslin/.grails/1.1.1-SNAPSHOT/projcts/myApp/plugins/app-engine-0.5/scripts. You will see a file in there called events. If you open it, you can add the session enabling line right after

<version>${appVersion}</version>

I have also included a copy of this file to download here: _Events.groovy

Modifications:
There are actually only 3 files changed in the attached zip :

  • Artifacts/DomainClass.groovy – to have all the necessary imports
  • Scaffolding/Controller.groovy – to handle the right JDO calls
  • web/web.xml – to exclude welcome files

Future Improvements:

  • The list operation is functional, but not very efficient. I need to change the total syntax to use the count() query in JDO and enable pagination, which is currently broken. Also need to take into account Params.sort and order.
  • It would be nice to be able to enable and disable the sessions via a configuration parameter.
  • The scaffold currently uses a long ID. You can see examples of how to handle keys in Graeme’s pet clinic example on Github.

OTHER PROJECTS YOU MUST SEE

Of course, after spending a few hours raking my brains around JDO, I remembered to check out Burt Beckwith’s amazing Grails Lite project. It is much more elegant than this, as it actually provides the CRUD dynamic methods for a lot of the GORM dynamic methods. Someone please spam him repeatedly to write this into a plugin. If you are looking for GORM functionality, you should start off with the DynamicUtils library in this project.

FINAL NOTE ( OT ) :
If you find that these templates are useful to you in any way, please spend some time checking out and spreading the word about http://www.empora.com – it’s a new website we launched on Grails that helps you find clothes and fashion accessories using an unique visual search technology. Thanks.

10 thoughts on “Grails Scaffolding CRUD Templates for Google App Engine / Java

  1. Graeme Rocher

    Hi Tomas, maybe you would like to consider contributing these templates and the changes you have made (to _Events.groovy for example) to the app-engine plugin?

    Reply
  2. gopoto

    Are you sure templates you merged in grails 1.1.1 manage well the scaffolding for entities that have some relationships ?

    Reply
  3. Pingback: Grails on Google Apps Engine. Step by Step | Konstantin Gurnov's Blog

  4. Pingback: Podcast grails.org.mx: Episodio 9 de la Temporada 0 | GrailsMX

  5. Pingback: Podcast grails.org.mx: Episodio 9 de la Temporada 0 |

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s