Injection of a default property in domain classes

classic Classic list List threaded Threaded
18 messages Options
Reply | Threaded
Open this post in threaded view
|

Injection of a default property in domain classes

williamdrai
Hi all,

I would like to know if a plugin can add default properties to all
domain classes (as it's done with id and version).
I've found a DefaultGrailsDomainClassInjector but I don't see how I can
add override it or better just call an additional plugin injector.

Can someone point me to the right direction.

William


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Injection of a default property in domain classes

Scott Burch
Take a look at the plugin programming section of the docs.  This is very
easy to do, and I believe there are even examples in there.

On Thu, 2009-06-25 at 14:39 +0200, William Draï wrote:

> Hi all,
>
> I would like to know if a plugin can add default properties to all
> domain classes (as it's done with id and version).
> I've found a DefaultGrailsDomainClassInjector but I don't see how I can
> add override it or better just call an additional plugin injector.
>
> Can someone point me to the right direction.
>
> William
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

packages and domains

Scott Burch
I have a problem with grailsApplication.getArtefact .getDomainClass and
such.

I put my domain classes in packages (like a good programmer :)

However, when I do that .getArtefact and such don't see them without the
full package name.

To get around this, I created  a closure that finds artefacts, but it is
not very efficient.

def findDomainClass = { name ->
            it = grailsApplication.domainClasses.iterator()
            while(it.hasNext()) {
                def n = it.next()
                if(n.name == name) {
                    return n
                }
            }
}

Does anyone know a better way.  Should we patch grailsApplication to handle this?



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: packages and domains

contact
What's the problem with actually putting the package name in the call to getDomainClass/getArtefact?  That's kind of what it's there for, to separate your com.example.foo.Book from your com.example.bar.Book.

On Thu, Jun 25, 2009 at 5:33 PM, Scott Burch <[hidden email]> wrote:
I have a problem with grailsApplication.getArtefact .getDomainClass and
such.

I put my domain classes in packages (like a good programmer :)

However, when I do that .getArtefact and such don't see them without the
full package name.

To get around this, I created  a closure that finds artefacts, but it is
not very efficient.

def findDomainClass = { name ->
           it = grailsApplication.domainClasses.iterator()
           while(it.hasNext()) {
               def n = it.next()
               if(n.name == name) {
                   return n
               }
           }
}

Does anyone know a better way.  Should we patch grailsApplication to handle this?



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: packages and domains

Scott Burch
Good question, but it is because I am looking up the name from the
controller passed to a filter.  I don't know what the package will be.
May not even belong to me.  We should have a way to look up a artefact
by the class name as well.



On Thu, 2009-06-25 at 17:42 -0500, Ted Naleid wrote:

> What's the problem with actually putting the package name in the call
> to getDomainClass/getArtefact?  That's kind of what it's there for, to
> separate your com.example.foo.Book from your com.example.bar.Book.
>
> On Thu, Jun 25, 2009 at 5:33 PM, Scott Burch <[hidden email]>
> wrote:
>         I have a problem with
>         grailsApplication.getArtefact .getDomainClass and
>         such.
>        
>         I put my domain classes in packages (like a good programmer :)
>        
>         However, when I do that .getArtefact and such don't see them
>         without the
>         full package name.
>        
>         To get around this, I created  a closure that finds artefacts,
>         but it is
>         not very efficient.
>        
>         def findDomainClass = { name ->
>                    it = grailsApplication.domainClasses.iterator()
>                    while(it.hasNext()) {
>                        def n = it.next()
>                        if(n.name == name) {
>                            return n
>                        }
>                    }
>         }
>        
>         Does anyone know a better way.  Should we patch
>         grailsApplication to handle this?
>        
>        
>        
>         ---------------------------------------------------------------------
>         To unsubscribe from this list, please visit:
>        
>            http://xircles.codehaus.org/manage_email
>        
>        
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: packages and domains

Scott Burch
Although I can't complain, it was still possible to do.  Maybe we could
add this to some Util class?

On Thu, 2009-06-25 at 19:05 -0400, Scott Burch wrote:

> Good question, but it is because I am looking up the name from the
> controller passed to a filter.  I don't know what the package will be.
> May not even belong to me.  We should have a way to look up a artefact
> by the class name as well.
>
>
>
> On Thu, 2009-06-25 at 17:42 -0500, Ted Naleid wrote:
> > What's the problem with actually putting the package name in the call
> > to getDomainClass/getArtefact?  That's kind of what it's there for, to
> > separate your com.example.foo.Book from your com.example.bar.Book.
> >
> > On Thu, Jun 25, 2009 at 5:33 PM, Scott Burch <[hidden email]>
> > wrote:
> >         I have a problem with
> >         grailsApplication.getArtefact .getDomainClass and
> >         such.
> >        
> >         I put my domain classes in packages (like a good programmer :)
> >        
> >         However, when I do that .getArtefact and such don't see them
> >         without the
> >         full package name.
> >        
> >         To get around this, I created  a closure that finds artefacts,
> >         but it is
> >         not very efficient.
> >        
> >         def findDomainClass = { name ->
> >                    it = grailsApplication.domainClasses.iterator()
> >                    while(it.hasNext()) {
> >                        def n = it.next()
> >                        if(n.name == name) {
> >                            return n
> >                        }
> >                    }
> >         }
> >        
> >         Does anyone know a better way.  Should we patch
> >         grailsApplication to handle this?
> >        
> >        
> >        
> >         ---------------------------------------------------------------------
> >         To unsubscribe from this list, please visit:
> >        
> >            http://xircles.codehaus.org/manage_email
> >        
> >        
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: packages and domains

contact
So you're using a convention based on the name of the controller?  ex: domain class Foo -> FooController

I normally extend that naming convention and have my controllers and domain classes that are named this way in the same package as well.  So domainClass com.example.Foo -> com.example.FooController

The directory structure of grails keeps them physically separate, but the packages (and the naming convention) unite them IMO.

If you have a reference to the controller in your filter, you could also get it's package name and continue to use the package name without needing to search through all of the domain classes (as well as lose half the benefit of having things in packages in the first place).

Alternatively, your method to search through works too :).

HTH,
Ted

On Thu, Jun 25, 2009 at 6:41 PM, Scott Burch <[hidden email]> wrote:
Although I can't complain, it was still possible to do.  Maybe we could
add this to some Util class?

On Thu, 2009-06-25 at 19:05 -0400, Scott Burch wrote:
> Good question, but it is because I am looking up the name from the
> controller passed to a filter.  I don't know what the package will be.
> May not even belong to me.  We should have a way to look up a artefact
> by the class name as well.
>
>
>
> On Thu, 2009-06-25 at 17:42 -0500, Ted Naleid wrote:
> > What's the problem with actually putting the package name in the call
> > to getDomainClass/getArtefact?  That's kind of what it's there for, to
> > separate your com.example.foo.Book from your com.example.bar.Book.
> >
> > On Thu, Jun 25, 2009 at 5:33 PM, Scott Burch <[hidden email]>
> > wrote:
> >         I have a problem with
> >         grailsApplication.getArtefact .getDomainClass and
> >         such.
> >
> >         I put my domain classes in packages (like a good programmer :)
> >
> >         However, when I do that .getArtefact and such don't see them
> >         without the
> >         full package name.
> >
> >         To get around this, I created  a closure that finds artefacts,
> >         but it is
> >         not very efficient.
> >
> >         def findDomainClass = { name ->
> >                    it = grailsApplication.domainClasses.iterator()
> >                    while(it.hasNext()) {
> >                        def n = it.next()
> >                        if(n.name == name) {
> >                            return n
> >                        }
> >                    }
> >         }
> >
> >         Does anyone know a better way.  Should we patch
> >         grailsApplication to handle this?
> >
> >
> >
> >         ---------------------------------------------------------------------
> >         To unsubscribe from this list, please visit:
> >
> >            http://xircles.codehaus.org/manage_email
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: packages and domains

Scott Burch
Ted,

Yes, but I don't control all of the code.  This is for the CMS plugin
that I built.  

And, yes, name conflicts between packages would be a problem.  I may
have to revisit this later.

Thanks for the advice.

Scott


On Thu, 2009-06-25 at 23:36 -0500, Ted Naleid wrote:

> So you're using a convention based on the name of the controller?  ex:
> domain class Foo -> FooController
>
>
> I normally extend that naming convention and have my controllers and
> domain classes that are named this way in the same package as well.
>  So domainClass com.example.Foo -> com.example.FooController
>
>
> The directory structure of grails keeps them physically separate, but
> the packages (and the naming convention) unite them IMO.
>
>
> If you have a reference to the controller in your filter, you could
> also get it's package name and continue to use the package name
> without needing to search through all of the domain classes (as well
> as lose half the benefit of having things in packages in the first
> place).
>
>
> Alternatively, your method to search through works too :).
>
>
> HTH,
> Ted
>
> On Thu, Jun 25, 2009 at 6:41 PM, Scott Burch <[hidden email]>
> wrote:
>         Although I can't complain, it was still possible to do.  Maybe
>         we could
>         add this to some Util class?
>        
>        
>         On Thu, 2009-06-25 at 19:05 -0400, Scott Burch wrote:
>         > Good question, but it is because I am looking up the name
>         from the
>         > controller passed to a filter.  I don't know what the
>         package will be.
>         > May not even belong to me.  We should have a way to look up
>         a artefact
>         > by the class name as well.
>         >
>         >
>         >
>         > On Thu, 2009-06-25 at 17:42 -0500, Ted Naleid wrote:
>         > > What's the problem with actually putting the package name
>         in the call
>         > > to getDomainClass/getArtefact?  That's kind of what it's
>         there for, to
>         > > separate your com.example.foo.Book from your
>         com.example.bar.Book.
>         > >
>         > > On Thu, Jun 25, 2009 at 5:33 PM, Scott Burch
>         <[hidden email]>
>         > > wrote:
>         > >         I have a problem with
>         > >         grailsApplication.getArtefact .getDomainClass and
>         > >         such.
>         > >
>         > >         I put my domain classes in packages (like a good
>         programmer :)
>         > >
>         > >         However, when I do that .getArtefact and such
>         don't see them
>         > >         without the
>         > >         full package name.
>         > >
>         > >         To get around this, I created  a closure that
>         finds artefacts,
>         > >         but it is
>         > >         not very efficient.
>         > >
>         > >         def findDomainClass = { name ->
>         > >                    it =
>         grailsApplication.domainClasses.iterator()
>         > >                    while(it.hasNext()) {
>         > >                        def n = it.next()
>         > >                        if(n.name == name) {
>         > >                            return n
>         > >                        }
>         > >                    }
>         > >         }
>         > >
>         > >         Does anyone know a better way.  Should we patch
>         > >         grailsApplication to handle this?
>         > >
>         > >
>         > >
>         > >
>         ---------------------------------------------------------------------
>         > >         To unsubscribe from this list, please visit:
>         > >
>         > >            http://xircles.codehaus.org/manage_email
>         > >
>         > >
>         > >
>         >
>         >
>         >
>         ---------------------------------------------------------------------
>         > To unsubscribe from this list, please visit:
>         >
>         >     http://xircles.codehaus.org/manage_email
>         >
>         >
>        
>        
>         ---------------------------------------------------------------------
>         To unsubscribe from this list, please visit:
>        
>            http://xircles.codehaus.org/manage_email
>        
>        
>        
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: packages and domains

