2 * Copyright 2004-2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.codehaus
.groovy
.grails
.plugins
.scaffolding
;
18 import org
.codehaus
.groovy
.grails
.commons
.GrailsClassUtils as GCU
19 import org
.codehaus
.groovy
.grails
.commons
.DomainClassArtefactHandler
20 import org
.codehaus
.groovy
.grails
.scaffolding
.*
23 * A plug-in that handles the configuration of Hibernate within Grails
25 * @author Graeme Rocher
28 class ScaffoldingGrailsPlugin
{
30 def version
= grails
.util
.GrailsUtil
.getGrailsVersion()
31 def dependsOn
= [hibernate
:version
, controllers
:version
]
32 def observe
= ['controllers']
34 static BEAN_DEFINITIONS
= { scaffoldClass
, controllerClass
->
35 "${scaffoldClass.name}Domain"( GrailsScaffoldDomain
,
37 ref("sessionFactory"))
38 // setup the default response handler that simply delegates to a view
39 "${scaffoldClass.name}ResponseHandler"(TemplateGeneratingResponseHandler
) {
40 templateGenerator
= {DefaultGrailsTemplateGenerator bean
->}
41 viewResolver
= ref("jspViewResolver")
42 grailsApplication
= ref("grailsApplication", true)
43 scaffoldedClass
= scaffoldClass
45 // setup a response handler factory which can be used to output different
46 // responses based on the model returned by the scaffold domain
48 "${scaffoldClass.name}ResponseHandlerFactory"( DefaultGrailsResponseHandlerFactory
,
49 ref("grailsApplication",true),
50 ref("${scaffoldClass.name}ResponseHandler") )
52 "${controllerClass.fullName}Scaffolder" (DefaultGrailsScaffolder
) {
53 scaffoldRequestHandler
= { DefaultScaffoldRequestHandler dsrh
->
54 scaffoldDomain
= ref("${scaffoldClass.name}Domain")
56 scaffoldResponseHandlerFactory
= ref( "${scaffoldClass.name}ResponseHandlerFactory")
62 application
.controllerClasses
.each
{ controller
->
63 log
.debug("Checking controller ${controller.name} for scaffolding settings")
65 if(controller
.isScaffolding()) {
66 def scaffoldClass
= controller
.scaffoldedClass
68 scaffoldClass
= application
.getArtefact(DomainClassArtefactHandler
.TYPE
, controller
.name
)?
.clazz
75 log
.debug("Configuring scaffolding for class [$scaffoldClass]")
76 def beans
= BEAN_DEFINITIONS
.curry(scaffoldClass
, controller
)
77 beans
.delegate
= delegate
86 * Handles registration of dynamic scaffolded methods
88 def doWithDynamicMethods
= { ctx
->
89 registerScaffoldedActions(application
, ctx
)
90 // configure scaffolders
91 GrailsScaffoldingUtil
.configureScaffolders(application
, ctx
);
94 def
registerScaffoldedActions(application
,ctx
) {
95 application
.controllerClasses
.each
{ controller
->
96 if(controller
.scaffolding
) {
97 if(ctx
.containsBean("${controller.fullName}Scaffolder")) {
98 def scaffolder
= ctx
."${controller.fullName}Scaffolder"
100 scaffolder
.supportedActionNames
.each
{ name
->
101 if(!controller
.hasProperty(name
)) {
102 def getter
= GCU
.getGetterName(name
)
103 controller
.metaClass
."$getter" = {->
104 scaffolder
.getAction(delegate
, name
)
115 def onChange
= { event
->
116 if(application
.isControllerClass(event
.source
)) {
117 def controllerClass
= application
.getControllerClass(event
.source
.name
)
119 if(controllerClass
.scaffolding
) {
120 def scaffoldClass
= controllerClass
.scaffoldedClass
124 scaffoldClass
= application
.getDomainClass(controllerClass
.name
)?
.clazz
129 log
.info
"Re-configuring scaffolding for reloaded class ${event.source}"
131 def beans
= beans(BEAN_DEFINITIONS
.curry(scaffoldClass
,controllerClass
))
133 beans
.registerBeans(ctx
)
135 registerScaffoldedActions(event
.application
, ctx
)
136 // configure scaffolders
137 GrailsScaffoldingUtil
.configureScaffolders(event
.application
, ctx
);