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

Last change on this file since 784 was 784, checked in by duh, 10 years ago
  • set keyword expansion
  • Property svn:keywords set to Author Rev Date
File size: 6.4 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: 784 $
8 * $Author: duh $
9 * $Date: 2010-08-06 10:21:06 +0000 (vr, 06 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        /**
104         * Return all subjects for a specific template
105         * @param Template
106         * @return ArrayList
107         */
108        def ArrayList<Subject> giveSubjectsForTemplate(Template template) {
109                subjects.findAll { it.template.equals(template) }
110        }
111
112        /**
113         * Return the unique Event and SamplingEvent templates that are used in this study
114         */
115        Set<Template> giveAllEventTemplates() {
116                // For some reason, giveAllEventTemplates() + giveAllSamplingEventTemplates()
117                // gives trouble when asking .size() to the result
118                // So we also use giveTemplates here
119                TemplateEntity.giveTemplates( ((events) ? events : []) + ((samplingEvents) ? samplingEvents : []) )
120        }
121
122
123        /**
124         * Return all events and samplingEvenets for a specific template
125         * @param Template
126         * @return ArrayList
127         */
128        def ArrayList giveEventsForTemplate(Template template) {
129                def events = events.findAll { it.template.equals(template) }
130                def samplingEvents = samplingEvents.findAll { it.template.equals(template) }
131
132                return (events) ? events : samplingEvents
133        }
134
135        /**
136         * Return the unique Event templates that are used in this study
137         */
138        Set<Template> giveEventTemplates() {
139                TemplateEntity.giveTemplates(events)
140        }
141
142        /**
143         * Return the unique SamplingEvent templates that are used in this study
144         */
145        Set<Template> giveSamplingEventTemplates() {
146                TemplateEntity.giveTemplates(samplingEvents)
147        }
148
149        /**
150         * Returns the unique Sample templates that are used in the study
151         */
152        Set<Template> giveSampleTemplates() {
153                TemplateEntity.giveTemplates(samples)
154        }
155        /**
156         * Returns the template of the study
157         */
158        Template giveStudyTemplate() {
159                return this.template
160        }
161
162
163        /**
164         * Delete a specific subject from this study, including all its relations
165         * @param subject The subject to be deleted
166         * @return A String which contains a (user-readable) message describing the changes to the database
167         */
168        String deleteSubject(Subject subject) {
169                String msg = "Subject ${subject.name} was deleted"
170
171                // Delete the subject from the event groups it was referenced in
172                this.eventGroups.each {
173                        if (it.subjects.contains(subject)) {
174                                it.removeFromSubjects(subject)
175                                msg += ", deleted from event group '${it.name}'"
176                        }
177                }
178
179                // Delete the samples that have this subject as parent
180                this.samples.findAll { it.parentSubject.equals(subject) }.each {
181                        // This should remove the sample itself too, because of the cascading belongsTo relation
182                        this.removeFromSamples(it)
183                        // But apparently it needs an explicit delete() too
184                        it.delete()
185                        msg += ", sample '${it.name}' was deleted"
186                }
187
188                // This should remove the subject itself too, because of the cascading belongsTo relation
189                this.removeFromSubjects(subject)
190                // But apparently it needs an explicit delete() too
191                subject.delete()
192
193                return msg
194        }
195
196        /**
197         * Delete an event from the study, including all its relations
198         * @param Event
199         * @return String
200         */
201        String deleteEvent(Event event) {
202                String msg = "Event ${event} was deleted"
203
204                // remove event from the study
205                this.removeFromEvents(event)
206
207                // remove event from eventGroups
208                this.eventGroups.each() { eventGroup ->
209                        eventGroup.removeFromEvents(event)
210                }
211
212                return msg
213        }
214
215        /**
216         * Delete a samplingEvent from the study, including all its relations
217         * @param SamplingEvent
218         * @return String
219         */
220        String deleteSamplingEvent(SamplingEvent samplingEvent) {
221                String msg = "SamplingEvent ${samplingEvent} was deleted"
222
223                // remove event from eventGroups
224                this.eventGroups.each() { eventGroup ->
225                        eventGroup.removeFromSamplingEvents(samplingEvent)
226                }
227
228                // remove event from the study
229                this.removeFromSamplingEvents(samplingEvent)
230
231                return msg
232        }
233        /**
234         * Delete an eventGroup from the study, including all its relations
235         * @param EventGroup
236         * @return String
237         */
238        String deleteEventGroup(EventGroup eventGroup) {
239                String msg = "EventGroup ${eventGroup} was deleted"
240
241                // remove the eventGroup from the study
242                this.removeFromEventGroups(eventGroup)
243
244                return msg
245        }
246}
Note: See TracBrowser for help on using the repository browser.