Thursday, January 21, 2010

Configuring Eclipse to use GWT within a Grails project

One nice thing about Eclipse is that it sets up your environments, compile macros and build paths for you when you do, say, File > New > Grails Project. GWT has a similar setup that works just as well and in exactly the same way. However, I'm merging Grails and GWT functionality and want the benefits of code completion, etc.

As mentioned in a previous blog entry, however, the merging of GWT and Grails is not very straightforward and is subject to some idiosyncrasies, including the need to execute Grails commands from a terminal and not from within Eclipse.  For some reason, installing GWT plugins from within Eclipse does not work (my theory has to do with environment variables but I just gave up on it and went with the sure-fire way).

The net effect is that GWT auto-suggest doesn't work.  Nor does auto-creation of import statements.  Bleh.  I found that you can get the former to work fine by right-clicking on the Project name in Package Explorer, then Google > Web Toolkit Settings...  You get the following screen:



Check "Use Google Web Toolkit" then auto-complete (at least) will work.

Tuesday, January 19, 2010

GWT vs. Grails

Both Google Web Toolkit (GWT) and Grails have been in the public domain for about two years now. I admit that my knowledge of, and experience with, both is very recent. When I started to investigate each technology I was very excited about the possibilities, namely:
  • Groovy and Grails provides the rapid-prototyping inherent with "convention-over-configuration"
  • Grails, being a Java framework, fits perfectly with legacy or yet-to-be-introduced Java technologies
  • GWT provides the tools for developing Rich Internet [client] Applications (RIA)
  • GWT, being based on Java, is Google's version of "write-once-run-on-any-browser"
After having spent merely a week on each technology, what follows in this entry is my interpretation and reaction of the above claims and discovery of the "buts..." hidden in the cracks.
First, I found all of the above claims to be true. Second, while both have client-side and server-side capabilities, Grails is the clear winner on the server side while GWT is a marginal winner on the client side. Third, if you want to use GWT and Grails together, the process can be cumbersome but it does work. Let me explain.
Grails:
  • The Object-Relational Mapping, which is based on Hibernate, is absolutely fantastic.
  • Scaffolding is even better than Ruby on Rails IMO because you can combine scaffolding with custom code. If anyone reading this can tell me otherwise about RoR, please feel free to comment politely.
  • Many operations, such as returning a list of objects in JSON format are one or two lines of code.
  • The generated controllers and views are still heavy on the "client-request/server-response" paradigm. A developer wanting to employ AJAX to develop a RIA still needs to install third-party JavaScript libraries and write framework code.
  • The suggested MVC structure doesn't [easily] encourage digressing from the Domain Class structure. For example, you're encouraged to edit a Task on the Task/edit page. However, RIA's are trending towards multi-faceted documents (think iGoogle home page) where a user interacts with diverse information on a single page.
GWT:
  • Creating Web pages by writing Java and compiling for all supported browsers brings RIA GUI creation to the Java purist and reduces testing overhead.
  • Developing a simple, self-contained (i.e., client-only) UI is easy and fun.
  • The Same Origin Policy (SOP) makes rapid development virtually impossible unless you try any of these methods:

    1. Compile your GWT application then copy to the Application server running your database services. If you do a lot of write-test-write-test, this simply is not a solution.
    2. Go through a complex procedure to "get around" the SOP, which only works for GETs. If you want to test UPDATE or INSERT you still need to find a better way.
    3. Develop a complete set of servlets to mimic your backend services.
    4. Use the GWT plug-in for Grails

  • While very powerful indeed, writing AJAX calls using GWT is far more complex than simply writing them in JavaScript.
You can probably sense the theme: Grails is easier to get up and running, but lacks sophisticated UI tools. GWT can do really neat things on the client side if you're willing to invest the time. Mixing the two is possible using the "official" way to integrate the two.

The caveat is that the GWT plug-in for Grails appears to be buggy if you compile and invoke from within Eclipse.  That means you should keep a terminal open for "grails run-app", a terminal for "grails run-gwt-client", and Eclipse.

Any other method doesn't really work very well.

Monday, January 18, 2010

Grails unable to create or alter a SQL table

To get to the point, the lesson learned here is, "be careful how you name your fields". When Grails updates the table schema due to changes you make in the Domain class, Grails returns this cryptic message:

