grails-audit-logging-plugin icon indicating copy to clipboard operation
grails-audit-logging-plugin copied to clipboard

embedded properties

Open phibo23 opened this issue 9 years ago • 7 comments

Is there a way to log changes to fields of embedded objects individually? The usual example is probably an Address class with fields such as "street", "city", "zip" etc which is embedded into a User class and something like a Building class. As far as I understand, old value and new value are logging address.toString() for each change to one of the fields inside address.

phibo23 avatar Dec 11 '15 16:12 phibo23

Not yet supported. I'm not sure if the oldValue / newValue maps hold the individual attributes. Need to setup an example project and test, as the embedded domain components are rarely used (I never did and always use associations to avoid data duplication). Have you had a chance to check the oldValue / newValue maps for composition object changes?

robertoschwald avatar May 18 '16 14:05 robertoschwald

Hi,

I am also having similar problem. I have a legacy class that I need to audit. This legacy class also has an embedded object in it.

class Employee { static auditable = [ignore:['lastUpdated']]

String id
String name
String email
String salary

class Address implements Serializable {
    String street
    String hNo
    String city
    String zip
}

}

At DB side, there is a single Employee table which has all columns.

Works fine when any of the Employee properties such as name, email, salary is udpated But when any of the Address properties is updated, it gets logged in audit_log_event table as:

class_name: com.test.web.api.Employee   
event_name: UPDATE  
property_name: pau  
old_value: com.test.web.api.Address : (unsaved) 
new_value: com.test.web.api.Address : (unsaved) 
date_created: 2016-10-27 07:42:33   
last_updated: 2016-10-27 07:42:33

I printed the oldMap and newMap in onChange() method, and the newMap does have the updated Address properties.

Is there a way I can change the newMap values so that it gets reflected in DB as well?

For testing, I tried updating the newMap in onChange() method for any of the simpler property e.g: Employee.name (when name property is getting changed), but that didn't work.

def onChange = { oldMap,newMap -> println "Employee was changed ..."

       oldMap.each({ key, oldVal ->
          if(oldVal != newMap[key]) {
             println " * $key changed from $oldVal to " + newMap[key]

             if(key == "name") {
                 newMap[key] = "UPDATED NAME VALUE***"
                 println "orientation was manually changed ..."
                 println " * $key changed from $oldVal to " + newMap[key]
             }
          }
       })
}

Console Output:

Employee was changed ...

  • name changed from Joe to Smith name was manually changed ...

  • name changed from Joe to UPDATED NAME VALUE***

    However, in DB: old_value = Joe new_value = Smith

I was expecting that new_value will be equal to "UPDATED NAME VALUE***"

Basically, I want to get the correct values getting reflected in database for any domain object update.

msg4ashish avatar Oct 27 '16 09:10 msg4ashish

please try version 2.0.2

robertoschwald avatar Nov 12 '16 19:11 robertoschwald

Thanks a lot Robert for looking into this. However, I am still facing that issue for embedded objects.

In my BuildConfig.groovy, I updated below line:

from compile "org.grails.plugins:audit-logging:1.1.0"

to compile "org.grails.plugins:audit-logging:1.1.1"

I still see below getting logged in database:

class_name: com.test.web.api.Employee   
event_name: UPDATE  
property_name: pau  
old_value: com.test.web.api.Address : (unsaved) 
new_value: com.test.web.api.Address : (unsaved) 
date_created: 2016-10-27 07:42:33   
last_updated: 2016-10-27 07:42:33

Am I doing something wrong? Do I need to do something else to upgrade to newer version which contains the fix.

msg4ashish avatar Nov 14 '16 05:11 msg4ashish

Can you please setup a small sample project on GH, so we can analyze?

Please use the same Grails version.

robertoschwald avatar Nov 14 '16 11:11 robertoschwald

any news on this @msg4ashis ?

robertoschwald avatar Apr 20 '17 14:04 robertoschwald

Hi Robert, Thanks for a great plugin! I am using the static embedded property in combination with Mongo GORM. Unfortunately, only the top level properties get logged. My log table contains an entry com.test.Address : (unsaved). Is there are way I can log the embedded properties as well? Thanks, Matt

mb0rn avatar May 31 '17 09:05 mb0rn