Index: src/groovy/AuditLogListener.groovy =================================================================== --- src/groovy/AuditLogListener.groovy (revision 41394) +++ src/groovy/AuditLogListener.groovy Wed May 28 12:32:04 CDT 2008 @@ -26,6 +26,7 @@ import org.hibernate.event.PostUpdateEvent; import org.hibernate.event.PostUpdateEventListener; import org.springframework.web.context.request.RequestContextHolder; +import groovy.sql.Sql public class AuditLogListener implements PreDeleteEventListener, PostInsertEventListener, PostUpdateEventListener, Initializable { @@ -51,8 +52,9 @@ */ static String actorKey = 'userPrincipal.name' // session.user.name static String sessionAttribute = null + def applicationContext - - def getUri() { + + def getUri() { def attr = RequestContextHolder?.getRequestAttributes() return (attr?.currentRequest?.uri?.toString())?:null } @@ -277,10 +279,8 @@ oldValue:truncate(oldMap[key]), newValue:truncate(newMap[key]), ) - if(!audit.save()) { - println audit.errors + saveAuditLog (audit) - } + } - } }) } else if(newMap && verbose) { @@ -296,9 +296,7 @@ oldValue:null, newValue:truncate(val), ) - if(!audit.save()) { - println audit.errors - } + saveAuditLog (audit) }) } else if(oldMap && verbose) { @@ -314,9 +312,7 @@ oldValue:truncate(val), newValue:null ) - if(!audit.save()) { - println audit.errors - } + saveAuditLog (audit) }) } else { @@ -328,11 +324,8 @@ persistedObjectId:persistedObjectId.toString(), persistedObjectVersion:persistedObjectVersion.toString() ) - if(!audit.save()) { - // TODO don't use println... - println audit.errors + saveAuditLog (audit) - } + } - } return } @@ -363,4 +356,52 @@ } } } + + + def saveAuditLog(audit) { + Sql sql = Sql.newInstance(applicationContext.getBean('dataSource')); + + def params = [0, + audit.actor, + audit.className, + audit.eventName, + audit.newValue, + audit.oldValue, + audit.persistedObjectId, + audit.persistedObjectVersion, + audit.propertyName] + + sql.execute('''INSERT +INTO + audit_log + ( + id, + version, + actor, + class_name, + date_created, + event_name, + last_updated, + new_value, + old_value, + persisted_object_id, + persisted_object_version, + property_name + ) + VALUES + ( + (select nextval ('hibernate_sequence')), + ?, + ?, + ?, + now(), + ?, + now(), + ?, + ?, + ?, + ?, + ? + )''', params) -} \ No newline at end of file + } +} \ No newline at end of file Index: AuditLoggingGrailsPlugin.groovy =================================================================== --- AuditLoggingGrailsPlugin.groovy (revision 41394) +++ AuditLoggingGrailsPlugin.groovy Wed May 28 12:28:03 CDT 2008 @@ -55,8 +55,10 @@ if(config?.username) { listener.sessionAttribute = (config?.username)?:null } - + + listener.applicationContext = applicationContext + - // use preDelete so we can see what is going to be destroyed + // use preDelete so we can see what is going to be destroyed // hook to the postInsert to grab the ID of the object // hook to postUpdate to use the old and new state hooks ['preDelete','postInsert', 'postUpdate',].each({