Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Audit trail
Audit trail [message #755110] Mon, 07 November 2011 07:36 Go to next message
A.Rothe  is currently offline A.Rothe
Messages: 12
Registered: November 2011
Location: Leipzig, Germany
Junior Member
Hi,

my application has an audit trail based on http://tapestryofthoughts.blogspot.com/2007/05/glassfish-and-audit-logging.html. The only problem is, during the insertion of a new record, I don't get a list of changes. Is it possible to get a list of all columns and their values of the new entity from an InsertObjectQuery? The listener returns a DescriptorEvent, which contains a WriteObjectQuery for insert and update statements. On update I can get the changes from the query, but the list is empty for a new record.

Thanks a lot
~Andre

[Updated on: Mon, 07 November 2011 10:29]

Report message to a moderator

Re: Audit trail [message #755165 is a reply to message #755110] Mon, 07 November 2011 10:26 Go to previous message
A.Rothe  is currently offline A.Rothe
Messages: 12
Registered: November 2011
Location: Leipzig, Germany
Junior Member
I found a solution (try and error) - the EclipseLink documentation is not adequate on this point.

It is possible to use on INSERT:

WriteObjectQuery query = (WriteObjectQuery) event.getQuery();
DatabaseRecord rec = (DatabaseRecord) query.getModifyRow();

for (Object column : rec.keySet()) {
   DatabaseField dbcol = (DatabaseField) column;
   // get the column name with dbcol.getName()
   // get the initial value with rec.getValues(dbcol).toString()
}


The UPDATE event on the original blog posting uses getObjectChangeSet() of class WriteObjectQuery. But the changes contain the field name of the Entity class, not the column name of the database.

I have read the mappings:

private String getDatabaseFieldMapping(DescriptorEvent event, String attribute) {
	DatabaseMapping mapping = event.getDescriptor().getMappingForAttributeName(attribute);
	return (mapping == null ? "<unknown>" : mapping.getField().getName());
}


and then I look for the attribute name from the ChangeRecord to get the column name:

WriteObjectQuery query = (WriteObjectQuery) event.getQuery();

for (ChangeRecord rec : query.getObjectChangeSet().getChanges()) {
	if (rec instanceof DirectToFieldChangeRecord) {
		DirectToFieldChangeRecord fieldChange = (DirectToFieldChangeRecord) rec;
		// get the field name with getDatabaseFieldMapping(event, fieldChange.getAttribute())
		// get the new value with fieldChange.getNewValue().toString()
	}
}


The current user can be obtained with (no Dependency Injection on customizer classes!):

private EJBContext getContext() {
	try {
		InitialContext ic = new InitialContext();
		return (EJBContext) ic.lookup("java:comp/EJBContext");
	} catch (NamingException ex) {
		throw new EJBException(ex);
	}
}


@Entity
@Customizer(AuditHandler.class)
public class Test {

 // ...

}


~André

[Updated on: Mon, 07 November 2011 10:38]

Report message to a moderator

Previous Topic:@OneToMany SortedSet
Next Topic:Calling a stored procedure with BLOB input parameter
Goto Forum:
  


Current Time: Fri Aug 22 04:04:59 EDT 2014

Powered by FUDForum. Page generated in 0.01679 seconds