Attribut change listener

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

Attribut change listener

Klesk
Hi,

Is there a way to implements an attribute listener ?
My goal is to be notified when an attribute content changed (getting attribute name, old and new value).
Is is feasible ?


Regards,
Reply | Threaded
Open this post in threaded view
|

Re: Attribut change listener

Klesk
My workaround (needs to be deeply tested):

Klesk wrote
    @Test
    public void testProxy() throws Exception {
        final EntityManager em = factory.createEntityManager();
        final SUti entity = em.find(SUti.class, 1);

        final ProxyFactory proxiter = new ProxyFactory();
        proxiter.setSuperclass(entity.getClass());
        proxiter.setHandler((instance, method, wrapper, args) -> {
            instance = entity;
            wrapper = entity.getClass().getMethod(method.getName(), method.getParameterTypes());
            if(method.getName().startsWith("set")) {
                final String methodName = method.getName();
                final Object previous = instance.getClass().getMethod("get" + methodName.substring(3)).invoke(instance);
                System.out.println("changed: " + previous + " -> " + args[0]);
            }
            return wrapper.invoke(instance, args);
        });

        final SUti mock = (SUti) proxiter.create(new Class[0], new Object[0]);
        assertEquals("AAAA", entity.getUtLibuti());
        assertEquals("AAAA", mock.getUtLibuti());
        mock.setUtLibuti("test");
        assertEquals("test", entity.getUtLibuti());
        assertEquals("test", mock.getUtLibuti());
    }
Reply | Threaded
Open this post in threaded view
|

RE: Attribut change listener

Boblitz John
Hello,

You can create a method and annotate it with    @PostPersist  @PostUpdate  @PostRemove (also pre) depending on your needs.

Then access the old and new states of the entity with

PersistenceCapable pcCurrentState = (PersistenceCapable) pCurrentState;
StateManagerImpl sm = (StateManagerImpl) pcCurrentState.pcGetStateManager();
PCState state = sm.getPCState();
SaveFieldManager sfm = sm.getSaveFieldManager();
PersistenceCapable pcOldState = sfm.getState();

You then cast the states to your entity

                oldState = (Entity) pcOldState;
                newState = (Entity) pcCurrentState;

I do believe you need to add something to your persistence.xml to allow this to work, but I don't recall for certain.



Hope this helps

John

> -----Original Message-----
> From: Klesk [mailto:[hidden email]]
> Sent: Dienstag, 4. Juli 2017 11:14
> To: [hidden email]
> Subject: Re: Attribut change listener
>
> My workaround (needs to be deeply tested):
>
>
> Klesk wrote
> >     @Test
> >     public void testProxy() throws Exception {
> >         final EntityManager em = factory.createEntityManager();
> >         final SUti entity = em.find(SUti.class, 1);
> >
> >         final ProxyFactory proxiter = new ProxyFactory();
> >         proxiter.setSuperclass(entity.getClass());
> >         proxiter.setHandler((instance, method, wrapper, args) -> {
> >             instance = entity;
> >             wrapper = entity.getClass().getMethod(method.getName(),
> > method.getParameterTypes());
> >             if(method.getName().startsWith("set")) {
> >                 final String methodName = method.getName();
> >                 final Object previous =
> > instance.getClass().getMethod("get" +
> > methodName.substring(3)).invoke(instance);
> >                 System.out.println("changed: " + previous + " -> " +
> > args[0]);
> >             }
> >             return wrapper.invoke(instance, args);
> >         });
> >
> >         final SUti mock = (SUti) proxiter.create(new Class[0], new
> > Object[0]);
> >         assertEquals("AAAA", entity.getUtLibuti());
> >         assertEquals("AAAA", mock.getUtLibuti());
> >         mock.setUtLibuti("test");
> >         assertEquals("test", entity.getUtLibuti());
> >         assertEquals("test", mock.getUtLibuti());
> >     }
>
>
>
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Attribut-change-listener-
> tp7590226p7590227.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|

RE: Attribut change listener

Klesk
This post was updated on .
Hello,

In fact, I need to be notified when an attribute content change ASAP. Methods with an event annotation are called when the EntityManager is called. That's why I don't use annotations ;)

Regards,
Reply | Threaded
Open this post in threaded view
|

RE: Attribut change listener

Boblitz John
In that case, why not just implement notification in the setter for the attribute?

public void setSomeAttribute(Object someNewValue) {
        Object oldValue = attributeValue;
        attibuteValue = someNewValue;
        System.out.println("changed: " + oldValue + " -> " attributeValue);
}

John


> -----Original Message-----
> From: Klesk [mailto:[hidden email]]
> Sent: Dienstag, 4. Juli 2017 12:11
> To: [hidden email]
> Subject: RE: Attribut change listener
>
> Hello,
>
> In fact, I need to be notified when an attribute content change ASAP.
> Methods with an event annotation are called when the EntityManager is
> called. That's mhy I don't use annotations ;)
>
> Regards,
>
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Attribut-change-listener-
> tp7590226p7590229.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.