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: 138 $ |
---|
19 | * $Author: duh $ |
---|
20 | * $Date: 2010-01-26 15:46:01 +0000 (di, 26 jan 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 | println "wizard started" |
---|
47 | |
---|
48 | // define flow variables |
---|
49 | flow.page = 0 |
---|
50 | flow.pages = [ |
---|
51 | [title: 'Study'], // study |
---|
52 | [title: 'Subjects'], // subjects |
---|
53 | [title: 'Form elements demo page'] |
---|
54 | ] |
---|
55 | |
---|
56 | } |
---|
57 | |
---|
58 | // render the main wizard page which immediately |
---|
59 | // triggers the 'next' action (hence, the main |
---|
60 | // page dynamically renders the study template |
---|
61 | // and makes the flow jump to the study logic) |
---|
62 | mainPage { |
---|
63 | render(view: "/wizard/index") |
---|
64 | onRender { |
---|
65 | flow.page = 1 |
---|
66 | } |
---|
67 | on("next").to "study" |
---|
68 | } |
---|
69 | |
---|
70 | // render the study page and handle study logic |
---|
71 | study { |
---|
72 | render(view: "_study") |
---|
73 | onRender { |
---|
74 | println "render page one" |
---|
75 | flow.page = 1 |
---|
76 | } |
---|
77 | on("next") { |
---|
78 | // create date instance from date string? |
---|
79 | // @see WizardTagLibrary::dateElement{...} |
---|
80 | if (params.get('startDate')) { |
---|
81 | params.startDate = new Date().parse("d/M/yyyy", params.get('startDate').toString()) |
---|
82 | } |
---|
83 | |
---|
84 | // create a study instance |
---|
85 | flow.study = new Study(params) |
---|
86 | |
---|
87 | // validate study |
---|
88 | if (flow.study.validate()) { |
---|
89 | println "ok" |
---|
90 | success() |
---|
91 | } else { |
---|
92 | // validation failed, feedback errors |
---|
93 | flash.errors = new LinkedHashMap() |
---|
94 | this.appendErrors(flow.study,flash.errors) |
---|
95 | error() |
---|
96 | } |
---|
97 | }.to "subjects" |
---|
98 | } |
---|
99 | |
---|
100 | // render page two |
---|
101 | subjects { |
---|
102 | render(view: "_subjects") |
---|
103 | onRender { |
---|
104 | flow.page = 2 |
---|
105 | |
---|
106 | if (!flow.subjects) { |
---|
107 | flow.subjects = new LinkedHashMap() |
---|
108 | } |
---|
109 | } |
---|
110 | on ("add") { |
---|
111 | def speciesTerm = Term.findByName(params.addSpecies) |
---|
112 | |
---|
113 | // add x subject of species y |
---|
114 | (params.addNumber as int).times { |
---|
115 | def increment = flow.subjects.size() |
---|
116 | flow.subjects[ increment ] = new Subject( |
---|
117 | name: 'Subject ' + (increment+1), |
---|
118 | species: speciesTerm |
---|
119 | ) |
---|
120 | } |
---|
121 | }.to "subjects" |
---|
122 | on("next") { |
---|
123 | // got one or more subjects? |
---|
124 | if (flow.subjects.size() < 1) { |
---|
125 | error() |
---|
126 | } |
---|
127 | }.to "pageThree" |
---|
128 | on("previous") { |
---|
129 | // handle data? |
---|
130 | // go to study page |
---|
131 | }.to "study" |
---|
132 | } |
---|
133 | |
---|
134 | // render page three |
---|
135 | pageThree { |
---|
136 | render(view: "_three") |
---|
137 | onRender { |
---|
138 | println "render page three" |
---|
139 | flow.page = 3 |
---|
140 | } |
---|
141 | on("previous") { |
---|
142 | println "previous page!" |
---|
143 | }.to "subjects" |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * transform domain class validation errors into a human readable |
---|
149 | * linked hash map |
---|
150 | * @param object validated domain class |
---|
151 | * @returns object linkedHashMap |
---|
152 | */ |
---|
153 | def getHumanReadableErrors(object) { |
---|
154 | def errors = new LinkedHashMap() |
---|
155 | |
---|
156 | object.errors.getAllErrors().each() { |
---|
157 | errors[it.getArguments()[0]] = it.getDefaultMessage() |
---|
158 | } |
---|
159 | |
---|
160 | return errors |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | * append errors of a particular object to a map |
---|
165 | * @param object |
---|
166 | * @param map linkedHashMap |
---|
167 | * @void |
---|
168 | */ |
---|
169 | def appendErrors(object, map) { |
---|
170 | this.appendErrorMap(this.getHumanReadableErrors(object), map) |
---|
171 | } |
---|
172 | |
---|
173 | /** |
---|
174 | * append errors of one map to another map |
---|
175 | * @param map linkedHashMap |
---|
176 | * @param map linkedHashMap |
---|
177 | * @void |
---|
178 | */ |
---|
179 | def appendErrorMap(map, mapToExtend) { |
---|
180 | map.each() {key, value -> |
---|
181 | mapToExtend[key] = value |
---|
182 | } |
---|
183 | } |
---|
184 | } |
---|