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

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

Using moduleCommunicationService now to fetch measurement data from module

  • 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     * Exports all assay information as an Excel file.
136     *
137     * @param params.id Assay id
138     */
139    def exportAssayAsExcel = {
140
141        Assay assay = Assay.get(params.assayId)
142
143        // check if assay exists
144        if (!assay) {
145            flash.errorMessage = "No assay found with id: $params.assayId."
146            redirect action: 'selectAssay'
147            return
148        }
149
150        try {
151
152            def assayData = assayService.collectAssayData(assay, grailsApplication.config.modules.metabolomics.url)
153
154            def filename = 'export.xlsx'
155            response.setHeader("Content-disposition", "attachment;filename=\"${filename}\"")
156            response.setContentType("application/octet-stream")
157
158            assayService.exportColumnWiseDataToExcelFile(assayData, response.outputStream)
159
160        } catch (Exception e) {
161
162            flash.errorMessage = e.message
163            redirect action: 'selectAssay'
164
165        }
166    }
167}
Note: See TracBrowser for help on using the browser.