root/trunk/grails-app/controllers/dbnp/studycapturing/AssayController.groovy @ 1724

Revision 1724, 7.7 KB (checked in by s.h.sikkema@…, 3 years ago)

changed Study.give[Readable,Writable]Studies to make max parameter optional; assay exporter now gives all readable studies instead of all owned studies

  • Property svn:keywords set to Rev Author Date
Line 
1package dbnp.studycapturing
2
3class AssayController {
4
5    def assayService
6    def authenticationService
7   
8    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
9
10    def index = {
11        redirect(action: "list", params: params)
12    }
13
14    def list = {
15        params.max = Math.min(params.max ? params.int('max') : 10, 100)
16        [assayInstanceList: Assay.list(params), assayInstanceTotal: Assay.count()]
17    }
18
19    def create = {
20        def assayInstance = new Assay()
21        assayInstance.properties = params
22        return [assayInstance: assayInstance]
23    }
24
25    def save = {
26        def assayInstance = new Assay(params)
27
28        // The following lines deviate from the generate-all generated code.
29        // See http://jira.codehaus.org/browse/GRAILS-3783 for why we have this shameful workaround...
30        def study = assayInstance.parent
31        study.addToAssays(assayInstance)
32
33        if (assayInstance.save(flush: true)) {
34            flash.message = "${message(code: 'default.created.message', args: [message(code: 'assay.label', default: 'Assay'), assayInstance.id])}"
35            redirect(action: "show", id: assayInstance.id)
36        }
37        else {
38            render(view: "create", model: [assayInstance: assayInstance])
39        }
40    }
41
42    def show = {
43        def assayInstance = Assay.get(params.id)
44        if (!assayInstance) {
45            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'assay.label', default: 'Assay'), params.id])}"
46            redirect(action: "list")
47        }
48        else {
49            [assayInstance: assayInstance]
50        }
51    }
52
53        def showByToken = {
54            def assayInstance = Assay.findByAssayUUID(params.id)
55            if (!assayInstance) {
56                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'assay.label', default: 'Assay'), params.id])}"
57                redirect(action: "list")
58            }
59            else {
60                    redirect(action: "show", id: assayInstance.id)
61            }
62        }
63
64    def edit = {
65        def assayInstance = Assay.get(params.id)
66        if (!assayInstance) {
67            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'assay.label', default: 'Assay'), params.id])}"
68            redirect(action: "list")
69        }
70        else {
71            return [assayInstance: assayInstance]
72        }
73    }
74
75    def update = {
76        def assayInstance = Assay.get(params.id)
77        if (assayInstance) {
78            if (params.version) {
79                def version = params.version.toLong()
80                if (assayInstance.version > version) {
81                   
82                    assayInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'assay.label', default: 'Assay')] as Object[], "Another user has updated this Assay while you were editing")
83                    render(view: "edit", model: [assayInstance: assayInstance])
84                    return
85                }
86            }
87            assayInstance.properties = params
88            if (!assayInstance.hasErrors() && assayInstance.save(flush: true)) {
89                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'assay.label', default: 'Assay'), assayInstance.id])}"
90                redirect(action: "show", id: assayInstance.id)
91            }
92            else {
93                render(view: "edit", model: [assayInstance: assayInstance])
94            }
95        }
96        else {
97            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'assay.label', default: 'Assay'), params.id])}"
98            redirect(action: "list")
99        }
100    }
101
102    def delete = {
103        def assayInstance = Assay.get(params.id)
104        if (assayInstance) {
105            try {
106                assayInstance.delete(flush: true)
107                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'assay.label', default: 'Assay'), params.id])}"
108                redirect(action: "list")
109            }
110            catch (org.springframework.dao.DataIntegrityViolationException e) {
111                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'assay.label', default: 'Assay'), params.id])}"
112                redirect(action: "show", id: params.id)
113            }
114        }
115        else {
116            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'assay.label', default: 'Assay'), params.id])}"
117            redirect(action: "list")
118        }
119    }
120
121    def excelExportFlow = {
122        entry {
123            action{
124                def user            = authenticationService.getLoggedInUser()
125                flow.userStudies    = Study.giveReadableStudies(user)
126            }
127            on("success").to "selectAssay"
128        }
129
130        selectAssay {
131            on ("submit"){
132                flow.assay = Assay.get(params.assayId)
133
134                // check if assay exists
135                if (!flow.assay) throw new Exception("No assay found with id: ${flow.assay.id}")
136
137                // obtain fields for each category
138                flow.fieldMap = assayService.collectAssayTemplateFields(flow.assay)
139
140                flow.measurementTokens = flow.fieldMap.remove('Module Measurement Data')*.name
141            }.to "selectFields"
142
143            on(Exception).to "handleError"
144        }
145
146        selectFields {
147            on ("submit"){
148                def fieldMapSelection = [:]
149
150                flow.fieldMap.eachWithIndex { cat, cat_i ->
151
152                    if (params."cat_$cat_i" == 'on') {
153                        fieldMapSelection[cat.key] = []
154
155                        cat.value.eachWithIndex { field, field_i ->
156
157                            if (params."cat_${cat_i}_${field_i}" == 'on')
158                                fieldMapSelection[cat.key] += field
159                        }
160
161                        if (fieldMapSelection[cat.key] == [])
162                            fieldMapSelection.remove(cat.key)
163                    }
164                }
165
166                def measurementTokensSelection = []
167
168                if (params."cat_4" == 'on') {
169
170                    def measurementToken = params.measurementToken
171
172                    if (measurementToken)
173                        if (measurementToken instanceof String)
174                            measurementTokensSelection = [[name: measurementToken]]
175                        else
176                            measurementTokensSelection = measurementToken.collect{[name: it]}
177                }
178
179                def assayData           = assayService.collectAssayData(flow.assay, fieldMapSelection, measurementTokensSelection)
180                flow.rowData            = assayService.convertColumnToRowStructure(assayData)
181                flow.assayDataPreview   = flow.rowData[0..4].collect{ it[0..4] as ArrayList }
182
183            }.to "compileExportData"
184
185            on(Exception).to "handleError"
186        }
187
188        compileExportData {
189            on ("ok"){session.rowData = flow.rowData}.to "export"
190            on ("cancel").to "selectAssay"
191        }
192
193        export {
194            redirect(action: 'doExport')
195        }
196
197        handleError() {
198            render(view: 'errorPage')
199        }
200    }
201
202    def doExport = {
203
204        def filename = 'export.xlsx'
205        response.setHeader("Content-disposition", "attachment;filename=\"${filename}\"")
206        response.setContentType("application/octet-stream")
207        try {
208
209            assayService.exportRowWiseDataToExcelFile(session.rowData, response.outputStream)
210            response.outputStream.flush()
211
212        } catch (Exception e) {
213
214            flash.errorMessage = e.message
215            redirect action: 'errorPage'
216
217        }
218    }
219
220    def errorPage = {
221        render(view: 'excelExport/errorPage')
222    }
223}
Note: See TracBrowser for help on using the browser.