1 | package generic.installation |
---|
2 | |
---|
3 | import grails.plugins.springsecurity.Secured |
---|
4 | import dbnp.authentication.SecUser |
---|
5 | import org.codehaus.groovy.grails.commons.ConfigurationHolder |
---|
6 | import groovy.sql.Sql |
---|
7 | |
---|
8 | /** |
---|
9 | * ajaxflow Controller |
---|
10 | * |
---|
11 | * @author Jeroen Wesbeek |
---|
12 | * @since 20110318 |
---|
13 | * |
---|
14 | * Revision information: |
---|
15 | * $Rev: 66849 $ |
---|
16 | * $Author: duh $ |
---|
17 | * $Date: 2010-12-08 15:12:54 +0100 (Wed, 08 Dec 2010) $ |
---|
18 | */ |
---|
19 | @Secured(['ROLE_ADMIN']) |
---|
20 | class SetupController { |
---|
21 | // the pluginManager is used to check if the Grom |
---|
22 | // plugin is available so we can 'Grom' development |
---|
23 | // notifications to the unified notifications daemon |
---|
24 | // (see http://www.grails.org/plugin/grom) |
---|
25 | def pluginManager |
---|
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 | /** |
---|
36 | * Do you believe it in your head? |
---|
37 | * I can go with the flow |
---|
38 | * Don't say it doesn't matter (with the flow) matter anymore |
---|
39 | * I can go with the flow (I can go) |
---|
40 | * Do you believe it in your head? |
---|
41 | */ |
---|
42 | redirect(action: 'pages') |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * WebFlow definition |
---|
47 | * @void |
---|
48 | */ |
---|
49 | def pagesFlow = { |
---|
50 | def authenticationService |
---|
51 | |
---|
52 | // start the flow |
---|
53 | onStart { |
---|
54 | // Grom a development message |
---|
55 | if (pluginManager.getGrailsPlugin('grom')) "entering the WebFlow".grom() |
---|
56 | |
---|
57 | // get configuration |
---|
58 | def config = ConfigurationHolder.config |
---|
59 | |
---|
60 | // define variables in the flow scope which is availabe |
---|
61 | // throughout the complete webflow also have a look at |
---|
62 | // the Flow Scopes section on http://www.grails.org/WebFlow |
---|
63 | // |
---|
64 | // The following flow scope variables are used to generate |
---|
65 | // wizard tabs. Also see common/_tabs.gsp for more information |
---|
66 | flow.page = 0 |
---|
67 | flow.pages = [ |
---|
68 | [title: 'Configuration Location'], |
---|
69 | [title: 'Database'], |
---|
70 | [title: 'Email / URL'], |
---|
71 | [title: 'Summary'], |
---|
72 | [title: 'Apache Configuration'], |
---|
73 | [title: 'Done'] |
---|
74 | ] |
---|
75 | flow.cancel = true |
---|
76 | flow.quickSave = true |
---|
77 | |
---|
78 | // define famfamfam icons |
---|
79 | flow.icons = [ |
---|
80 | true : 'accept', |
---|
81 | false : 'cancel' |
---|
82 | ] |
---|
83 | |
---|
84 | success() |
---|
85 | } |
---|
86 | |
---|
87 | // render the main wizard page which immediately |
---|
88 | // triggers the 'next' action (hence, the main |
---|
89 | // page dynamically renders the study template |
---|
90 | // and makes the flow jump to the study logic) |
---|
91 | mainPage { |
---|
92 | render(view: "/setup/index") |
---|
93 | onRender { |
---|
94 | // Grom a development message |
---|
95 | if (pluginManager.getGrailsPlugin('grom')) "rendering the main Ajaxflow page (index.gsp)".grom() |
---|
96 | |
---|
97 | // let the view know we're in page 1 |
---|
98 | flow.page = 1 |
---|
99 | success() |
---|
100 | } |
---|
101 | on("next").to "configuration" |
---|
102 | } |
---|
103 | |
---|
104 | // first wizard page |
---|
105 | configuration { |
---|
106 | render(view: "_configuration_location") |
---|
107 | onRender { |
---|
108 | // Grom a development message |
---|
109 | if (pluginManager.getGrailsPlugin('grom')) ".rendering the partial: pages/_configuration.gsp".grom() |
---|
110 | |
---|
111 | flow.page = 1 |
---|
112 | |
---|
113 | // try to load config |
---|
114 | loadPropertiesFile(flow) |
---|
115 | |
---|
116 | success() |
---|
117 | } |
---|
118 | on("next") { |
---|
119 | if (flow.configInfo.pathSummary && flow.configInfo.fileSummary) { success() } else { error() } |
---|
120 | }.to "database" |
---|
121 | on("toPageTwo") { |
---|
122 | if (flow.configInfo.pathSummary && flow.configInfo.fileSummary) { success() } else { error() } |
---|
123 | }.to "database" |
---|
124 | on("toPageThree") { |
---|
125 | if (flow.configInfo.pathSummary && flow.configInfo.fileSummary) { success() } else { error() } |
---|
126 | }.to "email" |
---|
127 | on("toPageFour") { |
---|
128 | if (flow.configInfo.pathSummary && flow.configInfo.fileSummary) { success() } else { error() } |
---|
129 | }.to "summary" |
---|
130 | on("toPageFive") { |
---|
131 | if (flow.configInfo.pathSummary && flow.configInfo.fileSummary) { success() } else { error() } |
---|
132 | }.to "apache" |
---|
133 | on("toPageSix") { |
---|
134 | if (flow.configInfo.pathSummary && flow.configInfo.fileSummary) { |
---|
135 | flow.page = 6 |
---|
136 | success() |
---|
137 | } else { |
---|
138 | error() |
---|
139 | } |
---|
140 | }.to "save" |
---|
141 | on("toConfigurationPath").to "configurationPath" |
---|
142 | on("toConfigurationFile").to "configurationFile" |
---|
143 | } |
---|
144 | |
---|
145 | // create the configuration path |
---|
146 | configurationPath { |
---|
147 | action { |
---|
148 | // does the path exist? |
---|
149 | if (!flow.configInfo.pathExists) { |
---|
150 | // no, attempt to create it |
---|
151 | if (flow.configInfo.path.mkdirs()) { |
---|
152 | // Grom a development message |
---|
153 | if (pluginManager.getGrailsPlugin('grom')) ".created ${flow.configInfo.path}".grom() |
---|
154 | |
---|
155 | // success! |
---|
156 | success() |
---|
157 | } else { |
---|
158 | error() |
---|
159 | } |
---|
160 | } else { |
---|
161 | error() |
---|
162 | } |
---|
163 | } |
---|
164 | on("success").to "configuration" |
---|
165 | on("error").to "configurationPathError" |
---|
166 | } |
---|
167 | |
---|
168 | // show manual procedure for creating configuration path |
---|
169 | configurationPathError { |
---|
170 | render(view: "_configuration_path_error") |
---|
171 | onRender { |
---|
172 | // Grom a development message |
---|
173 | if (pluginManager.getGrailsPlugin('grom')) ".rendering the partial: pages/_configuration_path_error.gsp".grom() |
---|
174 | |
---|
175 | flow.page = 1 |
---|
176 | } |
---|
177 | on("next").to "configuration" |
---|
178 | } |
---|
179 | |
---|
180 | // create the configuration file |
---|
181 | configurationFile { |
---|
182 | action { |
---|
183 | // does the file exist? |
---|
184 | if (!flow.configInfo.fileExists) { |
---|
185 | // no, attempt to create it |
---|
186 | try { |
---|
187 | flow.configInfo.file << "# ${meta(name: 'app.name')} ${grails.util.GrailsUtil.environment} configuration\n" |
---|
188 | flow.configInfo.file << "#\n" |
---|
189 | flow.configInfo.file << "# \$Author\$\n" |
---|
190 | flow.configInfo.file << "# \$Date\$\n" |
---|
191 | flow.configInfo.file << "# \$Rev\$\n" |
---|
192 | |
---|
193 | // grom debug message |
---|
194 | if (pluginManager.getGrailsPlugin('grom')) ".created ${flow.configInfo.file}".grom() |
---|
195 | |
---|
196 | // success! |
---|
197 | success() |
---|
198 | } catch (Exception e) { |
---|
199 | error() |
---|
200 | } |
---|
201 | } else { |
---|
202 | error() |
---|
203 | } |
---|
204 | } |
---|
205 | on("success").to "configuration" |
---|
206 | on("error").to "configurationFileError" |
---|
207 | } |
---|
208 | |
---|
209 | // show manual procedure for creating configuration path |
---|
210 | configurationFileError { |
---|
211 | render(view: "_configuration_file_error") |
---|
212 | onRender { |
---|
213 | // Grom a development message |
---|
214 | if (pluginManager.getGrailsPlugin('grom')) ".rendering the partial: pages/_configuration_file_error.gsp".grom() |
---|
215 | |
---|
216 | flow.page = 1 |
---|
217 | } |
---|
218 | on("next").to "configuration" |
---|
219 | } |
---|
220 | |
---|
221 | // database page |
---|
222 | database { |
---|
223 | render(view: "_database") |
---|
224 | onRender { |
---|
225 | // Grom a development message |
---|
226 | if (pluginManager.getGrailsPlugin('grom')) ".rendering the partial: pages/_database.gsp".grom() |
---|
227 | |
---|
228 | flow.page = 2 |
---|
229 | |
---|
230 | success() |
---|
231 | } |
---|
232 | on("next") { |
---|
233 | // store form values |
---|
234 | databasePage(flow, flash, params) ? success() : error() |
---|
235 | }.to "email" |
---|
236 | on("previous") { |
---|
237 | // store form values |
---|
238 | databasePage(flow, flash, params) ? success() : error() |
---|
239 | }.to "configuration" |
---|
240 | on("toPageOne") { |
---|
241 | // store form values |
---|
242 | databasePage(flow, flash, params) ? success() : error() |
---|
243 | }.to "configuration" |
---|
244 | on("toPageThree") { |
---|
245 | // store form values |
---|
246 | databasePage(flow, flash, params) ? success() : error() |
---|
247 | }.to "email" |
---|
248 | on("toPageFour") { |
---|
249 | // store form values |
---|
250 | databasePage(flow, flash, params) ? success() : error() |
---|
251 | }.to "summary" |
---|
252 | on("toPageFive") { |
---|
253 | // store form values |
---|
254 | databasePage(flow, flash, params) ? success() : error() |
---|
255 | }.to "apache" |
---|
256 | on("toPageSix") { |
---|
257 | // store form values |
---|
258 | if (databasePage(flow, flash, params)) { |
---|
259 | flow.page = 6 |
---|
260 | success() |
---|
261 | } else { |
---|
262 | error() |
---|
263 | } |
---|
264 | }.to "save" |
---|
265 | } |
---|
266 | |
---|
267 | // email configuration page |
---|
268 | email { |
---|
269 | render(view: "_email") |
---|
270 | onRender { |
---|
271 | // Grom a development message |
---|
272 | if (pluginManager.getGrailsPlugin('grom')) ".rendering the partial pages/_email.gsp".grom() |
---|
273 | |
---|
274 | flow.page = 3 |
---|
275 | success() |
---|
276 | } |
---|
277 | on("next") { |
---|
278 | emailPage(flow, flash, params) ? success() : error() |
---|
279 | }.to "summary" |
---|
280 | on("previous") { |
---|
281 | emailPage(flow, flash, params) ? success() : error() |
---|
282 | }.to "database" |
---|
283 | on("toPageOne") { |
---|
284 | emailPage(flow, flash, params) ? success() : error() |
---|
285 | }.to "configuration" |
---|
286 | on("toPageTwo") { |
---|
287 | emailPage(flow, flash, params) ? success() : error() |
---|
288 | }.to "database" |
---|
289 | on("toPageFour") { |
---|
290 | emailPage(flow, flash, params) ? success() : error() |
---|
291 | }.to "summary" |
---|
292 | on("toPageFive") { |
---|
293 | emailPage(flow, flash, params) ? success() : error() |
---|
294 | }.to "apache" |
---|
295 | on("toPageSix") { |
---|
296 | if (emailPage(flow, flash, params)) { |
---|
297 | flow.page = 6 |
---|
298 | success() |
---|
299 | } else { |
---|
300 | error() |
---|
301 | } |
---|
302 | }.to "save" |
---|
303 | } |
---|
304 | |
---|
305 | // summary page |
---|
306 | summary { |
---|
307 | render(view: "_summary") |
---|
308 | onRender { |
---|
309 | // Grom a development message |
---|
310 | if (pluginManager.getGrailsPlugin('grom')) ".rendering the partial pages/_summary.gsp".grom() |
---|
311 | |
---|
312 | // write properties file |
---|
313 | writePropertiesFile(flow) |
---|
314 | |
---|
315 | flow.page = 4 |
---|
316 | success() |
---|
317 | } |
---|
318 | on("next") { |
---|
319 | // put some logic in here |
---|
320 | flow.page = 6 |
---|
321 | }.to "save" |
---|
322 | on("previous").to "email" |
---|
323 | on("toPageOne").to "configuration" |
---|
324 | on("toPageTwo").to "database" |
---|
325 | on("toPageThree").to "email" |
---|
326 | on("toPageFive").to "apache" |
---|
327 | on("toPageSix") { |
---|
328 | flow.page = 6 |
---|
329 | }.to "save" |
---|
330 | } |
---|
331 | |
---|
332 | // apache page |
---|
333 | apache { |
---|
334 | render(view: "_apache") |
---|
335 | onRender { |
---|
336 | // Grom a development message |
---|
337 | if (pluginManager.getGrailsPlugin('grom')) ".rendering the partial pages/_apache.gsp".grom() |
---|
338 | |
---|
339 | flow.page = 5 |
---|
340 | |
---|
341 | flash.context= org.codehaus.groovy.grails.web.context.ServletContextHolder.getServletContext().contextPath |
---|
342 | flash.domain = flow.configInfo.properties.getProperty('grails.serverURL').replaceFirst(/http:\/\//,"").split(":|/").first() |
---|
343 | |
---|
344 | success() |
---|
345 | } |
---|
346 | on("next") { |
---|
347 | // put some logic in here |
---|
348 | flow.page = 6 |
---|
349 | }.to "save" |
---|
350 | on("previous").to "pageThree" |
---|
351 | on("toPageOne").to "configuration" |
---|
352 | on("toPageTwo").to "database" |
---|
353 | on("toPageThree").to "email" |
---|
354 | on("toPageFour").to "summary" |
---|
355 | on("toPageSix") { |
---|
356 | flow.page = 6 |
---|
357 | }.to "save" |
---|
358 | } |
---|
359 | |
---|
360 | // save action |
---|
361 | save { |
---|
362 | action { |
---|
363 | // here you can validate and save the |
---|
364 | // instances you have created in the |
---|
365 | // ajax flow. |
---|
366 | try { |
---|
367 | // Grom a development message |
---|
368 | if (pluginManager.getGrailsPlugin('grom')) ".persisting instances to the database...".grom() |
---|
369 | |
---|
370 | // put your bussiness logic in here |
---|
371 | success() |
---|
372 | } catch (Exception e) { |
---|
373 | // put your error handling logic in |
---|
374 | // here |
---|
375 | flow.page = 4 |
---|
376 | error() |
---|
377 | } |
---|
378 | } |
---|
379 | on("error").to "error" |
---|
380 | on(Exception).to "error" |
---|
381 | on("success").to "finalPage" |
---|
382 | } |
---|
383 | |
---|
384 | // render errors |
---|
385 | error { |
---|
386 | render(view: "_error") |
---|
387 | onRender { |
---|
388 | // Grom a development message |
---|
389 | if (pluginManager.getGrailsPlugin('grom')) ".rendering the partial pages/_error.gsp".grom() |
---|
390 | |
---|
391 | // set page to 4 so that the navigation |
---|
392 | // works (it is disabled on the final page) |
---|
393 | flow.page = 4 |
---|
394 | } |
---|
395 | on("next").to "save" |
---|
396 | on("previous").to "pageFour" |
---|
397 | on("toPageOne").to "configuration" |
---|
398 | on("toPageTwo").to "database" |
---|
399 | on("toPageThree").to "email" |
---|
400 | on("toPageFour").to "summary" |
---|
401 | on("toPageFive").to "apache" |
---|
402 | on("toPageSix").to "save" |
---|
403 | |
---|
404 | } |
---|
405 | |
---|
406 | // last wizard page |
---|
407 | finalPage { |
---|
408 | render(view: "_final_page") |
---|
409 | onRender { |
---|
410 | // Grom a development message |
---|
411 | if (pluginManager.getGrailsPlugin('grom')) ".rendering the partial pages/_final_page.gsp".grom() |
---|
412 | |
---|
413 | success() |
---|
414 | } |
---|
415 | } |
---|
416 | } |
---|
417 | |
---|
418 | /** |
---|
419 | * handle database configuration |
---|
420 | * |
---|
421 | * @param Map LocalAttributeMap (the flow scope) |
---|
422 | * @param Map localAttributeMap (the flash scope) |
---|
423 | * @param Map GrailsParameterMap (the flow parameters = form data) |
---|
424 | * @returns boolean |
---|
425 | */ |
---|
426 | def databasePage(flow, flash, params) { |
---|
427 | // update database properties |
---|
428 | params.dataSource.each { name, value -> |
---|
429 | flow.configInfo.properties.setProperty("dataSource.${name}", value) |
---|
430 | } |
---|
431 | |
---|
432 | // try to connect to the database |
---|
433 | try { |
---|
434 | def sql = Sql.newInstance( |
---|
435 | flow.configInfo.properties.getProperty('dataSource.url'), |
---|
436 | flow.configInfo.properties.getProperty('dataSource.username'), |
---|
437 | flow.configInfo.properties.getProperty('dataSource.password'), |
---|
438 | flow.configInfo.properties.getProperty('dataSource.driverClassName') |
---|
439 | ) |
---|
440 | |
---|
441 | writePropertiesFile(flow) |
---|
442 | |
---|
443 | flash.connection=true |
---|
444 | } catch (Exception e) { |
---|
445 | flash.connection=false |
---|
446 | } |
---|
447 | |
---|
448 | return flash.connection |
---|
449 | } |
---|
450 | |
---|
451 | /** |
---|
452 | * handle email and url configuration |
---|
453 | * |
---|
454 | * @param Map LocalAttributeMap (the flow scope) |
---|
455 | * @param Map localAttributeMap (the flash scope) |
---|
456 | * @param Map GrailsParameterMap (the flow parameters = form data) |
---|
457 | * @returns boolean |
---|
458 | */ |
---|
459 | def emailPage(flow, flash, params) { |
---|
460 | // update properties |
---|
461 | flow.configInfo.properties.setProperty('grails.plugins.springsecurity.ui.forgotPassword.emailFrom', params['grails.plugins.springsecurity.ui.forgotPassword.emailFrom']) |
---|
462 | flow.configInfo.properties.setProperty('grails.serverURL', params['grails.serverURL']) |
---|
463 | |
---|
464 | writePropertiesFile(flow) |
---|
465 | |
---|
466 | return true |
---|
467 | } |
---|
468 | |
---|
469 | /** |
---|
470 | * load the configuration properties |
---|
471 | * |
---|
472 | * @param flow |
---|
473 | * @return |
---|
474 | */ |
---|
475 | def loadPropertiesFile(flow) { |
---|
476 | // config |
---|
477 | def configPath = new File("${System.getProperty("user.home")}/.${meta(name: 'app.name')}/") |
---|
478 | def configFile = new File("${System.getProperty("user.home")}/.${meta(name: 'app.name')}/${grails.util.GrailsUtil.environment}.properties") |
---|
479 | |
---|
480 | // add configuration information to the flow scope |
---|
481 | flow.configInfo = [ |
---|
482 | path: configPath, |
---|
483 | pathExists: configPath.exists(), |
---|
484 | pathCanRead: configPath.canRead(), |
---|
485 | pathCanWrite: configPath.canWrite(), |
---|
486 | pathSummary: (configPath.exists() && configPath.canRead() && configPath.canWrite()), |
---|
487 | file: configFile, |
---|
488 | fileExists: configFile.exists(), |
---|
489 | fileCanRead: configFile.canRead(), |
---|
490 | fileCanWrite: configFile.canWrite(), |
---|
491 | fileSummary: (configFile.exists() && configFile.canRead() && configFile.canWrite()), |
---|
492 | properties: null |
---|
493 | ] |
---|
494 | |
---|
495 | // parse properties |
---|
496 | if (flow.configInfo.pathSummary && flow.configInfo.fileSummary) { |
---|
497 | def file = new FileInputStream(flow.configInfo.file.toString()) |
---|
498 | def properties = new Properties() |
---|
499 | properties.load(file) |
---|
500 | |
---|
501 | // and store in flowscope |
---|
502 | flow.configInfo.properties = properties |
---|
503 | } |
---|
504 | } |
---|
505 | |
---|
506 | /** |
---|
507 | * save the configuration properties |
---|
508 | * |
---|
509 | * @param flow |
---|
510 | * @return |
---|
511 | */ |
---|
512 | def writePropertiesFile(flow) { |
---|
513 | // write properties |
---|
514 | def file = new FileOutputStream(flow.configInfo.file.toString()) |
---|
515 | flow.configInfo.properties.store(file, " ${meta(name: 'app.name')} ${grails.util.GrailsUtil.environment} configuration\n#\n# \$Author\$\n# \$Date\$\n# \$Rev\$\n") |
---|
516 | |
---|
517 | // and load them back into the flow |
---|
518 | loadPropertiesFile(flow) |
---|
519 | } |
---|
520 | } |
---|