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: 1222 $ |
---|
12 | * $Author: work@osx.eu $ |
---|
13 | * $Date: 2010-11-30 13:10:42 +0000 (di, 30 nov 2010) $ |
---|
14 | */ |
---|
15 | package nmc |
---|
16 | |
---|
17 | import dbnp.studycapturing.*; |
---|
18 | import grails.plugins.springsecurity.Secured |
---|
19 | |
---|
20 | |
---|
21 | class PilotController { |
---|
22 | |
---|
23 | def authenticationService |
---|
24 | |
---|
25 | static allowedMethods = [save: "POST", update: "POST", delete: "POST"] |
---|
26 | |
---|
27 | /** |
---|
28 | * Fires after every action and determines the layout of the page |
---|
29 | */ |
---|
30 | def afterInterceptor = { model, modelAndView -> |
---|
31 | |
---|
32 | if ( params['dialog'] ) { |
---|
33 | model.layout = 'dialog'; |
---|
34 | model.extraparams = [ 'dialog': 'true' ] ; |
---|
35 | } else { |
---|
36 | model.layout = 'main'; |
---|
37 | model.extraparams = [] ; |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | @Secured(['IS_AUTHENTICATED_REMEMBERED']) |
---|
42 | def index = { |
---|
43 | |
---|
44 | session.pilot = true |
---|
45 | |
---|
46 | def user = authenticationService.getLoggedInUser() |
---|
47 | def max = Math.min(params.max ? params.int('max') : 10, 100) |
---|
48 | def studies = Study.giveReadableStudies(user, max); |
---|
49 | |
---|
50 | def studyInstance = new Study() |
---|
51 | studyInstance.properties = params |
---|
52 | |
---|
53 | [studyInstanceList: studies, studyInstance: studyInstance] |
---|
54 | } |
---|
55 | |
---|
56 | def list = { |
---|
57 | params.max = Math.min(params.max ? params.int('max') : 10, 100) |
---|
58 | [studyInstanceList: Study.list(params), studyInstanceTotal: Study.count()] |
---|
59 | } |
---|
60 | |
---|
61 | def save = { |
---|
62 | def studyInstance = new Study(params) |
---|
63 | |
---|
64 | //For Pilot we do not ask for code, we generate it for the user |
---|
65 | studyInstance.code = params?.title?.encodeAsMD5() |
---|
66 | studyInstance.owner = authenticationService.getLoggedInUser() |
---|
67 | |
---|
68 | def extraparams = new LinkedHashMap(); |
---|
69 | |
---|
70 | if( params[ 'dialog' ] ) { |
---|
71 | extraparams[ 'dialog' ] = params[ 'dialog' ] |
---|
72 | } |
---|
73 | |
---|
74 | if (studyInstance.save(flush: true)) { |
---|
75 | |
---|
76 | //Study was created, now setup a NMC - Metabolomics Assay for testing |
---|
77 | def assayInstance = new Assay() |
---|
78 | assayInstance.name = "${studyInstance.title} - Metabolomics Assay" |
---|
79 | assayInstance.module = AssayModule.findByName("Metabolomics module") |
---|
80 | assayInstance.externalAssayID = assayInstance?.name?.encodeAsMD5() |
---|
81 | studyInstance.addToAssays(assayInstance) |
---|
82 | assayInstance.save(flush: true) |
---|
83 | |
---|
84 | //flash.message = "${message(code: 'default.created.message', args: [message(code: 'study.label', default: 'Study'), ( studyInstance.title ? studyInstance.title : "" ) + " " + ( studyInstance.code ? studyInstance.code : "" )])}" |
---|
85 | |
---|
86 | redirect(action: "show", id: studyInstance.id, params: extraparams ) |
---|
87 | } |
---|
88 | else { |
---|
89 | render(view: "index", model: [studyInstance: studyInstance]) |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | def show = { |
---|
94 | |
---|
95 | def studyInstance = Study.get(params.id) |
---|
96 | if (!studyInstance) { |
---|
97 | flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}" |
---|
98 | redirect(action: "list") |
---|
99 | } |
---|
100 | else { |
---|
101 | |
---|
102 | //add all samples to the assay when not there yet! |
---|
103 | studyInstance.assays.each { assay -> |
---|
104 | //if (assay.samples.size() <= 0){ // needs improving!!! too much overhead, okay for pilot |
---|
105 | studyInstance.samples.each { sample -> |
---|
106 | log.info("ADD THE DIRTY WAY!!!") |
---|
107 | assay.addToSamples(sample) |
---|
108 | } |
---|
109 | assay.save() |
---|
110 | //} |
---|
111 | } |
---|
112 | |
---|
113 | [studyInstance: studyInstance] |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | def edit = { |
---|
118 | def studyInstance = Study.get(params.id) |
---|
119 | if (!studyInstance) { |
---|
120 | flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}" |
---|
121 | redirect(action: "list") |
---|
122 | } |
---|
123 | else { |
---|
124 | return [studyInstance: studyInstance] |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | def update = { |
---|
129 | def studyInstance = Study.get(params.id) |
---|
130 | |
---|
131 | //For Pilot we do not ask for code, we generate it for the user |
---|
132 | studyInstance.code = studyInstance?.title?.encodeAsMD5() |
---|
133 | |
---|
134 | def extraparams = new LinkedHashMap(); |
---|
135 | |
---|
136 | if( params[ 'dialog' ] ) { |
---|
137 | extraparams[ 'dialog' ] = params[ 'dialog' ] |
---|
138 | } |
---|
139 | |
---|
140 | if (studyInstance) { |
---|
141 | if (params.version) { |
---|
142 | def version = params.version.toLong() |
---|
143 | if (studyInstance.version > version) { |
---|
144 | |
---|
145 | 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") |
---|
146 | render(view: "edit", model: [studyInstance: studyInstance]) |
---|
147 | return |
---|
148 | } |
---|
149 | } |
---|
150 | studyInstance.properties = params |
---|
151 | if (!studyInstance.hasErrors() && studyInstance.save(flush: true)) { |
---|
152 | flash.message = "${message(code: 'default.created.message', args: [message(code: 'study.label', default: 'Study'), ( studyInstance.title ? studyInstance.title : "" ) + " " + ( studyInstance.code ? studyInstance.code : "" )])}" |
---|
153 | redirect(action: "show", id: studyInstance.id, params: extraparams) |
---|
154 | } |
---|
155 | else { |
---|
156 | render(view: "edit", model: [studyInstance: studyInstance]) |
---|
157 | } |
---|
158 | } |
---|
159 | else { |
---|
160 | flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}" |
---|
161 | redirect(action: "list", params: extraparams) |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | def delete = { |
---|
166 | def studyInstance = Study.get(params.id) |
---|
167 | |
---|
168 | def extraparams = new LinkedHashMap(); |
---|
169 | |
---|
170 | if( params[ 'dialog' ] ) { |
---|
171 | extraparams[ 'dialog' ] = params[ 'dialog' ] |
---|
172 | } |
---|
173 | |
---|
174 | if (studyInstance) { |
---|
175 | def studyName = ( studyInstance.title ? studyInstance.title : "" ) + " " + ( studyInstance.code ? studyInstance.code : "" ); |
---|
176 | try { |
---|
177 | studyInstance.delete(flush: true) |
---|
178 | flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'study.label', default: 'Study'), studyName])}" |
---|
179 | redirect(action: "list", params: extraparams) |
---|
180 | } |
---|
181 | catch (org.springframework.dao.DataIntegrityViolationException e) { |
---|
182 | flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'study.label', default: 'Study'), studyName])}" |
---|
183 | redirect(action: "show", id: params.id, params: extraparams) |
---|
184 | } |
---|
185 | } |
---|
186 | else { |
---|
187 | flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'study.label', default: 'Study'), studyName])}" |
---|
188 | redirect(action: "list", params: extraparams) |
---|
189 | } |
---|
190 | } |
---|
191 | } |
---|