-
Notifications
You must be signed in to change notification settings - Fork 11
/
ConstraintsGrailsPlugin.groovy
160 lines (136 loc) · 6.42 KB
/
ConstraintsGrailsPlugin.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright 2009 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 org.codehaus.groovy.grails.validation.ConstrainedProperty
import net.zorched.grails.plugins.validation.GrailsConstraintClass
import org.springframework.beans.factory.config.MethodInvokingFactoryBean
import net.zorched.grails.plugins.validation.ConstraintArtefactHandler
import net.zorched.constraints.UsZipConstraint
import net.zorched.constraints.UsPhoneConstraint
import net.zorched.constraints.SsnConstraint
import net.zorched.constraints.ComparisonConstraint
import net.zorched.grails.plugins.validation.CustomConstraintFactory
class ConstraintsGrailsPlugin {
// the plugin version
def version = "0.8.0"
// the version or versions of Grails the plugin is designed for
def grailsVersion = "2.0 > *"
// the other plugins this plugin depends on
def dependsOn = [:]
// resources that are excluded from plugin packaging
def pluginExcludes = [
"grails-app/views/*.gsp",
"grails-app/views/**/*.gsp",
"grails-app/domain/**/*.groovy",
"grails-app/services/**/*.groovy",
"grails-app/controllers/**/*.groovy",
"grails-app/utils/net/zorched/test/**/*.groovy"
]
def license = "APACHE"
def organization = [ name: "One-Line Fix, LLC", url: "http://www.onelinefix.com" ]
def author = "Geoff Lane"
def authorEmail = "[email protected]"
def title = "Custom domain constraints plugin"
def description = '''
This plugin allows you to create custom domain validations that are applied the same
way as built-in domain constraints.
'''
def developers = [ [ name: "Geoff Lane", email: "[email protected]" ] ]
def issueManagement = [ system: "Github", url: "https://github.com/geofflane/grails-constraints/issues" ]
def scm = [ url: "https://github.com/geofflane/grails-constraints" ]
def documentation = "http://grails.org/plugin/constraints"
// loadAfter just seemed to cause problems. It needs to load early.
// def loadAfter = ['core', 'hibernate', 'controllers']
// We can add provided constraints in this plugin using this
def providedArtefacts = [
ComparisonConstraint,
SsnConstraint,
UsPhoneConstraint,
UsZipConstraint
]
def artefacts = [new ConstraintArtefactHandler()]
def watchedResources = [
"file:./grails-app/utils/*Constraint.groovy"
]
def doWithSpring = {
// TODO Implement runtime spring config (optional)
application.constraintClasses.each {constraintClass ->
configureConstraintBeans.delegate = delegate
configureConstraintBeans(constraintClass)
}
}
def doWithDynamicMethods = { applicationContext ->
// TODO Implement registering dynamic methods to classes (optional)
application.constraintClasses.each {constraintClass ->
setupConstraintProperties(constraintClass)
registerConstraint(applicationContext, constraintClass)
}
}
def onChange = { event ->
// TODO Implement code that is executed when any artefact that this plugin is
// watching is modified and reloaded. The event contains: event.source,
// event.application, event.manager, event.ctx, and event.plugin.
if (application.isArtefactOfType(ConstraintArtefactHandler.TYPE, event.source)) {
def artefactClass = application.addArtefact(ConstraintArtefactHandler.TYPE, event.source)
setupConstraintProperties(artefactClass)
registerConstraint(event.ctx, artefactClass)
}
}
/**
* Register the beans with Spring so that we can inject them later.
* Not sure if this is really needed.
*/
def configureConstraintBeans = {GrailsConstraintClass constraintClass ->
// XXX: Not convinced this does anything
def fullName = constraintClass.fullName
"${fullName}Class"(MethodInvokingFactoryBean) {
targetObject = ref("grailsApplication", true)
targetMethod = "getArtefact"
arguments = [ConstraintArtefactHandler.TYPE, constraintClass.fullName]
}
"${fullName}"(ref("${fullName}Class")) {bean ->
bean.factoryMethod = "newInstance"
bean.autowire = true
}
}
/**
* Setup properties on the custom Constraints to make extra information available to them
*/
def setupConstraintProperties = { constraintClass ->
Object params = null
Object hibernateTemplate = null
Object constraintOwningClass = null
String constraintPropertyName = null
constraintClass.clazz.metaClass {
setParams = {val -> params = val}
getParams = {-> return params}
setHibernateTemplate = {val -> hibernateTemplate = val}
getHibernateTemplate = {-> return hibernateTemplate}
setConstraintOwningClass = {val -> constraintOwningClass = val}
getConstraintOwningClass = {-> return constraintOwningClass}
setConstraintPropertyName = {val -> constraintPropertyName = val}
getConstraintPropertyName = {-> return constraintPropertyName}
}
}
static void registerConstraint(ctx, constraintClass) {
def constraintName = constraintClass.name
// Removing the constraint is safe, as any current classes will continue to hold onto the old custom constraint
// factory reference, which in turn holds on to the DefaultGrailsConstraintClass class (which has already been
// auto reloaded in the case of a change event). So really, this just makes sure we don't keep creating
// CustomConstraintFactories every time an existing constraint is modified.
ConstrainedProperty.removeConstraint(constraintName, CustomConstraintFactory)
ConstrainedProperty.registerNewConstraint(constraintName, new CustomConstraintFactory(constraintClass, ctx))
}
}