views:

1085

answers:

1

I'm trying to dynamically create domain objects in Grails and encountered the problem that for any property referencing another domain object the metaproperty tells me its type is "java.lang.Object" and not the expected type.

For example:

class PhysicalSiteAssessment {
    // site info
    Site site
    Date sampleDate
    Boolean rainLastWeek
    String additionalNotes
    ...

is the beginning of a domain class, which references another domain class "Site".

If I try to dynamically find the property types for this class by using this code (in a service):

String entityName = "PhysicalSiteAssessment"
Class entityClass
try {
    entityClass = grailsApplication.getClassForName(entityName)
} catch (Exception e) {
    throw new RuntimeException("Failed to load class with name '${entityName}'", e)
}
entityClass.metaClass.getProperties().each() {
    println "Property '${it.name}' is of type '${it.type}'"
}

then the result is that it recognizes the Java classes, but not the Grails domain class. The output contains the following lines:

Property 'site' is of type 'class java.lang.Object'
Property 'siteId' is of type 'class java.lang.Object'
Property 'sampleDate' is of type 'class java.util.Date'
Property 'rainLastWeek' is of type 'class java.lang.Boolean'
Property 'additionalNotes' is of type 'class java.lang.String'

The problem is that I would like to use the dynamic lookup to find matching objects, e.g. do a

def targetObjects = propertyClass."findBy${idName}"(idValue)

where the propertyClass is retrieved via introspection, idName is the name of the property to look up (not necessarily the database ID) and idValue is the value to find.

It all ends in:

org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: static java.lang.Object.findByCode() is applicable for argument types: (java.lang.String) values: [T04]

Is there a way to find the actual domain class for the property? Or maybe some other solution to the problem of finding an instance of a domain class whose type is not given (only a property name that has the type)?

It works if I use the convention that the type name is the property name capitalized ("site"->"Site") to look up the class via the grailsApplication instance, but I would like to avoid that.

+6  A: 

Grails allows you to access some meta-information of your domain model via the GrailsApplication instance. You can look it up that way:

import org.codehaus.groovy.grails.commons.ApplicationHolder
import org.codehaus.groovy.grails.commons.DomainClassArtefactHandler

def grailsApplication = ApplicationHolder.application
def domainDescriptor = grailsApplication.getArtefact(DomainClassArtefactHandler.TYPE, "PhysicalSiteAssessment")

def property = domainDescriptor.getPropertyByName("site")
def type = property.getType()
assert type instanceof Class

API:

Siegfried Puchbauer
Thanks, that works. Is there some overview of this API somewhere? I had been looking for something like this but couldn't find it.
Peter Becker
There aren't any good references that I've ever seen besides the javadocs (http://grails.org/doc/1.1/api/org/codehaus/groovy/grails/commons/DefaultGrailsApplication.html). I also find looking at the Grails sourcecode very helpful. I also use this kind of stuff heavily in the build-test-data plugin to inspect domain object constraints and automatically generate test objects that pass the constraints if you're looking for examples (http://bitbucket.org/tednaleid/grails-test-data/wiki/Home).
Ted Naleid