Skip to content

Commit

Permalink
Merge pull request #195 from felixscheinost/feature/194_auditable_han…
Browse files Browse the repository at this point in the history
…dle_composite_id

Handle composite ID
  • Loading branch information
robertoschwald authored Sep 19, 2019
2 parents 8e3cc45 + 52484ca commit b861b3a
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 25 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: groovy
dist: trusty
sudo: false
cache:
directories:
Expand Down
19 changes: 19 additions & 0 deletions examples/audit-test/grails-app/domain/test/CompositeId.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package test

import grails.plugins.orm.auditable.Auditable

class CompositeId implements Auditable, Serializable {

Author author
String string
NonAuditableCompositeId nonAuditableCompositeId

String notIdString

static constraints = {
}

static mapping = {
id composite:['author', 'string', 'nonAuditableCompositeId']
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package test

class NonAuditableCompositeId implements Serializable {

String foo
String bar

@Override
String toString() {
"toString_for_non_auditable_${foo}_${bar}"
}

static constraints = {
}

static mapping = {
id composite: ['foo', 'bar']
}
}
20 changes: 20 additions & 0 deletions examples/audit-test/grails-app/domain/test/TestEntity.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package test

import grails.plugins.orm.auditable.Auditable

@SuppressWarnings("GroovyUnusedDeclaration")
class TestEntity implements Auditable {
String property
String otherProperty
String anotherProperty

// Just for testing
Serializable ident() {
"id"
}

@Override
String toString() {
property
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import grails.core.GrailsApplication
import grails.gorm.transactions.Rollback
import grails.plugins.orm.auditable.AuditEventType
import grails.plugins.orm.auditable.AuditLogContext
import grails.plugins.orm.auditable.Auditable
import grails.plugins.orm.auditable.resolvers.AuditRequestResolver
import grails.spring.BeanBuilder
import grails.testing.mixin.integration.Integration
import org.grails.datastore.gorm.GormEntity
import org.springframework.test.annotation.DirtiesContext
import spock.lang.Shared
import spock.lang.Specification
Expand Down Expand Up @@ -190,25 +188,23 @@ class AuditableSpec extends Specification {
uri == "http://foo.com"
actor == "Aaron"
}
}

@SuppressWarnings("GroovyUnusedDeclaration")
class TestEntity implements Auditable, GormEntity<TestEntity> {
String property
String otherProperty
String anotherProperty

// Just for testing
Serializable ident() {
"id"
}
void "composite ids are handled correctly"() {
when:
Author author = new Author(name: "Aaron", age: 37, famous: true).save(flush:true)
CompositeId compositeId = new CompositeId(
author: author,
string: "string",
nonAuditableCompositeId: new NonAuditableCompositeId(foo:"foo", bar:"bar")
)

@Override
String toString() {
property
then:
compositeId.logEntityId == "[author:$author.id, string:string, nonAuditableCompositeId:toString_for_non_auditable_foo_bar]"
}
}



class TestAuditRequestResolver implements AuditRequestResolver {
@Override
String getCurrentActor() {
Expand Down
8 changes: 3 additions & 5 deletions plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ buildscript {
classpath 'org.asciidoctor:asciidoctor-gradle-plugin:1.5.3'
classpath 'org.asciidoctor:asciidoctorj-epub3:1.5.0-alpha.6'
classpath 'org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.11'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'
}
}

plugins {
id "com.jfrog.bintray" version "1.4"
}

version file("$rootDir/version.txt").text.trim()
version project.file("../version.txt").text.trim()
group "org.grails.plugins"

apply plugin: "eclipse"
Expand All @@ -27,6 +24,7 @@ apply plugin: "org.grails.grails-plugin"
apply plugin: "org.grails.grails-plugin-publish"
apply plugin: "org.grails.grails-gsp"
apply plugin: "org.asciidoctor.convert"
apply plugin: 'com.jfrog.bintray'

// Used for publishing to central repository, remove if not needed
// apply from: 'https://raw.githubusercontent.com/grails/grails-profile-repository/master/profiles/plugin/templates/grailsCentralPublishing.gradle'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import grails.plugins.orm.auditable.resolvers.AuditRequestResolver
import grails.util.GrailsNameUtils
import grails.util.Holders
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.grails.datastore.gorm.GormEntity
import org.grails.datastore.mapping.dirty.checking.DirtyCheckable
import org.grails.datastore.mapping.model.PersistentEntity
import org.grails.datastore.mapping.model.PersistentProperty
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import javax.persistence.Transient

import static grails.plugins.orm.auditable.AuditLogListenerUtil.makeMap
/**
* Domain classes should implement this trait to provide auditing support
*/
Expand Down Expand Up @@ -152,8 +153,40 @@ trait Auditable {
String getLogEntityId() {
log.debug("getLogEntityId()")
if (this instanceof GormEntity) {
PersistentEntity persistentEntity = (PersistentEntity) getClass().invokeMethod("getGormPersistentEntity", null)
log.debug(" this instanceof GormEntity")
return convertLoggedPropertyToString("id", ((GormEntity)this).ident())
if (persistentEntity.identity != null) {
return convertLoggedPropertyToString("id", ((GormEntity)this).ident())
}
else {
// Fetch composite ID values
PersistentProperty[] idProperties = persistentEntity.compositeIdentity
Map<String, Object> map = makeMap(idProperties*.name, this)

// Build a string representation of this class that looks like: [<persistent id property>:<value or id>]
StringBuilder stringBuilder = new StringBuilder()
stringBuilder.append("[")
map.eachWithIndex { Map.Entry<String, Object> entry, int i ->
stringBuilder.append(entry.key)
stringBuilder.append(":")
switch (entry.value) {
case Auditable:
stringBuilder.append(((Auditable) entry.value).logEntityId)
break
case GormEntity:
stringBuilder.append(((GormEntity) entry.value).ident().toString())
break
default:
stringBuilder.append(convertLoggedPropertyToString(entry.key, entry.value))
break
}
if (i != (map.size() - 1)) {
stringBuilder.append(", ")
}
}
stringBuilder.append("]")
return stringBuilder.toString()
}
}
if (this.respondsTo("getId")) {
log.debug(" this respondsTo getId")
Expand Down

0 comments on commit b861b3a

Please sign in to comment.