{"id":5016,"date":"2012-01-23T13:22:38","date_gmt":"2012-01-23T07:52:38","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=5016"},"modified":"2012-01-23T13:27:39","modified_gmt":"2012-01-23T07:57:39","slug":"extending-audit-logging-plugin-to-track-changes-to-persistent-collections","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/extending-audit-logging-plugin-to-track-changes-to-persistent-collections\/","title":{"rendered":"Extending Audit Logging Plugin to track changes to Persistent Collections"},"content":{"rendered":"<p style=\"padding-bottom: 10px\">In one of our project we needed to maintain history of domain objects when they are updated. We saw <a href=\"http:\/\/grails.org\/plugin\/audit-logging\" target=\"_blank\">Grails Audit Logging Plugin <\/a> as a good candidate. But later, found that it doesn&#8217;t take care of persistent collections. So with help of my colleague <a href=\"http:\/\/www.tothenew.com\/blog\/author\/vivek\/\" target=\"_blank\">Vivek<\/a> and this <a href=\"http:\/\/stackoverflow.com\/questions\/812364\/how-to-determine-collection-changes-in-a-hibernate-postupdateeventlistener\" target=\"_blank\">Stack Overflow thread<\/a>, we extended this plugin without making it inline, to handle this limitation.<\/p>\n<p style=\"padding-bottom: 10px\">Audit Logging plugin provides a bean named auditLogListener to handle Hibernate events and provide handlers in Grails Domain classes with old values map and new values map. So what we have to do is create a class named CustomAuditLogListener which extends AuditLogListener from the plugin and overrides the onPostUpdate() method. Implemetation for this class is:<\/p>\n<p>[code]<br \/>\nimport org.codehaus.groovy.grails.plugins.orm.auditable.AuditLogListener<br \/>\nimport org.hibernate.collection.PersistentCollection<br \/>\nimport org.hibernate.engine.CollectionEntry<br \/>\nimport org.hibernate.engine.PersistenceContext<br \/>\nimport org.hibernate.event.PostUpdateEvent<\/p>\n<p>class CustomAuditLogListener extends AuditLogListener {<\/p>\n<p>    @Override<br \/>\n    void onPostUpdate(final PostUpdateEvent event) {<br \/>\n        if (isAuditableEntity(event)) {<br \/>\n            log.trace &quot;${event.getClass()} onChange handler has been called&quot;<br \/>\n            onChange(event)<br \/>\n        }<br \/>\n    }<\/p>\n<p>    private void onChange(final PostUpdateEvent event) {<br \/>\n        def entity = event.getEntity()<br \/>\n        String entityName = entity.getClass().getName()<br \/>\n        def entityId = event.getId()<\/p>\n<p>        \/\/ object arrays representing the old and new state<br \/>\n        def oldState = event.getOldState()<br \/>\n        def newState = event.getState()<\/p>\n<p>        List&lt;String&gt; propertyNames = event.getPersister().getPropertyNames()<br \/>\n        Map oldMap = [:]<br \/>\n        Map newMap = [:]<\/p>\n<p>        if (propertyNames) {<br \/>\n            for (int index = 0; index &lt; newState.length; index++) {<br \/>\n                if (propertyNames[index]) {<br \/>\n                    if (oldState) {<br \/>\n                        populateOldStateMap(oldState, oldMap, propertyNames[index], index)<br \/>\n                    }<br \/>\n                    if (newState) {<br \/>\n                        newMap[propertyNames[index]] = newState[index]<br \/>\n                    }<br \/>\n                }<br \/>\n            }<br \/>\n        }<\/p>\n<p>        if (!significantChange(entity, oldMap, newMap)) {<br \/>\n            return<br \/>\n        }<\/p>\n<p>        \/\/ allow user&#8217;s to over-ride whether you do auditing for them.<br \/>\n        if (!callHandlersOnly(event.getEntity())) {<br \/>\n            logChanges(newMap, oldMap, event, entityId, &#8216;UPDATE&#8217;, entityName)<br \/>\n        }<br \/>\n         executeHandler(event, &#8216;onChange&#8217;, oldMap, newMap)<br \/>\n        return<br \/>\n    }<\/p>\n<p>    private populateOldStateMap(def oldState, Map oldMap, String keyName, index) {<br \/>\n        def oldPropertyState = oldState[index]<br \/>\n        if (oldPropertyState instanceof PersistentCollection) {<br \/>\n            PersistentCollection pc = (PersistentCollection) oldPropertyState;<br \/>\n            PersistenceContext context = sessionFactory.getCurrentSession().getPersistenceContext();<br \/>\n            CollectionEntry entry = context.getCollectionEntry(pc);<br \/>\n            Object snapshot = entry.getSnapshot();<br \/>\n            if (pc instanceof List) {<br \/>\n                oldMap[keyName] = Collections.unmodifiableList((List) snapshot);<br \/>\n            }<br \/>\n            else if (pc instanceof Map) {<br \/>\n                oldMap[keyName] = Collections.unmodifiableMap((Map) snapshot);<br \/>\n            }<br \/>\n            else if (pc instanceof Set) {<br \/>\n                \/\/Set snapshot is actually stored as a Map<br \/>\n                Map snapshotMap = (Map) snapshot;<br \/>\n                oldMap[keyName] = Collections.unmodifiableSet(new HashSet(snapshotMap.values()));<br \/>\n            }<br \/>\n            else {<br \/>\n                oldMap[keyName] = pc;<br \/>\n            }<br \/>\n        } else {<br \/>\n            oldMap[keyName] = oldPropertyState<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n[\/code]<\/p>\n<p style=\"padding-bottom: 10px\">Now we need to register CustomAuditLogListener class as implementation for auditLogListener which will be done in resources.groovy. The bean has to be defined in resources.groovy as:<\/p>\n<p>[code]<br \/>\n auditLogListener(CustomAuditLogListener) {<br \/>\n        sessionFactory   = ref(&#8216;sessionFactory&#8217;)<br \/>\n        verbose          = application.config?.auditLog?.verbose?:false<br \/>\n        transactional    = application.config?.auditLog?.transactional?:false<br \/>\n        sessionAttribute = application.config?.auditLog?.sessionAttribute?:&quot;&quot;<br \/>\n        actorKey         = application.config?.auditLog?.actorKey?:&quot;&quot;<br \/>\n    }<br \/>\n[\/code]<\/p>\n<p style=\"padding-bottom: 10px\">Now we will be able to fetch older values for persistent collections in onChange handler as <a href=\"http:\/\/grails.org\/plugin\/audit-logging\" target=\"_blank\">documented in plugin documentation.<\/a><\/p>\n<p style=\"padding-bottom: 10px\">Hope you find this helpful.<\/p>\n<p>Ankur Tripathi<br \/>\nankur@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In one of our project we needed to maintain history of domain objects when they are updated. We saw Grails Audit Logging Plugin as a good candidate. But later, found that it doesn&#8217;t take care of persistent collections. So with help of my colleague Vivek and this Stack Overflow thread, we extended this plugin without [&hellip;]<\/p>\n","protected":false},"author":24,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":16},"categories":[7],"tags":[751],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/5016"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/24"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=5016"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/5016\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=5016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=5016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=5016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}