forked from limcheekin/activiti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActivitiGrailsPlugin.groovy
264 lines (225 loc) · 11.5 KB
/
ActivitiGrailsPlugin.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* Copyright 2010-2012 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.activiti.engine.runtime.ProcessInstance
import org.activiti.engine.task.Task
import grails.util.GrailsNameUtils
import grails.util.Environment
import org.codehaus.groovy.grails.commons.ConfigurationHolder as CH
import org.codehaus.groovy.grails.commons.ControllerArtefactHandler
import org.springframework.core.io.Resource
import org.grails.activiti.ActivitiConstants
import org.grails.activiti.serializable.SerializableVariableType
/**
*
* @author <a href='mailto:[email protected]'>Lim Chee Kin</a>
*
* @since 5.0.alpha3
*/
class ActivitiGrailsPlugin {
// the plugin version
def version = "5.12.1"
// the version or versions of Grails the plugin is designed for
def grailsVersion = "2.0.0 > *"
// the other plugins this plugin depends on
def dependsOn = [:]
// resources that are excluded from plugin packaging
def pluginExcludes = [
"grails-app/views/error.gsp"
]
def author = "Lim Chee Kin"
def authorEmail = "[email protected]"
def title = "Grails Activiti Plugin - Enabled Activiti BPM Suite support for Grails"
def description = '''
Grails Activiti Plugin is created to integrate Activiti BPM Suite and workflow system to Grails Framework.
With the Grails Activiti Plugin, workflow application can be created at your fingertips!
Project Site and Documentation: http://code.google.com/p/grails-activiti-plugin/
Source Code: https://github.com/limcheekin/activiti
Support: https://github.com/limcheekin/activiti/issues
Discussion Forum: http://groups.google.com/group/grails-activiti-plugin
'''
// URL to the plugin's documentation
def documentation = "http://grails.org/plugin/activiti"
def license = "APACHE"
def issueManagement = [system: 'GitHub', url: 'https://github.com/limcheekin/activiti/issues']
def scm = [url: 'https://github.com/limcheekin/activiti']
def confWatchedResources = [CH.config.activiti.deploymentResources, "file:./grails-app/controllers/**/*.groovy"].flatten()
def defaultWatchedResources = [ActivitiConstants.DEFAULT_DEPLOYMENT_RESOURCES, "file:./grails-app/controllers/**/*.groovy"].flatten()
def watchedResources = confWatchedResources?:defaultWatchedResources
String sessionUsernameKey = CH.config.activiti.sessionUsernameKey?:ActivitiConstants.DEFAULT_SESSION_USERNAME_KEY
boolean useFormKey = CH.config.activiti.useFormKey?:ActivitiConstants.DEFAULT_USE_FORM_KEY
def doWithWebDescriptor = { xml ->
// TODO Implement additions to web.xml (optional), this event occurs before
}
def doWithSpring = {
def disabledActiviti = System.getProperty("disabledActiviti")
if (!disabledActiviti && !CH.config.activiti.disabled) {
println "Configuring Activiti Process Engine ..."
processEngineConfiguration(org.activiti.spring.SpringProcessEngineConfiguration) {
processEngineName = CH.config.activiti.processEngineName?:ActivitiConstants.DEFAULT_PROCESS_ENGINE_NAME
databaseType = CH.config.activiti.databaseType?:ActivitiConstants.DEFAULT_DATABASE_TYPE
databaseSchemaUpdate = CH.config.activiti.databaseSchemaUpdate ? CH.config.activiti.databaseSchemaUpdate.toString() : ActivitiConstants.DEFAULT_DATABASE_SCHEMA_UPDATE
deploymentName = CH.config.activiti.deploymentName?:ActivitiConstants.DEFAULT_DEPLOYMENT_NAME
deploymentResources = CH.config.activiti.deploymentResources?:ActivitiConstants.DEFAULT_DEPLOYMENT_RESOURCES
jobExecutorActivate = CH.config.activiti.jobExecutorActivate?:ActivitiConstants.DEFAULT_JOB_EXECUTOR_ACTIVATE
history = CH.config.activiti.history?:ActivitiConstants.DEFAULT_HISTORY
mailServerHost = CH.config.activiti.mailServerHost?:ActivitiConstants.DEFAULT_MAIL_SERVER_HOST
mailServerPort = CH.config.activiti.mailServerPort?:ActivitiConstants.DEFAULT_MAIL_SERVER_PORT
mailServerUsername = CH.config.activiti.mailServerUsername
mailServerPassword = CH.config.activiti.mailServerPassword
mailServerDefaultFrom = CH.config.activiti.mailServerDefaultFrom?:ActivitiConstants.DEFAULT_MAIL_SERVER_FROM
dataSource = ref("dataSource")
transactionManager = ref("transactionManager")
// Define custom serializable types for fix issue with serialization
customPreVariableTypes = [new SerializableVariableType()]
}
processEngine(org.activiti.spring.ProcessEngineFactoryBean) {
processEngineConfiguration = ref("processEngineConfiguration")
}
runtimeService(processEngine:"getRuntimeService")
repositoryService(processEngine:"getRepositoryService")
taskService(processEngine:"getTaskService")
managementService(processEngine:"getManagementService")
identityService(processEngine:"getIdentityService")
historyService(processEngine:"getHistoryService")
formService(processEngine:"getFormService")
activitiService(org.grails.activiti.ActivitiService) {
runtimeService = ref("runtimeService")
taskService = ref("taskService")
identityService = ref("identityService")
formService = ref("formService")
}
println "... finished configuring Activiti Process Engine."
}
}
def doWithDynamicMethods = { ctx ->
application.controllerClasses.each { controllerClass ->
if (controllerClass.hasProperty("activiti") && controllerClass.clazz.activiti) {
controllerClass.metaClass.getActivitiService = {-> return ctx.activitiService}
// addActivitiActions(controllerClass) Not possible, find out more at URL below:
// http://archive.jrcs.codehaus.org/lists/org.codehaus.grails.dev/msg/[email protected]
addActivitiMethods(controllerClass)
}
}
}
def addActivitiMethods(controllerClass) {
controllerClass.metaClass.start = { Map params ->
activitiService.with {
params[sessionUsernameKey] = session[sessionUsernameKey]
ProcessInstance pi = startProcess(params)
Task task = getUnassignedTask(session[sessionUsernameKey], pi.id)
claimTask(task.id, session[sessionUsernameKey])
redirect uri:getTaskFormUri(task.id, useFormKey)
}
}
controllerClass.metaClass.startTask = { String taskId ->
activitiService.with {
claimTask(taskId, session[sessionUsernameKey])
redirect uri:getTaskFormUri(taskId, useFormKey)
}
}
controllerClass.metaClass.getForm = { String taskId ->
redirect uri:activitiService.getTaskFormUri(taskId, useFormKey)
}
controllerClass.metaClass.saveTask = { Map params ->
params.domainClassName = getDomainClassName(delegate)
activitiService.setTaskFormUri(params)
}
controllerClass.metaClass.completeTask = { Map params ->
params.domainClassName = getDomainClassName(delegate)
activitiService.completeTask(params.taskId, params)
}
controllerClass.metaClass.claimTask = { String taskId ->
activitiService.claimTask(taskId, session[sessionUsernameKey])
}
controllerClass.metaClass.revokeTask = { String taskId ->
activitiService.claimTask(taskId, null)
}
controllerClass.metaClass.deleteTask = { String taskId, String domainClassName = null ->
if (!domainClassName && delegate.class != org.grails.activiti.TaskController) {
domainClassName = getDomainClassName(delegate)
}
activitiService.deleteTask(taskId, domainClassName)
}
controllerClass.metaClass.setAssignee = { String taskId, String username ->
if (username) {
activitiService.setAssignee(taskId, username)
} else {
revokeTask(taskId)
}
}
controllerClass.metaClass.setPriority = { String taskId, int priority ->
activitiService.setPriority(taskId, priority)
}
controllerClass.metaClass.getUnassignedTasksCount = {->
activitiService.getUnassignedTasksCount(session[sessionUsernameKey])
}
controllerClass.metaClass.getAssignedTasksCount = {->
activitiService.getAssignedTasksCount(session[sessionUsernameKey])
}
controllerClass.metaClass.getAllTasksCount = {->
activitiService.getAllTasksCount()
}
controllerClass.metaClass.findUnassignedTasks = { Map params ->
params[sessionUsernameKey] = session[sessionUsernameKey]
if (!params.sort) {
params.sort = "createTime"
params.order = "desc"
}
activitiService.findUnassignedTasks(params)
}
controllerClass.metaClass.findAssignedTasks = { Map params ->
params[sessionUsernameKey] = session[sessionUsernameKey]
if (!params.sort) {
params.sort = "createTime"
params.order = "desc"
}
activitiService.findAssignedTasks(params)
}
controllerClass.metaClass.findAllTasks = { Map params ->
if (!params.sort) {
params.sort = "createTime"
params.order = "desc"
}
activitiService.findAllTasks(params)
}
}
private getDomainClassName(delegate) {
return "${delegate.class.package.name}.${GrailsNameUtils.getLogicalName(delegate.class.name, 'Controller')}"
}
def doWithApplicationContext = { applicationContext ->
// TODO Implement post initialization spring config (optional)
}
def onChange = { event ->
println "Reloading ${event.source}..."
if (!(event.source instanceof Resource)) {
if(application.isControllerClass(event.source)) {
def controllerClass = application.addArtefact(ControllerArtefactHandler.TYPE, event.source)
if (controllerClass.hasProperty("activiti") && controllerClass.clazz.activiti) {
controllerClass.metaClass.getActivitiService = {-> return event.ctx.activitiService}
addActivitiMethods(controllerClass)
}
}
} else { // it is org.springframework.core.io.Resource
event.ctx.repositoryService.createDeployment()
.name(ActivitiConstants.PLUGIN_AUTO_DEPLOYMENT_NAME)
.addInputStream(event.source.filename, event.source.inputStream)
.deploy()
}
}
def onConfigChange = { event ->
// TODO Implement code that is executed when the project configuration changes.
// The event is the same as for 'onChange'.
}
}