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.

Tomcat
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. 

Internationalization
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 ... 
    user.save(flush:true, 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 {

  @Override
  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) {
          u.name = "Joshua" 
          getBean(UserService).update(u)
          w.addComponent(new Label(u.getName()))
     }
     setMainWindow(w)
  }
}

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.

TextLab for Mac
Ultimate application to validate, clean and format JSON, XML, SQL, HTML.

Tuesday, March 13, 2012

Vaadin and Grails, part I., "Hello world, with DB!"

Welcome to the first tutorial called "Hello world, with DB!".

We are going to make simple application that is:

2 things must be done in order to setup your development environment.
  1. Eclipse STS 
    • Go to the link mentioned above, accept licence terms and go to the download page
    • Install it to e.g. C:\EclipseSTS in case you are developing on windows, because there might be problems with Grails plugin installation if  you put it to e.g. "C:\Program Files\EclipseSTS"
  2. Groovy and Grails plugins 
    • 3 plugins need to be installed. Open your Eclipse STS, go to Dashboard and click on Extensions tab.
    • Select "Grails (current production release)", "Grails Support" and "Groovy Eclipse" and click on Install button. 

That is all you need for this tutorial and maybe also for development of your Vaadin / Grails applications. 

Let's create a project called "vaadin-grails-hello-world". It is going to be Grails project. 


Dependencies and plugins are downloaded after we click on Finish button. It can take a while... and this is how a new Grails project looks like in Eclipse: 

Firstly, we need is to install Grails Vaadin plugin that will allow us to use Vaadin in Grails environment. Actually, we just open plugins window and click on the refresh button (so you get the list of latest plugins).


In case you don't see latest version in the plugins window, you can download the plugin from github and run this command. 
We should get this text in Eclipse console as the relsut of the successful instalation: 
| Loading Grails 2.0.1
| Configuring classpath.
| Environment set to development.....
| Installing zip vaadin-1.5.3.zip.....
| Installed plugin vaadin-1.5.3
| Resolving plugin JAR dependencies.....
| Plugin installed.
and VaadinConfig.groovy file should appear.
If that is not working, here is a good tutorial how to install that plugin: https://github.com/ondrej-kvasnovsky/grails-vaadin-plugin/wiki.  


Now, 4 simple steps are needed to get our Vaadin application working.

1. Set grails-app\vaadin folder as a source folder

2. Create your Vaadin application class. Let's call it HelloWorldApplication.groovy in "helloworld" package. 

3. Make a note about HelloWorldApplication to VaadinConfig.groovy
applicationClass = "helloworld.HelloWorldApplication"

4. Remove content of mappings field in UrlMappings.groovy.


Good job if you came that far! As a reward, let's write some code in Vaadin. The first thing we could do is to make an empty page, where only a title is going to be set.

package helloworld
import com.vaadin.Application
import com.vaadin.ui.Window
class HelloWorldApplication extends Application {
@Override
public void init() {
Window w = new Window("Hello world!")
setMainWindow(w)
}
}

Let's test it. Click on Grails icon in your eclipse and write the following command that will run your application in development environment: dev run-app

This is how your application should look like:

Well, our Vaadin application is running but we want to connect it with database and display some data. It is pretty simple in Grails. Just create a domain class e.g. helloworld.User and put there a variable called name.

What we have done? We have made User class which is connected to database via Hibernate. If you wish to change database setting, go to DataSource.groovy (you don't have to, it is going to work by default setting).

Let's insert dome users before the application is started. This has to be done only when running the application in development mode. Here is the way how to do it (BootStrap.groovy):

Let's display these users in our application (HelloWorldApplication.groovy).
Let's start the application and see if the users are displayed...

Congratulations if that works! Just little note: if you make some little changes in your code then your code is recompiled and you don't have to restart the server (just refresh the web page ;-) ).

What now? You can go to see Vaadin sampler, read Vaadin book and start coding your Vaadin application.

You can get source codes on github.

TextLab for Mac
Ultimate application to validate, clean and format JSON, XML, SQL, HTML.