Archive

Archive for the ‘Groovy’ Category

Grails cheatsheet

September 26th, 2011 No comments

This is supposed to be a cheatsheet for some common elements in Grails.
It’s a little bare at the moment but I hope that in time it will be a nice mashup of different Grails elements.

Grails Cheatsheet

Constraints

static constraints = {
    propertyName(
        blank: false,
        nullable: false,
        inList: ['a', 'b'], //Or reference property here
        matches: '[0-9]+',
        minSize: 1,
        maxSize: 10,
        min: 1,
        max: 10,
        range: 1..10,
        unique: true,
        url: true,
        email: true,
        notEqual: "passwd", //Or reference property here
        validator: { val, obj ->
            return val != obj.propertyName
        },
 
        /* Display Options */
        attributes: [year: 2000..2011], //Adds extra attributes to item when rendered.
        password: true, //Indicate that this is a password field
        widget: 'textarea', //Choose what widget will be used to render this item.
                //textField, hiddenField, submitButton, field, textArea, form, actionSubmit, actionSubmitImage, datePicker, renderNoSelectionOption, timeZoneSelect, localeSelect, currencySelect, select, checkBox, radio
        display: false //Hide this item when scaffolding
    )
}

Persistence Callbacks

  • beforeInsert – Executed before an object is initially persisted to the database
  • beforeUpdate – Executed before an object is updated
  • beforeDelete – Executed before an object is deleted
  • beforeValidate – Executed before an object is validated
  • afterInsert – Executed after an object is persisted to the database
  • afterUpdate – Executed after an object has been updated
  • afterDelete – Executed after an object has been deleted
  • onLoad – Executed when an object is loaded from the database

Sample:

def beforeInsert() {
	doSomething()
}
 
def beforeUpdate() {
	if (isDirty('fieldName')) {
		doSomething()
	}
}

Showing SQL-queries

Edit the DataSource.groovy file in the Configuration directory and add the following to the hibernate section (preferably in non-production environments):

hibernate {
	show_sql = true
}
Categories: Groovy Tags:

Configuring Tomcat, PostgreSQL and Grails on Ubuntu

September 18th, 2011 No comments

First install tomcat and tomcat on your ubuntu machine if you have not already done so.

sudo apt-get install tomcat6 tomcat6-admin postgresql

Setup the required PostgreSQL database on ubuntu. We will connect to it as a JNDI DataSource later on.

-- Connect to the database from command line
psql -d postgres
 
CREATE USER userName WITH PASSWORD 'yourPassword';
 
CREATE DATABASE databaseName;
 
GRANT ALL PRIVILEGES ON DATABASE databaseName TO userName;

Get the postgreSQL JDBC driver.

cd /usr/share/tomcat6/lib
sudo wget http://jdbc.postgresql.org/download/postgresql-9.1-901.jdbc3.jar

Create a Tomcat Context configuration for your grails webapp.

sudo vi /etc/tomcat6/Catalina/localhost/yourappname.xml

Fill it with something like the following, (adapted to your webapps name / context root of course)

<Context path="/yourContextPath" reloadable="false">
          <Resource name="jdbc/yourName" auth="Container"
                    type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"
                    url="jdbc:postgresql://127.0.0.1:5432/mydb"
                    username="myuser" password="mypasswd" maxActive="20" maxIdle="10"
                    maxWait="-1"/>
</Context>

We’re almost there. Update DataSource.groovy

    production {
        dataSource {
            dbCreate = "update"
            jndiName = "java:comp/env/jdbc/yourName"
        }
    }

Awesome! Restart tomcat (sudo /etc/init.d/tomcat6 restart). Build (grails prod war) and deploy (copy file to /var/lib/tomcat6/webapps) your grails webapp and go bananas!

Categories: Groovy Tags: