Monday, June 13, 2011

Debugging Grails Controllers & Closures in IntelliJ

We're using IntelliJ at work for our Grails development.  IntelliJ is a solid IDE with great Grails support.  I should have no complaints... but this one issue has been nagging at me.  When using IntelliJ to debug Grails, I couldn't use the Watch Window to interrogate a Controller Action's arguments.

Setting a watch variable to params or request in the following action closure yields...
Cannot find local variable 'params'
def myAction = {
}

Of course, if we set the params argument to a reference, then we can interrogate it. So we know the object is there... so what gives?
def myAction = {
  def myParams = params
  def myRequest = request
}

The answer is simple and opaque at the same time. Closures aren't methods. I'm guilty of forgetting this and Grails is great about hiding this fact from us.  However, IntelliJ isn't that smart.  When debugging in IntelliJ, we have ask the Closure the right question.
this.getProperty("params")

Ah Ha! There is my params argument in IntelliJ.  Now I can stop writing println everywhere...

More info on Closures
Formal Definition
Informal Guide

Thursday, May 19, 2011

Grails, Envers and Blob

We've added Hibernate Envers to our Grails project.  Thus far, Envers has handled all our object versioning requirements...  Until we tried to use a Blob.

@Audited
class Data {
    Blob data
}

org.hibernate.MappingException : Type not supported for auditing: org.hibernate.type.BlobType, on entity Data, property 'data'.

What is the solution?  Envers does not throw an exception for a Byte Array, so replace Blob with it.  However, MySQL will map the Byte Array to a TinyBlob, which may not work for you.  You can modify this by updating the GORM DSL on the domain.

@Audited
class Data {
    byte[] data
    static mapping = {
        data sqlType: 'blob'
    }
}

This resolved the problem for us.