/
MeasurementController.groovy
249 lines (210 loc) · 8.85 KB
/
MeasurementController.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package org.dbxp.sam
import org.dbnp.gdt.AssayModule
import org.dbxp.matriximporter.MatrixImporter
import dbnp.studycapturing.*
import org.dbnp.gdt.RelTime
import groovy.sql.Sql
class MeasurementController {
public static final SAMPLE_LAYOUT = "Sample layout"
public static final SUBJECT_LAYOUT = "Subject layout"
static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
def fuzzySearchService
def moduleService
def dataSource
def sessionFactory
def propertyInstanceMap = org.codehaus.groovy.grails.plugins.DomainClassGrailsPlugin.PROPERTY_INSTANCE_MAP
def index = {
redirect(action: "list", params: params)
}
def list = {
// Find all measurements this user has access to
def measurements = Measurement.giveReadableMeasurements( session.gscfUser );
if (moduleService.validateModule(params?.module)) {
[measurementInstanceList: measurements, measurementInstanceTotal: measurements.size(), module: params.module]
}
else {
redirect(controller: 'error', action: 'notFound')
}
}
def create = {
if (moduleService.validateModule(params?.module)) {
// If no samples are present, we can't add measurements
def features = Feature.list();
def samples = SAMSample.giveWritableSamples( session.gscfUser )
if( samples.size() == 0 ) {
redirect(action: 'noassays')
}
def measurementInstance = new Measurement()
measurementInstance.properties = params
return [measurementInstance: measurementInstance, samples: samples, features: features, module: params.module]
}
else {
redirect(controller: 'error', action: 'notFound')
}
}
def save = {
def measurementInstance = Measurement.findByFeatureAndSample(Feature.get(params.feature.id), SAMSample.get(params.sample.id))
if(measurementInstance!=null){
bindData(measurementInstance, params)
} else {
measurementInstance = new Measurement( params )
}
// Unfortunately, grails is unable to handle double values correctly. If
// one enters 10.20, the value of 1020.0 is stored in the database. For that
// reason, we convert the value ourselves
if( params.value?.isDouble() )
measurementInstance.value = params.value as Double
if (measurementInstance.save(flush: true)) {
flash.message = "The measurement has been created."
redirect(action: "show", id: measurementInstance.id, params: [module: params.module])
}
else {
def features = Feature.list();
def samples = SAMSample.giveWritableSamples( session.gscfUser )
render(view: "create", model: [measurementInstance: measurementInstance, samples: samples, features: features], module: params.module)
}
}
def show = {
if (moduleService.validateModule(params?.module)) {
def measurementInstance = Measurement.get(params.id)
if (!measurementInstance) {
flash.message = "The requested measurement could not be found."
redirect(action: "list", params: [module: params.module])
} else if( !measurementInstance.sample.parentAssay.parent.canRead( session.gscfUser ) ) {
flash.message = "You are not allowed to access the requested measurement."
redirect( action: "list", params: [module: params.module]);
} else {
[measurementInstance: measurementInstance, module: params.module]
}
}
else {
redirect(controller: 'error', action: 'notFound')
}
}
def edit = {
if (moduleService.validateModule(params?.module)) {
def measurementInstance = Measurement.get(params.id)
if (!measurementInstance) {
flash.message = "The requested measurement could not be found."
redirect(action: "list", params: [module: params.module])
}
else {
return [measurementInstance: measurementInstance, module: params.module]
}
}
else {
redirect(controller: 'error', action: 'notFound')
}
}
def update = {
def measurementInstance = Measurement.get(params.id)
if (measurementInstance) {
if (params.version) {
def version = params.version.toLong()
if (measurementInstance.version > version) {
measurementInstance.errors.rejectValue("Another user has updated this feature while you were editing. Because of this, your changes have not been saved to the database.")
render(view: "edit", model: [measurementInstance: measurementInstance], module: params.module)
return
}
}
measurementInstance.properties = params
// Unfortunately, grails is unable to handle double values correctly. If
// one enters 10.20, the value of 1020.0 is stored in the database. For that
// reason, we convert the value ourselves
if( params.value?.isDouble() )
measurementInstance.value = params.value as Double
if (!measurementInstance.hasErrors() && measurementInstance.save(flush: true)) {
flash.message = "The measurement has been updated."
redirect(action: "show", id: measurementInstance.id)
}
else {
render(view: "edit", model: [measurementInstance: measurementInstance], module: params.module)
}
}
else {
flash.message = "The requested measurement could not be found."
redirect(action: "list", params: [module: params.module])
}
}
def delete = {
def ids = params.list( 'ids' ).findAll { it.isLong() }.collect { it.toDouble() };
if( !ids ) {
response.sendError( 404 );
return;
}
def numDeleted = 0;
def numErrors = 0;
def numNotFound = 0;
ids.each { id ->
def measurementInstance = Measurement.get(id)
if (measurementInstance) {
def samSample = SAMSample.get(measurementInstance.sampleId)
try {
measurementInstance.delete(flush: true)
// If this was the last measurement in the SAMSample, delete it also
if (samSample.measurements.size() == 0) {
samSample.delete(flush: true)
}
numDeleted++;
} catch (org.springframework.dao.DataIntegrityViolationException e) {
log.error(e)
numErrors++;
}
}
else {
numNotFound++;
}
}
if( numDeleted == 1 )
flash.message = "1 measurement has been deleted from the database"
if( numDeleted > 1 )
flash.message = numDeleted + " measurements have been deleted from the database"
flash.error = ""
if( numNotFound == 1 )
flash.error += "1 measurement has been deleted before."
if( numNotFound > 1 )
flash.error += numNotFound+ " measurements have been deleted before."
if( numErrors == 1 )
flash.error += "1 measurement could not be deleted. Please try again"
if( numErrors > 1 )
flash.error += numErrors + " measurements could not be deleted. Please try again"
// Redirect to the assay list, because that is the only place where a
// delete button exists.
if( params.assayId ) {
redirect( controller: "SAMAssay", action: "show", id: params.assayId, params: [module: params.module] )
} else {
redirect(action: "list", params: [module: params.module])
}
}
def nofeatures = {
flash.message = "There are no features defined. Without features, you can't add measurements."
redirect( controller: 'feature', action: 'list', params: [module: params.module] );
}
def noassays = {
flash.message = "You have no assays that you are allowed to edit. Without writable samples, you can't add measurements."
redirect( controller: 'SAMAssay', action: 'list', params: [module: params.module] );
}
/**
* Deletes all measurements from the given assay
*/
def deleteByAssay = {
def assayId = params.id
if( !assayId || !assayId.isLong() ) {
flash.error = "No assay selected"
redirect( controller: "SAMAssay", view: "list", params: [module: params.module] );
return;
}
def assay = Assay.get( assayId.toLong() );
if( !assay ) {
flash.error = "Incorrect assay Id given"
redirect( controller: "SAMAssay", view: "list", params: [module: params.module] );
return;
}
if( Measurement.deleteByAssay( assay ) ) {
flash.message = "Your measurements for assay " + assay + " have been deleted."
} else {
flash.error = "An error occurred while deleting measurements for this assay. Please try again or contact your system administrator."
}
redirect( controller: "SAMAssay", view: "list", params: [module: params.module] );
}
}