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

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