forked from icescrum/iceScrum-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIcescrumCoreGrailsPlugin.groovy
733 lines (666 loc) · 30.7 KB
/
IcescrumCoreGrailsPlugin.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/*
* Copyright (c) 2010 iceScrum Technologies / 2011 Kagilum SAS
*
* This file is part of iceScrum.
*
* iceScrum is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* iceScrum is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with iceScrum. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
*
* Vincent Barrier (vbarrier@kagilum.com)
*/
import grails.converters.JSON
import grails.converters.XML
import org.atmosphere.cpr.BroadcasterFactory
import org.codehaus.groovy.grails.commons.GrailsClassUtils
import org.codehaus.groovy.grails.scaffolding.view.ScaffoldingViewResolver
import org.icescrum.components.UiControllerArtefactHandler
import org.icescrum.core.services.SecurityService
import org.icescrum.core.utils.IceScrumDomainClassMarshaller
import org.springframework.context.ApplicationContext
import org.springframework.web.context.request.RequestContextHolder
import org.codehaus.groovy.grails.commons.GrailsApplication
import grails.util.Environment
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean
import grails.plugin.springcache.web.key.WebContentKeyGenerator
import org.icescrum.cache.LocaleKeyGenerator
import org.icescrum.cache.ISKeyGeneratorHelper
import org.icescrum.cache.UserKeyGenerator
import org.icescrum.cache.RoleKeyGenerator
import org.icescrum.cache.ProjectUserKeyGenerator
import org.icescrum.cache.StoryKeyGenerator
import org.icescrum.cache.ActorKeyGenerator
import org.icescrum.cache.FeatureKeyGenerator
import org.icescrum.cache.TaskKeyGenerator
import org.icescrum.cache.ReleasesKeyGenerator
import org.icescrum.cache.ReleasesRoleKeyGenerator
import org.icescrum.cache.FeaturesKeyGenerator
import org.icescrum.cache.SprintKeyGenerator
import org.icescrum.cache.TasksKeyGenerator
import org.icescrum.cache.ActorsKeyGenerator
import org.icescrum.cache.StoriesKeyGenerator
import org.icescrum.cache.ProjectKeyGenerator
import org.icescrum.cache.ReleaseKeyGenerator
import org.icescrum.cache.TeamKeyGenerator
import org.icescrum.core.domain.Story
import org.icescrum.core.domain.Feature
import org.icescrum.core.domain.Sprint
import org.icescrum.core.domain.Actor
import org.icescrum.core.domain.Release
import org.icescrum.core.domain.Task
import org.icescrum.plugins.attachmentable.interfaces.AttachmentException
import org.codehaus.groovy.grails.plugins.jasper.JasperReportDef
import org.icescrum.core.domain.User
import org.codehaus.groovy.grails.plugins.jasper.JasperExportFormat
import org.springframework.web.servlet.support.RequestContextUtils as RCU
import grails.plugins.springsecurity.SpringSecurityService
import org.codehaus.groovy.grails.plugins.jasper.JasperService
import org.icescrum.core.support.ProgressSupport
class IcescrumCoreGrailsPlugin {
def groupId = 'org.icescrum'
// the plugin version
def version = "1.5-SNAPSHOT"
// the version or versions of Grails the plugin is designed for
def grailsVersion = "1.3.7 > *"
// the other plugins this plugin depends on
def dependsOn = [:]
// resources that are excluded from plugin packaging
def pluginExcludes = [
"grails-app/views/error.gsp"
]
def artefacts = [new UiControllerArtefactHandler()]
def observe = ['controllers']
def loadAfter = ['controllers', 'feeds', 'springcache']
// TODO Fill in these fields
def author = "iceScrum"
def authorEmail = "contact@icescrum.org"
def title = "iceScrum core plugin (include domain / services / taglib)"
def description = '''
iceScrum core plugin (include domain / services / taglib)
'''
// URL to the plugin's documentation
def documentation = "http://www.icescrum.org/plugin/icescrum-core"
def doWithWebDescriptor = { xml ->
mergeConfig(application)
def servlets = xml.'servlet'
servlets[servlets.size() - 1] + {
'servlet' {
'description'('AtmosphereServlet')
'servlet-name'('AtmosphereServlet')
'servlet-class'('org.atmosphere.cpr.AtmosphereServlet')
application.config.icescrum.push.servlet.initParams.each { initParam ->
'init-param' {
'param-name'(initParam.key)
'param-value'(initParam.value)
}
}
'load-on-startup'('0')
}
}
def mappings = xml.'servlet-mapping'
mappings[mappings.size() - 1] + {
'servlet-mapping' {
'servlet-name'('AtmosphereServlet')
def urlPattern = application.config.icescrum.push.servlet?.urlPattern ?: '/atmosphere/*'
'url-pattern'(urlPattern)
}
}
}
def doWithSpring = {
mergeConfig(application)
if (application.config.springcache.configLocation){
springcacheCacheManager(EhCacheManagerFactoryBean) {
shared = false
configLocation = application.config.springcache.configLocation
}
}
iSKeyGeneratorHelper(ISKeyGeneratorHelper){
springSecurityService = ref('springSecurityService')
securityService = ref('securityService')
}
localeKeyGenerator(LocaleKeyGenerator) {
iSKeyGeneratorHelper = ref('iSKeyGeneratorHelper')
contentType = true
}
userKeyGenerator(UserKeyGenerator) {
iSKeyGeneratorHelper = ref('iSKeyGeneratorHelper')
contentType = true
}
roleKeyGenerator(RoleKeyGenerator) {
iSKeyGeneratorHelper = ref('iSKeyGeneratorHelper')
contentType = true
}
projectKeyGenerator(ProjectKeyGenerator){
iSKeyGeneratorHelper = ref('iSKeyGeneratorHelper')
contentType = true
}
teamKeyGenerator(TeamKeyGenerator){
iSKeyGeneratorHelper = ref('iSKeyGeneratorHelper')
contentType = true
}
projectUserKeyGenerator(ProjectUserKeyGenerator){
iSKeyGeneratorHelper = ref('iSKeyGeneratorHelper')
contentType = true
}
sprintKeyGenerator(SprintKeyGenerator){
iSKeyGeneratorHelper = ref('iSKeyGeneratorHelper')
contentType = true
}
releaseKeyGenerator(ReleaseKeyGenerator){
iSKeyGeneratorHelper = ref('iSKeyGeneratorHelper')
contentType = true
}
releasesKeyGenerator(ReleasesKeyGenerator){
iSKeyGeneratorHelper = ref('iSKeyGeneratorHelper')
contentType = true
}
releasesRoleKeyGenerator(ReleasesRoleKeyGenerator){
iSKeyGeneratorHelper = ref('iSKeyGeneratorHelper')
contentType = true
}
storyKeyGenerator(StoryKeyGenerator) {
iSKeyGeneratorHelper = ref('iSKeyGeneratorHelper')
contentType = true
}
storiesKeyGenerator(StoriesKeyGenerator) {
iSKeyGeneratorHelper = ref('iSKeyGeneratorHelper')
contentType = true
}
actorKeyGenerator(ActorKeyGenerator) {
iSKeyGeneratorHelper = ref('iSKeyGeneratorHelper')
contentType = true
}
actorsKeyGenerator(ActorsKeyGenerator) {
iSKeyGeneratorHelper = ref('iSKeyGeneratorHelper')
contentType = true
}
featureKeyGenerator(FeatureKeyGenerator) {
iSKeyGeneratorHelper = ref('iSKeyGeneratorHelper')
contentType = true
}
featuresKeyGenerator(FeaturesKeyGenerator) {
iSKeyGeneratorHelper = ref('iSKeyGeneratorHelper')
contentType = true
}
taskKeyGenerator(TaskKeyGenerator) {
iSKeyGeneratorHelper = ref('iSKeyGeneratorHelper')
contentType = true
}
tasksKeyGenerator(TasksKeyGenerator) {
iSKeyGeneratorHelper = ref('iSKeyGeneratorHelper')
contentType = true
}
springcacheDefaultKeyGenerator(WebContentKeyGenerator){
contentType = true
}
}
private void mergeConfig(GrailsApplication app) {
ConfigObject currentConfig = app.config.icescrum
ConfigSlurper slurper = new ConfigSlurper(Environment.getCurrent().getName());
ConfigObject secondaryConfig = slurper.parse(app.classLoader.loadClass("DefaultIceScrumCoreConfig"))
ConfigObject config = new ConfigObject();
config.putAll((ConfigObject)secondaryConfig.getProperty('icescrum').merge(currentConfig))
app.config.icescrum = config
}
def doWithDynamicMethods = { ctx ->
// Manually match the UIController classes
SecurityService securityService = ctx.getBean('securityService')
SpringSecurityService springSecurityService = ctx.getBean('springSecurityService')
JasperService jasperService = ctx.getBean('jasperService')
application.controllerClasses.each {
if (it.hasProperty(UiControllerArtefactHandler.PROPERTY)) {
application.addArtefact(UiControllerArtefactHandler.TYPE, it)
def plugin = it.hasProperty(UiControllerArtefactHandler.PLUGINNAME) ? it.getPropertyValue(UiControllerArtefactHandler.PLUGINNAME) : null
addUIControllerMethods(it, ctx, plugin)
}
addBroadcastMethods(it, securityService, application)
addErrorMethod(it)
addWithObjectsMethods(it)
addJasperMethod(it, springSecurityService, jasperService)
}
application.serviceClasses.each {
addBroadcastMethods(it, securityService, application)
}
}
def doWithApplicationContext = { applicationContext ->
JSON.registerObjectMarshaller(new IceScrumDomainClassMarshaller(true, application.config?.icescrum?.json))
applicationContext.bootStrapService.start()
}
def onChange = { event ->
def controller = application.getControllerClass(event.source?.name)
if (controller?.hasProperty(UiControllerArtefactHandler.PROPERTY)) {
ScaffoldingViewResolver.clearViewCache()
application.addArtefact(UiControllerArtefactHandler.TYPE, controller)
def plugin = controller.hasProperty(UiControllerArtefactHandler.PLUGINNAME) ? controller.getPropertyValue(UiControllerArtefactHandler.PLUGINNAME) : null
addUIControllerMethods(controller, application.mainContext, plugin)
}
if (application.isControllerClass(event.source)) {
SecurityService securityService = event.ctx.getBean('securityService')
addBroadcastMethods(event.source, securityService, application)
addErrorMethod(event.source)
addWithObjectsMethods(event.source)
SpringSecurityService springSecurityService = event.ctx.getBean('springSecurityService')
JasperService jasperService = event.ctx.getBean('jasperService')
addJasperMethod(event.source, springSecurityService, jasperService)
}
}
def onConfigChange = { event ->
this.mergeConfig(application)
}
private addUIControllerMethods(clazz, ApplicationContext ctx, pluginName) {
def mc = clazz.metaClass
def dynamicActions = [
toolbar: {->
try {
render(plugin: pluginName, template: "window/toolbar", model: [currentView: session.currentView, id: id])
} catch (Exception e) {
render('')
e.printStackTrace()
}
},
toolbarWidget: {->
try {
render(plugin: pluginName, template: "widget/toolbar", model: [id: id])
} catch (Exception e) {
render('')
e.printStackTrace()
}
},
titleBarContent: {
try {
render(plugin: pluginName, template: "window/titleBarContent", model: [id: id])
} catch (Exception e) {
render('')
e.printStackTrace()
}
},
titleBarContentWidget: {
try {
render(plugin: pluginName, template: "widget/titleBarContent", model: [id: id])
} catch (Exception e) {
render('')
e.printStackTrace()
}
}
]
dynamicActions.each { actionName, actionClosure ->
if (!clazz.getPropertyValue(actionName)) {
mc."${GrailsClassUtils.getGetterName(actionName)}" = {->
actionClosure.delegate = delegate
actionClosure.resolveStrategy = Closure.DELEGATE_FIRST
actionClosure
}
clazz.registerMapping(actionName)
}
}
}
private addBroadcastMethods(source, securityService, application) {
source.metaClass.bufferBroadcast = { attrs ->
if (!application.config.icescrum.push?.enable)
return
attrs = attrs ?: [channel: '']
def request = RequestContextHolder.requestAttributes?.request
if (!request)
return
if (!attrs.channel) {
def id = securityService.parseCurrentRequestProduct()
attrs.channel = id ? 'product-' + id : '/push/app'
}
if (!request.bufferBroadcast) {
request.bufferBroadcast = [:]
}
if (request.bufferBroadcast."${attrs.channel}" == null) {
request.bufferBroadcast."${attrs.channel}" = []
}
}
source.metaClass.resumeBufferedBroadcast = { attrs ->
if (!application.config.icescrum.push?.enable)
return
attrs = attrs ?: [channel: '']
def request = RequestContextHolder.requestAttributes?.request
attrs.excludeCaller = attrs.excludeCaller ?: true
def size = attrs.batchSize ?: 10
if (!request)
return
if (!attrs.channel) {
def id = securityService.parseCurrentRequestProduct()
attrs.channel = id ? 'product-' + id : '/push/app'
}
if (attrs.channel instanceof String) {
attrs.channel = [attrs.channel]
}
attrs.channel.each {
if (request.bufferBroadcast && request.bufferBroadcast."${it}") {
if(BroadcasterFactory.default){
Class<? extends org.atmosphere.cpr.Broadcaster> bc = (Class<? extends org.atmosphere.cpr.Broadcaster>)((GrailsApplication) application).getClassLoader().loadClass(application.config.icescrum.push?.broadcaster?:'org.atmosphere.util.ExcludeSessionBroadcaster')
def broadcaster = BroadcasterFactory.default.lookup(bc, it)
def batch = []
def messages = request.bufferBroadcast."${it}"
int partitionCount = messages.size() / size
partitionCount.times { partitionNumber ->
def start = partitionNumber * size
def end = start + size - 1
batch << messages[start..end]
}
if (messages.size() % size) batch << messages[partitionCount * size..-1]
batch.each {
if (attrs.excludeCaller) {
broadcaster?.broadcast((it as JSON).toString(), request.getSession(false))
} else {
broadcaster?.broadcast((it as JSON).toString())
}
}
}
}
request.bufferBroadcast."${it}" = null
}
}
source.metaClass.broadcast = {attrs ->
if (!application.config.icescrum.push?.enable)
return
assert attrs.function, attrs.message
attrs.excludeCaller = attrs.excludeCaller ?: true
def request = RequestContextHolder.requestAttributes?.request
if (!request)
return
if (!attrs.channel) {
def id = securityService.parseCurrentRequestProduct()
attrs.channel = id ? 'product-' + id : '/push/app'
}
if (attrs.channel instanceof String) {
attrs.channel = [attrs.channel]
}
def message = [call: attrs.function, object: attrs.message]
attrs.channel.each {
if(BroadcasterFactory.default){
Class<? extends org.atmosphere.cpr.Broadcaster> bc = (Class<? extends org.atmosphere.cpr.Broadcaster>)((GrailsApplication) application).getClassLoader().loadClass(application.config.icescrum.push?.broadcaster?:'org.atmosphere.util.ExcludeSessionBroadcaster')
def broadcaster = BroadcasterFactory.default.lookup(bc, it)
if (request.bufferBroadcast?."${it}" != null) {
request.bufferBroadcast."${it}" << message
} else {
try {
if (attrs.excludeCaller) {
broadcaster?.broadcast((message as JSON).toString(), request.getSession(false))
} else {
broadcaster?.broadcast((message as JSON).toString())
}
}catch(IllegalStateException e){
log.error("Error when broadcasting, message: ${e.getMessage()}", e)
}
}
}
}
}
source.metaClass.broadcastToSingleUser = {attrs ->
ConfigObject conf = application.config.icescrum.push
if (!conf?.enable)
return
assert attrs.function
assert attrs.message
assert attrs.user
if (!attrs.user)
return
if (attrs.user instanceof String) {
attrs.user = [attrs.user]
}
def message = [call: attrs.function, object: attrs.message]
attrs.user.each {
if(BroadcasterFactory.default){
Class<? extends org.atmosphere.cpr.Broadcaster> bc = (Class<? extends org.atmosphere.cpr.Broadcaster>)((GrailsApplication) application).getClassLoader().loadClass(application.config.icescrum.push?.userBroadcaster?:'org.atmosphere.cpr.DefaultBroadcaster')
def broadcaster = BroadcasterFactory.default.lookup(bc, it)
try {
broadcaster?.broadcast((message as JSON).toString())
}catch(IllegalStateException e){
log.error("Error when broadcasting, message: ${e.getMessage()}", e)
}
}
}
}
}
private addErrorMethod(source) {
source.metaClass.returnError = { attrs ->
if (attrs.exception){
if (attrs.object && attrs.exception instanceof RuntimeException){
withFormat {
html { render(status: 400, contentType: 'application/json', text: [notice: [text: renderErrors(bean: attrs.object)]] as JSON) }
json { render(status: 500, contentType: 'application/json', text: is.renderErrors(bean: attrs.object, as:'json')) }
xml { render(status: 500, contentType: 'text/xml', text: is.renderErrors(bean: attrs.object, as:'xml')) }
}
}else if(attrs.text){
withFormat {
html { render(status: 400, contentType: 'application/json', text: [notice: [text:attrs.text?:'error']] as JSON) }
json { render(status: 500, contentType: 'application/json', text: [error: attrs.text?:'error'] as JSON) }
xml { render(status: 500, contentType: 'text/xml', text: [error: attrs.text?:'error'] as XML) }
}
}else{
withFormat {
html { render(status: 400, contentType: 'application/json', text: [notice: [text: message(code: attrs.exception.getMessage())]] as JSON) }
json { render(status: 500, contentType: 'application/json', text: [error: message(code: attrs.exception.getMessage())] as JSON) }
xml { render(status: 500, contentType: 'text/xml', text: [error: message(code: attrs.exception.getMessage())] as XML) }
}
}
if (log.debugEnabled) attrs.exception.printStackTrace()
}else{
withFormat {
html { render(status: 400, contentType: 'application/json', text: [notice: [text:attrs.text?:'error']] as JSON) }
json { render(status: 500, contentType: 'application/json', text: [error: attrs.text?:'error'] as JSON) }
xml { render(status: 500, contentType: 'text/xml', text: [error: attrs.text?:'error'] as XML) }
}
}
}
}
private void addJasperMethod(source, springSecurityService, jasperService){
try {
source.metaClass.outputJasperReport = { String reportName, String format, def data, String outputName = null, def parameters = null ->
outputName = (outputName ? outputName.replaceAll("[^a-zA-Z\\s]", "").replaceAll(" ", "") + '-' + reportName : reportName) + '-' + (g.formatDate(formatName: 'is.date.file'))
if (!session.progress){
session.progress = new ProgressSupport()
}
session.progress.updateProgress(50, message(code: 'is.report.processing'))
if (parameters){
parameters.SUBREPORT_DIR = "${servletContext.getRealPath('reports/subreports')}/"
}else{
parameters = [SUBREPORT_DIR: "${servletContext.getRealPath('reports/subreports')}/"]
}
def reportDef = new JasperReportDef(name: reportName,
reportData: data,
locale: springSecurityService.isLoggedIn() ? new Locale(User.get(springSecurityService.principal?.id)?.preferences?.language) : RCU.getLocale(request),
parameters: parameters,
fileFormat: JasperExportFormat.determineFileFormat(format))
response.setHeader("Content-disposition", "attachment; filename=" + outputName + "." + reportDef.fileFormat.extension)
response.contentType = reportDef.fileFormat.mimeTyp
response.characterEncoding = "UTF-8"
response.outputStream << jasperService.generateReport(reportDef).toByteArray()
session.progress?.completeProgress(message(code: 'is.report.complete'))
}
} catch (Exception e) {
if (log.debugEnabled) e.printStackTrace()
session.progress.progressError(message(code: 'is.report.error'))
}
}
private void addWithObjectsMethods(source){
source.metaClass.withFeature = { String id = 'id', Closure c ->
Feature feature = Feature.getInProduct(params.long('product'),params."$id"?.toLong()).list()[0]
if (feature){
try{
c.call feature
} catch (AttachmentException e) {
returnError(exception:e)
} catch (IllegalStateException e) {
returnError(exception:e)
} catch (RuntimeException e) {
if(feature.errors)
returnError(object:feature, exception:e)
else
returnError(exception:e)
}
} else {
returnError(text:message(code: 'is.feature.error.not.exist'))
}
}
source.metaClass.withFeatures = { String id = 'id', Closure c ->
List<Feature> features = Feature.getAll(params.list(id))
if (features){
try{
c.call features
} catch (IllegalStateException e) {
returnError(exception:e)
} catch (RuntimeException e) {
returnError(exception:e)
}
} else {
returnError(text:message(code: 'is.actor.error.not.exist'))
}
}
source.metaClass.withActor = { String id = 'id', Closure c ->
Actor actor = Actor.getInProduct(params.long('product'),params."$id"?.toLong()).list()[0]
if (actor){
try{
c.call actor
} catch (AttachmentException e) {
returnError(exception:e)
} catch (IllegalStateException e) {
returnError(exception:e)
} catch (RuntimeException e) {
if(actor.errors)
returnError(object:actor, exception:e)
else
returnError(exception:e)
}
} else {
returnError(text:message(code: 'is.actor.error.not.exist'))
}
}
source.metaClass.withActors = { String id = 'id', Closure c ->
List<Actor> actors = Actor.getAll(params.list(id))
if (actors){
try{
c.call actors
} catch (IllegalStateException e) {
returnError(exception:e)
} catch (RuntimeException e) {
returnError(exception:e)
}
} else {
returnError(text:message(code: 'is.actor.error.not.exist'))
}
}
source.metaClass.withStory = { String id = 'id', Closure c ->
Story story = Story.getInProduct(params.long('product'),params."$id"?.toLong()).list()[0]
if (story){
try{
c.call story
} catch (AttachmentException e) {
returnError(exception:e)
} catch (IllegalStateException e) {
returnError(exception:e)
} catch (RuntimeException e) {
if(story.errors)
returnError(object:story, exception:e)
else
returnError(exception:e)
}
} else {
returnError(text:message(code: 'is.story.error.not.exist'))
}
}
source.metaClass.withStories = { String id = 'id', Closure c ->
List<Story> stories = Story.getAll(params.list(id))
if (stories){
try{
c.call stories
} catch (IllegalStateException e) {
returnError(exception:e)
} catch (RuntimeException e) {
returnError(exception:e)
}
} else {
returnError(text:message(code: 'is.story.error.not.exist'))
}
}
source.metaClass.withTask = { String id = 'id', Closure c ->
Task task = Task.getInProduct(params.long('product'),params."$id"?.toLong())
if (task){
try{
c.call task
} catch (AttachmentException e) {
returnError(object: task, exception: e)
} catch (IllegalStateException e) {
returnError(exception: e)
} catch (RuntimeException e) {
if(task.errors)
returnError(object: task, exception: e)
else
returnError(exception: e)
}
} else {
returnError(text:message(code: 'is.task.error.not.exist'))
}
}
source.metaClass.withTasks = { String id = 'id', Closure c ->
def ids = params.list(id).collect{ it.toLong() }
List<Task> tasks = Task.getAllInProduct(params.long('product'),ids)
if (tasks){
try{
c.call tasks
} catch (IllegalStateException e) {
returnError(exception: e)
} catch (RuntimeException e) {
returnError(exception: e)
}
} else {
returnError(text:message(code: 'is.tasks.error.not.exist'))
}
}
source.metaClass.withSprint = { String id = 'id', Closure c ->
Sprint sprint = Sprint.getInProduct(params.long('product'),params."$id"?.toLong()).list()[0]
if (sprint){
try{
c.call sprint
} catch (IllegalStateException ise) {
returnError(text:message(code: ise.getMessage()))
} catch (AttachmentException e) {
returnError(object: sprint, exception: e)
} catch (IllegalStateException e) {
returnError(exception: e)
} catch (RuntimeException e) {
if(sprint.errors)
returnError(object: sprint, exception: e)
else
returnError(exception: e)
}
} else {
returnError(text:message(code: 'is.sprint.error.not.exist'))
}
}
source.metaClass.withRelease = { String id = 'id', Closure c ->
Release release = Release.getInProduct(params.long('product'),params."$id"?.toLong()).list()[0]
if (release){
try{
c.call release
} catch (AttachmentException e) {
returnError(object: release, exception: e)
} catch (IllegalStateException e) {
returnError(exception: e)
} catch (RuntimeException e) {
if(release.errors)
returnError(object: release, exception: e)
else
returnError(exception: e)
}
} else {
returnError(text:message(code: 'is.release.error.not.exist'))
}
}
}
}