Tuesday, June 15, 2010

externalize datasource settings

Although common practice, it is not so easy to find out how to externalize your settings in a good way. I found the solution in combination of two posts:

How the property file has to look like:
http://stackoverflow.com/questions/970133/externalizing-grails-datasource-configuration

How it should be included by grails:
http://phatness.com/2010/03/how-to-externalize-your-grails-configuration/comment-page-1/#comment-1484

Wednesday, June 9, 2010

take care when using it in groovy each loop

One mistake I am making over and over is the following situation:

 def someCollection = [...]
 someCollection.each{
   println it // it works here
   doSomethingWithClauses{
     println it // it cannot be resolved here
   }
 }

As soon as you use the it in encapsulated code {}, it cannot be resolved anymore. It took me ages to find that mistake in the first place, but unfortunately I produce something like that quite often and then I wonder what the hell is wrong.

What you have to do to make it right is giving the iteration variable another name:

 def someCollection = [...]
 someCollection.each{ someItem ->
   println someItem // it works here
   doSomethingWithClauses{
     println someItem // hey cool it works here too
   }
 }

Tuesday, June 8, 2010

How to get rid of compiler errors

Today I stumbled about a page that gives a pretty cool introduction in how to setup grails 1.1 in eclipse:

http://www.objectpartners.com/2009/05/27/eclipse-setup-for-grails-11-development/

The author also explains how to get rid of compiler errors that are due to the acegi plugin. This helped to erase almost all of them, for the rest I think I should check whether the grails path is set correctly.

including javascript in gsp

Today I have tried out at least a hundred different ways for including js files into my main.gsp by hand. My approach looked like that:



I tried ${resource} first, but then I found this in grails Q&A:

Q: Why won't XXX Javascript Library Display Properly?

A: Sometimes third party javascript libraries depend upon other resources loaded directly from the javascript files themselves like stylesheets. Examples of libraries that do this would be the Yahoo UI libraries and niftycube. When the javascript attempts to load the URL Grails changes the URL so that the resource is not found. Try adding a link using the createLinkTo tag for the particular resource.

So it tried ${createLink}, but in the end nothing worked. I finally succeeded when I discovered that there was an extra taglib to do the trick with YUI JS files, which I was interested in:



The solution can be found in the most obvious place: http://www.grails.org/YUI+Plugin

Really wish I had looked there sooner :-)

Now I wonder if the problem was YUI specific and if my approach would work on other files. Maybe I'm going to find out some day...

Monday, June 7, 2010

grails, sql, datasource

If you are trying to use groovy.sql.Sql to formulate your own queries: don't use the Sql.executeInsert() method as it won't work on HSQLDB. executeInsert() produces an exception claiming "unknown function". But if you use Sql.execute() you won't encounter such a problem.

Before you ask: the advantage of executeInsert would have been that you get the ids of auto-generated columns as return value(s).

Wednesday, June 2, 2010

Querying simple collections in grails

Imagine a domain class has a collection of simple types like e.g.

class Domain
{
static hasMany[strings: String]
}

What would you do if you wanted to have a set of all instances of Domain that have a string called "blabla". Well my first guess was to use the dynamic method findBy, but this does not work. The same goes for using Domain.withCriteria{}. I found the solution in a jira where you can find a feature request about that. Meanwhile you have to do the querying yourself. I have included a static method within my Domain class to avoid DRYness:

static def match(String matchThis)
{
Domain.executeQuery('from DBUser where :name in elements(strings)', [name: matchThis])
}

Problem solved...

Tuesday, June 1, 2010

datasources plugin for grails

In my current project I wanted to have content and security information separated from application specific settings. I therefore wanted to store the information into separate databases. This was far easier than expected with the datasources plugin from grails. I only had to install the plugin and add the file datasources.groovy to /grails-app/conf/

datasources = { 
 datasource(name: 'ds2'){
  domainClasses([org.openlab.settings.GlobalSetting, org.openlab.settings.UserSetting, org.openlab.security.Requestmap])
  pooled(true)
  driverClassName('org.hsqldb.jdbcDriver')
  url('jdbc:hsqldb:mem:devDB')
  username('sa')
  password('')
  dbCreate('create-drop')
  hibernate {
   cache {
    use_second_level_cache(false)
    use_query_cache(false)
   }
  }  
 } 
}

The usual datasource configuration remains intact and is the core or default database. In datasources.groovy you can define as many additional databases as you like. You only have to follow this pattern where the hibernate information is included as well as the definition of domain classes you wish to store in the new database.

The clear distribution of domain classes to datasources makes this approach easy and the only thing you have to consider are foreign key relations when separating the wrong domain classes.