Persistency event listeners in Grails

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Persistency event listeners in Grails

smakela
This post was updated on .
Hi!

What is the best way to get the persisted object (old data), when I'm using the API that is provided by Gorm. Meaning if I implement the AbstractPersistenceEventListener.

I don't want to use any Hibernate -stuff (I can get the native event and can get the oldState and newState from there).

class DomainObjectPersistencyEventListener extends AbstractPersistenceEventListener {

    public DomainObjectPersistencyEventListener(final Datastore datastore) {
        super(datastore)
    }

    @Override
    public boolean supportsEventType(Class<? extends ApplicationEvent> e) {
        return PreUpdateEvent.class.isAssignableFrom(e)
    }

    @Override
    protected void onPersistenceEvent(AbstractPersistenceEvent event) {

    }
}

Wiring:

mainContext.eventTriggeringInterceptor.datastores.each { k, datastore ->
            mainContext.addApplicationListener new DomainObjectPersistencyEventListener(datastore)
}


This way onPersistenceEvent -method I can get the entityObject, but the other instances are null.

Is the only way to ask from the entityObject "dirtyPropertyNames" or is there some nice way to get the previous state of the domain object?

 - Sami
Reply | Threaded
Open this post in threaded view
|

Re: Persisteny event listeners in Grails

roos
Take a look to the Audit-Logging plugin.
It has a mode to only fire entity event methods.
Or you review the plugin code to implement it yourself.

http://grails.org/plugin/audit-logging

Regards,
Robert


Am 25.11.2013 um 18:01 schrieb smakela <[hidden email]>:

> Hi!
>
> What is the best way to get the persisted object (old data), when I'm using
> the API that is provided by Gorm. Meaning if I implement the
> AbstractPersistenceEventListener.
>
> I don't want to use any Hibernate -stuff (I can get the native event and can
> get the oldState and newState from there).
>
> class DomainObjectPersistencyEventListener extends
> AbstractPersistenceEventListener {
>
>    public DomainObjectPersistencyEventListener(final Datastore datastore) {
>        super(datastore)
>    }
>
>    @Override
>    public boolean supportsEventType(Class<? extends ApplicationEvent> e) {
>        return PreUpdateEvent.class.isAssignableFrom(e)
>    }
>
>    @Override
>    protected void onPersistenceEvent(AbstractPersistenceEvent event) {
>
>    }
> }
>
> Wiring:
>
> mainContext.eventTriggeringInterceptor.datastores.each { k, datastore ->
>            mainContext.addApplicationListener new
> DomainObjectPersistencyEventListener(datastore)
> }
>
>
> This way onPersistenceEvent -method I can get the entityObject, but the
> other instances are null.
>
> Is the only way to ask from the entityObject "dirtyPropertyNames" or is
> there some nice way to get the previous state of the domain object?
>
> - Sami
>
>
>
>
> --
> View this message in context: http://grails.1312388.n4.nabble.com/Persisteny-event-listeners-in-Grails-tp4651809.html
> Sent from the Grails - dev mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> 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: Persisteny event listeners in Grails

Daniel Woods
In reply to this post by smakela
please ask usage questions on the user list.


On Mon, Nov 25, 2013 at 11:01 AM, smakela <[hidden email]> wrote:
Hi!

What is the best way to get the persisted object (old data), when I'm using
the API that is provided by Gorm. Meaning if I implement the
AbstractPersistenceEventListener.

I don't want to use any Hibernate -stuff (I can get the native event and can
get the oldState and newState from there).

class DomainObjectPersistencyEventListener extends
AbstractPersistenceEventListener {

    public DomainObjectPersistencyEventListener(final Datastore datastore) {
        super(datastore)
    }

    @Override
    public boolean supportsEventType(Class<? extends ApplicationEvent> e) {
        return PreUpdateEvent.class.isAssignableFrom(e)
    }

    @Override
    protected void onPersistenceEvent(AbstractPersistenceEvent event) {

    }
}

Wiring:

mainContext.eventTriggeringInterceptor.datastores.each { k, datastore ->
            mainContext.addApplicationListener new
DomainObjectPersistencyEventListener(datastore)
}


This way onPersistenceEvent -method I can get the entityObject, but the
other instances are null.

Is the only way to ask from the entityObject "dirtyPropertyNames" or is
there some nice way to get the previous state of the domain object?

 - Sami




--
View this message in context: http://grails.1312388.n4.nabble.com/Persisteny-event-listeners-in-Grails-tp4651809.html
Sent from the Grails - dev mailing list archive at Nabble.com.

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

    http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Persisteny event listeners in Grails

smakela
Audit plugin uses Hibernate -events and I don't want to make any dependencies to Hibernate.

I think this relates also to development, because there could be a need to make this API better.
Reply | Threaded
Open this post in threaded view
|

Re: Persisteny event listeners in Grails

smaldini
Just use grails-event:

BuildConfig.groovy:
 dependencies {     
     compile 'org.grails.plugins:events:1.0.0.BUILD-SNAPSHOT'
 }

SomeService.groovy:
class SomeService{

 @Selector void afterInsert(Book book){
    //stuff after insert
  }
}

in your conf/ApplicationEvents.groovy file:
import org.grails.plugins.events.reactor.api.EventsApi

includes = ['default']

doWithReactor = {
reactor(EventsApi.GRAILS_REACTOR){
ext 'gorm', true
}
}


On Mon, Nov 25, 2013 at 5:48 PM, smakela <[hidden email]> wrote:
Audit plugin uses Hibernate -events and I don't want to make any dependencies
to Hibernate.

I think this relates also to development, because there could be a need to
make this API better.




--
View this message in context: http://grails.1312388.n4.nabble.com/Persistency-event-listeners-in-Grails-tp4651809p4651816.html
Sent from the Grails - dev mailing list archive at Nabble.com.

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

    http://xircles.codehaus.org/manage_email





--
Stéphane
--

Reply | Threaded
Open this post in threaded view
|

Re: Persistency event listeners in Grails

smakela
In reply to this post by smakela
Always the answer is "use this plugin".

Why do we have this shitty API in the first place if you have to use plugins to do stuff?
Even in the documentation you have chapter for custom event handlers:

http://grails.org/doc/latest/guide/GORM.html#eventsAutoTimestamping (Custom Event Listeners)

So is there a clean simple way to get the persisted data. Could we just have oldState and newState like in the Hibernate event has?

- Sami
Reply | Threaded
Open this post in threaded view
|

Re: Persisteny event listeners in Grails

smakela
In reply to this post by smaldini
And by the way smaldini, you did not even answer to my question.
How do you get OLD state of the object (And I don't want to make a query to database)?
And where I can find documentation and source code of that plugin?
Reply | Threaded
Open this post in threaded view
|

Re: Persisteny event listeners in Grails

Lari Hotari -
And by the way smaldini, you did not even answer to my question.
How do you get OLD state of the object (And I don't want to make a query to
database)?
And where I can find documentation and source code of that plugin?



--
View this message in context: http://grails.1312388.n4.nabble.com/Persistency-event-listeners-in-Grails-tp4651809p4651901.html
Sent from the Grails - dev mailing list archive at Nabble.com.

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

    http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Persisteny event listeners in Grails

smakela
Hi Lari,

Yep. Been in another projects and now back in Grails again :)
We are just making an auditlog -plugin that is not tied to any database, so using gorm events
seemed best choice.

Could no get dirtyProperties work in PreUpdate -event for hasMany properties. You get always the new values. For String and those kind of properties it works fine.