grails (11) vaadin (11) meteor (6) java (4) elasticsearch (3) apple (2) centos (1) cloudbees (1) google analytics (1) gradle (1) heroku (1) javafx (1) javascript (1) jdbc (1) jug (1) logback (1) logging (1) mac os (1) management (1) mongodb (1) mongolab (1) mysql (1) twitter (1) ubuntu (1)

Tuesday, March 20, 2012

Vaadin and Grails, part II., "What is in the package?"

Let's have a look at what makes mixture of Grails and Vaadin so great tool that one should use it. 

Vaadin - first of all, this is awesome framework combining server and client side approach. We can make RIA just in plain Java without touching JavaScript, HTML or CSS. This is nice showcase. If you want to, you can add there your HTML, CSS or JavaScript and change default look & feel and behavior.

Grails - another framework which is setting quite high standarts for development of Java based applications. Grails gives us: 
  • Command line scripting environment built on the Groovy-powered Gant
  • Object Relational Mapping layer built on Hibernate (GORM)
  • View layer in Groovy Server Pages 
  • Controller layer built on Spring MVC
  • Embedded Tomcat container which is configured for on the fly reloading
  • Dependency injection with the inbuilt Spring container
  • Support for internationalization (i18n) built on Spring's core MessageSource concept
  • Transactional service layer built on Spring's transaction abstraction
We don't need these two features of Grails - "View layer in Groovy Server Pages" and "Controller layer built on Spring MVC" when using Vaadin. Both of these features are replaced by Vaadin components and listener pattern.

Let's have a look at how the remaining features are used together with Vaadin and how to benefit from that. 

Command line (Gant)
This is awesome, we can... 
  • run application or tests in different environments simply just by typing dev run-app or test run-app or test test-app.   So we can use different databases, test data or whatever we want to in certain stages of development process.  
  • make a production package with the following command prod run-app  

... and many others. Here is more info about command line.

Well, we just change a code, save the files, refresh a page and that is it. No server restarts needed. We can see the changes immediately. 

Also, tomcat is provided as a grails plugin. So when we want to upgrade a new version of tomcat, we just upgrade the plugin and the compatibility with grails comes really easily. 

There is i18n() method available in all Vaadin classes which makes localization pretty easy. We just type i18n("app.title") in your Vaadin code.  

Then you should create a record in i18n properties files. 

And we are done. Just start the application by typing dev run-app into the command line or refresh the page in case your application is already running. 

If the message doesn't exist the key in square brackets is returned (e.g. [app.title]). It is pretty useful when you don't want to make localizations immediately when producing a code.

GORM (Grails ORM)
I have mentioned how to setup working Grails & Vaadin application with access to database in this post. We can use all the GORM features without limitations. 

Transactional service layer (Services)
What is this service layer about? Grails services should contain logic of the application, which means that the logic should not be placed in Vaadin code (e.g. listeners). We also get transactional behavior inside our services. Grails services are providing quite many features, please study that from official tutorial.

It is as easy as to create a persistent domain class. Let's just create a service for a user (called helloworld.UserService).

It might happen that we don't see services folder in eclipse. If that is the case, we have to add it...
Let's implement our new service:

class UserService {

  static transactional = true
  void update(User user) {
    // TODO: ... implement the logic ..., failOnError:true)

So, everything is ready and we can get and use the service in Vaadin code. Let's rename all the users to Joshua.

class HelloWorldApplication extends Application {

  public void init() {
     Window w = new Window("Hello world!")
     w.addComponent(new Label(i18n("app.title")))
     List<User> users = User.list()
     for(User u : users) {
 = "Joshua" 
          w.addComponent(new Label(u.getName()))

And here is the output.

Dependency injection
There is a special method getBean() provided by Grails Vaadin plugin. This method is accessible in all the Vaadin classes (assuming they are located in grails-app/vaadin).

getBean() method gets a dependency from the Spring ApplicationContext. Obtaining the references is extremely fast and serialization-safe because the references are cached and Spring takes care abou that. 

Well, how to use it in practice? We have to call getBean() always when we want to get a service. This approach is going to avoid strong references to usually stateless service implementations.