source: trunk/grails-app/controllers/dbnp/studycapturing/WizardController.groovy @ 181

Last change on this file since 181 was 181, checked in by duh, 14 years ago
  • seperated wizard subject handling logic out of the webflow to make it re-usable for different flow actions
  • Property svn:keywords set to
    Date
    Author
    Rev
File size: 6.0 KB
Line 
1package dbnp.studycapturing
2
3import dbnp.studycapturing.*
4import dbnp.data.*
5import grails.converters.*
6
7/**
8 * Wizard Controler
9 *
10 * The wizard controller handles the handeling of pages and data flow
11 * through the study capturing wizard.
12 *
13 * @author Jeroen Wesbeek
14 * @since 20100107
15 * @package studycapturing
16 *
17 * Revision information:
18 * $Rev: 181 $
19 * $Author: duh $
20 * $Date: 2010-02-09 15:13:42 +0000 (di, 09 feb 2010) $
21 */
22class WizardController {
23        /**
24         * index method, redirect to the webflow
25         * @void
26         */
27        def index = {
28                /**
29                 * Do you believe it in your head?
30                 * I can go with the flow
31                 * Don't say it doesn't matter (with the flow) matter anymore
32                 * I can go with the flow (I can go)
33                 * Do you believe it in your head?
34                 */
35                redirect(action: 'pages')
36        }
37
38        /**
39         * WebFlow definition
40         * @see http://grails.org/WebFlow
41         * @void
42         */
43        def pagesFlow = {
44                // start the flow
45                onStart {
46                        // define flow variables
47                        flow.page = 0
48                        flow.pages = [
49                                [title: 'Study'],               // study
50                                [title: 'Subjects'],    // subjects
51                                [title: 'Groups'],              // groups
52                                [title: 'Form elements demo page']
53                        ]
54
55                }
56
57                // render the main wizard page which immediately
58                // triggers the 'next' action (hence, the main
59                // page dynamically renders the study template
60                // and makes the flow jump to the study logic)
61                mainPage {
62                        render(view: "/wizard/index")
63                        onRender {
64                                flow.page = 1
65                        }
66                        on("next").to "study"
67                }
68
69                // render the study page and handle study logic
70                study {
71                        render(view: "_study")
72                        onRender {
73                                flow.page = 1
74                        }
75                        on("next") {
76                                // create date instance from date string?
77                                // @see WizardTagLibrary::dateElement{...}
78                                if (params.get('startDate')) {
79                                        params.startDate = new Date().parse("d/M/yyyy", params.get('startDate').toString())
80                                }
81
82                                // if a template is selected, get template instance
83                                if (params.get('template')) {
84                                        params.template = Template.findByName(params.get('template'))
85                                }
86
87                                // create a study instance
88                                flow.study = new Study(params)
89
90                                // validate study
91                                if (flow.study.validate()) {
92                                        success()
93                                } else {
94                                        // validation failed, feedback errors
95                                        flash.errors = new LinkedHashMap()
96                                        this.appendErrors(flow.study, flash.errors)
97                                        error()
98                                }
99                        }.to "subjects"
100                }
101
102                // render page two
103                subjects {
104                        render(view: "_subjects")
105                        onRender {
106                                flow.page = 2
107
108                                if (!flow.subjects) {
109                                        flow.subjects = []
110                                }
111                        }
112                        on("add") {
113                                // fetch species by name (as posted by the form)
114                                def speciesTerm = Term.findByName(params.addSpecies)
115
116                                // add x subject of species y
117                                (params.addNumber as int).times {
118                                        def increment = flow.subjects.size()
119                                        flow.subjects[increment] = new Subject(
120                                                name: 'Subject ' + (increment + 1),
121                                                species: speciesTerm,
122                                                template: flow.study.template
123                                        )
124                                }
125                        }.to "subjects"
126                        on("next") {
127                                flash.errors = new LinkedHashMap()
128
129                                // handle form data
130                                if (!this.handleSubjects(flow, flash, params)) {
131                                        error()
132                                } else {
133                                        success()
134                                }
135                        }.to "groups"
136                        on("previous") {
137                                flash.errors = new LinkedHashMap()
138
139                                // handle form data
140                                if (!this.handleSubjects(flow, flash, params)) {
141                                        error()
142                                } else {
143                                        success()
144                                }
145                        }.to "study"
146                }
147
148                groups {
149                        render(view: "_groups")
150                        onRender {
151                                flow.page = 3
152
153                                if (!flow.groups) {
154                                        flow.groups = []
155                                }
156                        }
157                        on("add") {
158                                def increment = flow.groups.size()
159                                flow.groups[increment] = new SubjectGroup(params)
160                        }.to "groups"
161                        on("next") {
162                                // TODO
163                        }.to "groups"
164                        on("previous") {
165                                // TODO
166                        }.to "subjects"
167                }
168
169                // render page three
170                demo {
171                        render(view: "_three")
172                        onRender {
173                                flow.page = 4
174                        }
175                        on("previous") {
176                                // TODO
177                        }.to "groups"
178                }
179        }
180
181        /**
182         * re-usable code for handling subject form data in a web flow
183         * @param Map   LocalAttributeMap (the flow scope)
184         * @param Map   localAttributeMap (the flash scope)
185         * @param Map   GrailsParameterMap (the flow parameters = form data)
186         * @returns boolean
187         */
188        def handleSubjects(flow, flash, params) {
189                println flow.getClass()
190                println flash.getClass()
191                println params.getClass()
192               
193                if (flow.subjects.size() < 1) {
194                        return false
195                } else {
196                        def errors = false;
197                        def id = 0;
198                        flow.subjects.each() {
199                                // store subject properties
200                                it.name = params.get('subject_' + id + '_name')
201                                it.species = Term.findByName(params.get('subject_' + id + '_species'))
202
203                                // clear lists
204                                def stringList = new LinkedHashMap();
205                                def intList = new LinkedHashMap();
206
207                                // get all template fields
208                                flow.study.template.subjectFields.each() {
209                                        // get value
210                                        def value = params.get('subject_' + id + '_' + it.name);
211
212                                        if (value) {
213                                                // add to template parameters
214                                                switch (it.type) {
215                                                        case 'STRINGLIST':
216                                                                stringList[it.name] = value
217                                                                break;
218                                                        case 'INTEGER':
219                                                                intList[it.name] = value
220                                                                break;
221                                                        default:
222                                                                // unsupported type?
223                                                                println "ERROR: unsupported type: " + it.type
224                                                                break;
225                                                }
226                                        }
227                                }
228
229                                // set field data
230                                it.templateStringFields = stringList
231                                it.templateIntegerFields = intList
232
233                                // validate subject
234                                if (!it.validate()) {
235                                        errors = true
236                                        println id + ' :: ' + it.errors.getAllErrors()
237                                        this.appendErrors(it, flash.errors)
238                                }
239
240                                id++;
241                        }
242
243                        return !errors
244                }
245        }
246
247        /**
248         * transform domain class validation errors into a human readable
249         * linked hash map
250         * @param object validated domain class
251         * @returns object  linkedHashMap
252         */
253        def getHumanReadableErrors(object) {
254                def errors = new LinkedHashMap()
255
256                object.errors.getAllErrors().each() {
257                        errors[it.getArguments()[0]] = it.getDefaultMessage()
258                }
259
260                return errors
261        }
262
263        /**
264         * append errors of a particular object to a map
265         * @param object
266         * @param map linkedHashMap
267         * @void
268         */
269        def appendErrors(object, map) {
270                this.appendErrorMap(this.getHumanReadableErrors(object), map)
271        }
272
273        /**
274         * append errors of one map to another map
275         * @param map linkedHashMap
276         * @param map linkedHashMap
277         * @void
278         */
279        def appendErrorMap(map, mapToExtend) {
280                map.each() {key, value ->
281                        mapToExtend[key] = value
282                }
283        }
284}
Note: See TracBrowser for help on using the repository browser.