Graeme Rocher-3
In reply to this post by Scott Burch
domain.name returns to name without the package. You're probably after
domain.fullName

Cheers

On Thu, Jun 25, 2009 at 11:33 PM, Scott Burch<[hidden email]> wrote:

> I have a problem with grailsApplication.getArtefact .getDomainClass and
> such.
>
> I put my domain classes in packages (like a good programmer :)
>
> However, when I do that .getArtefact and such don't see them without the
> full package name.
>
> To get around this, I created  a closure that finds artefacts, but it is
> not very efficient.
>
> def findDomainClass = { name ->
>            it = grailsApplication.domainClasses.iterator()
>            while(it.hasNext()) {
>                def n = it.next()
>                if(n.name == name) {
>                    return n
>                }
>            }
> }
>
> Does anyone know a better way.  Should we patch grailsApplication to handle this?
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>



--
Graeme Rocher
Head of Grails Development
SpringSource - Weapons for the War on Java Complexity
http://www.springsource.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: packages and domains

Scott Burch
No, name is correct.  The issue was not having a built-in way to find
domain classes by name without the package info.  But, that may be
silly.  And, as I stated, it was possible just not a one-liner :)



On Fri, 2009-06-26 at 11:34 +0100, Graeme Rocher wrote:

> domain.name returns to name without the package. You're probably after
> domain.fullName
>
> Cheers
>
> On Thu, Jun 25, 2009 at 11:33 PM, Scott Burch<[hidden email]> wrote:
> > I have a problem with grailsApplication.getArtefact .getDomainClass and
> > such.
> >
> > I put my domain classes in packages (like a good programmer :)
> >
> > However, when I do that .getArtefact and such don't see them without the
> > full package name.
> >
> > To get around this, I created  a closure that finds artefacts, but it is
> > not very efficient.
> >
> > def findDomainClass = { name ->
> >            it = grailsApplication.domainClasses.iterator()
> >            while(it.hasNext()) {
> >                def n = it.next()
> >                if(n.name == name) {
> >                    return n
> >                }
> >            }
> > }
> >
> > Does anyone know a better way.  Should we patch grailsApplication to handle this?
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list, please visit:
> >
> >    http://xircles.codehaus.org/manage_email
> >
> >
> >
>
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: packages and domains

