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: 959 $ |
---|
12 | * $Author: j.a.m.wesbeek@umail.leidenuniv.nl $ |
---|
13 | * $Date: 2010-10-20 19:13:14 +0000 (wo, 20 okt 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 | } |
---|
40 | |
---|
41 | def list = { |
---|
42 | params.max = Math.min(params.max ? params.int('max') : 10, 100) |
---|
43 | [studyInstanceList: Study.list(params), studyInstanceTotal: Study.count()] |
---|
44 | } |
---|
45 | |
---|
46 | /** |
---|
47 | * create closure |
---|
48 | */ |
---|
49 | def create = { |
---|
50 | def studyInstance = new Study() |
---|
51 | studyInstance.properties = params |
---|
52 | return [studyInstance: studyInstance] |
---|
53 | } |
---|
54 | |
---|
55 | def save = { |
---|
56 | def studyInstance = new Study(params) |
---|
57 | |
---|
58 | //For Pilot we do not ask for code, we generate it for the user |
---|
59 | studyInstance.code = params?.title?.encodeAsMD5() |
---|
60 | |
---|
61 | def extraparams = new LinkedHashMap(); |
---|
62 | |
---|
63 | if( params[ 'dialog' ] ) { |
---|
64 | extraparams[ 'dialog' ] = params[ 'dialog' ] |
---|
65 | } |
---|
66 | |
---|
67 | if (studyInstance.save(flush: true)) { |
---|
68 | |
---|
69 | //Study was created, now setup a NMC - Metabolomics Assay for testing |
---|
70 | def assayInstance = new Assay() |
---|
71 | assayInstance.name = "${studyInstance.title} - Metabolomics Assay" |
---|
72 | assayInstance.module = AssayModule.findByName("Metabolomics module") |
---|
73 | assayInstance.externalAssayID = assayInstance?.name?.encodeAsMD5() |
---|
74 | studyInstance.addToAssays(assayInstance) |
---|
75 | assayInstance.save(flush: true) |
---|
76 | |
---|
77 | //flash.message = "${message(code: 'default.created.message', args: [message(code: 'study.label', default: 'Study'), ( studyInstance.title ? studyInstance.title : "" ) + " " + ( studyInstance.code ? studyInstance.code : "" )])}" |
---|
78 | |
---|
79 | redirect(action: "show", id: studyInstance.id, params: extraparams ) |
---|
80 | } |
---|
81 | else { |
---|
82 | render(view: "create", model: [studyInstance: studyInstance]) |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | def show = { |
---|
87 | def studyInstance = Study.get(params.id) |
---|
88 | if (!studyInstance) { |
---|
89 | flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}" |
---|
90 | redirect(action: "list") |
---|
91 | } |
---|
92 | else { |
---|
93 | [studyInstance: studyInstance] |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | def edit = { |
---|
98 | def studyInstance = Study.get(params.id) |
---|
99 | if (!studyInstance) { |
---|
100 | flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}" |
---|
101 | redirect(action: "list") |
---|
102 | } |
---|
103 | else { |
---|
104 | return [studyInstance: studyInstance] |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | def update = { |
---|
109 | def studyInstance = Study.get(params.id) |
---|
110 | |
---|
111 | //For Pilot we do not ask for code, we generate it for the user |
---|
112 | studyInstance.code = studyInstance?.title?.encodeAsMD5() |
---|
113 | |
---|
114 | def extraparams = new LinkedHashMap(); |
---|
115 | |
---|
116 | if( params[ 'dialog' ] ) { |
---|
117 | extraparams[ 'dialog' ] = params[ 'dialog' ] |
---|
118 | } |
---|
119 | |
---|
120 | if (studyInstance) { |
---|
121 | if (params.version) { |
---|
122 | def version = params.version.toLong() |
---|
123 | if (studyInstance.version > version) { |
---|
124 | |
---|
125 | 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") |
---|
126 | render(view: "edit", model: [studyInstance: studyInstance]) |
---|
127 | return |
---|
128 | } |
---|
129 | } |
---|
130 | studyInstance.properties = params |
---|
131 | if (!studyInstance.hasErrors() && studyInstance.save(flush: true)) { |
---|
132 | flash.message = "${message(code: 'default.created.message', args: [message(code: 'study.label', default: 'Study'), ( studyInstance.title ? studyInstance.title : "" ) + " " + ( studyInstance.code ? studyInstance.code : "" )])}" |
---|
133 | redirect(action: "show", id: studyInstance.id, params: extraparams) |
---|
134 | } |
---|
135 | else { |
---|
136 | render(view: "edit", model: [studyInstance: studyInstance]) |
---|
137 | } |
---|
138 | } |
---|
139 | else { |
---|
140 | flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}" |
---|
141 | redirect(action: "list", params: extraparams) |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | def delete = { |
---|
146 | def studyInstance = Study.get(params.id) |
---|
147 | |
---|
148 | def extraparams = new LinkedHashMap(); |
---|
149 | |
---|
150 | if( params[ 'dialog' ] ) { |
---|
151 | extraparams[ 'dialog' ] = params[ 'dialog' ] |
---|
152 | } |
---|
153 | |
---|
154 | if (studyInstance) { |
---|
155 | def studyName = ( studyInstance.title ? studyInstance.title : "" ) + " " + ( studyInstance.code ? studyInstance.code : "" ); |
---|
156 | try { |
---|
157 | studyInstance.delete(flush: true) |
---|
158 | flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'study.label', default: 'Study'), studyName])}" |
---|
159 | redirect(action: "list", params: extraparams) |
---|
160 | } |
---|
161 | catch (org.springframework.dao.DataIntegrityViolationException e) { |
---|
162 | flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'study.label', default: 'Study'), studyName])}" |
---|
163 | redirect(action: "show", id: params.id, params: extraparams) |
---|
164 | } |
---|
165 | } |
---|
166 | else { |
---|
167 | flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'study.label', default: 'Study'), studyName])}" |
---|
168 | redirect(action: "list", params: extraparams) |
---|
169 | } |
---|
170 | } |
---|
171 | } |
---|