Running Grails application..
2010-01-18 11:19:59,779 [main] ERROR hbm2ddl.SchemaExport - Unsuccessful: create table stock (id bigint not null auto_increment, version bigint not null, change float not null, price float not null, symbol varchar(255) not null, primary key (id))
2010-01-18 11:19:59,780 [main] ERROR hbm2ddl.SchemaExport - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'change float not null, price float not null, symbol varchar(255) not null, prima' at line 1

Obtaining the exact SQL can be tricky, but the cause was in how I named the table columns. When I changed "change" to "changePerc" and "price" to "priceDollars" everything worked.

Tuesday, January 12, 2010

Get MIME types to work in Grails

A powerful aspect of Grails is that you can instruct the server to return data in any number of formats declaratively. For example, when implementing AJAX, you might choose to return XML or JSON to the JavaScript callback function.

This would work fine except Grails, by default, disables this feature, always returning HTML. To fix this, edit: grails-app/conf/Config.groovy and change the "grails.mime.use.accept.header" value to "true".

Tuesday, January 5, 2010

Grails Cheat Sheet

Happy New Year! After a couple weeks of over-doing eating and vegetating, I dropped my mother off at the airport this morning and am jumping back into Grails. Unfortunately, um... I forgot most of what I picked up in December. To help me out, I'm creating a cheat sheet. So I will probably be editing this post to keep the information in one place.

Keystrokes (case sensitive):
  • ctrl-alt-G => open the Grails command line from within Eclipse
  • ctrl-space => auto-suggest
Commands
  • compile => (re)compiles the Grails Application
  • run-app => compiles then executes the current Grails project (development)
  • create-domain-class [class] => create a domain object (lower-case, singular)
  • create-controller [controller] => create a controller (lower-case, singular)
  • generate-all [Class] => generate default controller and view for the domain object
  • install-templates => generates the scaffolding templates for customization
  • install-plugin gwt => readies your application for use with GWT (or any other plugin like Hibernate)
  • create-service [class] => creates a service for the class (lower-case)
  • create-gwt-module => creates a GWT EntryPoint application
  • create-get-page => creates an HTML/GSP page to associate with the module EntryPoint
  • run-gwt-client => opens the GWT client manager and starts the Hosted Mode browser
  • compile-gwt-modules => (re)compiles GWT code. Required if you want to refresh the browser with new GWT functionality
Tutorials

Tuesday, December 22, 2009

Grails "pluginManager" error

STS has the cool feature where it dynamically recompiles your Grails application when it senses that you've made code changes. This can have an unexpected side effect. If you get an obscure message like the following:

Error creating bean with name 'pluginManager' defined in ServletContext...

Basically it seems related to a mismatch between the domain objects and the database schema. This is different from GORM and Object-Relational Impedance Mismatch... it is simply an unstable condition that might occur with the right combination of changes.

Unfortunately, I can only suggest saving and testing often, and if you see this message, try commenting out the "static hasMany" command in your domain object until it goes away.

Investigating and installing Grails

Based on a recommendation (and some newly acquired knowledge), I'm in the process of investigating Grails and perhaps GWT. This post, and likely the next few posts, will chronicle the installation, setup, and initial experience.

To install Grails, go to the Grails Home Page and download the latest version. Go to the Documentation page and follow the installation instructions. I am using a Debian-based Linux so I extracted Grails into /usr/share/grails and added GRAILS_HOME to my .bashrc. Testing Grails worked without any issue. My current version is 1.1.2.

It then occurred to me that I would really like to implement Grails from within Eclipse and to explore my options for Eclipse add-ons or integration. I chose to use SpringSource Tool Suite (STS).

For the most part, you'll follow the instructions on this page except STS-go here instead to download STS. Click on the appropriate OS link to the right of the Downloads section then follow the instructions at the bottom of that same page. Note: You may want to invoke the installer using "sudo" if you plan to install in a location like /usr/share. However, when asked to launch STS at the end, say "no" and launch STS explicitly. The executable is /usr/share/springsource/sts-2.3.0.RELEASE/STS

Once you have STS installed, go back to this page and follow the instructions to install the Groovy and Grails plug-ins. Note: If you installed STS as "sudo" in the previous step, you may want to invoke STS using "sudo" to install the plug-ins. Before I did this, it hung at about 67% for no apparent reason. For example, execute "sudo /usr/share/springsource/sts-2.3.0.RELEASE/STS" from a shell and use that session to install the plug-ins.