Grails/BlazeDS/Flex/iPhone Full Stack Part1/3


A couple of years ago, I published an article on this blog entitled “Flex, Spring and BlazeDS: the full stack!” and this article became very popular. Actually it broke my daily visits record. Today I’m gonna try to break this record again.

In the last couple of years, I’ve worked a lot with Flex and Spring. But in my eternal quest for productivity and user experience, I discovered Grails. Based on the same ideas as Ruby on Rails or Django, it combines a dynamic language – Groovy – with the power of “convention over configuration” to make it possible to create web applications in no time, thus allowing you to spend more time on your user experience.

Of course it’s not the only thing that has changed since then. Flex 4 is now finally out in the open. BlazeDS 4 too. And Flash Builder is finally productive enough for me to use it… in combination with IntelliJ IDEA of course.

All those evolutions in my toolset needed to be integrated, so I ended up building a Grails plugin to do just that: Grails BlazeDS plugin. And since this plugin could not be officially released without a proper demonstration, here we are.

I prepared this tutorial for a BeJUG talk I gave last week. So I want to thank Stephan Janssen and the BeJUG for inviting me… and for staying with me without throwing vegetables at me, given all the failures I had during the live coding session. For those of you who were there: I clearly identified PermGen space as the guilty part responsible for all these blank screens. In fact, since Grails migrated from Jetty to Tomcat as their runtime server, default java memory settings are not enough anymore, so I always configure my projects for that, but of course, in live, I forgot…

Anyway. I’m going to publish this tutorial in three episodes, each one of them being accompanied by its very own screencast on Vimeo (damn Youtube and their 10-minute limit!). But I’ll also publish the tutorial transcript right here for those who want to go faster.

Important note: this tutorial covers the following set of technologies in the specified versions:

In particular, it seems that Grails 1.3 that was just released a couple of days ago breaks Grails BlazeDS plugin. I’ll update both my plugin and this tutorial when I can make them all work together again.

Part 1: Grails Backend

From now on, I assume you have already installed Grails 1.2.2 on your machine, as well as IntelliJ IDEA and Flash Builder. It is very likely that you are not using IntelliJ IDEA as your java IDE, but maybe this tutorial is a good opportunity to test it out because their Grails support is really awesome.

Fire up IntelliJ IDEA and create a new Grails project:

The project is going to be called “todolist”. This is equivalent to creating your project on the command line using the following command: grails create-app todolist

Choose Grails 1.2.2 as the Grails SDK:

Hit finish and choose “create-app” as a project generator:

When the project is created, you can review the project structure in the left panel:

Right-click the grails-app/domain directory and choose New>Grails Domain Class:

Let’s give this domain entity a package and a name:

This is what should appear in the edition pane:

Before adding some code, we will first generate the other entity of our domain. So right-click the org.epseelon.todolist package, and choose New>Grails Domain Class:

Then give a name of Todo (IntelliJ will add the package automatically because we launched the wizard from it):

And there we have it. Our brand new Todo entity:

Now we add some fields to the Project entity:
[code=java]
package org.epseelon.todolist

class Project {
String title
String description

static hasMany = [todos:Todo]

static constraints = {
title blank:false, nullable:false
}
}
[/code]

We just added two properties (title and description) and a one-to-many relationship to our Project entity. No need for @Entity annotation since Grails expects to find only entity classes in the grails-app/domain entity. No need for getters and setters because Groovy generates them automatically. No need for @OneToMany annotation, the hasMany static field does the trick. And last we are using the constraints static field to specify what is considered as a valid project instance.

Let’s update the Todo class now:

[code=java]
package org.epseelon.todolist

class Todo {
String title
boolean done

static belongsTo = [project:Project]

static constraints = {
title blank:true, nullable:false
}
}
[/code]

The same as before for properties. By the way, no need to worry about the identifier property either: Grails automatically adds a Long id property for all entity classes. belongsTo is used to declare the manyToOne from Todo to Project.

Now I want to validate that this domain model works, and I want to do it quickly. We’re going to use dynamic scaffolding for that. Right-click the grails-app/controllers directory and choose New>Grails Controller:

We want to create a controller for the Project class:

Next we need to modify this class to replace the empty index action with a scaffold field:

[code=java]
package org.epseelon.todolist

class ProjectController {
def scaffold = Project
}
[/code]

Let’s do the same for Todo:

[code=java]
package org.epseelon.todolist

class TodoController {
def scaffold = Todo
}
[/code]

Finally, before running the Grails application, we need to make sure Grails has enough PermGen space for Tomcat to run properly. When the project was created, IntelliJ added a Build Configuration:

Edit the Grails:todolist configuration to add the following VM parameters: -XX:MaxPermSize=128m -XX:PermSize=128m -Xms1024m -Xmx1024m -XX:-UseGCOverheadLimit

When this is done, you can run this configuration and wait for your browser to display the following page:

You can use this interface to create a new project and add some todo items to it:

As you can see, when you try to add a todo item to the created project, the parent project is referenced by its id. Without shutting down the server, head back to IntelliJ and modify the Project class:

[code=java]
package org.epseelon.todolist

class Project {
String title
String description

static hasMany = [todos:Todo]

static constraints = {
title blank:false, nullable:false
}

String toString(){title}
}
[/code]

When you save the file, it is recompiled automatically. In the process, the in-memory database is cleared, so all the data we created before has been deleted. You can recreate your project and add a todo item to it: see how the todo creation form reference parent projects by title.

This ends the first step of this tutorial. In the video, I show you how to install the Grails BlazeDS plugin but I will explain that in the next installment. You can download the project as it is now here. Stay tuned for the next episode.

, , ,

6 responses to “Grails/BlazeDS/Flex/iPhone Full Stack Part1/3”

  1. Good job, thank’s to share you know about the topic, in this moment I’m proving and changing my mind about a project.

    Thank’s a lot.

  2. Excellent post – and thank’s a lot for the BlazeDS plugin!
    This is exactly what I was looking for and very helpful.

    Have you already looked into the BlazeDS issue with Grails-1.3?
    I would be very interested in this fix.

  3. Hi! Great start but where is the interesting part? I guess you ran out of time for it? I know the feeling! Anyway, any plans to continue?
    Cheers

    • I ran out of time… and out of a suitable technical solution. But now I’m experimenting with RestKit on the iPhone. Maybe I’ll document the results of my findings here when I’m done.

Leave a Reply

%d bloggers like this: