Grails is pretty fast to get up and running. We mainly need to be clear about one main thing, the domain model. The primary task is to identify the entities in the system as domain classes, and then elaborate on them adding properties. Then define the relationships between them.

Once this is done, we get a ready-made application to start with. Now to really play around with this application, it helps to add some test data. This is also useful to do some initial testing of our domain model, before we add controller logic to it.

Using the CSVReader class outlined here earlier, its easy to add some test data.

Create a service that does the importing.

class LoadDbService {

    boolean transactional = true
    def rootDir = 'something'

    // a set method for property rootDir
    public void setRootDir(String dir) {

        rootDir = dir
        if (! (dir.endsWith("/") || dir.endsWith("\\")))
            rootDir += File.separator

    def load() { .. detailed below .. }

This service would be called at startup, from Bootstrap.groovy, in the init method:

        def init = { servletContext ->

        def dbs = new LoadDbService()

        dbs.rootDir = servletContext.getRealPath("/data/")

        println "loading data... " + dbs.rootDir


This service imports CSV data into domain objects. If a domain object refers to other domain objects, do a find and replace the item in the map with the found object. Eg for a finance application having domain classes Bank, Account, Entity (a person or an institute who operates an account and makes a payment). Account below belongs to both a Bank and an Entity. So this is how the load method would load value to these tables:

        def csv

        csv = new CsvFileReader(rootDir +  'Bank.csv')

        csv.forEachRow () {
            new Bank(it).save(flush:true)

        csv = new CsvFileReader(rootDir +  'Entity.csv')

        csv.forEachRow () {
            new Entity(it).save(flush:true)

        csv = new CsvFileReader(rootDir +  'Account.csv')

        csv.forEachRow () {
            println it
            def b = it['bank'] = Bank.findByName(it['bank'])
            def e = it['entity'] = Entity.findByName(it['entity'])

            println it

            def a = new Account(it)



So what about the contents of the CSV files? As you might’ve guessed, each row of the CSV files contain a value for each non-null field of the domain class. Eg, the domain class Payment contains the following initial lines:


The first line of this CSV file contains a header that has the domain class property names. This is how the object is constructed – because the map that is loaded for each row, is passed to the constructor of the class (eg new Entity(it) – where ‘it’ is a map.

So we need a CSV file for each domain class. Now there’s an easy way to create this CSV file as well. In the index.gsp, we add the following code, that will iterate through the domain classes, and make a list of the header fields for each of them. We then just need to remove unnecessary fields like id, version and anything else thats nullable if necessary.

<div id=”domainList” class=”dialog”>
<h2>Available Domains:</h2>

<g:each var=”c” in=”${grailsApplication.domainClasses}”>
<li class=”controller”><g:link controller=”${c.logicalPropertyName}”>${}</g:link></li>
<li class=”controller”>
<g:each var=”p” in=”${}”>


When we view the index page in the browser, the above code renders all the domain classes and the header line for each CSV file (i.e. the comma seperated list of their properties)!