On August 20, 2010, in Uncategorized, by Rob Wilson

Burt has released the UI extensions for the spring-security-core plugin.
As ever very useful.



Burt’s new app-info plugin

On July 20, 2010, in Grails, by Rob Wilson

Burt Beckwith has released a new Grails plugin that provides usefull info on the Grails app.

As we have come expect, it is very good.


From his post:

“I released a new Grails plugin today, app-info. This plugin exposes a lot of internal information for a Grails application. Most of the code for this plugin dates back a long time and was originally mostly JSPs that I would copy into whatever application I was working on to give myself a view into what’s going on. Over time as I started using Spring and Hibernate I added more pages to display that information, and it now also shows information about Grails too.”

Tagged with:

Simple Grails App With Security and Ajax

On July 16, 2010, in Grails, Tutorials, by Rob Wilson
V Creating a grails application
A good tutorial needs a real world application problem to cover. I believe I have found just such a problem :-)

In this tutorial spread over three postings we are going to create a complete grails killer web application. This application will keep track of the total number of days spent in each country our users visit in any one tax year. As I’m sure you have already worked out. This useful public service web application is targeted at the  super rich international traveller who doesn’t happen to have an account. It will help them avoid accidental tax domicile status in any single country. (which would obviously be a disaster)

V The app will include
* Security and user management with roles, for a normal user and for a super user or admin.
* Persistence of users itineraries and static reference data
* Some ajax and the integration of out of the box open source javascript components to display handy views of the itinerary and results to the user.
V Setting Up
To build this app all you need is an internet connection, an installation of Grails and a text editor but life will be easier for you if you use an IDE. I will be using the STS (SpringSource Tool Suite) IDE for this tutorial so you should:
V Download and install STS
* set up the extensions for Grails and Groovy under the dashboard view.
* Download and install grails version 1.3.2
V Getting Started
* To create the app we use the standard grails app creation script
* grails create-app residency
* This will create a new folder with the name you specified for the application and inside the complete grails developement application structure.
V Plugins
* We are going to use the excellent spring-security-core plugin written by Burt Beckwith to handle the security and user management. We can install this plugin by doing:
* grails install-plugin spring-security-core
* When the plugin installation completes it will announce that two extra grails scripts are now available. The one that we need is the s2-quickstart script. This script will set up domain classes to handle the persistence of security details and config for the app. The script takes up to four arguments but we are going to use three. We will declare a package for the application security domain classes (actually this will also be the base package for the application), the name of the user class and the name of the authority class. As we will not be using any database that clashes with the obvious names “User” and “Role” we will use these.
* so we do:
* grails grails s2-quickstart com.yourcomapnyname.residency User Role RequestMap
* This will create three domain classes. The User class for storing the user credentials, the Role class for storing roles that a user can be assigned and a UserRole class that associates Users with Roles.
* The new spring-security-core plugin now uses the standard grails config.groovy file for config and if you look at the bottom of this file now you can see that the s2 script has inserted lines that tell the plugin the security class names you have chosen.
V Domain Classes
* Now we can create the domain classes we will need for the application.
V We will create three
* Airport
* Country
* Flight
* To do this we use the standard grails domain class creation script and do:
* grails create-domain-class com.yourcompanyname.residency.Airport
* grails create-domain-class com.yourcompanyname.residency.Country
* grails create-domain-class com.yourcompanyname.residency.Flight
V If you now look in your project grails-app/domain folder you will find the package com.yourcompanyname.residency containing three fairly empty groovy domain classes. That look like this:
class Airport {
static constraints = {
* We will need to fill out these domain classes in order to declare to grails the details of the model we will be using. Here is one of the major wins for grails. A single point of declaration for the domain model. This definition is used to great and update the database schema throughout the development process, freeing the developer from managing coupling across tiers. Believe me this saves LOTS of time.
V Ok so for our domain classes we will start by filling in the details of the Country class. Here is what this class should look like:
package com.yourcompanyname.residency
class Country {
String name
static hasMany = [airports:Airport]
static constraints = {
name blank:false, unique:true
airports nullable:true
V Lets go through what we have done.
* Firstly we can see that we have declared a “name” property of type String. Also if you look in the static constraints block you can see that we have declared that this property cannot be blank and that it must be unique. This tells Gorm (the grails object relational mapping code) to enforce these rules whenever a domain object is validated.
* As well as the name property we can see that we have declared a relationship that the domain class has to the Airport domain class. We have used the standard Gorm static hasMany property to declare that the Country domain class has a one to many relationship with Airport. As it stands this declaration does not describe the cascade direction of this relationship and we need to do this now as we fill in the details on the Airport domain class.
V The Airport domain class looks like this:
* package com.yourcompanyname.residency class Airport { String name String code static belongsTo = [country:Country] static constraints = { name blank:false, unique:true code blank:false, unique:true country nullable:false } }
* Looking at Airport we can see that we have declared name and code as String properties and defined constraints for them in a a similar way to the Country domain class but also we have declared the cascade direction for the relationship between Airport and Country. The static belongsTo property declares that by default Country is the parent Object and that when a Country is updated or deleted then the child objects Airport should also be updated or deleted.
* Next will declare the Flight domain class.
V package com.yourcompanyname.residency class Flight { Airport departingAirport Airport arrivingAirport Date departureTime Date arrivalTime static belongsTo = [user:User] static constraints = { departingAirport nullable:false arrivingAirport nullable:false departureTime nullable:false arrivalTime nullable:false user nullable:false } }
* You should begin to see the same use of the relationships and constraints definitions. This is standard GORM and very much the bread and butter work of creating a grails application.
V Finally we need to modify the autogenerated spring security User class to declare a relationship to the Flight domain class. This will allow users to be associated with their flights and will allow us to limit the visibility of flights to only the user that entered them.
* package com.yourcompanyname.residency class User { String username String password boolean enabled boolean accountExpired boolean accountLocked boolean passwordExpired // declare the relationship with the Flight domain class static hasMany = [flights:Flight] static constraints = { username blank: false, unique: true password blank: false } static mapping = { password column: '`password`' } Set<Role> getAuthorities() { UserRole.findAllByUser(this).collect { it.role } as Set } }
* To do this we have inserted the "static hasMany = [flights:Flight]" line.
V Scaffolding
* To see what we have created so far we can use the Grails scaffolding code to generate a set of basic CRUD (create, read, update, delete) views and controllers that will form a basic web app prototype we can modify and refine to our needs.
* Do “grails generate-all” and when the script prompts you for a domain class just press return and the script will generate tests, views and controllers for all domain classes in the application.
* You should now have a runnable web application. To start the application do “grails run-app” and point your browser the the url provided by the run-app script.
* In my case the url is ” http://localhost:8080/residency
* If your application fails to start make sure that you do not already have something running using port 8080. If all went well grails will have started an instance of Tomcat listening on port 8080 and deployed the “residency” application into it.
* You should see something like this:
* This is the grails generated index page for the application and under the Available Controllers section are links to the controllers for each of the domain classes. Have a play around with them just to see what grails has set up for us.
* Finished playing? Very cool isn’t it. Grails has been able to do a heck of a lot just by using the limited detail we declared in the domain classes.
* Don’t spend any time entering any data into the app at this stage. We are by default pointing to the HSQL in memory db. So any data you enter will be lost when the app shuts down.
* In the next article we will set up some bootstrap data, get the security and user management configured and working and tidy up the application into the general storyline / layout that we want.
Tagged with: