1 | package dbnp.studycapturing |
---|
2 | |
---|
3 | import dbnp.studycapturing.* |
---|
4 | import dbnp.data.* |
---|
5 | import 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: 180 $ |
---|
19 | * $Author: duh $ |
---|
20 | * $Date: 2010-02-09 14:22:46 +0000 (di, 09 feb 2010) $ |
---|
21 | */ |
---|
22 | class 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 | def errors = false |
---|
128 | flash.errors = new LinkedHashMap() |
---|
129 | |
---|
130 | // got one or more subjects? |
---|
131 | if (flow.subjects.size() < 1) { |
---|
132 | errors = true; |
---|
133 | } else { |
---|
134 | // handle form data |
---|
135 | def id = 0; |
---|
136 | flow.subjects.each() { |
---|
137 | // store subject properties |
---|
138 | it.name = params.get('subject_' + id + '_name') |
---|
139 | it.species = Term.findByName(params.get('subject_' + id + '_species')) |
---|
140 | |
---|
141 | // clear lists |
---|
142 | def stringList = new LinkedHashMap(); |
---|
143 | def intList = new LinkedHashMap(); |
---|
144 | |
---|
145 | // get all template fields |
---|
146 | flow.study.template.subjectFields.each() { |
---|
147 | // get value |
---|
148 | def value = params.get('subject_' + id + '_' + it.name); |
---|
149 | if (value) { |
---|
150 | // add to template parameters |
---|
151 | switch (it.type) { |
---|
152 | case 'STRINGLIST': |
---|
153 | stringList[it.name] = value |
---|
154 | break; |
---|
155 | case 'INTEGER': |
---|
156 | intList[ it.name ] = value |
---|
157 | break; |
---|
158 | default: |
---|
159 | // unsupported type? |
---|
160 | println "ERROR: unsupported type: "+it.type |
---|
161 | break; |
---|
162 | } |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | // set field data |
---|
167 | it.templateStringFields = stringList |
---|
168 | it.templateIntegerFields = intList |
---|
169 | |
---|
170 | // validate subject |
---|
171 | if (!it.validate()) { |
---|
172 | errors = true |
---|
173 | //println id + ' :: ' + it.errors.getAllErrors() |
---|
174 | this.appendErrors(it, flash.errors) |
---|
175 | } |
---|
176 | |
---|
177 | id++; |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | // got errors? |
---|
182 | if (errors) { |
---|
183 | println flash.errors |
---|
184 | error() |
---|
185 | } else { |
---|
186 | success() |
---|
187 | } |
---|
188 | }.to "groups" |
---|
189 | on("previous") { |
---|
190 | // TODO |
---|
191 | }.to "study" |
---|
192 | } |
---|
193 | |
---|
194 | groups { |
---|
195 | render(view: "_groups") |
---|
196 | onRender { |
---|
197 | flow.page = 3 |
---|
198 | |
---|
199 | if (!flow.groups) { |
---|
200 | flow.groups = [] |
---|
201 | } |
---|
202 | } |
---|
203 | on("add") { |
---|
204 | def increment = flow.groups.size() |
---|
205 | flow.groups[increment] = new SubjectGroup(params) |
---|
206 | |
---|
207 | println flow.groups |
---|
208 | }.to "groups" |
---|
209 | on("next") { |
---|
210 | // TODO |
---|
211 | }.to "groups" |
---|
212 | on("previous") { |
---|
213 | // TODO |
---|
214 | }.to "subjects" |
---|
215 | } |
---|
216 | |
---|
217 | // render page three |
---|
218 | demo { |
---|
219 | render(view: "_three") |
---|
220 | onRender { |
---|
221 | flow.page = 4 |
---|
222 | } |
---|
223 | on("previous") { |
---|
224 | // TODO |
---|
225 | }.to "groups" |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | /** |
---|
230 | * transform domain class validation errors into a human readable |
---|
231 | * linked hash map |
---|
232 | * @param object validated domain class |
---|
233 | * @returns object linkedHashMap |
---|
234 | */ |
---|
235 | def getHumanReadableErrors(object) { |
---|
236 | def errors = new LinkedHashMap() |
---|
237 | |
---|
238 | object.errors.getAllErrors().each() { |
---|
239 | errors[it.getArguments()[0]] = it.getDefaultMessage() |
---|
240 | } |
---|
241 | |
---|
242 | return errors |
---|
243 | } |
---|
244 | |
---|
245 | /** |
---|
246 | * append errors of a particular object to a map |
---|
247 | * @param object |
---|
248 | * @param map linkedHashMap |
---|
249 | * @void |
---|
250 | */ |
---|
251 | def appendErrors(object, map) { |
---|
252 | this.appendErrorMap(this.getHumanReadableErrors(object), map) |
---|
253 | } |
---|
254 | |
---|
255 | /** |
---|
256 | * append errors of one map to another map |
---|
257 | * @param map linkedHashMap |
---|
258 | * @param map linkedHashMap |
---|
259 | * @void |
---|
260 | */ |
---|
261 | def appendErrorMap(map, mapToExtend) { |
---|
262 | map.each() {key, value -> |
---|
263 | mapToExtend[key] = value |
---|
264 | } |
---|
265 | } |
---|
266 | } |
---|