Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

embedded properties #111

Open
phibo23 opened this issue Dec 11, 2015 · 7 comments
Open

embedded properties #111

phibo23 opened this issue Dec 11, 2015 · 7 comments

Comments

@phibo23
Copy link

phibo23 commented Dec 11, 2015

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.

@robertoschwald
Copy link
Contributor

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?

@msg4ashish
Copy link

msg4ashish commented Oct 27, 2016

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.

@robertoschwald
Copy link
Contributor

please try version 2.0.2

@msg4ashish
Copy link

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.

@robertoschwald
Copy link
Contributor

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

Please use the same Grails version.

@robertoschwald
Copy link
Contributor

any news on this @msg4ashis ?

@mb0rn
Copy link
Contributor

mb0rn commented May 31, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants