Optimize your Grails GGTS Development IDE: Domain Classes

Hi,

I use the Groovy/Grails Tool Suite GGTS 3.4.0 as development IDE. If you create a domain class,

grails create-domain-class com.mmi.store.City

you will get this boiler code created:

package com.mmi.store
    class City {
        static constraints = {}
}

While nothing is wrong with this, I would like to have a more “enriched” and descriptive domain class created, e.g.:

package com.mmi.store
import groovy.transform.ToString
import groovy.transform.EqualsAndHashCode

/**
* City
* A domain class describes the data object and it's mapping to the database
*/

@ToString(includeNames = true, includeFields = true, excludes = 'dateCreated,lastUpdated,metaClass')
@EqualsAndHashCode

class City {

    /* Default (injected) attributes of GORM */
    Long id
    Long version

    /* Automatic timestamping of GORM */
    Date dateCreated
    Date lastUpdated

    // static belongsTo = [] // tells GORM to cascade commands: e.g., delete this object if the "parent" is deleted.
    // static hasOne = [] // tells GORM to associate another domain object as an owner in a 1-1 mapping
    // static hasMany = [] // tells GORM to associate other domain objects for a 1-n or n-m mapping
    // static mappedBy = [] // specifies which property should be used in a mapping

    static mapping = {
        // http://grails.org/doc/latest/ref/Database%20Mapping/table.html
        // table name:"a table name", schema: "a schema", catalog: "a catalog" 
    } 
    static constraints = { 
        // http://grails.org/doc/latest/ref/Constraints/Usage.html 
    } 
    /* 
    * Methods of the Domain Class 
    */ 
    // @Override 
    // Override toString for a nicer / more descriptive UI 
    // public String toString() { 
    // return "${name}"; 
    // }
}

Let’s see how to modify GGTS to get this.
First install the Grails templates:

grails install-templates

Then you get the following directory structure:

src
    templates
        artifacts
        scaffolding
        testing
        war
    ...

Go to src/templates/artifacts and open DomainClass.groovy
Replace the default template with your specific boiler code settings.
To get the above mentioned domain class boilerplate code created, I use the following template in DomainClass.groovy:

@artifact.package@
import groovy.transform.ToString
import groovy.transform.EqualsAndHashCode
/**
* @artifact.name@
* A domain class describes the data object and it's mapping to the database
*/

@ToString(includeNames = true, includeFields = true, excludes = 'dateCreated,lastUpdated,metaClass')
@EqualsAndHashCode

class @artifact.name@ {

    /* Default (injected) attributes of GORM */
    Long id
    Long version

    /* Automatic timestamping of GORM */
    Date dateCreated
    Date lastUpdated

    // static belongsTo = [] // tells GORM to cascade commands: e.g., delete this object if the "parent" is deleted.
    // static hasOne = [] // tells GORM to associate another domain object as an owner in a 1-1 mapping
    // static hasMany = [] // tells GORM to associate other domain objects for a 1-n or n-m mapping
    // static mappedBy = [] // specifies which property should be used in a mapping

    static mapping = {
        // http://grails.org/doc/latest/ref/Database%20Mapping/table.html
        // table name: "a table name", schema: "a schema(optional)", catalog: "a catalog (optional)" 
    } 
    static constraints = { 
        // http://grails.org/doc/latest/ref/Constraints/Usage.html 
    } 
    /* 
     * Methods of the Domain Class 
    */ 
    // @Override 
    // Override toString for a nicer / more descriptive UI 
    // public String toString() { 
    // return "${name}"; 
    // }
}

The idea, I found here and here, so any credits go to them.

As you see, it is very easy to customize Grails and GGTS to your own needs or best practices.

HTH and have fun with Grails

Johannes

Advertisements
This entry was posted in Development, Grails and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s