contact
If you're looking for a one-liner with the same effect, you could always use something like this:

grailsApplication.domainClasses.find { it.name == "Book" }

On Fri, Jun 26, 2009 at 8:19 AM, Scott Burch <[hidden email]> wrote:
No, name is correct.  The issue was not having a built-in way to find
domain classes by name without the package info.  But, that may be
silly.  And, as I stated, it was possible just not a one-liner :)



On Fri, 2009-06-26 at 11:34 +0100, Graeme Rocher wrote:
> domain.name returns to name without the package. You're probably after
> domain.fullName
>
> Cheers
>
> On Thu, Jun 25, 2009 at 11:33 PM, Scott Burch<[hidden email]> wrote:
> > I have a problem with grailsApplication.getArtefact .getDomainClass and
> > such.
> >
> > I put my domain classes in packages (like a good programmer :)
> >
> > However, when I do that .getArtefact and such don't see them without the
> > full package name.
> >
> > To get around this, I created  a closure that finds artefacts, but it is
> > not very efficient.
> >
> > def findDomainClass = { name ->
> >            it = grailsApplication.domainClasses.iterator()
> >            while(it.hasNext()) {
> >                def n = it.next()
> >                if(n.name == name) {
> >                    return n
> >                }
> >            }
> > }
> >
> > Does anyone know a better way.  Should we patch grailsApplication to handle this?
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list, please visit:
> >
> >    http://xircles.codehaus.org/manage_email
> >
> >
> >
>
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: packages and domains

