1 | package dbnp.studycapturing |
---|
2 | |
---|
3 | import dbnp.authentication.SecUser |
---|
4 | import nl.grails.plugins.gdt.* |
---|
5 | import java.util.UUID; |
---|
6 | |
---|
7 | /** |
---|
8 | * Domain class describing the basic entity in the study capture part: the Study class. |
---|
9 | * |
---|
10 | * Revision information: |
---|
11 | * $Rev: 1440 $ |
---|
12 | * $Author: robert@isdat.nl $ |
---|
13 | * $Date: 2011-01-25 16:57:37 +0000 (di, 25 jan 2011) $ |
---|
14 | */ |
---|
15 | class Study extends nl.grails.plugins.gdt.TemplateEntity { |
---|
16 | static searchable = true |
---|
17 | |
---|
18 | def synchronizationService |
---|
19 | |
---|
20 | SecUser owner // The owner of the study. A new study is automatically owned by its creator. |
---|
21 | String title // The title of the study |
---|
22 | String description // A brief synopsis of what the study is about |
---|
23 | String code // currently used as the external study ID, e.g. to reference a study in a SAM module |
---|
24 | Date dateCreated |
---|
25 | Date lastUpdated |
---|
26 | Date startDate |
---|
27 | List subjects |
---|
28 | List events |
---|
29 | List samplingEvents |
---|
30 | List eventGroups |
---|
31 | List samples |
---|
32 | List assays |
---|
33 | boolean published = false // Determines whether a study is private (only accessable by the owner and writers) or published (also visible to readers) |
---|
34 | boolean publicstudy = false // Determines whether anonymous users are allowed to see this study. This has only effect when published = true |
---|
35 | |
---|
36 | /** |
---|
37 | * UUID of this study |
---|
38 | */ |
---|
39 | String studyUUID |
---|
40 | |
---|
41 | |
---|
42 | static hasMany = [ |
---|
43 | subjects: Subject, |
---|
44 | samplingEvents: SamplingEvent, |
---|
45 | events: Event, |
---|
46 | eventGroups: EventGroup, |
---|
47 | samples: Sample, |
---|
48 | assays: Assay, |
---|
49 | persons: StudyPerson, |
---|
50 | publications: Publication, |
---|
51 | readers: SecUser, |
---|
52 | writers: SecUser |
---|
53 | ] |
---|
54 | |
---|
55 | static constraints = { |
---|
56 | owner(nullable: true, blank: true) |
---|
57 | code(nullable: false, blank: true, unique: true) |
---|
58 | studyUUID(nullable:true, unique:true) |
---|
59 | // TODO: add custom validator for 'published' to assess whether the study meets all quality criteria for publication |
---|
60 | // tested by SampleTests.testStudyPublish |
---|
61 | } |
---|
62 | |
---|
63 | static mapping = { |
---|
64 | autoTimestamp true |
---|
65 | sort "title" |
---|
66 | |
---|
67 | // Make sure the TEXT field description is persisted with a TEXT field in the database |
---|
68 | description type: 'text' |
---|
69 | // Workaround for bug http://jira.codehaus.org/browse/GRAILS-6754 |
---|
70 | templateTextFields type: 'text' |
---|
71 | |
---|
72 | } |
---|
73 | |
---|
74 | // The external identifier (studyToken) is currently the code of the study. |
---|
75 | // It is used from within dbNP submodules to refer to particular study in this GSCF instance. |
---|
76 | |
---|
77 | def getToken() { return giveUUID() } |
---|
78 | |
---|
79 | /** |
---|
80 | * return the domain fields for this domain class |
---|
81 | * @return List |
---|
82 | */ |
---|
83 | static List<TemplateField> giveDomainFields() { return Study.domainFields } |
---|
84 | |
---|
85 | static final List<TemplateField> domainFields = [ |
---|
86 | new TemplateField( |
---|
87 | name: 'title', |
---|
88 | type: TemplateFieldType.STRING, |
---|
89 | required: true), |
---|
90 | new TemplateField( |
---|
91 | name: 'description', |
---|
92 | type: TemplateFieldType.TEXT, |
---|
93 | comment:'Give a brief synopsis of what your study is about', |
---|
94 | required: true), |
---|
95 | new TemplateField( |
---|
96 | name: 'code', |
---|
97 | type: TemplateFieldType.STRING, |
---|
98 | preferredIdentifier: true, |
---|
99 | comment: 'Fill out the code by which many people will recognize your study', |
---|
100 | required: true), |
---|
101 | new TemplateField( |
---|
102 | name: 'startDate', |
---|
103 | type: TemplateFieldType.DATE, |
---|
104 | comment: 'Fill out the official start date or date of first action', |
---|
105 | required: true), |
---|
106 | new TemplateField( |
---|
107 | name: 'published', |
---|
108 | type: TemplateFieldType.BOOLEAN, |
---|
109 | comment: 'Determines whether this study is published (accessible for the study readers and, if the study is public, for anonymous users). A study can only be published if it meets certain quality criteria, which will be checked upon save.', |
---|
110 | required: false) |
---|
111 | ] |
---|
112 | |
---|
113 | /** |
---|
114 | * return the title of this study |
---|
115 | */ |
---|
116 | def String toString() { |
---|
117 | return title |
---|
118 | } |
---|
119 | |
---|
120 | /** |
---|
121 | * returns all events and sampling events that do not belong to a group |
---|
122 | */ |
---|
123 | def List<Event> getOrphanEvents() { |
---|
124 | def orphans = events.findAll { event -> !event.belongsToGroup(eventGroups) } + |
---|
125 | samplingEvents.findAll { event -> !event.belongsToGroup(eventGroups) } |
---|
126 | |
---|
127 | return orphans |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | * Return the unique Subject templates that are used in this study |
---|
132 | */ |
---|
133 | def List<Template> giveSubjectTemplates() { |
---|
134 | TemplateEntity.giveTemplates(subjects) |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * Return all subjects for a specific template |
---|
139 | * @param Template |
---|
140 | * @return ArrayList |
---|
141 | */ |
---|
142 | def ArrayList<Subject> giveSubjectsForTemplate(Template template) { |
---|
143 | subjects.findAll { it.template.equals(template) } |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * Return all unique assay templates |
---|
148 | * @return Set |
---|
149 | */ |
---|
150 | List<Template> giveAllAssayTemplates() { |
---|
151 | TemplateEntity.giveTemplates(((assays) ? assays : [])) |
---|
152 | } |
---|
153 | |
---|
154 | /** |
---|
155 | * Return all assays for a particular template |
---|
156 | * @return ArrayList |
---|
157 | */ |
---|
158 | def ArrayList giveAssaysForTemplate(Template template) { |
---|
159 | assays.findAll { it.template.equals(template) } |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * Return the unique Event and SamplingEvent templates that are used in this study |
---|
164 | */ |
---|
165 | List<Template> giveAllEventTemplates() { |
---|
166 | // For some reason, giveAllEventTemplates() + giveAllSamplingEventTemplates() |
---|
167 | // gives trouble when asking .size() to the result |
---|
168 | // So we also use giveTemplates here |
---|
169 | TemplateEntity.giveTemplates(((events) ? events : []) + ((samplingEvents) ? samplingEvents : [])) |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * Return all events and samplingEvenets for a specific template |
---|
174 | * @param Template |
---|
175 | * @return ArrayList |
---|
176 | */ |
---|
177 | def ArrayList giveEventsForTemplate(Template template) { |
---|
178 | def events = events.findAll { it.template.equals(template) } |
---|
179 | def samplingEvents = samplingEvents.findAll { it.template.equals(template) } |
---|
180 | |
---|
181 | return (events) ? events : samplingEvents |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | * Return the unique Event templates that are used in this study |
---|
186 | */ |
---|
187 | List<Template> giveEventTemplates() { |
---|
188 | TemplateEntity.giveTemplates(events) |
---|
189 | } |
---|
190 | |
---|
191 | /** |
---|
192 | * Return the unique SamplingEvent templates that are used in this study |
---|
193 | */ |
---|
194 | List<Template> giveSamplingEventTemplates() { |
---|
195 | TemplateEntity.giveTemplates(samplingEvents) |
---|
196 | } |
---|
197 | |
---|
198 | /** |
---|
199 | * Returns the unique Sample templates that are used in the study |
---|
200 | */ |
---|
201 | List<Template> giveSampleTemplates() { |
---|
202 | TemplateEntity.giveTemplates(samples) |
---|
203 | } |
---|
204 | |
---|
205 | /** |
---|
206 | * Return all samples for a specific template |
---|
207 | * @param Template |
---|
208 | * @return ArrayList |
---|
209 | */ |
---|
210 | def ArrayList<Subject> giveSamplesForTemplate(Template template) { |
---|
211 | samples.findAll { it.template.equals(template) } |
---|
212 | } |
---|
213 | |
---|
214 | /** |
---|
215 | * Returns the template of the study |
---|
216 | */ |
---|
217 | Template giveStudyTemplate() { |
---|
218 | return this.template |
---|
219 | } |
---|
220 | |
---|
221 | /** |
---|
222 | * Delete a specific subject from this study, including all its relations |
---|
223 | * @param subject The subject to be deleted |
---|
224 | * @void |
---|
225 | */ |
---|
226 | void deleteSubject(Subject subject) { |
---|
227 | // Delete the subject from the event groups it was referenced in |
---|
228 | this.eventGroups.each { |
---|
229 | if (it.subjects?.contains(subject)) { |
---|
230 | it.removeFromSubjects(subject) |
---|
231 | } |
---|
232 | } |
---|
233 | |
---|
234 | // Delete the samples that have this subject as parent |
---|
235 | this.samples.findAll { it.parentSubject.equals(subject) }.each { |
---|
236 | this.deleteSample(it) |
---|
237 | } |
---|
238 | |
---|
239 | // This should remove the subject itself too, because of the cascading belongsTo relation |
---|
240 | this.removeFromSubjects(subject) |
---|
241 | |
---|
242 | // But apparently it needs an explicit delete() too |
---|
243 | subject.delete() |
---|
244 | } |
---|
245 | |
---|
246 | /** |
---|
247 | * Delete an assay from the study |
---|
248 | * @param Assay |
---|
249 | * @void |
---|
250 | */ |
---|
251 | def deleteAssay(Assay assay) { |
---|
252 | if (assay && assay instanceof Assay) { |
---|
253 | // iterate through linked samples |
---|
254 | assay.samples.findAll { true }.each() { sample -> |
---|
255 | assay.removeFromSamples(sample) |
---|
256 | } |
---|
257 | |
---|
258 | // remove this assay from the study |
---|
259 | this.removeFromAssays(assay) |
---|
260 | |
---|
261 | // and delete it explicitly |
---|
262 | assay.delete() |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | /** |
---|
267 | * Delete an event from the study, including all its relations |
---|
268 | * @param Event |
---|
269 | * @void |
---|
270 | */ |
---|
271 | void deleteEvent(Event event) { |
---|
272 | // remove event from the study |
---|
273 | this.removeFromEvents(event) |
---|
274 | |
---|
275 | // remove event from eventGroups |
---|
276 | this.eventGroups.each() { eventGroup -> |
---|
277 | eventGroup.removeFromEvents(event) |
---|
278 | } |
---|
279 | } |
---|
280 | |
---|
281 | /** |
---|
282 | * Delete a sample from the study, including all its relations |
---|
283 | * @param Event |
---|
284 | * @void |
---|
285 | */ |
---|
286 | void deleteSample(Sample sample) { |
---|
287 | // remove the sample from the study |
---|
288 | this.removeFromSamples(sample) |
---|
289 | |
---|
290 | // remove the sample from any sampling events it belongs to |
---|
291 | this.samplingEvents.findAll { it.samples.any { it == sample }}.each { |
---|
292 | it.removeFromSamples(sample) |
---|
293 | } |
---|
294 | |
---|
295 | // remove the sample from any assays it belongs to |
---|
296 | this.assays.findAll { it.samples.any { it == sample }}.each { |
---|
297 | it.removeFromSamples(sample) |
---|
298 | } |
---|
299 | |
---|
300 | // Also here, contrary to documentation, an extra delete() is needed |
---|
301 | // otherwise date is not properly deleted! |
---|
302 | sample.delete() |
---|
303 | } |
---|
304 | |
---|
305 | /** |
---|
306 | * Delete a samplingEvent from the study, including all its relations |
---|
307 | * @param SamplingEvent |
---|
308 | * @void |
---|
309 | */ |
---|
310 | void deleteSamplingEvent(SamplingEvent samplingEvent) { |
---|
311 | // remove event from eventGroups |
---|
312 | this.eventGroups.each() { eventGroup -> |
---|
313 | eventGroup.removeFromSamplingEvents(samplingEvent) |
---|
314 | } |
---|
315 | |
---|
316 | // Delete the samples that have this sampling event as parent |
---|
317 | this.samples.findAll { it.parentEvent.equals(samplingEvent) }.each { |
---|
318 | // This should remove the sample itself too, because of the cascading belongsTo relation |
---|
319 | this.deleteSample(it) |
---|
320 | } |
---|
321 | |
---|
322 | // Remove event from the study |
---|
323 | // This should remove the event group itself too, because of the cascading belongsTo relation |
---|
324 | this.removeFromSamplingEvents(samplingEvent) |
---|
325 | |
---|
326 | // But apparently it needs an explicit delete() too |
---|
327 | // (Which can be verified by outcommenting this line, then SampleTests.testDeleteViaParentSamplingEvent fails |
---|
328 | samplingEvent.delete() |
---|
329 | } |
---|
330 | |
---|
331 | /** |
---|
332 | * Delete an eventGroup from the study, including all its relations |
---|
333 | * @param EventGroup |
---|
334 | * @void |
---|
335 | */ |
---|
336 | void deleteEventGroup(EventGroup eventGroup) { |
---|
337 | // If the event group contains sampling events |
---|
338 | if (eventGroup.samplingEvents) { |
---|
339 | // remove all samples that originate from this eventGroup |
---|
340 | if (eventGroup.samplingEvents.size()) { |
---|
341 | // find all samples related to this eventGroup |
---|
342 | // - subject comparison is relatively straightforward and |
---|
343 | // behaves as expected |
---|
344 | // - event comparison behaves strange, so now we compare |
---|
345 | // 1. database id's or, |
---|
346 | // 2. object identifiers or, |
---|
347 | // 3. objects itself |
---|
348 | // this seems now to work as expected |
---|
349 | this.samples.findAll { sample -> |
---|
350 | ( |
---|
351 | (eventGroup.subjects.findAll { |
---|
352 | it.equals(sample.parentSubject) |
---|
353 | }) |
---|
354 | && |
---|
355 | (eventGroup.samplingEvents.findAll { |
---|
356 | ( |
---|
357 | (it.id && sample.parentEvent.id && it.id == sample.parentEvent.id) |
---|
358 | || |
---|
359 | (it.getIdentifier() == sample.parentEvent.getIdentifier()) |
---|
360 | || |
---|
361 | it.equals(sample.parentEvent) |
---|
362 | ) |
---|
363 | }) |
---|
364 | ) |
---|
365 | }.each() { sample -> |
---|
366 | // remove sample from study |
---|
367 | this.deleteSample(sample) |
---|
368 | } |
---|
369 | } |
---|
370 | |
---|
371 | // remove all samplingEvents from this eventGroup |
---|
372 | eventGroup.samplingEvents.findAll {}.each() { |
---|
373 | eventGroup.removeFromSamplingEvents(it) |
---|
374 | } |
---|
375 | } |
---|
376 | |
---|
377 | // If the event group contains subjects |
---|
378 | if (eventGroup.subjects) { |
---|
379 | // remove all subject from this eventGroup |
---|
380 | eventGroup.subjects.findAll {}.each() { |
---|
381 | eventGroup.removeFromSubjects(it) |
---|
382 | } |
---|
383 | } |
---|
384 | |
---|
385 | // remove the eventGroup from the study |
---|
386 | this.removeFromEventGroups(eventGroup) |
---|
387 | |
---|
388 | // Also here, contrary to documentation, an extra delete() is needed |
---|
389 | // otherwise cascaded deletes are not properly performed |
---|
390 | eventGroup.delete() |
---|
391 | } |
---|
392 | |
---|
393 | /** |
---|
394 | * Returns true if the given user is allowed to read this study |
---|
395 | */ |
---|
396 | public boolean canRead(SecUser loggedInUser) { |
---|
397 | // Anonymous readers are only given access when published and public |
---|
398 | if (loggedInUser == null) { |
---|
399 | return this.publicstudy && this.published; |
---|
400 | } |
---|
401 | |
---|
402 | // Administrators are allowed to read every study |
---|
403 | if (loggedInUser.hasAdminRights()) { |
---|
404 | return true; |
---|
405 | } |
---|
406 | |
---|
407 | // Owners and writers are allowed to read this study |
---|
408 | if (this.owner == loggedInUser || this.writers.contains(loggedInUser)) { |
---|
409 | return true |
---|
410 | } |
---|
411 | |
---|
412 | // Readers are allowed to read this study when it is published |
---|
413 | if (this.readers.contains(loggedInUser) && this.published) { |
---|
414 | return true |
---|
415 | } |
---|
416 | |
---|
417 | return false |
---|
418 | } |
---|
419 | |
---|
420 | /** |
---|
421 | * Returns true if the given user is allowed to write this study |
---|
422 | */ |
---|
423 | public boolean canWrite(SecUser loggedInUser) { |
---|
424 | if (loggedInUser == null) { |
---|
425 | return false; |
---|
426 | } |
---|
427 | |
---|
428 | // Administrators are allowed to write every study |
---|
429 | if (loggedInUser.hasAdminRights()) { |
---|
430 | return true; |
---|
431 | } |
---|
432 | |
---|
433 | return this.owner == loggedInUser || this.writers.contains(loggedInUser) |
---|
434 | } |
---|
435 | |
---|
436 | /** |
---|
437 | * Returns true if the given user is the owner of this study |
---|
438 | */ |
---|
439 | public boolean isOwner(SecUser loggedInUser) { |
---|
440 | if (loggedInUser == null) { |
---|
441 | return false; |
---|
442 | } |
---|
443 | return this.owner == loggedInUser |
---|
444 | } |
---|
445 | |
---|
446 | /** |
---|
447 | * Returns a list of studies that are writable for the given user |
---|
448 | */ |
---|
449 | public static giveWritableStudies(SecUser user, int max) { |
---|
450 | // User that are not logged in, are not allowed to write to a study |
---|
451 | if (user == null) |
---|
452 | return []; |
---|
453 | |
---|
454 | def c = Study.createCriteria() |
---|
455 | |
---|
456 | // Administrators are allowed to read everything |
---|
457 | if (user.hasAdminRights()) { |
---|
458 | return c.list { |
---|
459 | maxResults(max) |
---|
460 | } |
---|
461 | } |
---|
462 | |
---|
463 | return c.list { |
---|
464 | maxResults(max) |
---|
465 | or { |
---|
466 | eq("owner", user) |
---|
467 | writers { |
---|
468 | eq("id", user.id) |
---|
469 | } |
---|
470 | } |
---|
471 | } |
---|
472 | } |
---|
473 | |
---|
474 | /** |
---|
475 | * Returns a list of studies that are readable by the given user |
---|
476 | */ |
---|
477 | public static giveReadableStudies(SecUser user, int max) { |
---|
478 | def c = Study.createCriteria() |
---|
479 | |
---|
480 | // Administrators are allowed to read everything |
---|
481 | if (user == null) { |
---|
482 | return c.list { |
---|
483 | maxResults(max) |
---|
484 | and { |
---|
485 | eq("published", true) |
---|
486 | eq("publicstudy", true) |
---|
487 | } |
---|
488 | } |
---|
489 | } else if (user.hasAdminRights()) { |
---|
490 | return c.list { |
---|
491 | maxResults(max) |
---|
492 | } |
---|
493 | } else { |
---|
494 | return c.list { |
---|
495 | maxResults(max) |
---|
496 | or { |
---|
497 | eq("owner", user) |
---|
498 | writers { |
---|
499 | eq("id", user.id) |
---|
500 | } |
---|
501 | and { |
---|
502 | readers { |
---|
503 | eq("id", user.id) |
---|
504 | } |
---|
505 | eq("published", true) |
---|
506 | } |
---|
507 | } |
---|
508 | } |
---|
509 | } |
---|
510 | } |
---|
511 | |
---|
512 | /** |
---|
513 | * Returns the UUID of this study and generates one if needed |
---|
514 | */ |
---|
515 | public String giveUUID() { |
---|
516 | if( !this.studyUUID ) { |
---|
517 | this.studyUUID = UUID.randomUUID().toString(); |
---|
518 | this.save(); |
---|
519 | } |
---|
520 | |
---|
521 | return this.studyUUID; |
---|
522 | } |
---|
523 | |
---|
524 | // Send messages to modules about changes in this study |
---|
525 | def beforeInsert = { |
---|
526 | synchronizationService.invalidateStudy( this ); |
---|
527 | } |
---|
528 | def beforeUpdate = { |
---|
529 | synchronizationService.invalidateStudy( this ); |
---|
530 | } |
---|
531 | def beforeDelete = { |
---|
532 | synchronizationService.invalidateStudy( this ); |
---|
533 | } |
---|
534 | } |
---|