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.
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.
- call grails app-engine deploy
- you will get an error message
- download the appengine sdk from here
- unzip this into a directory ( for me /Users/tomaslin/appengine )
- your grails directory should now have a file called deploy, copy this into a new directory temp and unzip it.
- Remove the deploy file.
- call /Users/tomaslin/appengine/bin/appcfg.sh deploy temp
- 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
- Call grails create-app MyApp
- Go into MyApp directory, call grails install-templates
- 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
- Open grails-app/domains/com/test/sample
- 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
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.
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
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
I have also included a copy of this file to download here: _Events.groovy
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
- 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.