Showing posts with label Domain Class Common Structure. Show all posts
Showing posts with label Domain Class Common Structure. Show all posts

Monday, October 10, 2016

Grails application: Create an common structure for domain class as artifact for common functionality when create new domain class

Create a groovy file under directory 'src/groovy/templates/artifacts' with name 'DomainClass.groovy" with following contents.

NB: Class name is significant here.

And then when you create an domain class, the new domain class structure will be as like this.


@artifact.package@

class @artifact.name@ {
    Long id
    String name
    String displayName
    String description

    Date created
    Date updated

    static constraints = {
        name(blank: false, minSize: 1, maxSize: 255)
        displayName(nullable: true, minSize: 1, maxSize: 255)
        description(nullable: true, maxSize: 500)
    }
}


And another example for your controller then groovy file name must be 'Controller.groovy' with following contents


@artifact.package@
import com.pkm.GeneralExceptionHandler
import com.pkm.services.DataReadService

class @artifact.name@ implements GeneralExceptionHandler {
    DataReadService dataReadService

    def index() {

    }
}


And when you create any Controller that would be look like as follows


package com.pkm

import com.pkm.GeneralExceptionHandler
import com.pkm.services.DataReadService

class PkmController implements GeneralExceptionHandler {
    DataReadService dataReadService

    def index() {

    }
}