1 | /** |
---|
2 | * PilotController Controler |
---|
3 | * |
---|
4 | * Description of my controller |
---|
5 | * |
---|
6 | * @author m.s.vanvliet@lacdr.leidenuniv.nl (Michael van Vliet) |
---|
7 | * @since 20101019 |
---|
8 | * @package nmc |
---|
9 | * |
---|
10 | * Revision information: |
---|
11 | * $Rev: 1046 $ |
---|
12 | * $Author: m.s.vanvliet@lacdr.leidenuniv.nl $ |
---|
13 | * $Date: 2010-11-02 09:25:06 +0000 (di, 02 nov 2010) $ |
---|
14 | */ |
---|
15 | package nmc |
---|
16 | |
---|
17 | import dbnp.studycapturing.*; |
---|
18 | |
---|
19 | class PilotController { |
---|
20 | |
---|
21 | static allowedMethods = [save: "POST", update: "POST", delete: "POST"] |
---|
22 | |
---|
23 | /** |
---|
24 | * Fires after every action and determines the layout of the page |
---|
25 | */ |
---|
26 | def afterInterceptor = { model, modelAndView -> |
---|
27 | |
---|
28 | if ( params['dialog'] ) { |
---|
29 | model.layout = 'dialog'; |
---|
30 | model.extraparams = [ 'dialog': 'true' ] ; |
---|
31 | } else { |
---|
32 | model.layout = 'main'; |
---|
33 | model.extraparams = [] ; |
---|
34 | } |
---|
35 | } |
---|
36 | |
---|
37 | def index = { |
---|
38 | |
---|
39 | def studyInstance = new Study() |
---|
40 | studyInstance.properties = params |
---|
41 | |
---|
42 | [studyInstanceList: Study.list(params), studyInstance: studyInstance] |
---|
43 | } |
---|
44 | |
---|
45 | def list = { |
---|
46 | params.max = Math.min(params.max ? params.int('max') : 10, 100) |
---|
47 | [studyInstanceList: Study.list(params), studyInstanceTotal: Study.count()] |
---|
48 | } |
---|
49 | |
---|
50 | def save = { |
---|
51 | def studyInstance = new Study(params) |
---|
52 | |
---|
53 | //For Pilot we do not ask for code, we generate it for the user |
---|
54 | studyInstance.code = params?.title?.encodeAsMD5() |
---|
55 | |
---|
56 | def extraparams = new LinkedHashMap(); |
---|
57 | |
---|
58 | if( params[ 'dialog' ] ) { |
---|
59 | extraparams[ 'dialog' ] = params[ 'dialog' ] |
---|
60 | } |
---|
61 | |
---|
62 | if (studyInstance.save(flush: true)) { |
---|
63 | |
---|
64 | //Study was created, now setup a NMC - Metabolomics Assay for testing |
---|
65 | def assayInstance = new Assay() |
---|
66 | assayInstance.name = "${studyInstance.title} - Metabolomics Assay" |
---|
67 | assayInstance.module = AssayModule.findByName("Metabolomics module") |
---|
68 | assayInstance.externalAssayID = assayInstance?.name?.encodeAsMD5() |
---|
69 | studyInstance.addToAssays(assayInstance) |
---|
70 | assayInstance.save(flush: true) |
---|
71 | |
---|
72 | //flash.message = "${message(code: 'default.created.message', args: [message(code: 'study.label', default: 'Study'), ( studyInstance.title ? studyInstance.title : "" ) + " " + ( studyInstance.code ? studyInstance.code : "" )])}" |
---|
73 | |
---|
74 | redirect(action: "show", id: studyInstance.id, params: extraparams ) |
---|
75 | } |
---|
76 | else { |
---|
77 | render(view: "index", model: [studyInstance: studyInstance]) |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | def show = { |
---|
82 | def studyInstance = Study.get(params.id) |
---|
83 | if (!studyInstance) { |
---|
84 | flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}" |
---|
85 | redirect(action: "list") |
---|
86 | } |
---|
87 | else { |
---|
88 | [studyInstance: studyInstance] |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | def edit = { |
---|
93 | def studyInstance = Study.get(params.id) |
---|
94 | if (!studyInstance) { |
---|
95 | flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}" |
---|
96 | redirect(action: "list") |
---|
97 | } |
---|
98 | else { |
---|
99 | return [studyInstance: studyInstance] |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | def update = { |
---|
104 | def studyInstance = Study.get(params.id) |
---|
105 | |
---|
106 | //For Pilot we do not ask for code, we generate it for the user |
---|
107 | studyInstance.code = studyInstance?.title?.encodeAsMD5() |
---|
108 | |
---|
109 | def extraparams = new LinkedHashMap(); |
---|
110 | |
---|
111 | if( params[ 'dialog' ] ) { |
---|
112 | extraparams[ 'dialog' ] = params[ 'dialog' ] |
---|
113 | } |
---|
114 | |
---|
115 | if (studyInstance) { |
---|
116 | if (params.version) { |
---|
117 | def version = params.version.toLong() |
---|
118 | if (studyInstance.version > version) { |
---|
119 | |
---|
120 | studyInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'study.label', default: 'Study')] as Object[], "Another user has updated this Study while you were editing") |
---|
121 | render(view: "edit", model: [studyInstance: studyInstance]) |
---|
122 | return |
---|
123 | } |
---|
124 | } |
---|
125 | studyInstance.properties = params |
---|
126 | if (!studyInstance.hasErrors() && studyInstance.save(flush: true)) { |
---|
127 | flash.message = "${message(code: 'default.created.message', args: [message(code: 'study.label', default: 'Study'), ( studyInstance.title ? studyInstance.title : "" ) + " " + ( studyInstance.code ? studyInstance.code : "" )])}" |
---|
128 | redirect(action: "show", id: studyInstance.id, params: extraparams) |
---|
129 | } |
---|
130 | else { |
---|
131 | render(view: "edit", model: [studyInstance: studyInstance]) |
---|
132 | } |
---|
133 | } |
---|
134 | else { |
---|
135 | flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}" |
---|
136 | redirect(action: "list", params: extraparams) |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | def delete = { |
---|
141 | def studyInstance = Study.get(params.id) |
---|
142 | |
---|
143 | def extraparams = new LinkedHashMap(); |
---|
144 | |
---|
145 | if( params[ 'dialog' ] ) { |
---|
146 | extraparams[ 'dialog' ] = params[ 'dialog' ] |
---|
147 | } |
---|
148 | |
---|
149 | if (studyInstance) { |
---|
150 | def studyName = ( studyInstance.title ? studyInstance.title : "" ) + " " + ( studyInstance.code ? studyInstance.code : "" ); |
---|
151 | try { |
---|
152 | studyInstance.delete(flush: true) |
---|
153 | flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'study.label', default: 'Study'), studyName])}" |
---|
154 | redirect(action: "list", params: extraparams) |
---|
155 | } |
---|
156 | catch (org.springframework.dao.DataIntegrityViolationException e) { |
---|
157 | flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'study.label', default: 'Study'), studyName])}" |
---|
158 | redirect(action: "show", id: params.id, params: extraparams) |
---|
159 | } |
---|
160 | } |
---|
161 | else { |
---|
162 | flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'study.label', default: 'Study'), studyName])}" |
---|
163 | redirect(action: "list", params: extraparams) |
---|
164 | } |
---|
165 | } |
---|
166 | } |
---|