在 Grails Audit Loggin 插件的 onChange 方法中,如何获取对所属可审核域对象的引用?

发布于 2024-12-04 18:02:21 字数 1481 浏览 1 评论 0 原文

我已经成功地让 grails 审核日志记录插件正常工作,看起来正是我所需要的,只是我无法弄清楚如何从 onChange 方法中获取对可审核域对象的引用。下面是插件示例 Person 类的代码,还有几行我想要实现的目标:

class Person {

   static auditable = true 
   Long id 
   Long version

   String firstName 
   String middleName 
   String lastName

   String email

   static hasMany = [emailRecords : EmailRecord]    
   static constraints = { 
      firstName(nullable:true,size:0..60) 
      middleName(nullable:true,size:0..60) 
      lastName(nullable:false,size:1..60) 
      email(email:true) 
   }

   def onSave = { 
      println "new person inserted" // may optionally refer to newState map 
   } 

   def onDelete = { 
      println "person was deleted" // may optionally refer to oldState map 
   } 

   def onChange = { 
     oldMap,newMap -> 
        println "Person was changed ..." 
        oldMap.each({ key, oldVal -> 
           if(oldVal != newMap[key]) { 
              println " * $key changed from $oldVal to " + newMap[key] 
              // how can achieve something like this?
              if(key == "email"){
                 def personInstance = this // this didn't work, I'm not sure how I can get such a reference to the owning domain object
                 personInstance.addToEmailRecords(
                    new EmailRecord(
                       email:newMap[key],
                       date: new Date()
                    ).save()
                 )
              }
           } 
        }) 
     }
   }

I've successfully gotten the grails audit logging plugin to work, looks like exactly what I need except I can't figure out how to get a reference to the auditable domain object from within the onChange method. Below is the code from the plugin's example Person class, with a few additional lines of what I'm trying to achieve:

class Person {

   static auditable = true 
   Long id 
   Long version

   String firstName 
   String middleName 
   String lastName

   String email

   static hasMany = [emailRecords : EmailRecord]    
   static constraints = { 
      firstName(nullable:true,size:0..60) 
      middleName(nullable:true,size:0..60) 
      lastName(nullable:false,size:1..60) 
      email(email:true) 
   }

   def onSave = { 
      println "new person inserted" // may optionally refer to newState map 
   } 

   def onDelete = { 
      println "person was deleted" // may optionally refer to oldState map 
   } 

   def onChange = { 
     oldMap,newMap -> 
        println "Person was changed ..." 
        oldMap.each({ key, oldVal -> 
           if(oldVal != newMap[key]) { 
              println " * $key changed from $oldVal to " + newMap[key] 
              // how can achieve something like this?
              if(key == "email"){
                 def personInstance = this // this didn't work, I'm not sure how I can get such a reference to the owning domain object
                 personInstance.addToEmailRecords(
                    new EmailRecord(
                       email:newMap[key],
                       date: new Date()
                    ).save()
                 )
              }
           } 
        }) 
     }
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乞讨 2024-12-11 18:02:21

对于这个用例,您可能真的只想使用 标准 GORM 事件 使用 isDirty()getPersistentValue() 至少进行更新。特别是,如审计日志插件文档中所述,它旨在在实体提交到数据存储后对其进行操作(因此,例如,保证分配对象 ID)。

尝试如下操作:

class Person {
    // blah, blah, blah
    def beforeInsert = {
        if (email) {
            addToEmailRecords(new EmailRecord(email: email, date: new Date()))
        }
    }

    def beforeUpdate = {
        if (isDirty("email")) {
            addToEmailRecords(new EmailRecord(email: email, date: new Date()))
        }
    }
}

这样,在提交更改后,您就不应该因修改对象而遇到任何麻烦。

For this use-case, you probably really just want to use the standard GORM events using isDirty() and getPersistentValue() at least to make the updates. In particular, as noted in the documentation for the audit-logging plugin, it is designed to work on entities after they have been committed to the data store (and so, for example, the object id is guaranteed to be assigned).

Try something like the following:

class Person {
    // blah, blah, blah
    def beforeInsert = {
        if (email) {
            addToEmailRecords(new EmailRecord(email: email, date: new Date()))
        }
    }

    def beforeUpdate = {
        if (isDirty("email")) {
            addToEmailRecords(new EmailRecord(email: email, date: new Date()))
        }
    }
}

That way you shouldn't hit into any funkiness with modifying the object after it's changes have been committed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文