Grails Portal

Page 1

Dynamic website development using the Grails Platform

Joshua Davis Senior Architect Cognizant Technology Solutions joshua.davis@cognizant.com


Topics Covered • What is Groovy? • What is Grails? • What are the Grails Principles? • What can the Grails Portal do? • Using the Grails Portal Framework? • Grails Portal Visions for the future


What is Groovy? • • • • • • • • •

Is an agile and dynamic language for the Java Virtual Machine Builds upon the strengths of Java but has additional features. Makes modern programming features available to Java developers with almost-zero learning curve Supports Domain-Specific Languages and other compact syntax so your code becomes easy to read and maintain Makes writing shell and build scripts easy with its powerful processing primitives, OO abilities and an Ant DSL Increases developer productivity by reducing scaffolding code when developing web, GUI, database or console applications Simplifies testing by supporting unit testing and mocking out-of-thebox Seamlessly integrates with all existing Java objects and libraries Compiles straight to Java bytecode so you can use it anywhere you can use Java


What is Grails? • A simplification of existing Java Web Application • •

Frameworks. Embraces the Don't Repeat Yourself (DRY) principles. Originated from dynamic frameworks like Ruby on Rails and others. They have helped pave the way to a more modern way of thinking about web applications. Grails builds on these concepts and dramatically reduces the complexity of building web applications on the Java platform. Builds on already established Java technology like Spring & Hibernate.


What is Grails(continued)? Grails is a full stack framework and attempts to solve as many pieces of the web development puzzle through the core technology and it's associated plug-ins.

Included out the box are things like: • • • • • • •

An easy to use Object Relational Mapping (ORM) layer built on Hibernate An expressive view technology called Groovy Server Pages (GSP) A controller layer built on Spring MVC A command line scripting environment built on the Groovy-powered Gant An 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 • A transactional service layer built on Spring's transaction abstraction

All of these are made easy to use through the power of the Groovy language and the extensive use of Domain Specific Languages (DSLs)


What are the Grails Principles? DRY - Don't Repeat Yourself This principle is centered around the concept on a singular representation for everything in your application. The advantage of this is to lower the amount of maintenance needed and dramatically decrease complexity. Grails attempts to implement the DRY principle by having a well organized set of artifacts that the developer uses as well as using generators for source code.

Convention over Configuration Design a framework so that it enforces standard naming conventions for mapping classes to resources or events. A programmer only needs to write the mapping configurations when the naming convention fails.


What Can the Grails Portal Do? Fundamental user-based set of functionality that can be used to write web applications that require users to be authorized and have personalization of presentation and functionality based upon the context of the logged in user.


Why a Grails Portal  Most small web applications don’t merit being created within a JSR-

168/286 portal environment.  Fulfills the need for a rapid solution for allowing users to self-register and authenticate, and then manage the authorization of the user to specific functionality.  Grails poses a very interesting choice for the class of database/model backed web-applications evidenced by it’s Ability to incorporate new functionality quickly.  Enabled by features and plug-ins for Grails.   

GSP templates Grails Object Relational Mapping (GORM) Web-Flow plug-in enables business processed based portal functionality to be quickly implemented. Grails UI plug-in enables Yahoo Ui Ajax functionality


Using the Grails Portal Framework


Developing with Grails  What is recommended for Grails Development?  Spring STS (SpringSource Tool Suite) version 2.3.0  Grails 1.2.1 

Download the source code

 Java 1.6.x  Groovy 1.7.x  Download the source code

 Where can you go for help?  Grails Portal Site http://code.google.com/p/grailsportal/  Grails Site http://www.grails.org  Groovy Site http://groovy.codehaus.org/


Grails Portal Business Objects


Party Business Object ď‚— A Party is an abstraction that

signifies a specific individual that takes part in the system, but not necessarily only a person. ď‚— It could be a system, individual user, or anything that can be identified by First Name and Last Name. These fields are the mandatory fields on the equivalent domain class. ď‚— The Party class holds all of the information that composes an individual that participates in the functionality of the Portal.


Order Business Object  The Order is an abstraction of

