Grails: Querying across associations

Another nerdy post. Grails is a pretty good framework. I'm a python guy, so I prefer Django, but when forced to use Java-like things Grails is better than the alternative. However, it's still young. Django and Grails are both currently on their 1.1.1 releases, but Django is much more mature for its age.

In Django it's really easy to query across related objects (they're called "related objects" in Django and "associations" in Grails). Grails is still struggling with this. (Grails is also struggling with good, in-depth documentation, but that's not the purpose of this post.)

After much searching all I could find was some forum posts by the project lead of Grails, Graeme Rocher, from 2007 saying that nested associations aren't currently (as of 2007) supported.

Nested Associations: Suppose I have 3 classes: Person, Family, and Country. Suppose the classes are designed such that each person belongs to a family and each family is linked to an origin country. Now suppose you want to get a list of all persons whose family is from England. Persons don't have a direct link to that information, so you'd need to hop through the family to get to the country.

Based on the current setup you'd expect to be able to do something like:

Person.withCriteria {
family {
country {
eq("name", "England")
}
}
}

And you can. So for anyone searching for how to do this and finding that old post from 2007 saying you can't: it's wrong. You can.

But now let's pickup where I left off in my previous post with separating out query pieces for re-usability and adherence to the DRY principle.

We need to build a criteria object specifically and separate out the criteria to a separate closure:

def someView = {
def critBuilder = Person.createCriteria()
def critClosure = { filterByEngland.curry(critBuilder)() }
def results = critBuilder.list(max:params.max, offset:params.offset, critClosure)
def totalCount = results.totalCount
}

def filterByEngland = {critBuilder ->
critBuilder.family {
critBuilder.country {
eq("name", "England")
}
}
}

And now we can combine that with other pieces of modularized code. I have my queries broken up so that I can easy sort using different functions based on what kind of output the data is going to be used in. So you can have something like this:

def someView = {
def critBuilder = Person.createCriteria()
def critClosure = {
filterByEngland.curry(critBuilder)()
sortForCSV.curry(critBuilder)()
// sortForXML.curry(critBuilder)()
}
def results = critBuilder.list(max:params.max, offset:params.offset, critClosure)
def totalCount = results.totalCount
[results: results, totalCount: totalCount]
}

def filterByEngland = {critBuilder ->
critBuilder.family {
critBuilder.country {
eq("name", "England")
}
}
}

def sortForCSV = {critBuilder ->
critBuilder.order("lastName", "asc")
critBuilder.order("firstName", "asc")
critBuilder.order("age", "asc")
}

def sortForXML = {critBuilder ->
critBuilder.family {
critBuilder.country {
order("name", "asc")
}
order("id", "asc")
}
}


Since this nested association querying isn't documented anywhere (that I could find) and the only mention is that it _doesn't_ work, it was a pain in the butt figuring it out.

Other gripes with Grails. I can't define a relation to another class unless it is based on the primary_key of the classes. A less-than-usual case for sure, but there really isn't any good reason to disallow such a situation.

And now my feet hurt.

I've spent my afternoon scrubbing mildew out of windowsills. (How is it suddenly there in such large, disgusting amounts, when it was previously not there at all?) Blergh. The worst was pulling back the blinds in front of the patio door. [shudder]

If anybody has any magical tips for preventing/easily getting rid of* said pestilence, I'd love to hear them. This is something we've not had to deal with before. Do not like.

*I know I can use bleach, but I'm scared of getting it on the carpet (some of the mildew is around the patio door) or something and ruining the carpet. And I've heard that rolled up towels on the windowsills will catch the water and help, but I'm not sure how much that will help (I haven't tried it) and I don't like the idea of how it would look. I may be willing to at least try it, though, if there aren't any better options. Some of these are porous surfaces, which makes them rather difficult to clean. Today's method was just a wet rag and Fantastik, which worked okay, but there's gotta be something better. 🙁

Life at Work

I've been working some small projects to prototype using Groovy and Grails as our new framework. It's been fun to focus on a small project with enough time to actually polish it up nicely. The Grails framework lets us actually focus on functionality rather than minutiae that make the system function. So we've been putting together a nice little system which uses Ajax to provide a quick and clean interface.

Halloween

Ok, we've been lazy about Halloween. Ok, Ok, blogging Halloween was _my_ job, so I've been lazy.

Anyway. The week before Halloween we went out to a farm in Lathrop which was supposed to have a pumpkin patch which would allow you to pick a pumpkin straight from the vine (unlike the "pumpkin patch" in Livermore which trucks them in and lines them up). So we headed out there for the pumpkin patch, and corn mazes, aand pumpkin CANNONS!

It turned out that the pumpkin was a bit lame. It looks like it had been an actual patch, but it looked like they had done their best to destroy any vines that had been there.

But we did still go through a corn maze:DSCN4427Jess didn't particularly enjoy the corn maze. She felt that if she wanted to be lost, she could just go walk around town for 10 minutes. After wandering for a bit we picked a wall and followed it all around until we got out. There were 3 mazes, but we only did one.

Then we headed over to the CANNONS! You get a bucket of small pumpkins that you can load one-at-a-time into your compressed-air cannon. It was getting dark and the lighting was crappy so the pictures aren't very good. In this one you can see one of the targets you get to aim at, a giant jack o' lantern. There was a good 6 feet of piled pumpkin remains at its base:DSCN4430And here's Jess:DSCN4433
So that was fun. The pumpkin cannons were definitely worth it.

We bought a pumpkin from the grocery store because it was cheaper than the "pumpkin patch". We didn't get around to carving it until Halloween. I began working on a top-secret pumpkin exo-skeleton project:DSCN4442It didn't work out very well. But the jack o' lantern was pretty decent:DSCN4444We sat about waiting for trick-or-treaters (with a big bowl of candy) but were rather disappointed in that regard. We had one family with 3 small kids come by. That was it. So now we have a bunch of candy to eat (and Jess got more for 75% off at CVS after Halloween). Since the door wasn't busy Dr. Horrible walked down to Panda Express for traditional Halloween cuisine (the new SweetFire Chicken Breast is awesome, I can't eat Orange Chicken anymore):DSCN4450