Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine if a class is a Grails domain object?

Tags:

grails

groovy

dns

For an arbitrary object what is the easiest way to determine if the type of the object is a Grails domain class?

like image 350
Peter Kelley Avatar asked Jun 10 '11 01:06

Peter Kelley


People also ask

What is Grails domain class?

In Grails a domain is a class that lives in the grails-app/domain directory. A domain class can be created with the create-domain-class command: grails create-domain-class org.bookstore.Book. or with your favourite IDE or text editor.

How do I create a domain class in Grails?

If you don't specify a package (like "org. bookstore" in the example), then the name of the application will be used as the package. So if the application name is "bookstore" and you run create-domain-class Book , then the command will create the file grails-app/domain/bookstore/Book.

What is Grails Gorm?

GORM is the data access toolkit used by Grails and provides a rich set of APIs for accessing relational and non-relational data including implementations for Hibernate (SQL), MongoDB, Neo4j, Cassandra, an in-memory ConcurrentHashMap for testing and an automatic GraphQL schema generator.


2 Answers

You can use the GrailsApplication for that. Add a dependency injection to your controller or service:

def grailsApplication

and then you can use it like this:

def foo = ...
if (grailsApplication.isDomainClass(foo.getClass()) {
    ...
}
like image 95
Burt Beckwith Avatar answered Sep 28 '22 04:09

Burt Beckwith


Found the following snippet at https://svn.intuitive-collaboration.com/RiskAnalytics/trunk/riskanalytics-grails/src/java/org/codehaus/groovy/grails/web/binding/GrailsDataBinder.java

DomainClassArtefactHandler.isDomainClass(clazz)

The javadoc is here: http://grails.org/doc/latest/api/org/codehaus/groovy/grails/commons/DomainClassArtefactHandler.html#isDomainClass(java.lang.Class)

like image 45
Peter Kelley Avatar answered Sep 28 '22 02:09

Peter Kelley