Friday, October 25, 2013

Grails GORM constrained properties: check if a domain attributes properties mapping

Consider a grails domain class like below:



package com.pritom.domains.customer

class Customer {
    Integer id;
    String customerId
    String email;
    String password
    Date dateCreated = new Date()
    Date lastUpdated = new Date()

    static mapping = {

    }

    static constraints = {
        customerId unique: true
        email unique: true
        creditCard(nullable: true);
    }
}

Now get domain class constrained properties:



def domainClass = new DefaultGrailsDomainClass(com.pritom.domains.Customer)
def constrainedProperties = domainClass.constrainedProperties;

Now check if specific field has some specific property



String key = "customerId";

if (constrainedProperties.containsKey(key)) {
    ConstrainedProperty v = (ConstrainedProperty) constrainedProperties.get(key);
    if(v.isNullable()) println 'Nullable';
    if(v.isBlank()) println 'Blank';
    if(v.isCreditCard()) println 'Credit Card';
    if(v.isDisplay()) println 'Display';
    if(v.isEditable()) println 'Editable';
    if(v.isEmail()) println 'Email';
}


And you can check more properties as you wish.

http://pritomkumar.blogspot.com/2013/09/javagrails-class-constrainedproperty.html

No comments:

Post a Comment