Thursday, February 4, 2010

Use Exceptions To Control Validation Flow.

When using default grails scaffolding a controller action, for example save, will look some thing like this


def save = {
        def fooInstance = new Foo(params)
        if(!fooInstance.hasErrors() && fooInstance.save()) {
            flash.message = "Foo ${fooInstance.id} created"
            redirect(action:show,id:fooInstance.id)
        }
        else {
            render(view:'create',model:[fooInstance:fooInstance])
        }
    }

This has a number of problems.

First it is difficult to move this code into a service, because you cannot call render from a service.  Not moving this code to a service decreases reuse, and means that the database code is not transactional.

There are many reasons why you should use exceptions instead of return code logic to handle this tpe of error, some of them are detailed here.  The nested if else statements are hard to read, and it is easy for code to continue executing as if nothing had happened.

Runtime Exceptions in a service cause the transaction to be rolled back, which is the desired behavior when a validation error has occurred.

So what we want to do is move the code that saves foo to a service, and throw an exception if the input is not valid.  We will then catch the exception in the controller and render the create view.


Listing 2 FooService
class FooService {
  boolean transactional = true

  Foo saveFoo(Map params) {
    def fooInstance = new Foo(params)
    if (!fooInstance.hasErrors() && fooInstance.save()) {
      return fooInstance
    } else {
      throw new ValidationException("Foo is not Valid", fooInstance)
    }
  }
}

Tip: If you are using grails 1.2 support for this is built into the framework  You can now call save(failOnError:true)  and and exception will be thrown.  Or better yet you can set the configuration parameter grails.gorm.save.failOnError = true to enable in your entire application


Listing 3 FooController
class FooController {
  def fooService

  def save = {
    try {
      def fooInstance = fooService.saveFoo(params)
      redirect(action: show, id: fooInstance.id)
    } catch (ValidationException e) {
      log.warn("Validation Failed on Foo ${e.message}" )
      render(view: 'create', model: [fooInstance: e.invalidObject])
    }
  }
}


Listing 4 Validation Exception
class ValidationException extends RuntimeException {
  Object invalidObject
  
  ValidationException(String message, Object invalidObject){
    super(message)
    this.invalidObject = invalidObject
  }
}


If you fly and have an iphone you should check out this awesome holder for watching videos in flight. I use mine all the time!! 

Monday, January 4, 2010

Grails Plugins With In-House Repository Part 2.

In part one we covered setting up the subversion repository.  In this post we are going to look at how to configure your plugin project to publish to your repository, and how to configure your projects to use plugins from this repository. 

In your plugin project you will need to add a new file BuildConfig.groovy under grails-app/conf.
As you probably guessed from the name, this file is used to customize the behavior of the grails build system.  Among other things this file allows you to get dependencies from maven /ivy repositories (grails 1.2 only), use plugin repositories, and change the base directories for your plugins.
You need to add a couple of lines to this file to tell grails where to publish the plugin, and resolve other plugins.  



grails.plugin.repos.discovery.myRepository="http://url-to-repository.com"
grails.plugin.repos.distribution.myRepository="http://url-to-repository.com"

If you are using authentication on your repository, and you do not wish to be prompted every time you build you can include the username and password as part of your url.
Note grails assumes you are using authentication if you are using https as your protocol.

grails.plugin.repos.discovery.myRepository="https://username:password@url-to-repository.com"
grails.plugin.repos.distribution.myRepository="https://username:password@url-to-repository.com"

I recommend using the BuildConfig.groovy file as it makes your build more portable. However you can also set this information for all projects using the USER_HOME/.grails/settings.groovy

Now that you have configured your plugin, you can publish it to the repository using the grails release-plugin command.  You have to provide an argument of what repository to use.

grails release-plugin -repository=myRepository 

Tip: Due to a bug in grails, if you are using windows you have to add quotes around your arguments grails release-plugin "-repository=myRepository"

When you run this command grails will import your plugin into subversion with the following structure.
  • /.plugin-meta/plugins-list.xml (This file is updated each time you release a new version of a plugin it) 
  • /grails-/tags/LATEST_RELEASE (This is automatically updated everytime you release) 
  • /grails-/tags/ (This is used to resolve specific versions of your plugin) 
  • /grails-/trunk/ (This is the latest working copy.  You should check this out to make changes to your plugin)  

Finally you need to add a BuildConfig.groovy file to your projects where you wish to use this plugin. This file needs to contain the discovery url for your plugin repository.

grails.plugin.repos.discovery.myRepository="http://url-to-repository.com"
You will now be able to use the list-plugins and install-plugin commands on your in-house repository.