the concept of Orders and fulfillment.  This abstraction is important to the implementation of any selection for action from the website.  Orders are done to establish that something needs to happen.  Order Line items are created to track the specific Products that are selected by the user.


Product Business Object ď‚— Products are an abstraction of the concept of any

item that can be selected by a user. They are an abstract concept that is not necessarily something for sale. ď‚— Products have line items that enable bundling of products into a larger product


Event Business Object  Events are an abstraction of

the concept of an occurrence of a business “event.”  The recording of different types of business events enable business processes to be recorded along with all of the information associated with the process.  The implementation of this Portal uses the Grails WebFlow plug-in to initiate business events and track the progress of these events.


Grails Portal Domain Classes  The toString() method is implemented in each of the domain classes

in this application. This is used in Grails scaffolding.  Constraints are used in the domain classes to have a direct impact on how default Grails scaffolding generates controllers and views.  In this application the “blank:true” and “size:x…x+n” are commonly used to

control how the fields are generated when they are generated through scaffolding.  The nullable constraint relates directly back to how the domain class is generated into a table in that it makes the column mandatory (not null). Also, the size constraint gives the indication for how large the field will be in the database.

 All of the domain classes implement Serializable. This is mandatory

if Grails Web-Flow plug-in is used.


Grails Portal Business Process Management using Grails Web-Flow Plug-in


Introduction to Grails Web-Flow  Most web applications collect information in a stepwise

fashion and then make business rule decisions based upon the data collected to accomplish some business functionality.  This is a common requirement in any application, from a simple contact page to a commercial order management system.  These types of requirements have been fulfilled in the past in many different ways.


Issues Grails Web-Flow Can Solve  Termination/re-entry

 Workflow state  Workflow Implementation

Complexity


Grails Web-Flow  It uses the well known Spring Web-Flow framework to manage the 

 

workflow. This framework handles the persistence of the workflow state and manages the issues when the refresh and back buttons are hit by the user during a workflow “session”. Its convention-based paradigm of Groovy simplifies the development process to implement the flows in a concise, understandable manner. Flows are created in a manner consistent with other Grails development. Grails domain classes make it simple to save the result of the workflow.


Grails Web-Flow Definition There are many types of workflow processes that exist, but it should be known that not all business flows lend themselves to being defined using Grails Web-Flow. There are three basic types of workflow:

 Human-based workflow  This type of workflow is based upon the recording of activities strictly done by a human

being where the intervals between recordings are extended and are not immediately ongoing.  This can be, for example, the recording of a task such as a set of tests. Grails Web-Flow does not by default implement this type of workflow very well because it does not have the ability to re-constitute a workflow once the workflow session has been interrupted due to inactivity.

 Computer-based workflow  This instance is where Grails Web-Flow shines.  As in most web applications implemented using Grails Web-Flow, the intention is to

collect data and guide the user between screens in one “flow session .”

 Mixed workflow (computer and human) – used in Grails Portal  A combination of the first two types, this hybrid workflow concept is a common reality

when designing web applications.  Grails Web-Flow can also implement this situation with a combination of traditional Grails web development and “linked” flows.


Grails Web-Flow Quick How To  Planning is essential for success when

defining a flow within a Grails application and all business processes can be distilled down to a basic set of steps.  Whether you use a napkin or a formal diagram to document them, it is essential that you understand the required steps needed to implement the business process in your Grails application.  In Grails Web-Flow, you take each of the steps and create a Groovy closure to capture the functionality.  If you follow the normal process of GSP and Spring MVC web application development, you will find there are some slight adjustments required to enable flows to work.


Understanding Grails Web-Flow  The name of the flow is described by adding the “Flow” word to the end of a closure

in your controller.  When you create the views, you must use the same name as the web-flow as a directory that contains the views for the web-flow.  Navigating between the web-flows is pretty simple:  The “name” property of the submit button for the pages in the flow indicates which

closure to execute when the page is submitted.  If the user navigates away from the flow, it is possible to re-establish the flow.  Once the user ends the flow by hitting cancel, you have the ability to manage the data within the flow.

 Use the “flow” variable to store information for the span of a flow.  The default context in a view used for a web-flow uses the “flow” variable.  In your controller you should always have explicit entry points and exit points from

the web-flow. This can be implemented by creating a default closure that will be used by the web-flow.


Web-Flow Best Practices  Always use a service class when handling GORM objects. Either create a Grails service 

 

 

class or identify one that you will use to handle the saving of the data from the flow. Create a method to move the data from the posted web parameters to the domain objects. This process can vary widely in complexity. One of the easiest ways to do this is to have the names of the items on the view the same as the ones in your domain class. If you do this you can just instantiate the class using the parms variable–it can be that simple! Create a custom Java Exception class similar to ValidationException. Make sure that you have a try, catch block around the call to catch a validation error. Write a method in service class to validate the domain object. If there is an error, throw a custom validation error that will show to the user the field that needs to be changed. Handle the exception. If there is an exception, make sure the appropriate values are set in the view, and return the model. If there is no exception, create the model objects needed and move the flow to the next page.


Web-Flow Demo From Grails Portal Code


Grails Portal Views


Grails Portal Views Using Ajax  The decision was made to use the GrailsUI plug-in for implementing

Ajax and Web 2.0 functionality within the Grails portal. This plug-in has a few major advantages:  It fully uses the standard view functionality within Grails, adhering to the principles that I have laid out for the rest of the portal.  It uses the Yahoo UI and Bubbling JavaScript libraries which are rich in functionality, but also very stable.  It implements a minimal set of tag libraries which are easy to use and not too obtrusive to my existing GSP pages.  It allows me to create my own set of tag libraries to implement functionality that doesn't exist in the GrailsUI tags without conflicts.  It references the YahooUI JavaScript library is very well documented in theYahoo developer site.


Why are Grails Tag libraries so popular?  A simple explanation would be that they are much easier to to develop

than Java tag libraries.  Grails tag libraries can be developed quickly, easily, and take much of the frustration out of user interface development when done in a pragmatic manner.  In the chosen style of development for the Grails portal, JavaScript is doing much of the work to handle the user interaction in the web browser; consequently, the view files (GSP) often become a set of confusing CSS and JavaScript combined with Grails tags.  With custom Grails tags in place this issue is reduced to a point where views are easily maintained. The implementation of the user interface functionality becomes more generic, and therefore increasingly adaptable when JavaScript libraries change.


Creating a Tag Library Demo From Grails Portal Code


Grails Portal Visions for the Future


Future Functionality  Take advantage fully of Grails 1.2 features:  Use of Packages for Domain and Controller classes.  DSL’s for configuration  More Dynamic Functionality  Improved database flexibility and functionality  Dynamic Attributes  Full implementation of Ajax style user interface through scaffolding.  Context based scaffolding  Daisy Content Management System integration


Demo of Future Functionality


Questions? Comments? Joshua Davis Twitter – joshuad2 Email – joshua.davis@cognizant.com LinkedIn, CodeTown – Joshua Davis Groovy/Grails Magazine Author


Grails Portal Cheat Sheet Database - Grails Portal use of GORM Security - Shiro Plugin Presentation • YahooUI Plugin • Bubbling Plugin • GrailsUI Plugin • Custom GSP Tag Libraries

Business Process - Web-Flow Plugin Administration - Grails Scaffolding


References Shiro plugin: http://www.grails.org/plugin/shiro GrailsUI plugin: http://grails.org/plugin/grails-ui YahooUI plugin: http://www.grails.org/YUI%20Plugin Bubbling plugin: http://grails.org/Bubbling%20Plugin Grails Framework Reference Guide: http://grails.org/doc/1.21/ Java Serialization: http://java.sun.com/developer/technicalArticles/Programming /serialization/ The Definitive Guide to Grails (second edition) by Graeme Rocher and Jeff Brown ISBN: 9781590599952 Enterprise Patterns and MDA: Building Better Software with Achetype Patterns and UML by Jim Arlow, Ila Neustadt Addison Wesley Press ISBN-13: 978-0321112309


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.