You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by James Talbut <jt...@spudsoft.co.uk> on 2014/02/10 15:09:47 UTC

Any way to access original field values by name from Auditor context

Hi,

I'm trying to record old and new field values in an Auditor.
Using something like this I can get the new value (using the audited.getUpdatedFields() to provide the values for 'field'):

    private Object getFieldValue(Broker broker, Object object, String field) {
        if (object == null) {
            return null;
        }
        PersistenceCapable persistenceCapable = ImplHelper.toPersistenceCapable(object, JPAFacadeHelper.toEntityManager(broker));
        OpenJPAStateManager stateManager = (OpenJPAStateManager)persistenceCapable.pcGetStateManager();
        if (stateManager == null) {
            return null;
        }
        int fieldIdx = stateManager.getMetaData().getField(field).getIndex();
        Object value = stateManager.fetch(fieldIdx);
        if ((value instanceof RowSetHolder) &&
                (((RowSetHolder)value).getSize() == 0)) {
            return null;
        }
        return value;
    }

But the old value has no state manager, so pcGetStateManager always returns null.

The old object does have the correct values set on it, but I don't have to have to use reflection (and somehow work out the mapping from field name to method name).

Is there any equivalent to the above that will work for the old values (as returned by audited.getOriginalObject()) ?

Thanks.

Jim

Re: Any way to access original field values by name from Auditor context

Posted by "Leonardo K. Shikida" <sh...@gmail.com>.
nice to hear that ;-)



[]

Leo


On Mon, Feb 10, 2014 at 2:45 PM, James Talbut <jt...@spudsoft.co.uk>wrote:

> One quick upgrade to 2.3.0 and my tests are passing.
>
> Thank you, that's a lot neater than I feared it would be.
>
> Jim
>
>
> On Mon, Feb 10, 2014 at 02:36:00PM -0200, Leonardo K. Shikida wrote:
> > [leoks@oc7612866413 ~]$ ls Downloads/apache-tomee-plus-1.6.0/lib/*jpa*
> > Downloads/apache-tomee-plus-1.6.0/lib/openejb-jpa-integration-4.6.0.jar
> > Downloads/apache-tomee-plus-1.6.0/lib/openjpa-2.3.0-nonfinal-1540826.jar
> >
> >
> > seems to be 2.3.0
> >
> > []
> >
> > Leo
> >
> >
> > On Mon, Feb 10, 2014 at 2:34 PM, Leonardo K. Shikida <shikida@gmail.com
> >wrote:
> >
> > > it should be enabled since 2.2.0
> > >
> > >
> > >
> http://openjpa.apache.org/builds/2.2.0/apache-openjpa/docs/ref_guide_audit.html
> > >
> > > []
> > >
> > > Leo
> > >
> > > []
> > >
> > > Leo
> > >
> > >
> > > On Mon, Feb 10, 2014 at 2:28 PM, James Talbut <jtalbut@spudsoft.co.uk
> >wrote:
> > >
> > >> Hi Leo,
> > >>
> > >> That looks great, but my version of OpenJPA (2.2.2) doesn't seem to
> have
> > >> two key functions:
> > >> audited.getManangedFieldValue(field)
> > >> audited.getOriginalFieldValue(field)
> > >>
> > >> What version are you using?
> > >>
> > >> Thanks
> > >>
> > >> Jim
> > >>
> > >>
> > >> On Mon, Feb 10, 2014 at 02:06:18PM -0200, Leonardo K. Shikida wrote:
> > >> > I am not sure if this is what want, but I do something like this
> with my
> > >> > classes
> > >> >
> > >> > my audited entity
> > >> >
> > >> > @Auditable(values = { AuditableOperation.CREATE,
> > >> AuditableOperation.UPDATE
> > >> > })
> > >> > public class MyClass implements Serializable {
> > >> > (...)
> > >> > }
> > >> >
> > >> > my persistence.xml
> > >> >
> > >> > <property name="openjpa.Auditor"
> > >> >
> > >>
> value="my.DatabaseAuditor(sequence='false',synchDriver='oracle.jdbc.OracleDriver',synchDriverUser='myuser',synchDriverPassword='mypass',synchDriverJdbc='jdbc:oracle:thin:@localhost
> > >> > :1521:XE')"/>
> > >> >
> > >> > my Auditor
> > >> >
> > >> > public class DatabaseAuditor implements Auditor {
> > >> >
> > >> >     private AuditLogger    defaultAuditor    = new AuditLogger();
> > >> >     private String        synchDriver;
> > >> >     private String        synchDriverUser;
> > >> >     private String        synchDriverPassword;
> > >> >     private String        synchDriverJdbc;
> > >> >     private String        sequence;
> > >> >
> > >> >     @Override
> > >> >     public void audit(Broker broker, Collection<Audited> newObjects,
> > >> > Collection<Audited> updates, Collection<Audited> deletes) {
> > >> > (...)
> > >> >         if (updates.size() > 0) {
> > >> >             for (Audited a : updates) {
> > >> >                     this.addChangeLogForUpdate(a);
> > >> >             }
> > >> >         }
> > >> >     }
> > >> >
> > >> > and addChangeLogForUpdate can access both previous and new values
> just
> > >> > using
> > >> >
> > >> >         for (String field : audited.getUpdatedFields()) {
> > >> >             if
> (Arrays.asList(DatabaseAuditor.FIELDS).contains(field)) {
> > >> >                 newdata.put(field,
> > >> audited.getManangedFieldValue(field));
> > >> >                 olddata.put(field,
> > >> audited.getOriginalFieldValue(field));
> > >> >                 //use driver info to save this using plain JDBC
> > >> >
> > >> > I hope it helps
> > >> >
> > >> > Leo
> > >> >
> > >> >
> > >> >
> > >> > []
> > >> >
> > >> > Leo
> > >> >
> > >> >
> > >> > On Mon, Feb 10, 2014 at 1:36 PM, Boblitz John <
> > >> john.boblitz@bertschi.com>wrote:
> > >> >
> > >> > > I was working on something similar and this might get you closer
> > >> > >
> > >> > >         StateManagerImpl sm =
> > >> > > (StateManagerImpl)currentState.pcGetStateManager();
> > >> > >         SaveFieldManager sfm = sm.getSaveFieldManager();
> > >> > >         PersistenceCapable oldState = sfm.getState();
> > >> > >
> > >> > > I believe you must also specifiy openjpa.RestoreState so that
> this can
> > >> > > work ...
> > >> > >
> > >> > > Hope this helps ...
> > >> > >
> > >> > > John
> > >> > >
> > >> > > > -----Original Message-----
> > >> > > > From: James Talbut [mailto:jtalbut@spudsoft.co.uk]
> > >> > > > Sent: Monday, February 10, 2014 3:10 PM
> > >> > > > To: users@openjpa.apache.org
> > >> > > > Subject: Any way to access original field values by name from
> > >> Auditor
> > >> > > context
> > >> > > >
> > >> > > > Hi,
> > >> > > >
> > >> > > > I'm trying to record old and new field values in an Auditor.
> > >> > > > Using something like this I can get the new value (using the
> > >> > > > audited.getUpdatedFields() to provide the values for 'field'):
> > >> > > >
> > >> > > >     private Object getFieldValue(Broker broker, Object object,
> > >> String
> > >> > > field) {
> > >> > > >         if (object == null) {
> > >> > > >             return null;
> > >> > > >         }
> > >> > > >         PersistenceCapable persistenceCapable =
> > >> > > > ImplHelper.toPersistenceCapable(object,
> > >> > > > JPAFacadeHelper.toEntityManager(broker));
> > >> > > >         OpenJPAStateManager stateManager =
> > >> > > > (OpenJPAStateManager)persistenceCapable.pcGetStateManager();
> > >> > > >         if (stateManager == null) {
> > >> > > >             return null;
> > >> > > >         }
> > >> > > >         int fieldIdx =
> > >> > > stateManager.getMetaData().getField(field).getIndex();
> > >> > > >         Object value = stateManager.fetch(fieldIdx);
> > >> > > >         if ((value instanceof RowSetHolder) &&
> > >> > > >                 (((RowSetHolder)value).getSize() == 0)) {
> > >> > > >             return null;
> > >> > > >         }
> > >> > > >         return value;
> > >> > > >     }
> > >> > > >
> > >> > > > But the old value has no state manager, so pcGetStateManager
> always
> > >> > > returns
> > >> > > > null.
> > >> > > >
> > >> > > > The old object does have the correct values set on it, but I
> don't
> > >> have
> > >> > > to have to
> > >> > > > use reflection (and somehow work out the mapping from field
> name to
> > >> > > method
> > >> > > > name).
> > >> > > >
> > >> > > > Is there any equivalent to the above that will work for the old
> > >> values
> > >> > > (as
> > >> > > > returned by audited.getOriginalObject()) ?
> > >> > > >
> > >> > > > Thanks.
> > >> > > >
> > >> > > > Jim
> > >> > >
> > >>
> > >
> > >
>

Re: Any way to access original field values by name from Auditor context

Posted by James Talbut <jt...@spudsoft.co.uk>.
One quick upgrade to 2.3.0 and my tests are passing.

Thank you, that's a lot neater than I feared it would be.

Jim


On Mon, Feb 10, 2014 at 02:36:00PM -0200, Leonardo K. Shikida wrote:
> [leoks@oc7612866413 ~]$ ls Downloads/apache-tomee-plus-1.6.0/lib/*jpa*
> Downloads/apache-tomee-plus-1.6.0/lib/openejb-jpa-integration-4.6.0.jar
> Downloads/apache-tomee-plus-1.6.0/lib/openjpa-2.3.0-nonfinal-1540826.jar
> 
> 
> seems to be 2.3.0
> 
> []
> 
> Leo
> 
> 
> On Mon, Feb 10, 2014 at 2:34 PM, Leonardo K. Shikida <sh...@gmail.com>wrote:
> 
> > it should be enabled since 2.2.0
> >
> >
> > http://openjpa.apache.org/builds/2.2.0/apache-openjpa/docs/ref_guide_audit.html
> >
> > []
> >
> > Leo
> >
> > []
> >
> > Leo
> >
> >
> > On Mon, Feb 10, 2014 at 2:28 PM, James Talbut <jt...@spudsoft.co.uk>wrote:
> >
> >> Hi Leo,
> >>
> >> That looks great, but my version of OpenJPA (2.2.2) doesn't seem to have
> >> two key functions:
> >> audited.getManangedFieldValue(field)
> >> audited.getOriginalFieldValue(field)
> >>
> >> What version are you using?
> >>
> >> Thanks
> >>
> >> Jim
> >>
> >>
> >> On Mon, Feb 10, 2014 at 02:06:18PM -0200, Leonardo K. Shikida wrote:
> >> > I am not sure if this is what want, but I do something like this with my
> >> > classes
> >> >
> >> > my audited entity
> >> >
> >> > @Auditable(values = { AuditableOperation.CREATE,
> >> AuditableOperation.UPDATE
> >> > })
> >> > public class MyClass implements Serializable {
> >> > (...)
> >> > }
> >> >
> >> > my persistence.xml
> >> >
> >> > <property name="openjpa.Auditor"
> >> >
> >> value="my.DatabaseAuditor(sequence='false',synchDriver='oracle.jdbc.OracleDriver',synchDriverUser='myuser',synchDriverPassword='mypass',synchDriverJdbc='jdbc:oracle:thin:@localhost
> >> > :1521:XE')"/>
> >> >
> >> > my Auditor
> >> >
> >> > public class DatabaseAuditor implements Auditor {
> >> >
> >> >     private AuditLogger    defaultAuditor    = new AuditLogger();
> >> >     private String        synchDriver;
> >> >     private String        synchDriverUser;
> >> >     private String        synchDriverPassword;
> >> >     private String        synchDriverJdbc;
> >> >     private String        sequence;
> >> >
> >> >     @Override
> >> >     public void audit(Broker broker, Collection<Audited> newObjects,
> >> > Collection<Audited> updates, Collection<Audited> deletes) {
> >> > (...)
> >> >         if (updates.size() > 0) {
> >> >             for (Audited a : updates) {
> >> >                     this.addChangeLogForUpdate(a);
> >> >             }
> >> >         }
> >> >     }
> >> >
> >> > and addChangeLogForUpdate can access both previous and new values just
> >> > using
> >> >
> >> >         for (String field : audited.getUpdatedFields()) {
> >> >             if (Arrays.asList(DatabaseAuditor.FIELDS).contains(field)) {
> >> >                 newdata.put(field,
> >> audited.getManangedFieldValue(field));
> >> >                 olddata.put(field,
> >> audited.getOriginalFieldValue(field));
> >> >                 //use driver info to save this using plain JDBC
> >> >
> >> > I hope it helps
> >> >
> >> > Leo
> >> >
> >> >
> >> >
> >> > []
> >> >
> >> > Leo
> >> >
> >> >
> >> > On Mon, Feb 10, 2014 at 1:36 PM, Boblitz John <
> >> john.boblitz@bertschi.com>wrote:
> >> >
> >> > > I was working on something similar and this might get you closer
> >> > >
> >> > >         StateManagerImpl sm =
> >> > > (StateManagerImpl)currentState.pcGetStateManager();
> >> > >         SaveFieldManager sfm = sm.getSaveFieldManager();
> >> > >         PersistenceCapable oldState = sfm.getState();
> >> > >
> >> > > I believe you must also specifiy openjpa.RestoreState so that this can
> >> > > work ...
> >> > >
> >> > > Hope this helps ...
> >> > >
> >> > > John
> >> > >
> >> > > > -----Original Message-----
> >> > > > From: James Talbut [mailto:jtalbut@spudsoft.co.uk]
> >> > > > Sent: Monday, February 10, 2014 3:10 PM
> >> > > > To: users@openjpa.apache.org
> >> > > > Subject: Any way to access original field values by name from
> >> Auditor
> >> > > context
> >> > > >
> >> > > > Hi,
> >> > > >
> >> > > > I'm trying to record old and new field values in an Auditor.
> >> > > > Using something like this I can get the new value (using the
> >> > > > audited.getUpdatedFields() to provide the values for 'field'):
> >> > > >
> >> > > >     private Object getFieldValue(Broker broker, Object object,
> >> String
> >> > > field) {
> >> > > >         if (object == null) {
> >> > > >             return null;
> >> > > >         }
> >> > > >         PersistenceCapable persistenceCapable =
> >> > > > ImplHelper.toPersistenceCapable(object,
> >> > > > JPAFacadeHelper.toEntityManager(broker));
> >> > > >         OpenJPAStateManager stateManager =
> >> > > > (OpenJPAStateManager)persistenceCapable.pcGetStateManager();
> >> > > >         if (stateManager == null) {
> >> > > >             return null;
> >> > > >         }
> >> > > >         int fieldIdx =
> >> > > stateManager.getMetaData().getField(field).getIndex();
> >> > > >         Object value = stateManager.fetch(fieldIdx);
> >> > > >         if ((value instanceof RowSetHolder) &&
> >> > > >                 (((RowSetHolder)value).getSize() == 0)) {
> >> > > >             return null;
> >> > > >         }
> >> > > >         return value;
> >> > > >     }
> >> > > >
> >> > > > But the old value has no state manager, so pcGetStateManager always
> >> > > returns
> >> > > > null.
> >> > > >
> >> > > > The old object does have the correct values set on it, but I don't
> >> have
> >> > > to have to
> >> > > > use reflection (and somehow work out the mapping from field name to
> >> > > method
> >> > > > name).
> >> > > >
> >> > > > Is there any equivalent to the above that will work for the old
> >> values
> >> > > (as
> >> > > > returned by audited.getOriginalObject()) ?
> >> > > >
> >> > > > Thanks.
> >> > > >
> >> > > > Jim
> >> > >
> >>
> >
> >

Re: Any way to access original field values by name from Auditor context

Posted by "Leonardo K. Shikida" <sh...@gmail.com>.
[leoks@oc7612866413 ~]$ ls Downloads/apache-tomee-plus-1.6.0/lib/*jpa*
Downloads/apache-tomee-plus-1.6.0/lib/openejb-jpa-integration-4.6.0.jar
Downloads/apache-tomee-plus-1.6.0/lib/openjpa-2.3.0-nonfinal-1540826.jar


seems to be 2.3.0

[]

Leo


On Mon, Feb 10, 2014 at 2:34 PM, Leonardo K. Shikida <sh...@gmail.com>wrote:

> it should be enabled since 2.2.0
>
>
> http://openjpa.apache.org/builds/2.2.0/apache-openjpa/docs/ref_guide_audit.html
>
> []
>
> Leo
>
> []
>
> Leo
>
>
> On Mon, Feb 10, 2014 at 2:28 PM, James Talbut <jt...@spudsoft.co.uk>wrote:
>
>> Hi Leo,
>>
>> That looks great, but my version of OpenJPA (2.2.2) doesn't seem to have
>> two key functions:
>> audited.getManangedFieldValue(field)
>> audited.getOriginalFieldValue(field)
>>
>> What version are you using?
>>
>> Thanks
>>
>> Jim
>>
>>
>> On Mon, Feb 10, 2014 at 02:06:18PM -0200, Leonardo K. Shikida wrote:
>> > I am not sure if this is what want, but I do something like this with my
>> > classes
>> >
>> > my audited entity
>> >
>> > @Auditable(values = { AuditableOperation.CREATE,
>> AuditableOperation.UPDATE
>> > })
>> > public class MyClass implements Serializable {
>> > (...)
>> > }
>> >
>> > my persistence.xml
>> >
>> > <property name="openjpa.Auditor"
>> >
>> value="my.DatabaseAuditor(sequence='false',synchDriver='oracle.jdbc.OracleDriver',synchDriverUser='myuser',synchDriverPassword='mypass',synchDriverJdbc='jdbc:oracle:thin:@localhost
>> > :1521:XE')"/>
>> >
>> > my Auditor
>> >
>> > public class DatabaseAuditor implements Auditor {
>> >
>> >     private AuditLogger    defaultAuditor    = new AuditLogger();
>> >     private String        synchDriver;
>> >     private String        synchDriverUser;
>> >     private String        synchDriverPassword;
>> >     private String        synchDriverJdbc;
>> >     private String        sequence;
>> >
>> >     @Override
>> >     public void audit(Broker broker, Collection<Audited> newObjects,
>> > Collection<Audited> updates, Collection<Audited> deletes) {
>> > (...)
>> >         if (updates.size() > 0) {
>> >             for (Audited a : updates) {
>> >                     this.addChangeLogForUpdate(a);
>> >             }
>> >         }
>> >     }
>> >
>> > and addChangeLogForUpdate can access both previous and new values just
>> > using
>> >
>> >         for (String field : audited.getUpdatedFields()) {
>> >             if (Arrays.asList(DatabaseAuditor.FIELDS).contains(field)) {
>> >                 newdata.put(field,
>> audited.getManangedFieldValue(field));
>> >                 olddata.put(field,
>> audited.getOriginalFieldValue(field));
>> >                 //use driver info to save this using plain JDBC
>> >
>> > I hope it helps
>> >
>> > Leo
>> >
>> >
>> >
>> > []
>> >
>> > Leo
>> >
>> >
>> > On Mon, Feb 10, 2014 at 1:36 PM, Boblitz John <
>> john.boblitz@bertschi.com>wrote:
>> >
>> > > I was working on something similar and this might get you closer
>> > >
>> > >         StateManagerImpl sm =
>> > > (StateManagerImpl)currentState.pcGetStateManager();
>> > >         SaveFieldManager sfm = sm.getSaveFieldManager();
>> > >         PersistenceCapable oldState = sfm.getState();
>> > >
>> > > I believe you must also specifiy openjpa.RestoreState so that this can
>> > > work ...
>> > >
>> > > Hope this helps ...
>> > >
>> > > John
>> > >
>> > > > -----Original Message-----
>> > > > From: James Talbut [mailto:jtalbut@spudsoft.co.uk]
>> > > > Sent: Monday, February 10, 2014 3:10 PM
>> > > > To: users@openjpa.apache.org
>> > > > Subject: Any way to access original field values by name from
>> Auditor
>> > > context
>> > > >
>> > > > Hi,
>> > > >
>> > > > I'm trying to record old and new field values in an Auditor.
>> > > > Using something like this I can get the new value (using the
>> > > > audited.getUpdatedFields() to provide the values for 'field'):
>> > > >
>> > > >     private Object getFieldValue(Broker broker, Object object,
>> String
>> > > field) {
>> > > >         if (object == null) {
>> > > >             return null;
>> > > >         }
>> > > >         PersistenceCapable persistenceCapable =
>> > > > ImplHelper.toPersistenceCapable(object,
>> > > > JPAFacadeHelper.toEntityManager(broker));
>> > > >         OpenJPAStateManager stateManager =
>> > > > (OpenJPAStateManager)persistenceCapable.pcGetStateManager();
>> > > >         if (stateManager == null) {
>> > > >             return null;
>> > > >         }
>> > > >         int fieldIdx =
>> > > stateManager.getMetaData().getField(field).getIndex();
>> > > >         Object value = stateManager.fetch(fieldIdx);
>> > > >         if ((value instanceof RowSetHolder) &&
>> > > >                 (((RowSetHolder)value).getSize() == 0)) {
>> > > >             return null;
>> > > >         }
>> > > >         return value;
>> > > >     }
>> > > >
>> > > > But the old value has no state manager, so pcGetStateManager always
>> > > returns
>> > > > null.
>> > > >
>> > > > The old object does have the correct values set on it, but I don't
>> have
>> > > to have to
>> > > > use reflection (and somehow work out the mapping from field name to
>> > > method
>> > > > name).
>> > > >
>> > > > Is there any equivalent to the above that will work for the old
>> values
>> > > (as
>> > > > returned by audited.getOriginalObject()) ?
>> > > >
>> > > > Thanks.
>> > > >
>> > > > Jim
>> > >
>>
>
>

Re: Any way to access original field values by name from Auditor context

Posted by "Leonardo K. Shikida" <sh...@gmail.com>.
it should be enabled since 2.2.0

http://openjpa.apache.org/builds/2.2.0/apache-openjpa/docs/ref_guide_audit.html

[]

Leo

[]

Leo


On Mon, Feb 10, 2014 at 2:28 PM, James Talbut <jt...@spudsoft.co.uk>wrote:

> Hi Leo,
>
> That looks great, but my version of OpenJPA (2.2.2) doesn't seem to have
> two key functions:
> audited.getManangedFieldValue(field)
> audited.getOriginalFieldValue(field)
>
> What version are you using?
>
> Thanks
>
> Jim
>
>
> On Mon, Feb 10, 2014 at 02:06:18PM -0200, Leonardo K. Shikida wrote:
> > I am not sure if this is what want, but I do something like this with my
> > classes
> >
> > my audited entity
> >
> > @Auditable(values = { AuditableOperation.CREATE,
> AuditableOperation.UPDATE
> > })
> > public class MyClass implements Serializable {
> > (...)
> > }
> >
> > my persistence.xml
> >
> > <property name="openjpa.Auditor"
> >
> value="my.DatabaseAuditor(sequence='false',synchDriver='oracle.jdbc.OracleDriver',synchDriverUser='myuser',synchDriverPassword='mypass',synchDriverJdbc='jdbc:oracle:thin:@localhost
> > :1521:XE')"/>
> >
> > my Auditor
> >
> > public class DatabaseAuditor implements Auditor {
> >
> >     private AuditLogger    defaultAuditor    = new AuditLogger();
> >     private String        synchDriver;
> >     private String        synchDriverUser;
> >     private String        synchDriverPassword;
> >     private String        synchDriverJdbc;
> >     private String        sequence;
> >
> >     @Override
> >     public void audit(Broker broker, Collection<Audited> newObjects,
> > Collection<Audited> updates, Collection<Audited> deletes) {
> > (...)
> >         if (updates.size() > 0) {
> >             for (Audited a : updates) {
> >                     this.addChangeLogForUpdate(a);
> >             }
> >         }
> >     }
> >
> > and addChangeLogForUpdate can access both previous and new values just
> > using
> >
> >         for (String field : audited.getUpdatedFields()) {
> >             if (Arrays.asList(DatabaseAuditor.FIELDS).contains(field)) {
> >                 newdata.put(field, audited.getManangedFieldValue(field));
> >                 olddata.put(field, audited.getOriginalFieldValue(field));
> >                 //use driver info to save this using plain JDBC
> >
> > I hope it helps
> >
> > Leo
> >
> >
> >
> > []
> >
> > Leo
> >
> >
> > On Mon, Feb 10, 2014 at 1:36 PM, Boblitz John <john.boblitz@bertschi.com
> >wrote:
> >
> > > I was working on something similar and this might get you closer
> > >
> > >         StateManagerImpl sm =
> > > (StateManagerImpl)currentState.pcGetStateManager();
> > >         SaveFieldManager sfm = sm.getSaveFieldManager();
> > >         PersistenceCapable oldState = sfm.getState();
> > >
> > > I believe you must also specifiy openjpa.RestoreState so that this can
> > > work ...
> > >
> > > Hope this helps ...
> > >
> > > John
> > >
> > > > -----Original Message-----
> > > > From: James Talbut [mailto:jtalbut@spudsoft.co.uk]
> > > > Sent: Monday, February 10, 2014 3:10 PM
> > > > To: users@openjpa.apache.org
> > > > Subject: Any way to access original field values by name from Auditor
> > > context
> > > >
> > > > Hi,
> > > >
> > > > I'm trying to record old and new field values in an Auditor.
> > > > Using something like this I can get the new value (using the
> > > > audited.getUpdatedFields() to provide the values for 'field'):
> > > >
> > > >     private Object getFieldValue(Broker broker, Object object, String
> > > field) {
> > > >         if (object == null) {
> > > >             return null;
> > > >         }
> > > >         PersistenceCapable persistenceCapable =
> > > > ImplHelper.toPersistenceCapable(object,
> > > > JPAFacadeHelper.toEntityManager(broker));
> > > >         OpenJPAStateManager stateManager =
> > > > (OpenJPAStateManager)persistenceCapable.pcGetStateManager();
> > > >         if (stateManager == null) {
> > > >             return null;
> > > >         }
> > > >         int fieldIdx =
> > > stateManager.getMetaData().getField(field).getIndex();
> > > >         Object value = stateManager.fetch(fieldIdx);
> > > >         if ((value instanceof RowSetHolder) &&
> > > >                 (((RowSetHolder)value).getSize() == 0)) {
> > > >             return null;
> > > >         }
> > > >         return value;
> > > >     }
> > > >
> > > > But the old value has no state manager, so pcGetStateManager always
> > > returns
> > > > null.
> > > >
> > > > The old object does have the correct values set on it, but I don't
> have
> > > to have to
> > > > use reflection (and somehow work out the mapping from field name to
> > > method
> > > > name).
> > > >
> > > > Is there any equivalent to the above that will work for the old
> values
> > > (as
> > > > returned by audited.getOriginalObject()) ?
> > > >
> > > > Thanks.
> > > >
> > > > Jim
> > >
>

Re: Any way to access original field values by name from Auditor context

Posted by James Talbut <jt...@spudsoft.co.uk>.
Hi Leo,

That looks great, but my version of OpenJPA (2.2.2) doesn't seem to have two key functions:
audited.getManangedFieldValue(field)
audited.getOriginalFieldValue(field)

What version are you using?

Thanks

Jim


On Mon, Feb 10, 2014 at 02:06:18PM -0200, Leonardo K. Shikida wrote:
> I am not sure if this is what want, but I do something like this with my
> classes
> 
> my audited entity
> 
> @Auditable(values = { AuditableOperation.CREATE, AuditableOperation.UPDATE
> })
> public class MyClass implements Serializable {
> (...)
> }
> 
> my persistence.xml
> 
> <property name="openjpa.Auditor"
> value="my.DatabaseAuditor(sequence='false',synchDriver='oracle.jdbc.OracleDriver',synchDriverUser='myuser',synchDriverPassword='mypass',synchDriverJdbc='jdbc:oracle:thin:@localhost
> :1521:XE')"/>
> 
> my Auditor
> 
> public class DatabaseAuditor implements Auditor {
> 
>     private AuditLogger    defaultAuditor    = new AuditLogger();
>     private String        synchDriver;
>     private String        synchDriverUser;
>     private String        synchDriverPassword;
>     private String        synchDriverJdbc;
>     private String        sequence;
> 
>     @Override
>     public void audit(Broker broker, Collection<Audited> newObjects,
> Collection<Audited> updates, Collection<Audited> deletes) {
> (...)
>         if (updates.size() > 0) {
>             for (Audited a : updates) {
>                     this.addChangeLogForUpdate(a);
>             }
>         }
>     }
> 
> and addChangeLogForUpdate can access both previous and new values just
> using
> 
>         for (String field : audited.getUpdatedFields()) {
>             if (Arrays.asList(DatabaseAuditor.FIELDS).contains(field)) {
>                 newdata.put(field, audited.getManangedFieldValue(field));
>                 olddata.put(field, audited.getOriginalFieldValue(field));
>                 //use driver info to save this using plain JDBC
> 
> I hope it helps
> 
> Leo
> 
> 
> 
> []
> 
> Leo
> 
> 
> On Mon, Feb 10, 2014 at 1:36 PM, Boblitz John <jo...@bertschi.com>wrote:
> 
> > I was working on something similar and this might get you closer
> >
> >         StateManagerImpl sm =
> > (StateManagerImpl)currentState.pcGetStateManager();
> >         SaveFieldManager sfm = sm.getSaveFieldManager();
> >         PersistenceCapable oldState = sfm.getState();
> >
> > I believe you must also specifiy openjpa.RestoreState so that this can
> > work ...
> >
> > Hope this helps ...
> >
> > John
> >
> > > -----Original Message-----
> > > From: James Talbut [mailto:jtalbut@spudsoft.co.uk]
> > > Sent: Monday, February 10, 2014 3:10 PM
> > > To: users@openjpa.apache.org
> > > Subject: Any way to access original field values by name from Auditor
> > context
> > >
> > > Hi,
> > >
> > > I'm trying to record old and new field values in an Auditor.
> > > Using something like this I can get the new value (using the
> > > audited.getUpdatedFields() to provide the values for 'field'):
> > >
> > >     private Object getFieldValue(Broker broker, Object object, String
> > field) {
> > >         if (object == null) {
> > >             return null;
> > >         }
> > >         PersistenceCapable persistenceCapable =
> > > ImplHelper.toPersistenceCapable(object,
> > > JPAFacadeHelper.toEntityManager(broker));
> > >         OpenJPAStateManager stateManager =
> > > (OpenJPAStateManager)persistenceCapable.pcGetStateManager();
> > >         if (stateManager == null) {
> > >             return null;
> > >         }
> > >         int fieldIdx =
> > stateManager.getMetaData().getField(field).getIndex();
> > >         Object value = stateManager.fetch(fieldIdx);
> > >         if ((value instanceof RowSetHolder) &&
> > >                 (((RowSetHolder)value).getSize() == 0)) {
> > >             return null;
> > >         }
> > >         return value;
> > >     }
> > >
> > > But the old value has no state manager, so pcGetStateManager always
> > returns
> > > null.
> > >
> > > The old object does have the correct values set on it, but I don't have
> > to have to
> > > use reflection (and somehow work out the mapping from field name to
> > method
> > > name).
> > >
> > > Is there any equivalent to the above that will work for the old values
> > (as
> > > returned by audited.getOriginalObject()) ?
> > >
> > > Thanks.
> > >
> > > Jim
> >

Re: Any way to access original field values by name from Auditor context

Posted by "Leonardo K. Shikida" <sh...@gmail.com>.
I am not sure if this is what want, but I do something like this with my
classes

my audited entity

@Auditable(values = { AuditableOperation.CREATE, AuditableOperation.UPDATE
})
public class MyClass implements Serializable {
(...)
}

my persistence.xml

<property name="openjpa.Auditor"
value="my.DatabaseAuditor(sequence='false',synchDriver='oracle.jdbc.OracleDriver',synchDriverUser='myuser',synchDriverPassword='mypass',synchDriverJdbc='jdbc:oracle:thin:@localhost
:1521:XE')"/>

my Auditor

public class DatabaseAuditor implements Auditor {

    private AuditLogger    defaultAuditor    = new AuditLogger();
    private String        synchDriver;
    private String        synchDriverUser;
    private String        synchDriverPassword;
    private String        synchDriverJdbc;
    private String        sequence;

    @Override
    public void audit(Broker broker, Collection<Audited> newObjects,
Collection<Audited> updates, Collection<Audited> deletes) {
(...)
        if (updates.size() > 0) {
            for (Audited a : updates) {
                    this.addChangeLogForUpdate(a);
            }
        }
    }

and addChangeLogForUpdate can access both previous and new values just
using

        for (String field : audited.getUpdatedFields()) {
            if (Arrays.asList(DatabaseAuditor.FIELDS).contains(field)) {
                newdata.put(field, audited.getManangedFieldValue(field));
                olddata.put(field, audited.getOriginalFieldValue(field));
                //use driver info to save this using plain JDBC

I hope it helps

Leo



[]

Leo


On Mon, Feb 10, 2014 at 1:36 PM, Boblitz John <jo...@bertschi.com>wrote:

> I was working on something similar and this might get you closer
>
>         StateManagerImpl sm =
> (StateManagerImpl)currentState.pcGetStateManager();
>         SaveFieldManager sfm = sm.getSaveFieldManager();
>         PersistenceCapable oldState = sfm.getState();
>
> I believe you must also specifiy openjpa.RestoreState so that this can
> work ...
>
> Hope this helps ...
>
> John
>
> > -----Original Message-----
> > From: James Talbut [mailto:jtalbut@spudsoft.co.uk]
> > Sent: Monday, February 10, 2014 3:10 PM
> > To: users@openjpa.apache.org
> > Subject: Any way to access original field values by name from Auditor
> context
> >
> > Hi,
> >
> > I'm trying to record old and new field values in an Auditor.
> > Using something like this I can get the new value (using the
> > audited.getUpdatedFields() to provide the values for 'field'):
> >
> >     private Object getFieldValue(Broker broker, Object object, String
> field) {
> >         if (object == null) {
> >             return null;
> >         }
> >         PersistenceCapable persistenceCapable =
> > ImplHelper.toPersistenceCapable(object,
> > JPAFacadeHelper.toEntityManager(broker));
> >         OpenJPAStateManager stateManager =
> > (OpenJPAStateManager)persistenceCapable.pcGetStateManager();
> >         if (stateManager == null) {
> >             return null;
> >         }
> >         int fieldIdx =
> stateManager.getMetaData().getField(field).getIndex();
> >         Object value = stateManager.fetch(fieldIdx);
> >         if ((value instanceof RowSetHolder) &&
> >                 (((RowSetHolder)value).getSize() == 0)) {
> >             return null;
> >         }
> >         return value;
> >     }
> >
> > But the old value has no state manager, so pcGetStateManager always
> returns
> > null.
> >
> > The old object does have the correct values set on it, but I don't have
> to have to
> > use reflection (and somehow work out the mapping from field name to
> method
> > name).
> >
> > Is there any equivalent to the above that will work for the old values
> (as
> > returned by audited.getOriginalObject()) ?
> >
> > Thanks.
> >
> > Jim
>

RE: Any way to access original field values by name from Auditor context

Posted by Boblitz John <jo...@bertschi.com>.
I was working on something similar and this might get you closer

        StateManagerImpl sm = (StateManagerImpl)currentState.pcGetStateManager();
        SaveFieldManager sfm = sm.getSaveFieldManager();
        PersistenceCapable oldState = sfm.getState();

I believe you must also specifiy openjpa.RestoreState so that this can work ...

Hope this helps ...

John

> -----Original Message-----
> From: James Talbut [mailto:jtalbut@spudsoft.co.uk]
> Sent: Monday, February 10, 2014 3:10 PM
> To: users@openjpa.apache.org
> Subject: Any way to access original field values by name from Auditor context
> 
> Hi,
> 
> I'm trying to record old and new field values in an Auditor.
> Using something like this I can get the new value (using the
> audited.getUpdatedFields() to provide the values for 'field'):
> 
>     private Object getFieldValue(Broker broker, Object object, String field) {
>         if (object == null) {
>             return null;
>         }
>         PersistenceCapable persistenceCapable =
> ImplHelper.toPersistenceCapable(object,
> JPAFacadeHelper.toEntityManager(broker));
>         OpenJPAStateManager stateManager =
> (OpenJPAStateManager)persistenceCapable.pcGetStateManager();
>         if (stateManager == null) {
>             return null;
>         }
>         int fieldIdx = stateManager.getMetaData().getField(field).getIndex();
>         Object value = stateManager.fetch(fieldIdx);
>         if ((value instanceof RowSetHolder) &&
>                 (((RowSetHolder)value).getSize() == 0)) {
>             return null;
>         }
>         return value;
>     }
> 
> But the old value has no state manager, so pcGetStateManager always returns
> null.
> 
> The old object does have the correct values set on it, but I don't have to have to
> use reflection (and somehow work out the mapping from field name to method
> name).
> 
> Is there any equivalent to the above that will work for the old values (as
> returned by audited.getOriginalObject()) ?
> 
> Thanks.
> 
> Jim