Scott Burch
I forgot about .find :)  Thanks.

Still embedding Groovy in my head.  

On Fri, 2009-06-26 at 08:43 -0500, Ted Naleid wrote:

> If you're looking for a one-liner with the same effect, you could
> always use something like this:
>
>
> grailsApplication.domainClasses.find { it.name == "Book" }
>
> On Fri, Jun 26, 2009 at 8:19 AM, Scott Burch <[hidden email]>
> wrote:
>         No, name is correct.  The issue was not having a built-in way
>         to find
>         domain classes by name without the package info.  But, that
>         may be
>         silly.  And, as I stated, it was possible just not a
>         one-liner :)
>        
>        
>        
>        
>         On Fri, 2009-06-26 at 11:34 +0100, Graeme Rocher wrote:
>         > domain.name returns to name without the package. You're
>         probably after
>         > domain.fullName
>         >
>         > Cheers
>         >
>         > On Thu, Jun 25, 2009 at 11:33 PM, Scott
>         Burch<[hidden email]> wrote:
>         > > I have a problem with
>         grailsApplication.getArtefact .getDomainClass and
>         > > such.
>         > >
>         > > I put my domain classes in packages (like a good
>         programmer :)
>         > >
>         > > However, when I do that .getArtefact and such don't see
>         them without the
>         > > full package name.
>         > >
>         > > To get around this, I created  a closure that finds
>         artefacts, but it is
>         > > not very efficient.
>         > >
>         > > def findDomainClass = { name ->
>         > >            it = grailsApplication.domainClasses.iterator()
>         > >            while(it.hasNext()) {
>         > >                def n = it.next()
>         > >                if(n.name == name) {
>         > >                    return n
>         > >                }
>         > >            }
>         > > }
>         > >
>         > > Does anyone know a better way.  Should we patch
>         grailsApplication to handle this?
>         > >
>         > >
>         > >
>         > >
>         ---------------------------------------------------------------------
>         > > To unsubscribe from this list, please visit:
>         > >
>         > >    http://xircles.codehaus.org/manage_email
>         > >
>         > >
>         > >
>         >
>         >
>         >
>        
>        
>         ---------------------------------------------------------------------
>         To unsubscribe from this list, please visit:
>        
>            http://xircles.codehaus.org/manage_email
>        
>        
>        
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Injection of a default property in domain classes

