source: trunk/grails-app/controllers/dbnp/studycapturing/StudyWizardController.groovy @ 1353

Last change on this file since 1353 was 1353, checked in by work@…, 12 years ago
  • fixed some issues with ajaxbuttons that were still left unworking after refactoring into ajaxflows (improvement #183)
  • updated ajaxflow to 0.1.15
File size: 52.6 KB
Line 
1package dbnp.studycapturing
2
3import dbnp.data.*
4import grails.converters.*
5import grails.plugins.springsecurity.Secured
6import dbnp.authentication.AuthenticationService
7import dbnp.authentication.SecUser
8import org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib
9
10/**
11 * ajaxflow Controller
12 *
13 * @author      Jeroen Wesbeek
14 * @since       20101220
15 *
16 * Revision information:
17 * $Rev:  66849 $
18 * $Author:  duh $
19 * $Date:  2010-12-08 15:12:54 +0100 (Wed, 08 Dec 2010) $
20 */
21@Secured(['IS_AUTHENTICATED_REMEMBERED'])
22class StudyWizardController {
23        def pluginManager
24        def authenticationService
25        def validationTagLib = new ValidationTagLib()
26
27        /**
28         * index method, redirect to the webflow
29         * @void
30         */
31        def index = {
32                // Grom a development message
33                if (pluginManager.getGrailsPlugin('grom')) "redirecting into the webflow".grom()
34
35                def jump = [:]
36
37                // allow quickjumps to:
38                //      edit a study    : /wizard?jump=edit&id=1
39                //      create a study  : /wizard?jump=create
40                if (params.get('jump')) {
41                        switch (params.get('jump')) {
42                                case 'create':
43                                        jump = [
44                                            action: 'create'
45                                        ]
46                                        break
47                                case 'edit':
48                                        jump = [
49                                            action      : 'edit',
50                                                id              : params.get('id')
51                                        ]
52                                        break
53                                default:
54                                        break
55                        }
56                }
57
58                // store in session
59                session.jump = jump
60
61                /**
62                 * Do you believe it in your head?
63                 * I can go with the flow
64                 * Don't say it doesn't matter (with the flow) matter anymore
65                 * I can go with the flow (I can go)
66                 * Do you believe it in your head?
67                 */
68                redirect(action: 'pages')
69        }
70
71        /**
72         * WebFlow definition
73         * @void
74         */
75        def pagesFlow = {
76                // start the flow
77                onStart {
78                        // Grom a development message
79                        if (pluginManager.getGrailsPlugin('grom')) "entering the WebFlow".grom()
80
81                        // define variables in the flow scope which is availabe
82                        // throughout the complete webflow also have a look at
83                        // the Flow Scopes section on http://www.grails.org/WebFlow
84                        //
85                        // The following flow scope variables are used to generate
86                        // wizard tabs. Also see common/_tabs.gsp for more information
87                        // define flow variables
88                        flow.page = 0
89                        flow.pages = [
90                                //[title: 'Templates'],                 // templates
91                                [title: 'Start'],                               // load or create a study
92                                [title: 'Subjects'],                    // subjects
93                                [title: 'Events'],                              // events and event grouping
94                                //[title: 'Event Groups'],              // groups
95                                [title: 'Samples'],                             // samples
96                                [title: 'Assays'],                              // assays
97                                //[title: 'Assay Groups'],              // assays
98                                [title: 'Confirmation'],                // confirmation page
99                                [title: 'Done']                                 // finish page
100                        ]
101                        flow.jump = session.jump
102
103                        success()
104                }
105
106                // render the main wizard page which immediately
107                // triggers the 'next' action (hence, the main
108                // page dynamically renders the study template
109                // and makes the flow jump to the study logic)
110                mainPage {
111                        render(view: "/studyWizard/index")
112                        onRender {
113                                // Grom a development message
114                                if (pluginManager.getGrailsPlugin('grom')) "rendering the main Ajaxflow page (index.gsp)".grom()
115
116                                // let the view know we're in page 1
117                                flow.page = 1
118                                success()
119                        }
120                        on("next").to "handleJump"
121                }
122
123                // handle the jump parameter
124                //
125                // I came to get down [2x]
126                // So get out your seats and jump around
127                // Jump around [3x]
128                // Jump up Jump up and get down
129                // Jump [18x]
130                handleJump {
131                        action {
132                                // Grom a development message
133                                if (pluginManager.getGrailsPlugin('grom')) "entering handleJump".grom()
134
135                                if (flow.jump && flow.jump.action == 'edit') {
136                                        if (flow.jump.id) {
137                                                // load study
138                                                if (this.loadStudy(flow, flash, [studyid:flow.jump.id],authenticationService.getLoggedInUser())) {
139                                                        toStudyPage()
140                                                } else {
141                                                        toStartPage()
142                                                }
143                                        } else {
144                                                toModifyPage()
145                                        }
146                                } else if (flow.jump && flow.jump.action == 'create') {
147                                        if (!flow.study) flow.study = new Study()
148                                        toStudyPage()
149                                } else {
150                                        toStartPage()
151                                }
152                        }
153                        on("toStartPage").to "start"
154                        on("toStudyPage").to "study"
155                        on("toModifyPage").to "modify"
156                }
157
158                // create or modify a study
159                start {
160                        render(view: "_start")
161                        onRender {
162                                // Grom a development message
163                                if (pluginManager.getGrailsPlugin('grom')) "rendering the partial: pages/_start.gsp".grom()
164
165                                flow.page = 1
166                                success()
167                        }
168                        on("next") {
169                                // clean the flow scope
170                                flow.remove('study')
171
172                                // create a new study instance
173                                if (!flow.study) flow.study = new Study()
174
175                                // set 'quicksave' variable to false
176                                flow.quickSave = false
177                        }.to "study"
178                        on("modify").to "modify"
179                        on("import").to "redirectToImport"
180                }
181
182                // redirect to the import wizard
183                redirectToImport {
184                        render(view: "_redirect")
185                        onRender {
186                                // Grom a development message
187                                if (pluginManager.getGrailsPlugin('grom')) "rendering the partial: pages/_redirect.gsp".grom()
188
189                                flash.uri = "/importer/index"
190                        }
191                        on("next").to "start"
192                }
193
194                // load a study to modify
195                modify {
196                        render(view: "_modify")
197                        onRender {
198                                // Grom a development message
199                                if (pluginManager.getGrailsPlugin('grom')) "rendering the partial: pages/_modify.gsp".grom()
200
201                                flow.page = 1
202                                flash.showCancel = true
203                                success()
204                        }
205                        on("cancel") {
206                                flow.remove('study')
207
208                                success()
209                        }.to "start"
210                        on("next") {
211                                // load study
212                                if (this.loadStudy(flow, flash, params, authenticationService.getLoggedInUser())) {
213                                        success()
214                                } else {
215                                        error()
216                                }
217                        }.to "study"
218                }
219
220                // render and handle the study page
221                study {
222                        render(view: "_study")
223                        onRender {
224                                // Grom a development message
225                                if (pluginManager.getGrailsPlugin('grom')) "rendering the partial: pages/_study.gsp".grom()
226
227                                flow.page = 1
228
229                                // since this controller was refactored it's technically
230                                // safe to enable quicksave throughout the application.
231                                // However, we must keep an eye on the quality of the
232                                // studies as this change makes it easier to create dummy
233                                // studies which will create garbage in out database.
234                                flow.quickSave = true
235
236                                success()
237                        }
238                        on("refresh") {
239                                // handle form data
240                                studyPage(flow, flash, params)
241
242                                // force refresh of the template
243                                if (flow.study.template && flow.study.template instanceof Template) {
244                                        flow.study.template.refresh()
245                                }
246
247                                // reset errors
248                                flash.wizardErrors = [:]
249                                success()
250                        }.to "study"
251            on("switchTemplate") {
252                                // handle form data
253                                studyPage(flow, flash, params)
254
255                                // reset errors
256                                flash.wizardErrors = [:]
257                                success()
258                        }.to "study"
259                        on("previous") {
260                                // handle form data
261                                studyPage(flow, flash, params)
262
263                                // reset errors
264                                flash.wizardErrors = [:]
265                                success()
266                        }.to "start"
267                        on("next") {
268                                studyPage(flow, flash, params) ? success() : error()
269                        }.to "subjects"
270                        on("quickSave") {
271                                studyPage(flow, flash, params) ? success() : error()
272                        }.to "save"
273                        on("toPageTwo") {
274                                studyPage(flow, flash, params) ? success() : error()
275                        }.to "subjects"
276                        on("toPageThree") {
277                                studyPage(flow, flash, params) ? success() : error()
278                        }.to "events"
279                        on("toPageFour") {
280                                studyPage(flow, flash, params) ? success() : error()
281                        }.to "samples"
282                        on("toPageFive") {
283                                studyPage(flow, flash, params) ? success() : error()
284                        }.to "assays"
285                        on("toPageSix") {
286                                studyPage(flow, flash, params) ? success() : error()
287                        }.to "confirm"
288                        on("toPageSeven") {
289                                studyPage(flow, flash, params) ? success() : error()
290                        }.to "save"
291                }
292
293                // render and handle subjects page
294                subjects {
295                        render(view: "_subjects")
296                        onRender {
297                                // Grom a development message
298                                if (pluginManager.getGrailsPlugin('grom')) "rendering the partial: pages/_subjects.gsp".grom()
299
300                                flow.page = 2
301
302                                if (!flash.values || !flash.values.addNumber) flash.values = [addNumber:1]
303
304                                success()
305                        }
306                        on("refresh") {
307                                // remember the params in the flash scope
308                                flash.values = params
309
310                                // refresh templates
311                                if (flow.study.subjects) {
312                                        flow.study.giveSubjectTemplates().each() {
313                                                it.refresh()
314                                        }
315                                }
316
317                                success()
318                        }.to "subjects"
319                        on("add") {
320                                // handle form data
321                                addSubjects(flow, flash, params) ? success() : error()
322                        }.to "subjects"
323                        on("delete") {
324                                // handle form data
325                                subjectPage(flow, flash, params)
326
327                                // reset errors
328                                flash.wizardErrors = [:]
329
330                                // remove subject
331                                def subjectToRemove = flow.study.subjects.find { it.identifier == (params.get('do') as int) }
332                                if (subjectToRemove) {
333                                        flow.study.deleteSubject( subjectToRemove )
334                                }
335                        }.to "subjects"
336                        on("previous") {
337                                // handle form data
338                                subjectPage(flow, flash, params)
339
340                                // reset errors
341                                flash.wizardErrors = [:]
342                                success()
343                        }.to "study"
344                        on("next") {
345                                // handle form data
346                                subjectPage(flow, flash, params) ? success() : error()
347                        }.to "events"
348                        on("quickSave") {
349                                // handle form data
350                                subjectPage(flow, flash, params) ? success() : error()
351                        }.to "save"
352                        on("toPageOne") {
353                                subjectPage(flow, flash, params) ? success() : error()
354                        }.to "study"
355                        on("toPageThree") {
356                                subjectPage(flow, flash, params) ? success() : error()
357                        }.to "events"
358                        on("toPageFour") {
359                                subjectPage(flow, flash, params) ? success() : error()
360                        }.to "samples"
361                        on("toPageFive") {
362                                subjectPage(flow, flash, params) ? success() : error()
363                        }.to "assays"
364                        on("toPageSix") {
365                                subjectPage(flow, flash, params) ? success() : error()
366                        }.to "confirm"
367                        on("toPageSeven") {
368                                subjectPage(flow, flash, params) ? success() : error()
369                        }.to "save"
370                }
371
372                // render events page
373                events {
374                        render(view: "_events")
375                        onRender {
376                                // Grom a development message
377                                if (pluginManager.getGrailsPlugin('grom')) "rendering the partial: pages/_events.gsp".grom()
378
379                                flow.page = 3
380
381                                // add initial eventGroup to study
382                                if (!flow.study.eventGroups?.size()) {
383                                        flow.study.addToEventGroups(
384                                                new EventGroup(name: 'Group 1')
385                                        )
386                                }
387
388                                success()
389                        }
390                        on("clear") {
391                                // remove all events
392                                (flow.study.events + flow.study.samplingEvents).each() { event ->
393                                        if (event instanceof SamplingEvent) {
394                                                flow.study.deleteSamplingEvent( event )
395                                        } else {
396                                                flow.study.deleteEvent( event )
397                                        }
398                                }
399
400                                success()
401                        }.to "events"
402                        on("switchTemplate") {
403                                // handle form data
404                                eventPage(flow, flash, params)
405
406                                // get template
407                                def type        = params.get('eventType')
408                                def template= Template.findByName( params.get( type + 'Template' ) )
409
410                                // change template and/or instance?
411                                if (!flow.event || (flow.event instanceof Event && type == "sample") || (flow.event instanceof SamplingEvent && type == "event")) {
412                                        // create new instance
413                                        flow.event = (type == "event") ? new Event(template: template) : new SamplingEvent(template: template)
414                                } else {
415                                        flow.event.template = template
416                                }
417
418                                // reset errors
419                                flash.wizardErrors = [:]
420                                success()
421
422                        }.to "events"
423                        on("refresh") {
424                                // handle form data
425                                eventPage(flow, flash, params)
426
427                                // refresh templates
428                                flow.study.giveEventTemplates().each() {
429                                        it.refresh()
430                                }
431
432                                // refresh event template
433                                if (flow.event?.template) flow.event.template.refresh()
434
435                                // reset errors
436                                flash.wizardErrors = [:]
437                                success()
438                        }.to "events"
439                        on("add") {
440                                // handle form data
441                                eventPage(flow, flash, params)
442
443                                // reset errors
444                                flash.wizardErrors = [:]
445
446                                // add event to study
447                                if (flow.event instanceof SamplingEvent) {
448                                        flow.study.addToSamplingEvents( flow.event )
449                                } else {
450                                        flow.study.addToEvents( flow.event )
451                                }
452
453                                // validate event
454                                if (flow.event.validate()) {
455                                        // remove event from the flowscope
456                                        flow.remove('event')
457
458                                        success()
459                                } else {
460                                        // event does not validate
461                                        // remove from study
462                                        if (flow.event instanceof SamplingEvent) {
463                                                flow.study.removeFromSamplingEvents( flow.event )
464                                        } else {
465                                                flow.study.removeFromEvents( flow.event )
466                                        }
467
468                                        // append errors
469                                        this.appendErrors(flow.event, flash.wizardErrors)
470                                        error()
471                                }
472                        }.to "events"
473                        on("deleteEvent") {
474                                // handle form data
475                                eventPage(flow, flash, params)
476
477                                // reset errors
478                                flash.wizardErrors = [:]
479
480                                // find matching (sampling) event
481                                def event                       = flow.study.events.find { it.getIdentifier() == (params.get('do') as int) }
482                                def samplingEvent       = flow.study.samplingEvents.find { it.getIdentifier() == (params.get('do') as int) }
483
484                                // perform delete
485                                if (event) flow.study.deleteEvent( event )
486                                if (samplingEvent) flow.study.deleteSamplingEvent( samplingEvent )
487                        }.to "events"
488                        on("addEventGroup") {
489                                // handle form data
490                                eventPage(flow, flash, params)
491
492                                // set work variables
493                                def groupName = 'Group '
494                                def tempGroupIterator = 1
495                                def tempGroupName = groupName + tempGroupIterator
496
497                                // make sure group name is unique
498                                if (flow.study.eventGroups) {
499                                        while (flow.study.eventGroups.find { it.name == tempGroupName }) {
500                                                tempGroupIterator++
501                                                tempGroupName = groupName + tempGroupIterator
502                                        }
503                                }
504                                groupName = tempGroupName
505
506                                // add a new eventGroup
507                                flow.study.addToEventGroups(
508                                        new EventGroup(
509                                                name    : groupName
510                                        )
511                                )
512
513                                // reset errors
514                                flash.wizardErrors = [:]
515                                success()
516                        }.to "events"
517                        on("deleteEventGroup") {
518                                // handle form data
519                                eventPage(flow, flash, params)
520
521                                // reset errors
522                                flash.wizardErrors = [:]
523
524                                // remove eventGroup
525                                def eventGroupToRemove = flow.study.eventGroups.find { it.getIdentifier() == (params.get('do') as int) }
526                                if (eventGroupToRemove) {
527                                        flow.study.deleteEventGroup( eventGroupToRemove )
528                                }
529                        }.to "events"
530                        on("duplicate") {
531                                // handle form data
532                                eventPage(flow, flash, params)
533
534                                // reset errors
535                                flash.wizardErrors = [:]
536
537                                // clone event
538                                def event = null
539                                (((flow.study.events) ? flow.study.events : []) + ((flow.study.samplingEvents) ? flow.study.samplingEvents : [])).find { it.getIdentifier() == (params.get('do') as int) }.each {
540                                        event = (it instanceof SamplingEvent) ? new SamplingEvent() : new Event()
541
542                                        // set template
543                                        event.template = it.template
544
545                                        // copy data
546                                        it.giveFields().each() { field ->
547                                                event.setFieldValue(
548                                                        field.name,
549                                                        it.getFieldValue(field.name)
550                                                )
551                                        }
552
553                                        // assign duplicate event to study
554                                        if (event instanceof SamplingEvent) {
555                                                flow.study.addToSamplingEvents(event)
556                                        } else {
557                                                flow.study.addToEvents(event)
558                                        }
559                                }
560
561                                success()
562                        }.to "events"
563                        on("previous") {
564                                // handle form data
565                                eventPage(flow, flash, params)
566
567                                // reset errors
568                                flash.wizardErrors = [:]
569                                success()
570                        }.to "subjects"
571                        on("next") {
572                                // handle form data
573                                eventPage(flow, flash, params) ? success() : error()
574                        }.to "eventsNext"
575                        on("quickSave") {
576                                // handle form data
577                                eventPage(flow, flash, params) ? success() : error()
578                        }.to "save"
579                        on("toPageOne") {
580                                eventPage(flow, flash, params) ? success() : error()
581                        }.to "study"
582                        on("toPageTwo") {
583                                eventPage(flow, flash, params) ? success() : error()
584                        }.to "subjects"
585                        on("toPageFour") {
586                                eventPage(flow, flash, params) ? success() : error()
587                        }.to "samples"
588                        on("toPageFive") {
589                                eventPage(flow, flash, params) ? success() : error()
590                        }.to "assays"
591                        on("toPageSix") {
592                                eventPage(flow, flash, params) ? success() : error()
593                        }.to "confirm"
594                        on("toPageSeven") {
595                                eventPage(flow, flash, params) ? success() : error()
596                        }.to "save"
597                }
598
599                // decide to show a warning page or not
600                eventsNext {
601                        action {
602                                // Grom a development message
603                                if (pluginManager.getGrailsPlugin('grom')) ".entering eventsNext".grom()
604
605                                def assigned = false
606
607                                // check if all sampling events are in an eventGroup
608                                flow.study.samplingEvents.each() { samplingEvent ->
609                                        // iterate through eventGroups
610                                        flow.study.eventGroups.each() { eventGroup ->
611                                                if ( eventGroup.samplingEvents.find { it.equals(samplingEvent) } ) {
612                                                        assigned = true
613                                                }
614                                        }
615                                }
616
617                                if (assigned) {
618                                        toGroupsPage()
619                                } else {
620                                        toWarningPage()
621                                }
622                        }
623                        on("toWarningPage").to "unassignedSamplingEventWarning"
624                        on("toGroupsPage").to "groups"
625                }
626
627                // warning page for unassigned samplingEvent
628                unassignedSamplingEventWarning {
629                        render(view: "_unassigned_samplingEvent_warning")
630                        onRender {
631                                // Grom a development message
632                                if (pluginManager.getGrailsPlugin('grom')) "rendering the partial: pages/_unnassigned_samplingEvent_warning.gsp".grom()
633
634                                flow.page = 3
635                                success()
636                        }
637                        on("next").to "groups"
638                        on("previous").to "events"
639                        on("toPageOne") {
640                                eventPage(flow, flash, params) ? success() : error()
641                        }.to "study"
642                        on("toPageTwo") {
643                                eventPage(flow, flash, params) ? success() : error()
644                        }.to "subjects"
645                        on("toPageFour") {
646                                eventPage(flow, flash, params) ? success() : error()
647                        }.to "samples"
648                        on("toPageFive") {
649                                eventPage(flow, flash, params) ? success() : error()
650                        }.to "assays"
651                        on("toPageSix") {
652                                eventPage(flow, flash, params) ? success() : error()
653                        }.to "confirm"
654                        on("toPageSeven") {
655                                eventPage(flow, flash, params) ? success() : error()
656                        }.to "save"
657                }
658
659                // groups page
660                groups {
661                        render(view: "_groups")
662                        onRender {
663                                // Grom a development message
664                                if (pluginManager.getGrailsPlugin('grom')) "rendering the partial: pages/_groups.gsp".grom()
665
666                                flow.page = 3
667                                success()
668                        }
669                        on("previous") {
670                                // handle form data
671                                groupPage(flow, flash, params) ? success() : error()
672                        }.to "events"
673                        on("next") {
674                                // handle form data
675                                groupPage(flow, flash, params) ? success() : error()
676                        }.to "samples"
677                        on("quickSave") {
678                                // handle form data
679                                groupPage(flow, flash, params) ? success() : error()
680                        }.to "save"
681                        on("toPageOne") {
682                                groupPage(flow, flash, params) ? success() : error()
683                        }.to "study"
684                        on("toPageTwo") {
685                                groupPage(flow, flash, params) ? success() : error()
686                        }.to "subjects"
687                        on("toPageFour") {
688                                groupPage(flow, flash, params) ? success() : error()
689                        }.to "samples"
690                        on("toPageFive") {
691                                groupPage(flow, flash, params) ? success() : error()
692                        }.to "assays"
693                        on("toPageSix") {
694                                groupPage(flow, flash, params) ? success() : error()
695                        }.to "confirm"
696                        on("toPageSeven") {
697                                groupPage(flow, flash, params) ? success() : error()
698                        }.to "save"
699                }
700
701                // sample 'previous' page with warning
702                samplePrevious {
703                        render(view: "_samples_previous_warning")
704                        onRender {
705                                // Grom a development message
706                                if (pluginManager.getGrailsPlugin('grom')) "rendering the partial: pages/_samples_previous_warning.gsp".grom()
707
708                                flow.page = 4
709                        }
710                        on("next").to "samples"
711                        on("previous").to "groups"
712                        on("toPageOne").to "study"
713                        on("toPageTwo").to "subjects"
714                        on("toPageThree").to "events"
715                        on("toPageFive").to "assays"
716                        on("toPageSix").to "confirm"
717                        on("toPageSeven").to "save"
718                }
719
720                // samples page
721                samples {
722                        render(view: "_samples")
723                        onRender {
724                                // Grom a development message
725                                if (pluginManager.getGrailsPlugin('grom')) "rendering the partial: pages/_samples.gsp".grom()
726
727                                flow.page = 4
728                                success()
729                        }
730                        on("switchTemplate") {
731                                // handle form data
732                                samplePage(flow, flash, params)
733
734                                // ignore errors
735                                flash.wizardErrors = [:]
736
737                                succes()
738                        }.to "samples"
739                        on("refresh") {
740                                // handle samples
741                                samplePage(flow, flash, params)
742
743                                // refresh all sample templates
744                                flow.study.giveSampleTemplates().each() {
745                                        it.refresh()
746                                }
747
748                                // ignore errors
749                                flash.wizardErrors = [:]
750
751                                success()
752                        }.to "samples"
753                        on("regenerate") {
754                                // handle samples
755                                samplePage(flow, flash, params)
756
757                                // remove all samples from the study
758                                flow.study.samples.findAll{true}.each() { sample ->
759                                        flow.study.removeFromSamples(sample)
760                                }
761
762                                // ignore errors
763                                flash.wizardErrors = [:]
764
765                                success()
766                        }.to "samples"
767                        on("previous") {
768                                // handle samples
769                                samplePage(flow, flash, params)
770
771                                // ignore errors
772                                flash.wizardErrors = [:]
773
774                                success()
775                        }.to "samplePrevious"
776                        on("next") {
777                                // handle form data
778                                samplePage(flow, flash, params) ? success() : error()
779                        }.to "assays"
780                        on("quickSave") {
781                                // handle form data
782                                samplePage(flow, flash, params) ? success() : error()
783                        }.to "save"
784                        on("toPageOne") {
785                                samplePage(flow, flash, params) ? success() : error()
786                        }.to "study"
787                        on("toPageTwo") {
788                                samplePage(flow, flash, params) ? success() : error()
789                        }.to "subjects"
790                        on("toPageThree") {
791                                samplePage(flow, flash, params) ? success() : error()
792                        }.to "events"
793                        on("toPageFive") {
794                                samplePage(flow, flash, params) ? success() : error()
795                        }.to "assays"
796                        on("toPageSix") {
797                                samplePage(flow, flash, params) ? success() : error()
798                        }.to "confirm"
799                        on("toPageSeven") {
800                                samplePage(flow, flash, params) ? success() : error()
801                        }.to "save"
802                }
803
804                // assays page
805                assays {
806                        render(view: "_assays")
807                        onRender {
808                                // Grom a development message
809                                if (pluginManager.getGrailsPlugin('grom')) "rendering the partial: pages/_assays.gsp".grom()
810
811                                flow.page = 5
812                        }
813                        on("refresh") {
814                                // handle form data
815                                assayPage(flow, flash, params)
816
817                                // force refresh of the template
818                                if (flow.assay && flow.assay.template && flow.assay.template instanceof Template) {
819                                        flow.assay.template.refresh()
820                                }
821
822                                // reset errors
823                                flash.wizardErrors = [:]
824                                success()
825                        }.to "assays"
826            on("switchTemplate") {
827                                // handle form data
828                    assayPage(flow, flash, params)
829
830                    // find assay template
831                    def template = Template.findByName(params.get('template'))
832                    if (flow.assay) {
833                            // set template
834                            flow.assay.template = template
835                            if (template) {
836                                    flow.assay.setFieldValue(
837                                            'externalAssayID',
838                                            ucwords(flow.study.code).replaceAll("([ ]{1,})", "") + '_' + ucwords(template.name).replaceAll("([ ]{1,})", "")
839                                    )
840                            }
841                    } else {
842                            // create a new assay instance
843                            flow.assay = new Assay(template: template)
844                            if (template) {
845                                    flow.assay.setFieldValue(
846                                            'externalAssayID',
847                                            ucwords(flow.study.code).replaceAll("([ ]{1,})", "") + '_' + ucwords(template.name).replaceAll("([ ]{1,})", "")
848                                    )
849                            }
850                    }
851
852                                // reset errors
853                                flash.wizardErrors = [:]
854                                success()
855                        }.to "assays"
856                        on("add") {
857                                // handle form data
858                                assayPage(flow, flash, params)
859
860                                // reset errors
861                                flash.wizardErrors = [:]
862
863                                // add assay to study
864                                flow.study.addToAssays( flow.assay )
865
866                                // validate assay
867                                if (flow.assay.validate()) {
868                                        // remove assay from the flowscope
869                                        flow.remove('assay')
870                                        success()
871                                } else {
872                                        // assay does not validate
873                                        // remove from study
874                                        flow.study.removeFromAssays( flow.assay )
875
876                                        // append errors
877                                        this.appendErrors(flow.assay, flash.wizardErrors)
878                                        error()
879                                }
880                        }.to "assays"
881                        on("deleteAssay") {
882                                // handle form data
883                                assayPage(flow, flash, params)
884
885                                // reset errors
886                                flash.wizardErrors = [:]
887
888                                // find this assay
889                                def assay = flow.study.assays.find { it.getIdentifier() == (params.get('do') as int) }
890
891                                // perform delete
892                                if (assay) flow.study.deleteAssay( assay )
893                        }.to "assays"
894                        on("previous") {
895                                // handle form data
896                                assayPage(flow, flash, params)
897
898                                // ignore errors
899                                flash.wizardErrors = [:]
900
901                                success()
902                        }.to "samples"
903                        on("next") {
904                                // handle form data
905                                assayPage(flow, flash, params) ? success() : error()
906                        }.to "assayNext"
907                        on("quickSave") {
908                                // handle form data
909                                assayPage(flow, flash, params) ? success() : error()
910                        }.to "save"
911                        on("toPageOne") {
912                                assayPage(flow, flash, params) ? success() : error()
913                        }.to "study"
914                        on("toPageTwo") {
915                                assayPage(flow, flash, params) ? success() : error()
916                        }.to "subjects"
917                        on("toPageThree") {
918                                assayPage(flow, flash, params) ? success() : error()
919                        }.to "events"
920                        on("toPageFour") {
921                                assayPage(flow, flash, params) ? success() : error()
922                        }.to "samples"
923                        on("toPageSix") {
924                                assayPage(flow, flash, params) ? success() : error()
925                        }.to "confirm"
926                        on("toPageSeven") {
927                                assayPage(flow, flash, params) ? success() : error()
928                        }.to "save"
929                }
930
931                // assayNext
932                assayNext {
933                        action {
934                                // Grom a development message
935                                if (pluginManager.getGrailsPlugin('grom')) "entering assayNext".grom()
936
937                                // have we got samples and assays?
938                                if (flow.study.assays && flow.study.samples) {
939                                        // yes, go to the group page
940                                        toAssayGroups()
941                                } else {
942                                        // no need to show the group page as
943                                        // there's nothing to group
944                                        toConfirm()
945                                }
946                        }
947                        on("toAssayGroups").to "assayGroups"
948                        on("toConfirm").to "confirm"
949                }
950
951                // assay grouping page
952                assayGroups {
953                        render(view: "_assay_groups")
954                        onRender {
955                                // Grom a development message
956                                if (pluginManager.getGrailsPlugin('grom')) "rendering the partial: pages/_assay_groups.gsp".grom()
957
958                                flow.page = 5
959                        }
960                        on("previous") {
961                                // handle form data
962                                assayGroupPage(flow, flash, params)
963
964                                // ignore errors
965                                flash.wizardErrors = [:]
966
967                                success()
968                        }.to "assays"
969                        on("next") {
970                                // handle form data
971                                assayGroupPage(flow, flash, params) ? success() : error()
972                        }.to "confirm"
973                        on("quickSave") {
974                                // handle form data
975                                assayGroupPage(flow, flash, params) ? success() : error()
976                        }.to "save"
977                        on("toPageOne") {
978                                assayGroupPage(flow, flash, params) ? success() : error()
979                        }.to "study"
980                        on("toPageTwo") {
981                                assayGroupPage(flow, flash, params) ? success() : error()
982                        }.to "subjects"
983                        on("toPageThree") {
984                                assayGroupPage(flow, flash, params) ? success() : error()
985                        }.to "events"
986                        on("toPageFour") {
987                                assayGroupPage(flow, flash, params) ? success() : error()
988                        }.to "samples"
989                        on("toPageSix") {
990                                assayGroupPage(flow, flash, params) ? success() : error()
991                        }.to "confirm"
992                        on("toPageSeven") {
993                                assayGroupPage(flow, flash, params) ? success() : error()
994                        }.to "save"
995                }
996
997                // confirm Previous
998                confirmPrevious {
999                        action {
1000                                // Grom a development message
1001                                if (pluginManager.getGrailsPlugin('grom')) "entering confirmPrevious".grom()
1002
1003                                // have we got samples and assays?
1004                                if (flow.study.assays && flow.study.samples) {
1005                                        // yes, go to the group page
1006                                        toAssayGroups()
1007                                } else {
1008                                        // no need to show the group page as
1009                                        // there's nothing to group
1010                                        toAssays()
1011                                }
1012                        }
1013                        on("toAssayGroups").to "assayGroups"
1014                        on("toAssays").to "assays"
1015                }
1016
1017                // confirmation
1018                confirm {
1019                        render(view: "_confirmation")
1020                        onRender {
1021                                // Grom a development message
1022                                if (pluginManager.getGrailsPlugin('grom')) "rendering the partial: pages/_confirmation.gsp".grom()
1023
1024                                flow.page = 6
1025                        }
1026                        on("toStudy").to "study"
1027                        on("toSubjects").to "subjects"
1028                        on("toEvents").to "events"
1029                        on("toGroups").to "groups"
1030                        on("toSamples").to "samples"
1031                        on("toAssays").to "assays"
1032                        on("toAssayGroups").to "assayGroups"
1033                        on("previous").to "confirmPrevious"
1034                        on("next").to "save"
1035                        on("quickSave").to "save"
1036                        on("toPageOne").to "study"
1037                        on("toPageTwo").to "subjects"
1038                        on("toPageThree").to "events"
1039                        on("toPageFour").to "samples"
1040                        on("toPageFive").to "assays"
1041                        on("toPageSeven").to "save"
1042                }
1043
1044                // store all study data
1045                save {
1046                        action {
1047                                // Grom a development message
1048                                if (pluginManager.getGrailsPlugin('grom')) "entering save".grom()
1049
1050                                flash.wizardErrors = [:]
1051
1052                                // persist data to the database
1053                                try {
1054                                        // save study
1055                                        // Grom a development message
1056                                        if (pluginManager.getGrailsPlugin('grom')) "saving study".grom()
1057
1058                                        // Make sure the owner of the study is set right
1059                                        flow.study.owner = authenticationService.getLoggedInUser()
1060
1061                                        if (!flow.study.save(flush:true)) {
1062                                                this.appendErrors(flow.study, flash.wizardErrors)
1063                                                throw new Exception('error saving study')
1064                                        }
1065                                        log.info ".saved study "+flow.study+" (id: "+flow.study.id+")"
1066
1067                                        success()
1068                                } catch (Exception e) {
1069                                        // rollback
1070                                        this.appendErrorMap(['exception': e.toString() + ', see log for stacktrace' ], flash.wizardErrors)
1071
1072                                        // stacktrace in flash scope
1073                                        flash.debug = e.getStackTrace()
1074
1075                                        error()
1076                                }
1077                        }
1078                        on("error").to "error"
1079                        on(Exception).to "error"
1080                        on("success").to "done"
1081                }
1082
1083                // error storing data
1084                error {
1085                        render(view: "_error")
1086                        onRender {
1087                                // Grom a development message
1088                                if (pluginManager.getGrailsPlugin('grom')) "rendering the partial: pages/_error.gsp".grom()
1089
1090                                flow.page = 6
1091                        }
1092                        on("next").to "save"
1093                        on("previous").to "samples"
1094                        on("toPageOne").to "study"
1095                        on("toPageTwo").to "subjects"
1096                        on("toPageThree").to "events"
1097                        on("toPageFour").to "samples"
1098                        on("toPageFive").to "assays"
1099                        on("toPageSix").to "confirm"
1100                        on("toPageSeven").to "save"             }
1101
1102                // render finish page
1103                done {
1104                        render(view: "_done")
1105                        onRender {
1106                                // Grom a development message
1107                                if (pluginManager.getGrailsPlugin('grom')) "rendering the partial: pages/_done.gsp".grom()
1108
1109                                flow.page = 7
1110                        }
1111                        onEnd {
1112                                // clean flow scope
1113                                flow.clear()
1114                        }
1115                }
1116        }
1117
1118        /**
1119         * load a study
1120         * @param Map LocalAttributeMap (the flow scope)
1121         * @param Map localAttributeMap (the flash scope)
1122         * @param Map GrailsParameterMap (the flow parameters = form data)
1123         * @returns boolean
1124         */
1125        def loadStudy(flow, flash, params, user) {
1126                flash.wizardErrors      = [:]
1127
1128                // load study
1129                try {
1130                        // load study
1131                        def study = (params.studyid) ? Study.findById( params.studyid ) : Study.findByTitle( params.study )
1132
1133                        // Check whether the user is allowed to edit this study. If it is not allowed
1134                        // the used should had never seen a link to this page, so he should never get
1135                        // here. That's why we just return false
1136            if (!study.canWrite(user)){
1137                                return false
1138                        }
1139
1140                        // store study in the flowscope
1141                        flow.study = study
1142
1143                        // set 'quicksave' variable
1144                        flow.quickSave = true
1145
1146                        return true
1147                } catch (Exception e) {
1148                        // rollback
1149                        this.appendErrorMap(['exception': e.getMessage() + ', see log for stacktrace'], flash.wizardErrors)
1150
1151                        return false
1152                }
1153        }
1154
1155        /**
1156         * Handle the wizard study page
1157         *
1158         * @param Map LocalAttributeMap (the flow scope)
1159         * @param Map localAttributeMap (the flash scope)
1160         * @param Map GrailsParameterMap (the flow parameters = form data)
1161         * @returns boolean
1162         */
1163        def studyPage(flow, flash, params) {
1164                flash.values            = params
1165                flash.wizardErrors      = [:]
1166
1167                // instantiate study of it is not yet present
1168                if (!flow.study) flow.study = new Study()
1169
1170                // did the study template change?
1171                if (params.get('template').size() && flow.study.template?.name != params.get('template')) {
1172                        // set the template
1173                        flow.study.template = Template.findByName(params.remove('template'))
1174                }
1175
1176                // does the study have a template set?
1177                if (flow.study.template && flow.study.template instanceof Template) {
1178                        // yes, iterate through template fields
1179                        flow.study.giveFields().each() {
1180                                // and set their values
1181                                flow.study.setFieldValue(it.name, params.get(it.escapedName()))
1182                        }
1183                }
1184
1185                // handle publications
1186                handlePublications(flow, flash, params)
1187
1188                // handle contacts
1189                handleContacts(flow, flash, params)
1190
1191                // handle users (readers, writers)
1192                handleUsers(flow, flash, params, 'readers')
1193                handleUsers(flow, flash, params, 'writers')
1194
1195                // handle public checkbox
1196                if (params.get("publicstudy")) {
1197                        flow.study.publicstudy = params.get("publicstudy")
1198                }
1199
1200                // have we got a template?
1201                if (flow.study.template && flow.study.template instanceof Template) {
1202                        // validate the study
1203                        if (flow.study.validate()) {
1204                                // instance is okay
1205                                return true
1206                        } else {
1207                                // validation failed
1208                                this.appendErrors(flow.study, flash.wizardErrors)
1209                                return false
1210                        }
1211                } else {
1212                        // no, return an error that the template is not set
1213                        this.appendErrorMap(['template': g.message(code: 'select.not.selected.or.add', args: ['template'])], flash.wizardErrors)
1214                        return false
1215                }
1216        }
1217
1218        /**
1219         * re-usable code for handling publications form data in a web flow
1220         * @param Map LocalAttributeMap (the flow scope)
1221         * @param Map localAttributeMap (the flash scope)
1222         * @param Map GrailsParameterMap (the flow parameters = form data)
1223         * @returns boolean
1224         */
1225        def handlePublications(flow, flash, params) {
1226                flash.wizardErrors      = [:]
1227
1228                if (!flow.study.publications) flow.study.publications = []
1229
1230                // Check the ids of the pubblications that should be attached
1231                // to this study. If they are already attached, keep 'm. If
1232                // studies are attached that are not in the selected (i.e. the
1233                // user deleted them), remove them
1234                def publicationIDs = params.get('publication_ids')
1235                if (publicationIDs) {
1236                        // Find the individual IDs and make integers
1237                        publicationIDs = publicationIDs.split(',').collect { Integer.parseInt(it, 10) }
1238
1239                        // First remove the publication that are not present in the array
1240                        flow.study.publications.removeAll { publication -> !publicationIDs.find { id -> id == publication.id } }
1241
1242                        // Add those publications not yet present in the database
1243                        publicationIDs.each { id ->
1244                                if (!flow.study.publications.find { publication -> id == publication.id }) {
1245                                        def publication = Publication.get(id)
1246                                        if (publication) {
1247                                                flow.study.addToPublications(publication)
1248                                        } else {
1249                                                log.info('.publication with ID ' + id + ' not found in database.')
1250                                        }
1251                                }
1252                        }
1253
1254                } else {
1255                        log.info('.no publications selected.')
1256                        flow.study.publications.clear()
1257                }
1258
1259        }
1260
1261        /**
1262         * re-usable code for handling contacts form data in a web flow
1263         * @param Map LocalAttributeMap (the flow scope)
1264         * @param Map localAttributeMap (the flash scope)
1265         * @param Map GrailsParameterMap (the flow parameters = form data)
1266         * @return boolean
1267         */
1268        def handleContacts(flow, flash, params) {
1269                flash.wizardErrors      = [:]
1270
1271                if (!flow.study.persons) flow.study.persons = []
1272
1273                // Check the ids of the contacts that should be attached
1274                // to this study. If they are already attached, keep 'm. If
1275                // studies are attached that are not in the selected (i.e. the
1276                // user deleted them), remove them
1277
1278                // Contacts are saved as [person_id]-[role_id]
1279                def contactIDs = params.get('contacts_ids')
1280                if (contactIDs) {
1281                        // Find the individual IDs and make integers
1282                        contactIDs = contactIDs.split(',').collect {
1283                                def parts = it.split('-')
1284                                return [person: Integer.parseInt(parts[0]), role: Integer.parseInt(parts[1])]
1285                        }
1286
1287                        // First remove the contacts that are not present in the array
1288                        flow.study.persons.removeAll {
1289                                studyperson -> !contactIDs.find { ids -> (ids.person == studyperson.person.id) && (ids.role == studyperson.role.id) }
1290                        }
1291
1292                        // Add those contacts not yet present in the database
1293                        contactIDs.each { ids ->
1294                                if (!flow.study.persons.find { studyperson -> (ids.person == studyperson.person.id) && (ids.role == studyperson.role.id) }) {
1295                                        def person = Person.get(ids.person)
1296                                        def role = PersonRole.get(ids.role)
1297                                        if (person && role) {
1298                                                // Find a studyperson object with these parameters
1299                                                def studyPerson = StudyPerson.findAll().find { studyperson -> studyperson.person.id == person.id && studyperson.role.id == role.id }
1300
1301                                                // If if does not yet exist, save the example
1302                                                if (!studyPerson) {
1303                                                        studyPerson = new StudyPerson(
1304                                                                person: person,
1305                                                                role: role
1306                                                        )
1307                                                        studyPerson.save(flush: true)
1308                                                }
1309
1310                                                flow.study.addToPersons(studyPerson)
1311                                        } else {
1312                                                log.info('.person ' + ids.person + ' or Role ' + ids.role + ' not found in database.')
1313                                        }
1314                                }
1315                        }
1316                } else {
1317                        log.info('.no persons selected.')
1318                        flow.study.persons.clear()
1319                }
1320
1321        }
1322
1323        /**
1324         * re-usable code for handling contacts form data in a web flow
1325         * @param Map LocalAttributeMap (the flow scope)
1326         * @param Map localAttributeMap (the flash scope)
1327         * @param Map GrailsParameterMap (the flow parameters = form data)
1328         * @param String    'readers' or 'writers'
1329         * @return boolean
1330         */
1331        def handleUsers(flow, flash, params, type) {
1332                flash.wizardErrors = [:]
1333
1334                def users = []
1335
1336                if (type == "readers") {
1337                        users = flow.study.readers ?: []
1338                } else if (type == "writers") {
1339                        users = flow.study.writers ?: []
1340                }
1341
1342                // Check the ids of the contacts that should be attached
1343                // to this study. If they are already attached, keep 'm. If
1344                // studies are attached that are not in the selected (i.e. the
1345                // user deleted them), remove them
1346
1347                // Users are saved as user_id
1348                def userIDs = params.get(type + '_ids')
1349                if (userIDs) {
1350                        // Find the individual IDs and make integers
1351                        userIDs = userIDs.split(',').collect { Integer.parseInt(it, 10) }
1352
1353                        // First remove the publication that are not present in the array
1354                        users.removeAll { user -> !userIDs.find { id -> id == user.id } }
1355
1356                        // Add those publications not yet present in the database
1357                        userIDs.each { id ->
1358                                if (!users.find { user -> id == user.id }) {
1359                                        def user = SecUser.get(id)
1360                                        if (user) {
1361                                                users.add(user)
1362                                        } else {
1363                                                log.info('.user with ID ' + id + ' not found in database.')
1364                                        }
1365                                }
1366                        }
1367
1368                } else {
1369                        log.info('.no users selected.')
1370                        users.clear()
1371                }
1372
1373                if (type == "readers") {
1374                        if (flow.study.readers)
1375                                flow.study.readers.clear()
1376                        users.each { flow.study.addToReaders(it) }
1377                } else if (type == "writers") {
1378                        if (flow.study.writers)
1379                                flow.study.writers.clear()
1380
1381                        users.each { flow.study.addToWriters(it) }
1382                }
1383        }
1384
1385        /**
1386         * Handle the wizard subject page
1387         *
1388         * @param Map LocalAttributeMap (the flow scope)
1389         * @param Map localAttributeMap (the flash scope)
1390         * @param Map GrailsParameterMap (the flow parameters = form data)
1391         * @returns boolean
1392         */
1393        def subjectPage(flow, flash, params) {
1394                def errors = false
1395                flash.wizardErrors = [:]
1396
1397                // remember the params in the flash scope
1398                flash.values = params
1399
1400                // iterate through subjects
1401                flow.study.subjects.each() { subject ->
1402                        // iterate through (template and domain) fields
1403                        subject.giveFields().each() { field ->
1404                                // set field
1405                                subject.setFieldValue(
1406                                        field.name,
1407                                        params.get('subject_' + subject.getIdentifier() + '_' + field.escapedName())
1408                                )
1409                        }
1410
1411                        // validate subject
1412                        if (!subject.validate()) {
1413                                errors = true
1414                                this.appendErrors(subject, flash.wizardErrors, 'subject_' + subject.getIdentifier() + '_')
1415                        }
1416                }
1417
1418                return !errors
1419        }
1420
1421        /**
1422         * Add a number of subjects to a study
1423         *
1424         * required params entities:
1425         * -addNumber (int)
1426         * -species   (string)
1427         * -template  (string)
1428         *
1429         * @param Map LocalAttributeMap (the flow scope)
1430         * @param Map localAttributeMap (the flash scope)
1431         * @param Map GrailsParameterMap (the flow parameters = form data)
1432         * @returns boolean
1433         */
1434        def addSubjects(flow, flash, params) {
1435                // remember the params in the flash scope
1436                flash.values = params
1437
1438                // handle the subject page
1439                subjectPage(flow, flash, params)
1440
1441                // (re)set error message
1442                flash.wizardErrors = [:]
1443
1444                // set work variables
1445                def errors              = false
1446                def number              = params.get('addNumber') as int
1447                def species             = Term.findByName(params.get('species'))
1448                def template    = Template.findByName(params.get('template'))
1449
1450                // can we add subjects?
1451                if (number > 0 && species && template) {
1452                        // add subjects to study
1453                        number.times {
1454                                // work variables
1455                                def subjectName = 'Subject '
1456                                def subjectIterator = 1
1457                                def tempSubjectName = subjectName + subjectIterator
1458
1459                                // make sure subject name is unique
1460                                if (flow.study.subjects) {
1461                                        while (flow.study.subjects.find { it.name == tempSubjectName }) {
1462                                                subjectIterator++
1463                                                tempSubjectName = subjectName + subjectIterator
1464                                        }
1465                                }
1466                                subjectName = tempSubjectName
1467
1468                                // create a subject instance
1469                                def subject = new Subject(
1470                                        name            : subjectName,
1471                                        species         : species,
1472                                        template        : template
1473                                )
1474
1475                                // add it to the study
1476                                flow.study.addToSubjects( subject )
1477                        }
1478                } else {
1479                        // add feedback
1480                        errors = true
1481                        if (number < 1) this.appendErrorMap(['addNumber': 'Enter a positive number of subjects to add'], flash.wizardErrors)
1482                        if (!species)   this.appendErrorMap(['species': g.message(code: 'select.not.selected.or.add', args: ['species'])], flash.wizardErrors)
1483                        if (!template)  this.appendErrorMap(['template': g.message(code: 'select.not.selected.or.add', args: ['template'])], flash.wizardErrors)
1484                }
1485
1486                return !errors
1487        }
1488
1489        /**
1490         * Handle the wizard event page
1491         *
1492         * @param Map LocalAttributeMap (the flow scope)
1493         * @param Map localAttributeMap (the flash scope)
1494         * @param Map GrailsParameterMap (the flow parameters = form data)
1495         * @returns boolean
1496         */
1497        def eventPage(flow, flash, params) {
1498                def errors = false
1499                flash.wizardErrors = [:]
1500
1501                // remember the params in the flash scope
1502                flash.values = params
1503
1504                // handle the 'add event' form
1505                if (flow.event) {
1506                        flow.event.giveFields().each() { field ->
1507                                // set field
1508                                flow.event.setFieldValue(
1509                                        field.name,
1510                                        params.get(field.escapedName())
1511                                )
1512                        }
1513                }
1514
1515                // handle the eventGroup names and grouping
1516                def name        = ""
1517                def tempName= ""
1518                flow.study.eventGroups.each() { eventGroup ->
1519                        // iterate through templates
1520                        flow.study.giveAllEventTemplates().each() { template ->
1521                                tempName = params.get( 'eventGroup_' + eventGroup.getIdentifier() + '_' + template.getIdentifier() )
1522
1523                                // is the name different?
1524                                if (tempName != eventGroup.name) {
1525                                        name = tempName
1526                                }
1527                        }
1528
1529                        // should the name change?
1530                        if (name) {
1531                                // yes, change it
1532                                eventGroup.name = name
1533                                name = ""
1534                        }
1535
1536                        // handle eventGrouping
1537                        ( ((flow.study.events) ? flow.study.events : []) + ((flow.study.samplingEvents) ? flow.study.samplingEvents : []) ) .each() { event ->
1538                                if (params.get( 'event_' + event.getIdentifier() + '_group_' + eventGroup.getIdentifier() )) {
1539                                        // add to eventGroup
1540                                        if (event instanceof SamplingEvent) {
1541                                                // check if we are already in this eventGroup
1542                                                if (!eventGroup.samplingEvents.find { it.equals(event) }) {
1543                                                        // no, add it
1544                                                        eventGroup.addToSamplingEvents(event)
1545
1546                                                        // iterate through subjects for this eventGroup
1547                                                        eventGroup.subjects.each() { subject ->
1548                                                                // instantiate a sample for this subject / event
1549                                                                def samplingEventName = ucwords(event.template.name)
1550                                                                def eventGroupName = ucwords(eventGroup.name).replaceAll("([ ]{1,})", "")
1551                                                                def sampleName = (ucwords(subject.name) + '_' + samplingEventName + '_' + eventGroupName + '_' + new RelTime(event.startTime).toString()).replaceAll("([ ]{1,})", "")
1552                                                                def tempSampleIterator = 0
1553                                                                def tempSampleName = sampleName
1554
1555                                                                // make sure sampleName is unique
1556                                                                if (flow.study.samples) {
1557                                                                        while (flow.study.samples.find { it.name == tempSampleName }) {
1558                                                                                tempSampleIterator++
1559                                                                                tempSampleName = sampleName + "_" + tempSampleIterator
1560                                                                        }
1561                                                                        sampleName = tempSampleName
1562                                                                }
1563
1564                                                                // instantiate a sample
1565                                                                flow.study.addToSamples(
1566                                                                        new Sample(
1567                                                                                parentSubject   : subject,
1568                                                                                parentEvent             : event,
1569                                                                                parentEventGroup: eventGroup,
1570                                                                                name                    : sampleName,
1571                                                                                template                : (event.sampleTemplate) ? event.sampleTemplate : ''
1572                                                                        )
1573                                                                )
1574                                                        }
1575                                                }
1576                                        } else {
1577                                                eventGroup.addToEvents(event)
1578                                        }
1579                                } else {
1580                                        // remove from eventGroup
1581                                        if (event instanceof SamplingEvent) {
1582                                                // iterate through subjects (if we have them)
1583                                                eventGroup.subjects.each() { subject ->
1584                                                        // find all samples for this subject / event
1585                                                        flow.study.samples.findAll { (it.parentEvent.equals(event) && it.parentSubject.equals(subject) ) }.each() {
1586                                                                // delete this sample
1587                                                                flow.study.removeFromSamples( it )
1588                                                                it.delete()
1589                                                        }
1590                                                }
1591
1592                                                eventGroup.removeFromSamplingEvents(event)
1593                                        } else {
1594                                                eventGroup.removeFromEvents(event)
1595                                        }
1596                                }
1597                        }
1598                }
1599
1600                // handle the (sampling) events
1601                ( ((flow.study.events) ? flow.study.events : []) + ((flow.study.samplingEvents) ? flow.study.samplingEvents : []) ) .each() { event ->
1602                        event.giveFields().each() { field ->
1603                                event.setFieldValue(
1604                                        field.name,
1605                                        params.get( 'event_' + event.getIdentifier() + '_' + field.escapedName() )
1606                                )
1607                        }
1608
1609                        // validate event
1610                        if (!event.validate()) {
1611                                errors = true
1612                                this.appendErrors(event, flash.wizardErrors)
1613                        }
1614                }
1615
1616                return !errors
1617        }
1618
1619        /**
1620         * Handle the wizard group page
1621         *
1622         * @param Map LocalAttributeMap (the flow scope)
1623         * @param Map localAttributeMap (the flash scope)
1624         * @param Map GrailsParameterMap (the flow parameters = form data)
1625         * @returns boolean
1626         */
1627        def groupPage(flow, flash, params) {
1628                def errors = false
1629                flash.wizardErrors = [:]
1630
1631                // remember the params in the flash scope
1632                flash.values = params
1633
1634                // iterate through groups
1635                flow.study.eventGroups.each() { eventGroup ->
1636                        // iterate through subjects
1637                        flow.study.subjects.each() { subject ->
1638                                if (params.get('subject_' + subject.getIdentifier() + '_group_' + eventGroup.getIdentifier() )) {
1639                                        // check if this subject is already part of this eventGroup
1640                                        if ( !eventGroup.subjects.find { it.equals(subject) } ) {
1641                                                // add to eventGroup
1642                                                eventGroup.addToSubjects(subject)
1643
1644                                                // iterate through samplingEvents
1645                                                eventGroup.samplingEvents.each() { samplingEvent ->
1646                                                        def samplingEventName = ucwords(samplingEvent.template.name)
1647                                                        def eventGroupName = ucwords(eventGroup.name)
1648                                                        def sampleName = (ucwords(subject.name) + '_' + samplingEventName + '_' + eventGroupName + '_' + new RelTime(samplingEvent.startTime).toString()).replaceAll("([ ]{1,})", "")
1649                                                        def tempSampleIterator = 0
1650                                                        def tempSampleName = sampleName
1651
1652                                                        // make sure sampleName is unique
1653                                                        if (flow.study.samples) {
1654                                                                while (flow.study.samples.find { it.name == tempSampleName }) {
1655                                                                        tempSampleIterator++
1656                                                                        tempSampleName = sampleName + "_" + tempSampleIterator
1657                                                                }
1658                                                                sampleName = tempSampleName
1659                                                        }
1660
1661                                                        // instantiate a sample
1662                                                        flow.study.addToSamples(
1663                                                                new Sample(
1664                                                                        parentSubject   : subject,
1665                                                                        parentEvent             : samplingEvent,
1666                                                                        parentEventGroup: eventGroup,
1667                                                                        name                    : sampleName,
1668                                                                        template                : (samplingEvent.sampleTemplate) ? samplingEvent.sampleTemplate : ''
1669                                                                )
1670                                                        )
1671                                                }
1672                                        } else {
1673                                        }
1674                                } else {
1675                                        // check if this subject is a member of this eventGroup
1676                                        if (eventGroup.subjects.find { it.equals(subject) }) {
1677                                                // remove from eventGroup
1678                                                eventGroup.removeFromSubjects(subject)
1679
1680                                                // iterate through samplingEvents
1681                                                eventGroup.samplingEvents.each() { samplingEvent ->
1682                                                        flow.study.samples.findAll { (it.parentEvent.equals(samplingEvent) && it.parentSubject.equals(subject) && it.parentEventGroup.equals(eventGroup)) }.each() {
1683                                                                // delete this sample
1684                                                                flow.study.removeFromSamples(it)
1685                                                                it.delete()
1686                                                        }
1687                                                }
1688                                        }
1689                                }
1690                        }
1691                }
1692        }
1693
1694        /**
1695         * Handle the wizard samples page
1696         *
1697         * @param Map LocalAttributeMap (the flow scope)
1698         * @param Map localAttributeMap (the flash scope)
1699         * @param Map GrailsParameterMap (the flow parameters = form data)
1700         * @returns boolean
1701         */
1702        def samplePage(flow, flash, params) {
1703                def errors = false
1704                flash.wizardErrors = [:]
1705
1706                // remember the params in the flash scope
1707                flash.values = params
1708
1709                // iterate through samples
1710                flow.study.samples.each() { sample ->
1711                        // iterate through sample fields
1712                        sample.giveFields().each() { field ->
1713                                def value = params.get('sample_'+sample.getIdentifier()+'_'+field.escapedName())
1714
1715                                // set field value
1716                                if (!(field.name == 'name' && !value)) {
1717                                        log.info "setting "+field.name+" to "+value
1718                                        sample.setFieldValue(field.name, value)
1719                                }
1720                        }
1721
1722                        // has the template changed?
1723                        def templateName = params.get('template_' + sample.getIdentifier())
1724                        if (templateName && sample.template?.name != templateName) {
1725                                sample.template = Template.findByName(templateName)
1726                        }
1727
1728                        // validate sample
1729                        if (!sample.validate()) {
1730                                errors = true
1731                                this.appendErrors(sample, flash.wizardErrors, 'sample_' + sample.getIdentifier() + '_' )
1732                                log.info 'error-> sample_'+sample.getIdentifier()
1733                        }
1734                }
1735
1736                return !errors
1737        }
1738
1739        /**
1740         * Handle the wizard assays page
1741         *
1742         * @param Map LocalAttributeMap (the flow scope)
1743         * @param Map localAttributeMap (the flash scope)
1744         * @param Map GrailsParameterMap (the flow parameters = form data)
1745         * @returns boolean
1746         */
1747        def assayPage(flow, flash, params) {
1748                def errors = false
1749                flash.wizardErrors = [:]
1750
1751                // remember the params in the flash scope
1752                flash.values = params
1753
1754                // handle the 'add assay' form
1755                if (flow.assay) {
1756                        flow.assay.giveFields().each() { field ->
1757                                // set field
1758                                flow.assay.setFieldValue(
1759                                        field.name,
1760                                        params.get(field.escapedName())
1761                                )
1762                        }
1763                }
1764
1765                // handle the assay data
1766                flow.study.assays.each() { assay ->
1767                        // set data
1768                        assay.giveFields().each() { field ->
1769                                assay.setFieldValue(
1770                                        field.name,
1771                                        params.get( 'assay_' + assay.getIdentifier() + '_' + field.escapedName() )
1772                                )
1773                        }
1774
1775                        // validate assay
1776                        if (!assay.validate()) {
1777                                errors = true
1778                                this.appendErrors(assay, flash.wizardErrors, 'assay_' + assay.getIdentifier() + '_')
1779                        }
1780                }
1781
1782                return !errors
1783        }
1784
1785        /**
1786         * Handle the wizard assayGroups page
1787         *
1788         * @param Map LocalAttributeMap (the flow scope)
1789         * @param Map localAttributeMap (the flash scope)
1790         * @param Map GrailsParameterMap (the flow parameters = form data)
1791         * @returns boolean
1792         */
1793        def assayGroupPage(flow, flash, params) {
1794                def errors = false
1795                flash.wizardErrors = [:]
1796
1797                // remember the params in the flash scope
1798                flash.values = params
1799
1800                // iterate through samples
1801                flow.study.samples.each() { sample ->
1802                        // iterate through assays
1803                        flow.study.assays.each() { assay ->
1804                                if (params.get( 'sample_' + sample.getIdentifier() + '_assay_' + assay.getIdentifier() )) {
1805                                        // add sample to assay
1806                                        assay.addToSamples( sample )
1807                                } else {
1808                                        // remove sample from assay
1809                                        assay.removeFromSamples( sample )
1810                                }
1811                        }
1812                }
1813
1814                return !errors
1815        }
1816
1817        /**
1818         * groovy / java equivalent of php's ucwords function
1819         *
1820         * Capitalize all first letters of separate words
1821         *
1822         * @param String
1823         * @return String
1824         */
1825        public static ucwords(String text) {
1826                def newText = ''
1827
1828                // change case to lowercase
1829                text = text.toLowerCase()
1830
1831                // iterate through words
1832                text.split(" ").each() {
1833                        newText += it[0].toUpperCase() + it.substring(1) + " "
1834                }
1835
1836                return newText.substring(0, newText.size()-1)
1837        }
1838
1839        /**
1840         * return the object from a map of objects by searching for a name
1841         * @param String name
1842         * @param Map map of objects
1843         * @return Object
1844         */
1845        def getObjectByName(name, map) {
1846                def result = null
1847                map.each() {
1848                        if (it.name == name) {
1849                                result = it
1850                        }
1851                }
1852
1853                return result
1854        }
1855
1856        /**
1857         * transform domain class validation errors into a human readable
1858         * linked hash map
1859         * @param object validated domain class
1860         * @return object  linkedHashMap
1861         */
1862        def getHumanReadableErrors(object) {
1863                def errors = [:]
1864                object.errors.getAllErrors().each() { error ->
1865                        // error.codes.each() { code -> println code }
1866
1867                        // generally speaking g.message(...) should work,
1868                        // however it fails in some steps of the wizard
1869                        // (add event, add assay, etc) so g is not always
1870                        // availably. Using our own instance of the
1871                        // validationTagLib instead so it is always
1872                        // available to us
1873                        errors[ error.getArguments()[0] ] = validationTagLib.message(error: error)
1874                }
1875
1876                return errors
1877        }
1878
1879        /**
1880         * append errors of a particular object to a map
1881         * @param object
1882         * @param map linkedHashMap
1883         * @void
1884         */
1885        def appendErrors(object, map) {
1886                this.appendErrorMap(getHumanReadableErrors(object), map)
1887        }
1888
1889        def appendErrors(object, map, prepend) {
1890                this.appendErrorMap(getHumanReadableErrors(object), map, prepend)
1891        }
1892
1893        /**
1894         * append errors of one map to another map
1895         * @param map linkedHashMap
1896         * @param map linkedHashMap
1897         * @void
1898         */
1899        def appendErrorMap(map, mapToExtend) {
1900                map.each() {key, value ->
1901                        mapToExtend[key] = ['key': key, 'value': value, 'dynamic': false]
1902                }
1903        }
1904
1905        def appendErrorMap(map, mapToExtend, prepend) {
1906                map.each() {key, value ->
1907                        mapToExtend[prepend + key] = ['key': key, 'value': value, 'dynamic': true]
1908                }
1909        }
1910
1911        /**
1912         * Parses a RelTime string and returns a nice human readable string
1913         *
1914         * @return Human Readable string or a HTTP response code 400 on error
1915         */
1916        def ajaxParseRelTime = {
1917                if (params.reltime == null) {
1918                        response.status = 400
1919                        render('reltime parameter is expected')
1920                }
1921
1922                try {
1923                        def reltime = RelTime.parseRelTime(params.reltime)
1924                        render reltime.toPrettyString()
1925                } catch (IllegalArgumentException e) {
1926                        response.status = 400
1927                        render(e.getMessage())
1928                }
1929        }
1930
1931        /**
1932         * Proxy for searching PubMed articles (or other articles from the Entrez DB).
1933         *
1934         * This proxy is needed because it is not allowed to fetch XML directly from a different
1935         * domain using javascript. So we have the javascript call a function on our own domain
1936         * and the proxy will fetch the data from Entrez
1937         *
1938         * @since       20100609
1939         * @param       _utility        The name of the utility, without the complete path. Example: 'esearch.fcgi'
1940         * @return      XML
1941         */
1942        def entrezProxy = {
1943                // Remove unnecessary parameters
1944                params.remove( "action" )
1945                params.remove( "controller" )
1946
1947                def url = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils";
1948                def util = params.remove( "_utility" )
1949                def paramString = params.collect { k, v -> k + '=' + v.encodeAsURL() }.join( '&' );
1950
1951                def fullUrl = url + '/' + util + '?' + paramString;
1952
1953                // Return the output of the request
1954                // render fullUrl;
1955                render(
1956                    text:           new URL( fullUrl ).getText(),
1957                    contentType:    "text/xml",
1958                    encoding:       "UTF-8"
1959                );
1960        }
1961}
Note: See TracBrowser for help on using the repository browser.