{ Developer { GRAILS: An Open Source Framework for Rapid App Development

Groovy on Rails (GRAILS) framework is a typical Model View Controller Architecture (MVC) with Groovy as a high level Java wrap around language built upon the J2EE framework and is very easy to use even for non-Java programmers

— Rahul Kumar

ou might get confused with the name of a musical band group but here we will talk Snapshot about a serious technology. The framework Yis inspired by Ruby on Rails that makes Applies to: Java developers use of Groovy language which is a dynamic and agile USP: Rapid application development, Based on Coding by scripting language. The syntax is somewhat very Convention paradigm similar to that of Java. In fact, you can use groovyc Primary Link: http://grails.org/ just like javac to produce bytecode files. Also, Groovy integrates with Bean scripting framework, which Search Engine Keywords: Grails, Grails framework allows you to embed any scripting engine into your Java code. It is intended to be a high-productivity framework by following the “coding by convention” paradigm, providing a stand-alone development environment and hiding much of the configuration detail from the developer.

How GRAILS works? Like any other MVC based framework, GRAILS also has three components to handle the overall working --Domain Classes (Model), GSP (Groovy Server Pages) or JSP (Java Server Pages) (View) and Action Controller (Controller) Frontend is designed and developed with gsp/jsp integrated with HTML and CSS files. Any user will make use of only this part. The request is generated by the user here only. Afterwards, the action controller takes over the control. Controller bundles and routes HTTP request to other objects in framework. Where they get to meet with the business entities and other domain classes in order to satisfy the request generated by the user. After that, the result is outputted with the help Process diagram shows how a user interacts with the system of gsp/jsp. and how his request is processed & output is displayed.

84 PCQuest NOVEMBER 2012 A CYBERMEDIA Publication www.pcquest.com GRAILS vs Other J2EE Frameworks and users from a database. Of course there is a lot more You could find GRAILS extremely easy to use in comparison that you can add to this application, but for the sake of with other Java frameworks like Struts. Grails has three simplicity, we’ll stick to this one. The database we’re using properties which attempt to increase productivity when is created on MySQL and is named ‘Inventory_Table’. First compared to traditional Java web frameworks -No XML create classes for each of the database entities. This is configuration, Ready-to-use development environment, done using the following command from the root directory Functionality available through mixins and Reduce the of your product. need for configuration files and other boilerplate code. Apart from these properties there are several other grails create-domain-class items features such as Language constructs, List & Map support, XML & capabilities which are rarely found at a single The next step in building your domain model is to link framework. One of the major advantages of using GRAILS these classes so you can handle relationships between over other j2ee frameworks is that it can easily work on them. You do this by telling GORM that which type of re- any j2ee container. All you have to do is to just deploy the lationship exists between the database entities. After that war file in any j2ee container and tweak web.xml. you will add the functionality and validation rules to the different domain classes. GORM (Groovy Object class items { Relational Mapping) static hasMany = [location:location, category:category] Domain classes are objects that need to be mapped to the int items_id database. That’s where GORM comes into effect. The basis String version of GORM is formulated by another very popular framework Date lastUpdated i.e. Hibernate. It would be helpful in linking Domain Boolean recorded= false classes using relationship. GORM provides very powerful SortedSet comments dynamic methods for CRUD (Create/Read/Update/Delete) static constraints = { operations. Moreover, an HSQL database comes built-in title(nullable:false, blank:false, length:1..50) with GRAILS. version (length:0..100) lastUpdated(nullable:true) Let’s Start Building Apps recorded(nullable:false) You can find install instructions on the framework’s } website. Just install it and set the Groovy_Home and Java_ } Home environment variable. Also, add Groovy_Home/bin The next step is to expose your domain objects to users to your path. After configuring the GRAILS with any IDE through some controllers and views. This will allow you to like Netbeans or , you are ready for the application manipulate your domain objects through a . development. First go to the development workspace and One important thing is that, you don’t have to use getters run the following in the command prompt: grails create-app Enterprise Inventory

This will create a folder called Enterprise Inventory and a grails-app directory under that, which contains the sub-directories named as Configuration, Control- lers, Domain classes, Serv- ices, Tag Libraries, Utility and Views. The application simply Code snippet and Sub-directory structure of the application development configured on displays products, location Netbeans IDE.

www.pcquest.com A CYBERMEDIA Publication NOVEMBER 2012 PCQuest 85 { Developer {

Different stages of inventory application, login page at top left, data entry page at the bottom and report generation page at the right

and setters of Java Bean classes like we use to do in other j2ee framework like Struts.To create the controllers for your domain, go back to the command line and run the follow- ing commands:

grails create-controller items

Once you are through with your classes, you can con- figure the application to use the MySQL database. As mentioned earlier, the whole working revolves around the MVC architecture and the user will interact through the gsp/jsp page.

86 PCQuest NOVEMBER 2012 A CYBERMEDIA Publication www.pcquest.com