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
> <[email protected]>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:[email protected]]
> > > Sent: Monday, February 10, 2014 3:10 PM
> > > To: [email protected]
> > > 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
> >