source: trunk/grails-app/domain/dbnp/studycapturing/Study.groovy @ 774

Last change on this file since 774 was 774, checked in by keesvb, 11 years ago

added deleteSubject method to Study, updated Study and Sample integration tests, disabled unused ontology tests

  • Property svn:keywords set to Date Rev Author
File size: 4.6 KB
Line 
1package dbnp.studycapturing
2
3/**
4 * Domain class describing the basic entity in the study capture part: the Study class.
5 *
6 * Revision information:
7 * $Rev: 774 $
8 * $Author: keesvb $
9 * $Date: 2010-08-04 14:12:38 +0000 (wo, 04 aug 2010) $
10 */
11class Study extends TemplateEntity {
12        static searchable = {
13        [only: ['title', 'Description']]
14    }
15
16        nimble.User owner   // The owner of the study. A new study is automatically owned by its creator.
17        String title        // The title of the study
18        String code             // currently used as the external study ID, e.g. to reference a study in a SAM module
19        Date dateCreated
20        Date lastUpdated
21        Date startDate
22    List subjects
23        List events
24        List samplingEvents
25        List eventGroups
26        List samples
27        List assays
28
29        static hasMany = [
30                editors: nimble.User,   // Users with read/write access to the study
31                readers: nimble.User,   // Users with only read access to the study
32                subjects: Subject,
33                samplingEvents: SamplingEvent,
34                events: Event,
35                eventGroups: EventGroup,
36                samples: Sample,
37                assays: Assay,
38                persons: StudyPerson,
39                publications: Publication
40        ]
41
42        static constraints = {
43                owner(nullable: true, blank: true)
44                code(nullable:false, blank:true,unique:true) 
45        }
46
47        static mapping = {
48                researchQuestion type: 'text'
49                description type: 'text'
50                autoTimestamp true
51        }
52
53        // The external study ID is currently defined as the code of the study.
54        // It is used from within dbNP submodules to refer to particular study in this GSCF instance.
55        def getExternalStudyId() { code }
56
57        /**
58         * return the domain fields for this domain class
59         * @return List
60         */
61        static List<TemplateField> giveDomainFields() { return Study.domainFields }
62
63        static final List<TemplateField> domainFields = [
64                new TemplateField(
65                        name: 'title',
66                        type: TemplateFieldType.STRING),
67                new TemplateField(
68                        name: 'code',
69                        type: TemplateFieldType.STRING,
70                        preferredIdentifier:true,
71                        comment: 'Fill out the code by which many people will recognize your study'),
72                new TemplateField(
73                        name: 'startDate',
74                        type: TemplateFieldType.DATE,
75                        comment: 'Fill out the official start date or date of first action')
76        ]
77
78        /**
79         * return the title of this study
80         */
81        def String toString() {
82                return title
83        }
84
85        /**
86         * returns all events and sampling events that do not belong to a group
87         */
88        def Set<Event> getOrphanEvents() {
89                def orphans =   events.findAll { event -> !event.belongsToGroup(eventGroups) } +
90                                                samplingEvents.findAll { event -> !event.belongsToGroup(eventGroups) }
91
92                return orphans
93        }
94
95        /**
96         * Return the unique Subject templates that are used in this study
97         */
98        def Set<Template> giveSubjectTemplates() {
99                TemplateEntity.giveTemplates(subjects)
100        }
101
102        /**
103         * Return the unique Event and SamplingEvent templates that are used in this study
104         */
105        Set<Template> giveAllEventTemplates() {
106                // For some reason, giveAllEventTemplates() + giveAllSamplingEventTemplates()
107                // gives trouble when asking .size() to the result
108                // So we also use giveTemplates here
109                TemplateEntity.giveTemplates(events + samplingEvents)
110        }
111
112        /**
113         * Return the unique Event templates that are used in this study
114         */
115        Set<Template> giveEventTemplates() {
116                TemplateEntity.giveTemplates(events)
117        }
118
119        /**
120         * Return the unique SamplingEvent templates that are used in this study
121         */
122        Set<Template> giveSamplingEventTemplates() {
123                TemplateEntity.giveTemplates(samplingEvents)
124        }
125
126        /**
127         * Returns the unique Sample templates that are used in the study
128         */
129        Set<Template> giveSampleTemplates() {
130                TemplateEntity.giveTemplates(samples)
131        }
132        /**
133         * Returns the template of the study
134         */
135        Template giveStudyTemplate() {
136                return this.template
137        }
138
139
140         /**
141        * Delete a specific subject from this study, including all its relations
142        * @param subject The subject to be deleted
143        * @return A String which contains a (user-readable) message describing the changes to the database
144        */
145        String deleteSubject(Subject subject) {
146
147                String msg = "Subject ${subject.name} was deleted"
148
149                // Delete the subject from the event groups it was referenced in
150                this.eventGroups.each {
151                        if (it.subjects.contains(subject)) {
152                                it.removeFromSubjects(subject)
153                                msg += ", deleted from event group '${it.name}'"
154                        }
155                }
156
157                // Delete the samples that have this subject as parent
158                this.samples.findAll { it.parentSubject.equals(subject) }.each {
159                        // This should remove the sample itself too, because of the cascading belongsTo relation
160                        this.removeFromSamples(it)
161                        // But apparently it needs an explicit delete() too
162                        it.delete()
163                        msg += ", sample '${it.name}' was deleted"
164                }
165
166                // This should remove the subject itself too, because of the cascading belongsTo relation
167                this.removeFromSubjects(subject)
168                // But apparently it needs an explicit delete() too
169                subject.delete()
170
171                return msg
172        }
173}
Note: See TracBrowser for help on using the repository browser.