Thursday, May 9, 2013

Vaadin 7 Cookbook

Few things I would like to share about Vaadin 7 Cookbook.
We started writing the book since Vaadin 7 alfa versions - which was hard because we needed to made a lot of changes before releasing the book. But, it gave us a lot of knowledge about Vaadin 7 and we could put it into that book. And that makes the book amazing for those who are starting with Vaadin 7. 
One of the first feedback, was "If I would be a beginner with Vaadin, this is the kind of book I’d like to study - a lot of examples that do a single thing, and everything regarding to that one thing is well explained. ... that’s how I learn new things, not by reading theoretical ramblings regarding things and stuff." - vaadin blog 
Luckily, I could participate a project where we used Vaadin 7 with Grails, which was amazing experience. Many notes are included in the book. 
Jaroslav Holan accepted my offer to be co-author, thanks to him the book is more rich for recipes that I would hardly include. Thank you for that, Jaroslav
Looking forward to hear you reflections.  

Thursday, December 27, 2012

Lazy loaded table in Vaadin 7

There are few options how to make lazy loaded table. When I say lazy loaded table then I mean a table that fetches data lazily from database.

  1. Use standard Vaadin table with scroll bar.



    Client (running in browser) loads items lazily from server (and that is awesome). But there is unfortunately no "lazy container" in Vaadin core and therefore we need to use an add-on. We can use JPAContainer for $299 USD "only". Or LazyQueryContainer for free. I tried LazyQueryContainer and worked well but I noticed that design of that add-on is a bit heavy. Therefore I have decided to build a new LazyContainer.

    Vaadin scrollable table needs to call COUNT of the selected items from a database table when rendering table and also when scrolling. There are also not efficient calls of getItemIds method from container in standard Vaadin table. It is easy to explain. When we scroll in the table then client requests data from the server. Server calls the container method getItemIds(startIndex, numberOfIds) where startIndex and numberOfIds have usually this kind of values. 

    startIndex: 0, numberOfIds: 15
    startIndex: 0, numberOfIds: 55
    startIndex: 10, numberOfIds: 72
    startIndex: 33, numberOfIds: 98

    ...

    Then we make SQL or some other kind of query to database that looks like this select * from orders limit 0 offset 15; 
    It is nice that the data is lazy loaded and therefore we don't fetch N thousands of items from database but it is wasting resources on the server. I tried to play with cache rates but I couldn't tune it to this kind of result. 

    startIndex: 0, numberOfIds: 50
    startIndex: 50, numberOfIds: 50
    startIndex: 100, numberOfIds: 50
    startIndex: 150, numberOfIds: 50
  2. We need to use PagedTable add on in order to get that kind of strict fetching data from database. But there are few problems with PagedTable. First it doesn't have good architecture and it not easy to use when we need to do localization or customization of navigation items plus there are some defects. I have fixed some of the defects, converted it to Maven project and made customization of the navigation components easier. It is accesible from forked PagedTable



    When we use PagedTable together with LazyContainer then we get quite efficient lazy loaded table. How the code of that kind of table could look like? 

    Here is the example https://github.com/ondrej-kvasnovsky/lazy-loaded-paged-table

Wednesday, November 21, 2012

Hello world in Grails and Vaadin

Download and install Eclipse STS and install the extensions. 

  1. Create new Grails project
  2. Fill in the name of the new project
  3. Try to run the application. The plain Grails application (without Vaadin) should start straight away without any additional configuration.
  4. This is how the started application should look like. 
  5. Install Vaadin plugin. This command will install the latest version of the plugin.
  6. Try to run the application (on this screen I have added option where I run the application on different port... it might be handy for you).
  7. Add Vaadin folder into the source folder list.
  8. Try to run the application again. You will see the Vaadin hello world.
  9. We are done. 

Wednesday, October 3, 2012

Grails and Vaadin 7 beta - first version of the plugin

It is finally out. The first working version of Grails plugin for Vaadin 7.

Vaadin 7 differers from Vaadin 6 a lot and therefore there is no backward compatibility beween the plugins for 6th and 7th version. Actually, the plugin for Vaadin 7 is made almost from scratch. Many things have been simplified and plugin is just handling the connection between Grails and Vaadin. For example dynamic methods getBean and i18n are not injected anymore. 

One thing I have noticed after playing with Grails 2.2.0 and Vaadin 7 - it is fast (actually in two ways). First, I have made localized application with access to database via ORM (Hibernate) and Spring (Services), displaying list of users in a good looking table in just 5 minutes (sometimes, in other frameworks, I don't even setup the project in that short time). Second, it is really responsive user interface. I think the guys in Vaadin did amazing job with the 7th version. 

Let's have a look how to install the latest version and try it out. Setup Grails console before you start or you can just make it in your favorite IDE. 
  1. Create new project and run it.
    grails create-app bookstore
    cd bookstrore
    grails run-app
  2. You can stop the application now. It was just to verify that you have successfully created the project. Download and install the plugin
    grails install-plugin /path/to/plugin/grails-vaadin-1.7.0-beta2.zip
  3. And run the application
    grails run-app
Quite simple, right? You might be wondering... what about access to database or localization. There is a new class Grails and that one is meant to be used when you need to get the access to the services or i18n. Few examples follows.

Let's create a service class in Grails. It just returns all the users from the database.
class UserService {
   List<User> listUsers() {
      return User.list()
   }
}
Now get the service and call the method.
List<User> users = Grails.get(UserService).listUsers()

Or just create new Label and pass there localized string. 
String text = Grails.i18n("key.to.localization.from.i18n")
Label label = new Label(text)

If you find an issue, please report it in Jira or preferably in Github

Grails and Vaadin 6 - few changes in the plugin

There are few changes in the plugin. You can find the changed Vaadin plugin under this version number 1.5.5
  • No configuration needed after the plugin installation. 
Previously we had to add a new class that extends Vaadin application, add a reference to VaadinConfig about that class and finally remove content of UrlMapping. This all is automated now. We just create Grails project install the Vaadin plugin and run the application.
  • Versions updated.
Grails updated to 2.2.0 and Vaadin to 6.8.4. 
Follow these steps in case you want to try it out. 
  1. First set Grails console
  2. Create new Grails project and try to run it (so we can verify successful creation of the project + dependencies are downloaded).
    grails create-app bookstore
    cd bookstrore
    grails run-app
  3. Download and install the plugin
    grails install-plugin /path/to/plugin/grails-vaadin-1.5.5.zip
  4. Run the application
    grails run-app

Friday, July 27, 2012

Trying to update Grails Vaadin plugin to Vaadin 7

I am a quite in shock. I have tried to update grails-vaadin plugin so I could use the new version of Vaadin 7.0.0alfa3 together with Grails 2.1.0. But what a surprise... alfa3 contains a bit more classes than previous version and it makes it imposible to integrate Vaadin 7.0.0 alfa3 with Grails 2.1.0. 

Have a look how size of Vaadin jars increased...

What does it mean. When I try to add com.vaadin:vaadin:7.0.0.alpha3 as dependency then Grails fails to load with the following error: 
Error Interactive mode exited with error: loader constraint violation: when resolving method "javax.xml.parsers.SAXParser.getXMLReader()Lorg/xml/sax/XMLReader;" the class loader (instance of org/codehaus/groovy/grails/cli/support/GrailsRootLoader) of the current class, groovy/util/XmlSlurper, and the class loader (instance of <bootloader>) for resolved class, javax/xml/parsers/SAXParser, have different Class objects for the type org/xml/sax/XMLReader used in the signature.

I have already complained about it http://dev.vaadin.com/ticket/9180. Let's see what happens with the next releases. I still hope it is a kind of a bug or bad joke that there are so many libraries included in the package. 

Here you can find working version of Vaadin 7 (alfa2) for Grails (it is still experimental version, but it worked for few people already): https://github.com/ondrej-kvasnovsky/grails-vaadin-plugin/downloads.

Tuesday, July 24, 2012

Hints for Grails app running on Cloudbees

Here are my most used commands I use during development and deployment of Grails application which is running on Cloudbees.

Dependencies when changing BuildConfig 

  1. It happens quite usually - you add a new dependency to BuildConfig and it is not visible in your code. This helps to load it. I am also using --include-source option to get jars (because I might be wondering what code is behind).
    grails refresh-dependencies --include-source

Testing during development

  1. I am not using integration tests. The are two reasons why. First, it is possible to mock everything. Second, integration tests requires context and it is time consuming when starting it up. 
    dev test-app unit:
  2. I usually run only one specific unit test when developing a feature.
    dev test-app unit: UserServiceTests
  3. From time to time I need to have more applications running at the same time (e.g. when doing an integration between client application, admin application and some web services). 
    dev run-app -Dserver.port=8080

Validation before deploying war into the production

  1. At first, run all the tests. E.g. my web services written in Grails have 95% line coverage, applications about 80%. So I am quite confident when deploying it to production. 
    test-app
  2. Simulate running the war on local machine. Running application by run-war might uncover some issues which could occur during deployment of war on cloud. It can happen that application is running well when it is started by run-app but it fails during deployment (which causes your production app is not available). 
    run-war
  3. After the steps above are without problems then I make a production war file.
    prod war
  4. It is necessary to install CloudBees SDK so the applications can be deployed from command line. 
    bees app:deploy myapp.war -a myaccount/myapplication
  5. This is how to restart application from command line... it is quite handy. bees app:restart myaccount/myapplication