source: trunk/grails-app/controllers/nmc/PilotController.groovy @ 1769

Last change on this file since 1769 was 1594, checked in by robert@…, 12 years ago

Finishing touch after refactoring entity tokens (#329)

  • Property svn:keywords set to Rev Author Date
File size: 6.1 KB
Line 
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: 1594 $
12 * $Author: robert@isdat.nl $
13 * $Date: 2011-03-07 14:28:25 +0000 (ma, 07 mrt 2011) $
14 */
15package nmc
16
17import dbnp.studycapturing.*;
18import grails.plugins.springsecurity.Secured
19
20
21class 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                   studyInstance.addToAssays(assayInstance)
81                   assayInstance.save(flush: true)
82                                   
83                   //flash.message = "${message(code: 'default.created.message', args: [message(code: 'study.label', default: 'Study'), ( studyInstance.title ? studyInstance.title : "" ) + " " + ( studyInstance.code ? studyInstance.code : "" )])}"
84                   
85                   redirect(action: "show", id: studyInstance.id, params: extraparams )
86           }
87           else {
88                   render(view: "index", model: [studyInstance: studyInstance])
89           }
90   }
91   
92   def show = {
93                           
94           def studyInstance = Study.get(params.id)
95           if (!studyInstance) {
96                   flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}"
97                   redirect(action: "list")
98           }
99           else {
100                   
101                   //add all samples to the assay when not there yet!
102                   studyInstance.assays.each { assay ->
103                           //if (assay.samples.size() <= 0){ // needs improving!!! too much overhead, okay for pilot
104                                   studyInstance.samples.each { sample ->
105                                           log.info("ADD THE DIRTY WAY!!!")
106                                           assay.addToSamples(sample)
107                                   }
108                                   assay.save()
109                           //}                     
110                   }
111                   
112                   [studyInstance: studyInstance]
113           }
114   }
115   
116   def edit = {
117           def studyInstance = Study.get(params.id)
118           if (!studyInstance) {
119                   flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}"
120                   redirect(action: "list")
121           }
122           else {
123                   return [studyInstance: studyInstance]
124           }
125   }
126
127   def update = {
128           def studyInstance = Study.get(params.id)
129           
130           //For Pilot we do not ask for code, we generate it for the user
131           studyInstance.code = studyInstance?.title?.encodeAsMD5()
132
133           def extraparams = new LinkedHashMap();
134
135           if( params[ 'dialog' ] ) {
136                 extraparams[ 'dialog' ] = params[ 'dialog' ]
137           }
138
139           if (studyInstance) {
140                   if (params.version) {
141                           def version = params.version.toLong()
142                           if (studyInstance.version > version) {
143                                   
144                                   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")
145                                   render(view: "edit", model: [studyInstance: studyInstance])
146                                   return
147                           }
148                   }
149                   studyInstance.properties = params
150                   if (!studyInstance.hasErrors() && studyInstance.save(flush: true)) {
151                           flash.message = "${message(code: 'default.created.message', args: [message(code: 'study.label', default: 'Study'), ( studyInstance.title ? studyInstance.title : "" ) + " " + ( studyInstance.code ? studyInstance.code : "" )])}"
152                           redirect(action: "show", id: studyInstance.id, params: extraparams)
153                   }
154                   else {
155                           render(view: "edit", model: [studyInstance: studyInstance])
156                   }
157           }
158           else {
159                   flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'study.label', default: 'Study'), params.id])}"
160                   redirect(action: "list", params: extraparams)
161           }
162   }
163
164   def delete = {
165           def studyInstance = Study.get(params.id)
166
167           def extraparams = new LinkedHashMap();
168
169           if( params[ 'dialog' ] ) {
170                 extraparams[ 'dialog' ] = params[ 'dialog' ]
171           }
172
173           if (studyInstance) {
174                   def studyName = ( studyInstance.title ? studyInstance.title : "" ) + " " + ( studyInstance.code ? studyInstance.code : "" );
175                   try {
176                           studyInstance.delete(flush: true)
177                           flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'study.label', default: 'Study'), studyName])}"
178                           redirect(action: "list", params: extraparams)
179                   }
180                   catch (org.springframework.dao.DataIntegrityViolationException e) {
181                           flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'study.label', default: 'Study'), studyName])}"
182                           redirect(action: "show", id: params.id, params: extraparams)
183                   }
184           }
185           else {
186                   flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'study.label', default: 'Study'), studyName])}"
187                   redirect(action: "list", params: extraparams)
188           }
189   }
190}
Note: See TracBrowser for help on using the repository browser.