Skip to content

Commit

Permalink
Merge pull request #129 from tkvw/master
Browse files Browse the repository at this point in the history
Fixed issue with Hibernate stamping, stamping was ignored with dynami…
  • Loading branch information
robertoschwald authored Feb 1, 2017
2 parents 60c5bc6 + 0b8e7d9 commit 062ffb4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package grails.plugins.orm.auditable

import com.fasterxml.jackson.annotation.JsonIgnore
import org.apache.commons.lang.ArrayUtils

import static grails.plugins.orm.auditable.AuditLogListenerUtil.*
import groovy.util.logging.Commons
Expand Down Expand Up @@ -69,6 +70,8 @@ class AuditLogListener extends AbstractPersistenceEventListener {
@JsonIgnore
Closure actorClosure

boolean usingHibernate = false

Boolean stampEnabled = true
Boolean stampAlways = false
String stampCreatedBy
Expand All @@ -92,7 +95,7 @@ class AuditLogListener extends AbstractPersistenceEventListener {
return
}
if (stampEnabled && (stampAlways || isStampable(event.entityObject, event.eventType))) {
stamp(event.entityObject, event.eventType);
stamp(event);
}
if (isAuditableEntity(event.entityObject, getEventName(event))) {
log.trace "Audit logging: ${event.eventType.name()} for ${event.entityObject.class.name}"
Expand All @@ -111,12 +114,15 @@ class AuditLogListener extends AbstractPersistenceEventListener {
}
}

void stamp(entity, EventType eventType) {
if (EventType.PreInsert == eventType) {
stampCreatedBy(entity)
stampLastUpdatedBy(entity)
void stamp(AbstractPersistenceEvent event) {
def entity = event.entityObject
def actor = getActor()

if (EventType.PreInsert == event.eventType) {
stampCreatedBy(entity,actor,event)
stampLastUpdatedBy(entity,actor,event)
} else {
stampLastUpdatedBy(entity)
stampLastUpdatedBy(entity,actor,event)
}
}

Expand Down Expand Up @@ -149,12 +155,12 @@ class AuditLogListener extends AbstractPersistenceEventListener {
actorClosure = null
}
}
// If we couldn't find an actor, use the configured default or just 'system'
if (!actor) {
actor = AuditLoggingConfigUtils.auditConfig.defaultActor ?: 'system'
}
}
log.trace("Actor: $actor")
// If we couldn't find an actor, use the configured default or just 'system'
if (!actor) {
actor = AuditLoggingConfigUtils.auditConfig.defaultActor ?: 'system'
}
if(log.traceEnabled) log.trace("Actor: $actor")
return actor?.toString()
}

Expand Down Expand Up @@ -268,14 +274,36 @@ class AuditLogListener extends AbstractPersistenceEventListener {
return mask
}

protected stampCreatedBy(entity) {
entity."${stampCreatedBy}" = getActor()
protected stampCreatedBy(entity,actor,event) {
entity."${stampCreatedBy}" = actor

if(usingHibernate){
String[] propertyNames = event.nativeEvent.getPersister().getEntityMetamodel().getPropertyNames();
Object[] state = event.nativeEvent.state

syncHibernateState(state,propertyNames,stampCreatedBy,actor)
}
}

protected stampLastUpdatedBy(entity) {
entity."${stampLastUpdatedBy}" = getActor()
protected stampLastUpdatedBy(entity,actor,event) {
entity."${stampLastUpdatedBy}" = actor

if(usingHibernate){
String[] propertyNames = event.nativeEvent.getPersister().getEntityMetamodel().getPropertyNames();
Object[] state = event.nativeEvent.state

syncHibernateState(state,propertyNames,stampCreatedBy,entity."${stampCreatedBy}")
syncHibernateState(state,propertyNames,stampLastUpdatedBy,actor)
}
}

private void syncHibernateState(Object[] state,String[] propertyNames,String propertyName,Object value){
int index = ArrayUtils.indexOf(propertyNames, propertyName);
if(index>=0){
state[index] = value
}
}

/**
* We must use the preDelete event if we want to capture
* what the old object was like.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,16 @@ When called, the event handlers have access to oldObj and newObj definitions tha
boolean dataStoreDisabled = datastore.hasProperty("config") ? datastore.config.auditLog.disabled : false
// Don't register the listener if we are disabled
if (!disabled && !dataStoreDisabled) {
boolean isHibernateDataStore = datastore.class.simpleName == 'HibernateDatastore'

def listener = new AuditLogListener(datastore)
listener.grailsApplication = application
listener.stampEnabled = stampEnabled
listener.stampAlways = stampAlways
listener.stampCreatedBy = stampCreatedBy
listener.stampLastUpdatedBy = stampLastUpdatedBy
listener.verbose = verbose
listener.usingHibernate = isHibernateDataStore
listener.nonVerboseDelete = nonVerboseDelete
listener.logFullClassName = logFullClassName
listener.transactional = transactional
Expand All @@ -124,7 +127,7 @@ When called, the event handlers have access to oldObj and newObj definitions tha
if (!conf || !conf.active) {
return
}

log.trace 'onConfigChange'
}
}

0 comments on commit 062ffb4

Please sign in to comment.