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

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

UI improvements to assay exporter (ticket #159)

  • 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    /**
122     * Shows a page where an assay from a study can be selected
123     *
124     * @param none
125     */
126    def selectAssay = {
127        def user = authenticationService.getLoggedInUser()
128        def studies = Study.findAllByOwner(user)
129        def assays = Assay.findAllByParent(studies[0])
130
131        [userStudies: studies, assays: assays]
132    }
133
134    /**
135     * Shows a page where individual fields for the different categories (ie.
136     * subject data, sampling events... etc.) can be selected for export
137     *
138     * @param params.id Assay id
139     */
140    def selectFields = {
141        // receives an assay id
142        def assay = Assay.get(params.assayId)
143
144        // obtain fields for each category
145                def fieldMap
146        try {
147            fieldMap = assayService.collectAssayTemplateFields(assay)
148        } catch (Exception e) {
149                        e.printStackTrace();
150            flash.errorMessage = e.message
151            redirect action: 'selectAssay'
152                        return
153
154        }
155        def measurementTokens = fieldMap.remove('Module Measurement Data')
156
157        flash.fieldMap = fieldMap
158        flash.measurementTokens = measurementTokens
159        flash.assayId = params.assayId
160
161        [fieldMap: fieldMap, measurementTokens: measurementTokens.name]
162    }
163
164    /**
165     * Exports all assay information as an Excel file.
166     *
167     * @param params.id Assay id
168     */
169    def compileExportData = {
170
171        def fieldMap = flash.fieldMap
172
173        def fieldMapSelection = [:]
174
175        fieldMap.eachWithIndex { cat, cat_i ->
176
177            if (params."cat_$cat_i" == 'on') {
178                fieldMapSelection[cat.key] = []
179
180                cat.value.eachWithIndex { field, field_i ->
181
182                    if (params."cat_${cat_i}_${field_i}" == 'on') {
183
184                        fieldMapSelection[cat.key] += field
185
186                    }
187
188                }
189
190                if (fieldMapSelection[cat.key] == []) fieldMapSelection.remove(cat.key)
191
192            }
193
194        }
195
196        def measurementTokensSelection = []
197
198        if (params."cat_4" == 'on') {
199
200            def measurementToken = params.measurementToken
201
202            if (measurementToken) {
203
204                if (measurementToken instanceof String)
205                    measurementTokensSelection = [[name: measurementToken]]
206                else
207                    measurementTokensSelection = measurementToken.collect{[name: it]}
208
209            } else {
210                measurementTokensSelection = flash.measurementTokens
211            }
212
213        }
214
215        Assay assay = Assay.get(flash.assayId)
216
217        // check if assay exists
218        if (!assay) {
219
220            flash.errorMessage = flash.assayId ? "No assay found with id: ${flash.assayId}" : 'Assay has no value (null).'
221            redirect action: 'selectAssay'
222            return
223
224        }
225
226        try {
227
228            def assayData = assayService.collectAssayData(assay, fieldMapSelection, measurementTokensSelection)
229
230            def rowData = assayService.convertColumnToRowStructure(assayData)
231
232            flash.rowData = rowData
233
234            def assayDataPreview = rowData[0..4].collect{it[0..4]}
235
236
237            [assayDataPreview: assayDataPreview]
238
239        } catch (Exception e) {
240
241            flash.errorMessage = e.message
242            redirect action: 'selectAssay'
243
244        }
245    }
246
247    def doExport = {
248
249        def filename = 'export.xlsx'
250        response.setHeader("Content-disposition", "attachment;filename=\"${filename}\"")
251        response.setContentType("application/octet-stream")
252        try {
253           
254            assayService.exportRowWiseDataToExcelFile(flash.rowData, response.outputStream)
255
256        } catch (Exception e) {
257
258            flash.errorMessage = e.message
259            redirect action: 'selectAssay'
260
261        }
262
263
264    }
265}
Note: See TracBrowser for help on using the browser.