williamdrai
In reply to this post by Scott Burch
The docs describes a way to add methods such as save(), update()... in doWithDynamicMethod but what I would like is to add a new default property for domain classes (such as id and version) that should be mapped to the database.
But maybe I did not get into the correct section of the docs.


Take a look at the plugin programming section of the docs.  This is very
easy to do, and I believe there are even examples in there.

On Thu, 2009-06-25 at 14:39 +0200, William Draï wrote:
  
Hi all,

I would like to know if a plugin can add default properties to all 
domain classes (as it's done with id and version).
I've found a DefaultGrailsDomainClassInjector but I don't see how I can 
add override it or better just call an additional plugin injector.

Can someone point me to the right direction.

William


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


    


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



  

Reply | Threaded
Open this post in threaded view
|

Re: Injection of a default property in domain classes

steviemo
Did you make any progress with this williamdrai?

I am looking to add the same functionality, ie: a plugin injects a domain property at compile time.

I have googled and searched forums and looked at multiple plugins for examples but no luck!
Reply | Threaded
Open this post in threaded view
|

Re: Injection of a default property in domain classes

Göran Ehrsson
I'm also very interested in this. Have plugins inject domain properties
(including relations) at compile time.
Keep us posted.

/Göran

steviemo wrote 2010-10-25 12:42:
> Did you make any progress with this williamdrai?
>
> I am looking to add the same functionality, ie: a plugin injects a domain
> property at compile time.
>
> I have googled and searched forums and looked at multiple plugins for
> examples but no luck!

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Injection of a default property in domain classes

booja pathy
Hi 

We can do this using the AST transformations.
The transformations can work on domains and generate the new property at compile time.

 If you need further inputs, I can get a plugin code snippet hosted some where in Github.

Thanks
Boojapathy

On Mon, Oct 25, 2010 at 7:11 PM, Goran Ehrsson <[hidden email]> wrote:
I'm also very interested in this. Have plugins inject domain properties (including relations) at compile time.
Keep us posted.

/Göran

steviemo wrote 2010-10-25 12:42:

Did you make any progress with this williamdrai?

I am looking to add the same functionality, ie: a plugin injects a domain
property at compile time.

I have googled and searched forums and looked at multiple plugins for
examples but no luck!

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Injection of a default property in domain classes

steviemo
Hi booja,

Yes I've been trying to do this using Global AST Transformations but haven't managed to get a working example.

If you could write up a coding example that would be a great help!

Thanks,
Stephen
Reply | Threaded
Open this post in threaded view
|

Re: Injection of a default property in domain classes

steviemo
In reply to this post by booja pathy
Hi booja pathy,

Where you able to get an example up and running?

Thanks,
Stephen