Showing posts with label properties. Show all posts
Showing posts with label properties. Show all posts

Monday, September 30, 2013

Grails: how to get/set a meta-constraint on domain class property such as belongsTo or other variables

In the scaffolding templates there is a property domainClass from type org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass. These object has the property constrainedProperties. To have access to any properties such in this example: 'readonly' you have to do this:

Your domain class:


class Contact {
   static belongsTo = [subscription: Subscription]

   static constraints = {
       subscription(nullable: false, attributes: [readonly: true])  
   }

   String description
}


The DefaultGrailsDomainClass has a constructor with a attribute from type Class maybe you can do this:


def domainClass = new DefaultGrailsDomainClass(Contact.class)
def ro = domainClass.constrainedProperties.subscription.attributes.readonly



def domainClass = new DefaultGrailsDomainClass(it.metaClass.getTheClass())
def constrainedProperties = domainClass.constrainedProperties;
if(constrainedProperties.containsKey("subscription")) {
    ConstrainedProperty v = (ConstrainedProperty) constrainedProperties.get("subscription");
    /**
     * Now check
     */
    if(v.isNullable()) {
        println "Nullable";
    }
    if(v.isBlank()) {
        println "Blank";
    }
    /**
     * There are may other constraints
     * http://grails.org/doc/latest/api/org/codehaus/groovy/grails/validation/ConstrainedProperty.html 
     * Java/Grails Class ConstrainedProperty
     */ 
 }

Thursday, September 19, 2013

Grails access something.properties settings under conf directory in BuildConfig.groovy

Create a file named 'config.properties' under conf direcotry with the following properties:

dataSource.driverClassName = com.mysql.jdbc.Driver
dataSource.username = root
tomcatVersion = 2.2.4 

Write the following code in 'BuildConfig.groovy' file:


def props = new Properties();
new File("grails-app/conf/config.properties").withReader {
    props.load(it);
}
def conf = new ConfigSlurper().parse(props);
println conf; 
println conf.dataSource.driverClassName;
println conf.dataSource.username;
println conf.tomcatVeriosn; 

And output will be as:


[dataSource:[driverClassName:com.mysql.jdbc.Driver, username:root], tomcatVersion: 2.2.4]
com.mysql.jdbc.Driver
root
2.2.4 

Then you can use this settings in your 'BuildConfig.groovy' files like this (highlighted yellow):


plugins {
    runtime ":hibernate:2.2.4"
    runtime ":jquery:1.8.3"
    runtime ":resources:1.2"

    // Uncomment these (or add new ones) to enable additional resources capabilities
    //runtime ":zipped-resources:1.0"
    //runtime ":cached-resources:1.0"
    //runtime ":yui-minify-resources:0.1.5"

    build ":tomcat:"+conf.tomcatVersion 
    /* OR USE */
    build ":tomcat:${conf.tomcatVersion}"
 
    runtime ":database-migration:1.3.2" 
    compile ':cache:1.0.1'
}

Monday, September 16, 2013

Getting Started with Grails and MySQL database

Is is so much simple.

Create a file suppose named 'db-config.properties' in 'conf' folder with the following contents:
dataSource.driverClassName = com.mysql.jdbc.Driver
dataSource.username = root
dataSource.password =
dataSource.url = jdbc:mysql://localhost/license?useUnicode=yes&characterEncoding=UTF-8
dataSource.dbCreate = update
dataSource.logSql = true
dataSource.insertDummy = true
dataSource.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

/* Your custom class to do some extra work if need */
dataSource.configClass = src.gorm.GormConfiguration;


Where root is your database username.
Where 'license' is your database name.

And make a entry in 'Config.groovy' file under 'conf' folder as following:
grails.config.locations = [ "classpath:db-config.properties" ]

And you need to must do one thing is to download a  java-mysql connector and put it inside lib folder of your project.
Sometimes you need to clean your project to adjust these changes.

You can use more config file like this as:

grails.config.locations =
[
    "classpath:db-config.properties",
    "classpath:some-config.properties" ,
    "file:${userHome}/.grails/${appName}-config.propertis"
]