Showing posts with label Quick Tip. Show all posts
Showing posts with label Quick Tip. Show all posts

Monday, February 15, 2010

Accessing Grails domain constraints at runtime

Grails (well GORM, Grails Object Relational Mapping) makes the validation of persistable objects easy. Take the following domain class.
class User {

String username
String passwd

static constraints = {
username(blank:false, unique: true)
passwd(blank:false, minSize:7)
}

}
The above domain class defines a "User" that contains a user name and password. The user name must not be blank and must be unique. The password must not be blank and must be a minimum of 7 characters in length.

I recently stumbled upon a use case where I was required to automatically generate a password, and of course this password needed to conform to the validation constraints defined in the "User" domain class. I didn't want to hard code the minimum size of the password because there is always a chance it will change in the future. What I needed was a way to obtain the "minSize" constraint value of the "passwd" property.

Here is how I did it
def constrainedProperty = User.constraints.passwd
def minSizeConstraint = constrainedProperty.getAppliedConstraint('minSize')
def length = minSizeConstraint.getMinSize()
The above code snippet first obtains an instance of org.codehaus.groovy.grails.validation.ConstrainedProperty which is an under the hood class that provides the ability to set constraints against a property of a domain class (in this case "user.passwd"). I then call the "ConstrainedProperty" instance to get the applied constraint of "minSize" which then returns an instance of org.codehaus.groovy.grails.validation.MinSizeConstraint. The "MinSizeConstraint" class is the class that is charged with performing the actual validation of the "minSize" constraint, and so therefore also contains the value of the minimum size of the "passwd" field I am concerned with. I can now easily generate a password that is greater in length than the "length" variable, currently 7.

Easy peasy

Sunday, February 14, 2010

Grails partial validation of domain objects

I found this funky little feature in Grails the other day. Partial validation of domain objects. If you are fortunate enough to be developing an application that's domain model can be designed in a way that closely models your required screens (say a bunch of GRUD screens) then this feature will probably be of little interest to you. However if like me you are finding that the default Grails CRUD type approach is not flexible enough for your requirements check this out!

I am developing a web flow application that will populate a domain object (the user is filling in an application form) across multiple controllers and or actions. At each stage of the flow I am wanting to only validate the properties which the current action is charged with binding.

Lets say this is my domain object
class Application {

String firstName
String lastName
String emailAddress
Date dateOfBirth
String sex

String address1
String address2
String address3
String address4

//etc..

static constraints = {
firstName(blank:true)
lastName(blank:true)
emailAddress(email:true)
dateOfBirth(nullable:false)
sex(inList:["Male", "Female"])
address1(blank:false)
address2(blank:false)
address3(blank:false)
address4(blank:false)
}
}
I would like to populate this domain across three controller actions. To do this I just need to pass a map of property names to the "validate" method of the domain object. Yep, its as simple as that!
//...action1
applicationInstance.validate(['firstName', 'lastName', 'emailAddress'])

//...action2
applicationInstance.validate(['dateOfBirth', 'sex'])

//...action3
applicationInstance.validate(['address1', 'address2', 'address3', 'address4'])

Accessing Grails application properties (metadata)

Accessing Grails application properties (metadata)

A Grails application.properties file contains meta type information about your grails application. This file is user editable and should only be used for declaring metadata that describes your application. Do not store configuration information in this file.

There are two ways to access the metadata contained within this file.

Accessing from GSP:

There is a GSP tag within the standard Grails tag library that allows access to this metadata, and surprise surprise its called g:meta.
<g:meta name="app.grails.version"/>
The above tag will render the version of the Grails Framework that is expected by your application.

Accessing from application code:

There is a very useful class within the Grails commons package called GrailsApplication. An instance of this class is automatically wired into Grails controllers and can be referenced from the following variable.
grailsApplication
If you wish to access the GrailsApplication instance outside of a controller you will need to do as follows.
import org.codehaus.groovy.grails.commons.ApplicationHolder

...

def grailsApplication = ApplicationHolder.application
Once you have access to the GrailsApplication instance you can then reference the application metadata as follows.
//Print application version
println grailsApplication.metadata.'app.version'

//Print application name
println grailsApplication.metadata.'app.name'

//Print expected Grails version
println grailsApplication.metadata.'app.grails.version'