Contents

[divider]

Intro + Building Grails App + Domain Layer

This project is a walk through of what it took to build the application in the embedded video below:


I had a need to build an inventory management system – specifically for a new hobby of mine: Trading Cards.  Yes, I am a nerd.  Yes I know it’s not popular anymore… but hey, I had a need here and used this need to infuse me with motivation.

I learned quite a bit about deploying a project on a paid host.  Unlike where I work, I didn’t have unlimited memory and had to work within many constraints.  I put together this walkthrough as notes for myself, as well as to help anyone who gets stuck.

This presentation will assume little knowledge with Grails, but might be useful for anyone of varying skillets.  You should have some knowledge of programming, but could use this tutorial as a first programing project.

This walkthrough will cover:

  • Build a Grails web application that takes user input and stores it in a database
  • A grid plugin will be used to display user data to the user
  • Provide a User registration system
  • Form/data restrictions
  • Deployment (Tomcat/MySQL)

Building The Grails Application

Which is better, Rails or Grails?  In my humble and limited experience, development wise it’s a toss up.  Hosting wise, there seems to be a plethora of Rails hosting solutions (many that are free) and far less for Grails specific hosting.  At the end of the day a Grails app is a Java app, so you can get away with a Java/Tomcat host provider – but again the choices are far limited for private hosters.  If you are developing for a internal system that uses corporate infrastructure, then this isn’t of course an issue.

Requirements

My requirements where the following:

  • The web application should have a user registration system
  • Each user can only see their own data
  • Users can not edit/delete other people’s data
  • Users will login to a grid that looks like excel.
  • They will have a input form that lets them add inventory details
  • An image upload system would be nice (but optional)
  • Add some extra stuff for research (Google Trends integration and eBay integration.)

What is Grails

Grails is a groovy based MVC framework.  At the end of the day it compiles to the JVM and becomes Java Byte code.  So it’s a solution to write Java without having to use Java with Spring, etc.  Grails comes with many features built in, and Groovy is much easier to write then straight Java.

Grails is like a Java form of Rails.  It provides a method of making applications with less building core infrastructure.  Since it’s Java, it leads one to learn and understand Java concepts which is great since Java is a huge development platform for web and mobile.

MVC stands for Model View Controller and depicts a framework that uses Domain Models to build out the data layer, Views that build out the presentation layer and Controllers that build out the logic layer.  So think: Model/Domains = database, View = web and Controller = logic.  We add things like services into the mix as well, to offload logic from the controllers, but in a  nutshell that’s grails.

Since Data is being stored in the database for this application we’ll start there for our application.  So let’s begin.

Creating a Grails App

I use Intellij, and generally create a grails application through Intellij.  You’ll want to use a IDE.  A free IDE would be Eclipse of Spring Source’s Grails IDE.  Intellij offers a 30day trial of their Complete IDE.

From the command line you could do a: grails create-app [app name]

Or in your IDE you could create a new project.  Either way create a new project/application for Grails.

Within your IDE, open the application and you’ll see a project structure/tree already made for you.  You have a file structure like this in your project root:

/
/-grails-app
/-lib
/-scripts
/-src
/-target
/-web-app

Of those folders the ones that are the most commonly used (at least for me) are the grails-app, target and web-app. The grails-app folder holds the MVC aspects of the app.  The Domains, Controllers, Services, and Views are all stored and created here.  The target folder is the default place your WAR file will be created (for deployment) and the web-app folder is where the js and css files are stored (all your static files.)

If you open grails-app up, you’ll see it breaks down to these sub folders:

/grails-app
|–>conf
|–>controllers
|–>domain
|–>i18n
|–>services
|–>views

While there are other folders, I’ll briefly focus on these.

Conf is where all your configuration files are.  These files will tell Grails how to connect to a database, or what plugins you might use for the app.

Controllers is where you will create/generate logic for your application.

Domain is of course where we build our domain models – that will tell the application how the data it uses will be created, maintained, constrained in the database of our choice.

i18n is for internationalization

Services is where we build externalized logic.  We want the controllers to be DRY and limited in scope/code.  When they start to build up, we move the logic to a service(s) and reference them within the controller.  It’s also useful if we have logic that is used across different controllers – so we have the code in one place and referenced by each controller.  An example would be a email service – all the logic handling emailing could be in the email service, and each controller that needs to use it would reference it.

Views is where our GSP (grails/groovy server pages) are created. These dynamic files create a web layer for the user who visits the site.

Grails generates a lot of the code we need.  It can do this with scaffolding.  But we don’t need to use scaffolding in many cases (and save some unnecessary code from being generated.)  However, to start, I’m going to generate some code.

Creating The Domain

In the IDE create a new domain (this is done in Intellij by right clicking the Domain folder in the project and choose New Domain Class.  Give it a name (some people like the com.Name format.  I usually just use a name without the prepending org/com. Whatever you name it, stick with it through out the tutorial.)

I’ll name mine: TradingCardInventory

Once the domain is created, open it.

It will have a closure at the top, like:

class TradingCardInventory {}

and a closure below it:

static constraints = {}

We will be defining the fields (columns) the data tables will have, and also how they will be constrained.  Inside the first closure add the following:

String description
String tradingCardType
Boolean autograph
BigDecimal marketPrice
String image

That tells Grails that when we create the data layer, this table (TradingCardInventory) will have columns description, tradingCardType, autograph and marketPrice.  The first two take strings, the third is a boolean and the fourth column is a BigDecimal.  The last is a String as well… images are bytes, but we won’t be storing the actual image in the database, just it’s path on the file system.

In the static constraints, lets add some conditions.  We don’t want people typing html codes into the description field. Since it takes user input, we’ll need to define what we allow, we do that with a constraint:

description blank: false, matches: “[a-zA-Z _ 0-9]+”

That constraint says that description is a field that can not be blank (so therefore is required), it will take a-z characters, as well as A-Z (capital), it will take an underscore and it will take number digits.

For tradingCardType, lets say we want a drop down selection.  We want to say the cards can be: MTG (magic the gathering), Pokemon, Baseball, NFL, Hockey and Theatrical.

tradingCardType inList: [‘MTG’,’Pokemon’,’Baseball’,’NFL’,’Hockey’,’Theatrical’], blank:  false

If we didn’t have blank: false, it would show a blank value to start and the user would have to pick a value.  By saying blank: false it puts the first value in the list as the default.

For marketPrice we just want to say:

marketPrice nullable: true

That means it’s not required and can be null.  Users may not have price data for their cards and that’s ok. The same goes with image nullable: true

Ok save the project and within the IDE do a generate-all [domain name]

Grails will create controllers and views for you.  The view created will be a default CRUD application. It will let you create data based on your data model, edit this data, and delete it.  It will display a list of paginated data from the database as well.

To try it out, just run your grails app.  You can do this within your IDE by clicking the “play icon” in Intellij, or do a grails run-app in your project folder.

So it works.  Where’s the database?  By default grails uses an in memory database for the development mode.  This database is called H2.  You can view the data in the grails H2 app by appending dbconsole to your url… like: localhost:8080/dbconsole

 

#

5 Responses

  1. Nice posting and appreciated. Which version of GRAILS did you use? Also you mention ‘ACL’ in the title of your part 3 posting. Did you use the ACL grails plugin in your deployment? Any chance of getting a version of this project posted? Thanks …

Leave a Reply

Your email address will not be published. Required fields are marked *

Archives
Categories