Getting started with NMC-DSP

Installation

Step 1. Download the latest available version from the svn repository.
Step 2. Open is as a Project in Netbeans. Netbeans should be configured with the Groovy and Grails Plug-in. Also the correct version of Grails should be configured correctly. For more information you can read the Getting started with Groovy and Grails guide on this page.
Step 3. Right-click on the project and select "Run".

When all goes well a new browser(tab) should open with the "development" version of the NMC-DSP.

Give your project a name and click on Finish. For this example I will use DemoApp.

First we will test to see if the application runs. You do this by right clicking with the mouse on the name of the project and selecting Run. Depending on the version of Grails you will end up in your browser looking at the Grails logo with a Welcome to Grails message. If not please have a look at the FAQ section at the bottom of this page.

Stop the application (in the footer of Netbeans you can click on the X to stop the Servlet Engine)

Creating the model

There are many tutorials on the internet like the PetClinic and the BookStore. For this tutorial I will use something more related to our business :). After this tutorial you will have a simple digital Lablog to use in your own laboratory.

To build the model we have to create domain classes. Right-click with the mouse on Domain Classes within the DemoApp. Select New > Grails Domain Class. Give it a name and define the package. Now create the following:

Entry

User

This is a good moment to see the results. Right-click on the DemoApp project and select Run again. When the servlet engine is ready Netbeans will open your default browser again and show you the homepage. On the homepage you now see the Grails logo with a Welcome to Grails message again + the three controllers we just created. Select one of the controllers to have a look at the Scaffolding.

Bootstrap the Development environment

At this moment you are using a non-persistent in-memory database called HSqlDB. For now this is fine for your local development version of the application. However the downside is that you loose all data every time you restart the application. Within the NMC-DSP project we use the bootstrap to inject test data at start-up. In the Configuration folder of the DemoApp you will find a file called BootStrap.groovy. Open this file in the IDE and add the following code and restart the application

Page (main/homepage)

package nl.nbic.bioassist
class PageController {
def index = {
def entries = [] //create an empty list
// check if a user is set. Without a user we cannot log anything
if (!session.user){
// no user was set, forward to set user page
forward(controller:"user",action:"select")
}
// check if a project is set. Without a project we cannot log anything
if (!session.project){
// no project was set, forward to set project page
forward(controller:"project",action:"select")
}
// this means we have a user and a project, load all entries (if there)
if (session.project?.entries){
entries = session.project.entries
}
[ entries: entries ]
}
}

This is a very basic controller that checks if a user and a project are set in the session. When no user is selected you will be forwarded to the user/select page. Next is the project, like the user we need a project to be selected. Same as with the user you are forwarded to the project/select page when there is no project.

Set new homepage

By default you will be redirected to /grails-app/views/index.gsp. For our application the homepage is the page/index view. To change this go to the URLMappings file (/grails-app/conf/UrlMappings.groovy) and change it to:

<html>
<head>
<title>Welcome to Lablog</title>
<meta name="layout" content="main" />
</head>
<body>
<h1 style="margin-left:20px;">Select a user</h1>
<p style="margin-left:20px;">
<!-- see if a user has been selected -->
<g:if test="${session?.user?.id}">
Selected user: ${session?.user?.name}<br /><br />
</g:if>
<!-- the ArrayList with users was passed to the view by the controller -->
<g:each in="${users}" var="user">
<!-- the g:each tag can be used to iterate over the array values -->
<!-- we can use g:link to create a link to be able to select a user -->
<g:link controller="user" action="select" params="[userId:user.id]">${user.name}</g:link><br />
</g:each>
</p>
</body>
</html>

<html>
<head>
<title>Welcome to Lablog</title>
<meta name="layout" content="main" />
</head>
<body>
<h1 style="margin-left:20px;">Select a project</h1>
<p style="margin-left:20px;">
<!-- see if a project has been selected -->
<g:if test="${session?.project?.id}">
Selected user: ${session?.project?.name}<br /><br />
</g:if>
<!-- the ArrayList with projects was passed to the view by the controller -->
<g:each in="${projects}" var="project">
<!-- the g:each tag can be used to iterate over the array values -->
<!-- we can use g:link to create a link to be able to select a project -->
<g:link controller="project" action="select" params="[projectId:project.id]">${project.name}</g:link><br />
</g:each>
</p>
</body>
</html>

Entry create

To be able to create a new entry we setup a postnew function in the EntryController class. This allows other pages to post a new Entry. We don't have to create a view for this because we are either redirected to where we came from or we "render" an error message on the screen. Normally you would not do this with a render but for now it's fine.

package nl.nbic.bioassist
class EntryController {
def Scaffold = true
def postnew = {
//check if a new entry was created via the form
if (params.entry){
new Entry( user: session.user,
project: session.project,
data: params.entry
).save(flush: true)
//we forse a flush to the database
//redirect where you came from
redirect(url: request.getHeader('referer'))
} else {
//render is easy, we don't need a view now.
render("Was unable to create an entry, no entry data was set")
}
}
}

Main layout

By default the layout is very basic. For this guide I made some changes to the default template (/grails-app/views/layouts/main.gsp) and I added an image (/web-app/images/nbiclogo.png)

Template (s)

The Grails application comes with a main.gsp template by default. You can edit this or create your own. Also you can setup your own templates for a header, menu, footer, etc... You can place them anywhere in the /grails-app/views/ folder. Make sure the name of your own templates start with a "_" underscore. This is the convention Grails uses to be able to identify a template. You can easily add a template to a gsp-file by using the following syntax

<g:render template="/header" />

This will include the /grails-app/views/_header.gsp template.

For our Lablog application we start by replacing the main.gsp (/grails-app/views/layouts/main.gsp) by the following:

Deploying the application as a WAR-file

Now that we have a basic Grails application setup we can deploy it to our Tomcat/Jetty or other server. The best way to do that is to run "grails war" in the root of your application or right click on the project in Netbeans and select "Build". More about deploying you application on a server can be found on: http://www.grails.org/Deployment

Useful Plug-ins

dbUtil

Basic interface to the database tables. Useful when developing to see if database updates were done correctly.

Creates a nice overview of your domain classes and the relationships between them. Please note: the version we use is not able to display Floats and BigDecimals correctly. Also Graphviz (http://www.graphviz.org/) is required to see the diagram.