-
Notifications
You must be signed in to change notification settings - Fork 23
/
EnversGrailsPlugin.groovy
110 lines (92 loc) · 4.23 KB
/
EnversGrailsPlugin.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import net.lucasward.grails.plugin.EnversPluginSupport
import net.lucasward.grails.plugin.RevisionsOfEntityQueryMethod
import org.codehaus.groovy.grails.commons.GrailsApplication
import org.codehaus.groovy.grails.commons.GrailsDomainClass
import org.codehaus.groovy.grails.orm.hibernate.HibernateEventListeners
import org.hibernate.SessionFactory
import net.lucasward.grails.plugin.AuditEventListenerForDefaultDatasource
class EnversGrailsPlugin {
// the plugin version
def version = "2.1.0"
// the version or versions of Grails the plugin is designed for
def grailsVersion = "2.1.0 > *"
// the other plugins this plugin depends on
def observe = ['hibernate']
def loadAfter = ['hibernate']
// resources that are excluded from plugin packaging
def pluginExcludes = [
"grails-app/views/error.gsp",
"grails-app/domain/**",
"src/groovy/net/lucasward/grails/plugin/StubSpringSecurityService.groovy",
"src/groovy/net/lucasward/grails/plugin/SpringSecurityRevisionListener.groovy",
"src/groovy/net/lucasward/grails/plugin/SpringSecurityServiceHolder.groovy",
"src/java/net/lucasward/grails/plugin/Book.java",
"src/java/net/lucasward/grails/plugin/UserRevisionEntity.java",
"grails-app/conf/hibernate/hibernate.cfg.xml",
"web-app/**"
]
def author = "Lucas Ward, Jay Hogan"
def authorEmail = ""
def title = "Grails Envers Plugin"
def description = 'Plugin to integrate grails with Hibernate Envers'
// URL to the plugin's documentation
def documentation = "http://grails.org/plugin/envers"
def doWithWebDescriptor = { xml ->
}
def doWithSpring = {
auditEventListenerForDefaultDatasource(AuditEventListenerForDefaultDatasource)
hibernateEventListeners(HibernateEventListeners) {
listenerMap = [
'post-insert': auditEventListenerForDefaultDatasource,
'post-update': auditEventListenerForDefaultDatasource,
'post-delete': auditEventListenerForDefaultDatasource,
'pre-collection-update': auditEventListenerForDefaultDatasource,
'pre-collection-remove': auditEventListenerForDefaultDatasource,
'post-collection-recreate': auditEventListenerForDefaultDatasource
]
}
}
def doWithDynamicMethods = { ctx ->
for (entry in ctx.getBeansOfType(SessionFactory)) {
SessionFactory sessionFactory = entry.value
registerDomainMethods(application, sessionFactory)
}
}
private def registerDomainMethods(GrailsApplication application, SessionFactory sessionFactory) {
application.domainClasses.each { GrailsDomainClass gc ->
def getAllRevisions = new RevisionsOfEntityQueryMethod(sessionFactory, gc.clazz)
if (EnversPluginSupport.isAudited(gc)) {
MetaClass mc = gc.getMetaClass()
mc.static.findAllRevisions = {
getAllRevisions.query(null, null, [:])
}
mc.static.findAllRevisions = { Map parameters ->
getAllRevisions.query(null, null, parameters)
}
EnversPluginSupport.generateFindAllMethods(gc, sessionFactory)
EnversPluginSupport.generateAuditReaderMethods(gc, sessionFactory)
}
}
}
def doWithApplicationContext = { applicationContext ->
}
def onChange = { event ->
}
def onConfigChange = { event ->
}
}