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)
Showing posts with label vaadin. Show all posts
Showing posts with label vaadin. Show all posts

Monday, June 3, 2013

Vaadin 7 and Grails: How to compile widget set

I have promised to publish a tutorial showing how to compile the widget set inside Grails project with Vaadin 7. Here you are.

  1. Create a new widget set definition file AppWidgetSet.gwt.xml in the grails-app/vaadin directory.
  2. Open VaadinConfig.groovy that is inside the grails-app/conf folder and add there a note about the new widgetset file.
    widgetset = "app.AppWidgetSet"
  3. Create a folder libs-widgetset (for example inside the project root folder) for libraries that are needed during the widget set compilation. Download all-in-one archive of Vaadin 7 from https://vaadin.com/download and move all the .jar files from the archive into libs-widgetset folder. Do not forgot to move also .jar files from lib folder from the archive.
  4. Create build.xml file with the following content.
  5. Open the console inside the project root and run the ant command. The widget set compilation will start. Before you run the ant command, remove all the content of vaadin-grails-addon/web-app/VAADIN folder. 
This is a short version of tutorial "Adding Vaadin Add-on into Grails project" taken from the Vaadin 7 Cookbook.
TextLab for Mac
Ultimate application to validate, clean and format JSON, XML, SQL, HTML.

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
TextLab for Mac
Ultimate application to validate, clean and format JSON, XML, SQL, HTML.

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. 
TextLab for Mac
Ultimate application to validate, clean and format JSON, XML, SQL, HTML.

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. 
TextLab for Mac
Ultimate application to validate, clean and format JSON, XML, SQL, HTML.

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 quite in a 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, 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.

Friday, December 23, 2011

Vaadin 7

New version (Vaadin 7 alpha 1) of Vaadin has been released! If you are not aware of Vaadin roadmap, here you are: https://vaadin.com/roadmap


What has been changed so far?

  • Redesign of the window and application APIs
    • Multiple browser tabs (and windows) are supported by default
    • Faster startup
    • Customized UI starting from the first request
  • Redesign of forms and data binding (dev info)
    • Form is deprecated
    • CustomField, converters, validators  and typed properties using Java generics added

Vaadin is great framework with great documentation. So here is the little tutorial about new features: https://vaadin.com/wiki/-/wiki/Main/Vaadin+7 

If you feel like you might want to comment on these changes, visit these links:

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

Saturday, August 13, 2011

How to add Vaadin Add-on into Grails project

Here is the new version of this tutorial for Vaadin 7: .../vaadin-7-and-grails-how-to-compile.html

I have spent quite a while by integrating Grails and Vaadin Add-on (I decided to use invient-charts). There are many things one has to do in order to get it working. The reason is that support of Vaadin Add-ons is not yet supported by Grails Vaadin plugin...

Versions: Vaadin plugin 1.4, Vaadin: 6.5.1, Grails 1.3.7
  1. Eclipse and GRails plugin - use this tutorial to get it work http://grails.org/STS+Integration 
  2. Create a GRails project (in this tutorial named "your-project") in Eclipse 
  3. Install Vaadin plugin http://www.grails.org/plugin/vaadin 
  4. Download and copy paste jar file into the lib folder: /your-project/lib/invientcharts-0.8.4.jar and add it to Java Build Path in Eclipse
  5. Add this folder to Build Path /your-project/grails-app/vaadin 
  6. Create your package structure and create widgetset configuration file: /your-project/grails-app/vaadin/com/yourproject/ui/vaadin/YourProjectWidgetset.gwt.xml
  7. Add the widgetset set to /your-project/grails-app/conf/VaadinConfig.groovy 
    vaadin {
        // ...
        widgetset = "com.yourproject.ui.vaadin.YourProjectWidgetset"
    }
  8. You have to update the creation of Vaadin page - because you want to use custom JavaScript (so you have to force Vaadin plugin to add it there) - as described in next step. Add the following code into this class: /your-project/.link_to_grails_plugins/vaadin-1.4/src/java/com/vaadin/terminal/gwt/server/GrailsAwareApplicationServlet.java. If you are using GAE, update the another class with GAE text in it's name.
    @Override
    protected void writeAjaxPageHtmlVaadinScripts(Window window, String themeName, Application application, BufferedWriter page, String appUrl, String themeUri, String appId, HttpServletRequest request) throws ServletException, IOException {
      super.writeAjaxPageHtmlVaadinScripts(window, themeName, application, page, appUrl, themeUri, appId, request);
      page.write("<script type=\"text/javascript\">\n");
      page.write("//<![CDATA[\n");
      page.write("document.write(\"<script language='javascript' src='VAADIN/jquery/jquery-1.4.4.min.js'><\\/script>\");\n");
      page.write("document.write(\"<script language='javascript' src='VAADIN/js/highcharts.js'><\\/script>\");\n");
      page.write("document.write(\"<script language='javascript' src='VAADIN/js/modules/exporting.js'><\\/script>\");\n");
      page.write("//]]>\n</script>\n");
    }
  9. Upload JavaScript from invient-chart package 
    1. /your-project/web-app/VAADIN/jquery/jquery-1.4.4.min.js 
    2. /your-project/web-app/VAADIN/js/highcharts.js + sub folders
  10. Compile the widget set (you have to use Ant because this is not supported by Vaadin plugin). Here is the file which must be modified (just go though and fill in missing parts or upload needed jars to "lib" folder). In case you don't know what version of gwt-user.jar and gwt-dev.jar you should use, check the release notes for your version of Vaadin:  http://vaadin.com/download/release/6.5/6.5.1/release-notes.html (you can download these jars from:  http://code.google.com/webtoolkit/versions.html) 
  11. One more thing, I am not 100% sure - but when you run "clean" then you have to update GrailsAwareApplicationServlet file again (it happens to me quite offten).
I hope that my notes helped you. If you have any questions, just ask me and I will try to provide an answer. 
TextLab for Mac
Ultimate application to validate, clean and format JSON, XML, SQL, HTML.