1 | package dbnp.studycapturing |
---|
2 | |
---|
3 | import dbnp.user.User |
---|
4 | |
---|
5 | /** |
---|
6 | * Domain class describing the basic entity in the study capture part: the Study class. |
---|
7 | * |
---|
8 | * Revision information: |
---|
9 | * $Rev: 805 $ |
---|
10 | * $Author: duh $ |
---|
11 | * $Date: 2010-08-12 15:50:54 +0000 (do, 12 aug 2010) $ |
---|
12 | */ |
---|
13 | class Study extends TemplateEntity { |
---|
14 | static searchable = { |
---|
15 | [only: ['title', 'Description']] |
---|
16 | } |
---|
17 | |
---|
18 | User owner // The owner of the study. A new study is automatically owned by its creator. |
---|
19 | String title // The title of the study |
---|
20 | String code // currently used as the external study ID, e.g. to reference a study in a SAM module |
---|
21 | Date dateCreated |
---|
22 | Date lastUpdated |
---|
23 | Date startDate |
---|
24 | List subjects |
---|
25 | List events |
---|
26 | List samplingEvents |
---|
27 | List eventGroups |
---|
28 | List samples |
---|
29 | List assays |
---|
30 | |
---|
31 | static hasMany = [ |
---|
32 | editors: User, // Users with read/write access to the study |
---|
33 | readers: User, // Users with only read access to the study |
---|
34 | subjects: Subject, |
---|
35 | samplingEvents: SamplingEvent, |
---|
36 | events: Event, |
---|
37 | eventGroups: EventGroup, |
---|
38 | samples: Sample, |
---|
39 | assays: Assay, |
---|
40 | persons: StudyPerson, |
---|
41 | publications: Publication |
---|
42 | ] |
---|
43 | |
---|
44 | static constraints = { |
---|
45 | owner(nullable: true, blank: true) |
---|
46 | code(nullable:false, blank:true,unique:true) |
---|
47 | } |
---|
48 | |
---|
49 | static mapping = { |
---|
50 | researchQuestion type: 'text' |
---|
51 | description type: 'text' |
---|
52 | autoTimestamp true |
---|
53 | } |
---|
54 | |
---|
55 | // The external study ID is currently defined as the code of the study. |
---|
56 | // It is used from within dbNP submodules to refer to particular study in this GSCF instance. |
---|
57 | def getExternalStudyId() { code } |
---|
58 | |
---|
59 | /** |
---|
60 | * return the domain fields for this domain class |
---|
61 | * @return List |
---|
62 | */ |
---|
63 | static List<TemplateField> giveDomainFields() { return Study.domainFields } |
---|
64 | |
---|
65 | static final List<TemplateField> domainFields = [ |
---|
66 | new TemplateField( |
---|
67 | name: 'title', |
---|
68 | type: TemplateFieldType.STRING), |
---|
69 | new TemplateField( |
---|
70 | name: 'code', |
---|
71 | type: TemplateFieldType.STRING, |
---|
72 | preferredIdentifier:true, |
---|
73 | comment: 'Fill out the code by which many people will recognize your study'), |
---|
74 | new TemplateField( |
---|
75 | name: 'startDate', |
---|
76 | type: TemplateFieldType.DATE, |
---|
77 | comment: 'Fill out the official start date or date of first action') |
---|
78 | ] |
---|
79 | |
---|
80 | /** |
---|
81 | * return the title of this study |
---|
82 | */ |
---|
83 | def String toString() { |
---|
84 | return title |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * returns all events and sampling events that do not belong to a group |
---|
89 | */ |
---|
90 | def Set<Event> getOrphanEvents() { |
---|
91 | def orphans = events.findAll { event -> !event.belongsToGroup(eventGroups) } + |
---|
92 | samplingEvents.findAll { event -> !event.belongsToGroup(eventGroups) } |
---|
93 | |
---|
94 | return orphans |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Return the unique Subject templates that are used in this study |
---|
99 | */ |
---|
100 | def Set<Template> giveSubjectTemplates() { |
---|
101 | TemplateEntity.giveTemplates(subjects) |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * Return all subjects for a specific template |
---|
106 | * @param Template |
---|
107 | * @return ArrayList |
---|
108 | */ |
---|
109 | def ArrayList<Subject> giveSubjectsForTemplate(Template template) { |
---|
110 | subjects.findAll { it.template.equals(template) } |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * Return the unique Event and SamplingEvent templates that are used in this study |
---|
115 | */ |
---|
116 | Set<Template> giveAllEventTemplates() { |
---|
117 | // For some reason, giveAllEventTemplates() + giveAllSamplingEventTemplates() |
---|
118 | // gives trouble when asking .size() to the result |
---|
119 | // So we also use giveTemplates here |
---|
120 | TemplateEntity.giveTemplates( ((events) ? events : []) + ((samplingEvents) ? samplingEvents : []) ) |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | /** |
---|
125 | * Return all events and samplingEvenets for a specific template |
---|
126 | * @param Template |
---|
127 | * @return ArrayList |
---|
128 | */ |
---|
129 | def ArrayList giveEventsForTemplate(Template template) { |
---|
130 | def events = events.findAll { it.template.equals(template) } |
---|
131 | def samplingEvents = samplingEvents.findAll { it.template.equals(template) } |
---|
132 | |
---|
133 | return (events) ? events : samplingEvents |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * Return the unique Event templates that are used in this study |
---|
138 | */ |
---|
139 | Set<Template> giveEventTemplates() { |
---|
140 | TemplateEntity.giveTemplates(events) |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * Return the unique SamplingEvent templates that are used in this study |
---|
145 | */ |
---|
146 | Set<Template> giveSamplingEventTemplates() { |
---|
147 | TemplateEntity.giveTemplates(samplingEvents) |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * Returns the unique Sample templates that are used in the study |
---|
152 | */ |
---|
153 | Set<Template> giveSampleTemplates() { |
---|
154 | TemplateEntity.giveTemplates(samples) |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | * Return all samples for a specific template |
---|
159 | * @param Template |
---|
160 | * @return ArrayList |
---|
161 | */ |
---|
162 | def ArrayList<Subject> giveSamplesForTemplate(Template template) { |
---|
163 | samples.findAll { it.template.equals(template) } |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | * Returns the template of the study |
---|
168 | */ |
---|
169 | Template giveStudyTemplate() { |
---|
170 | return this.template |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | /** |
---|
175 | * Delete a specific subject from this study, including all its relations |
---|
176 | * @param subject The subject to be deleted |
---|
177 | * @return A String which contains a (user-readable) message describing the changes to the database |
---|
178 | */ |
---|
179 | String deleteSubject(Subject subject) { |
---|
180 | String msg = "Subject ${subject.name} was deleted" |
---|
181 | |
---|
182 | // Delete the subject from the event groups it was referenced in |
---|
183 | this.eventGroups.each { |
---|
184 | if (it.subjects.contains(subject)) { |
---|
185 | it.removeFromSubjects(subject) |
---|
186 | msg += ", deleted from event group '${it.name}'" |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | // Delete the samples that have this subject as parent |
---|
191 | this.samples.findAll { it.parentSubject.equals(subject) }.each { |
---|
192 | // This should remove the sample itself too, because of the cascading belongsTo relation |
---|
193 | this.removeFromSamples(it) |
---|
194 | // But apparently it needs an explicit delete() too |
---|
195 | it.delete() |
---|
196 | msg += ", sample '${it.name}' was deleted" |
---|
197 | } |
---|
198 | |
---|
199 | // This should remove the subject itself too, because of the cascading belongsTo relation |
---|
200 | this.removeFromSubjects(subject) |
---|
201 | // But apparently it needs an explicit delete() too |
---|
202 | subject.delete() |
---|
203 | |
---|
204 | return msg |
---|
205 | } |
---|
206 | |
---|
207 | /** |
---|
208 | * Delete an event from the study, including all its relations |
---|
209 | * @param Event |
---|
210 | * @return String |
---|
211 | */ |
---|
212 | String deleteEvent(Event event) { |
---|
213 | String msg = "Event ${event} was deleted" |
---|
214 | |
---|
215 | // remove event from the study |
---|
216 | this.removeFromEvents(event) |
---|
217 | |
---|
218 | // remove event from eventGroups |
---|
219 | this.eventGroups.each() { eventGroup -> |
---|
220 | eventGroup.removeFromEvents(event) |
---|
221 | } |
---|
222 | |
---|
223 | return msg |
---|
224 | } |
---|
225 | |
---|
226 | /** |
---|
227 | * Delete a samplingEvent from the study, including all its relations |
---|
228 | * @param SamplingEvent |
---|
229 | * @return String |
---|
230 | */ |
---|
231 | String deleteSamplingEvent(SamplingEvent samplingEvent) { |
---|
232 | String msg = "SamplingEvent ${samplingEvent} was deleted" |
---|
233 | |
---|
234 | // remove event from eventGroups |
---|
235 | this.eventGroups.each() { eventGroup -> |
---|
236 | eventGroup.removeFromSamplingEvents(samplingEvent) |
---|
237 | } |
---|
238 | |
---|
239 | // remove event from the study |
---|
240 | this.removeFromSamplingEvents(samplingEvent) |
---|
241 | |
---|
242 | return msg |
---|
243 | } |
---|
244 | |
---|
245 | /** |
---|
246 | * Delete an eventGroup from the study, including all its relations |
---|
247 | * @param EventGroup |
---|
248 | * @return String |
---|
249 | */ |
---|
250 | String deleteEventGroup(EventGroup eventGroup) { |
---|
251 | // TODO THIS DOESNT WORK!!!! |
---|
252 | println "-" |
---|
253 | println "-" |
---|
254 | println "-" |
---|
255 | println "-" |
---|
256 | println "-" |
---|
257 | println "REMOVING AND ENENTGROUP DOES NOT DELETE SAMPLES" |
---|
258 | println "-" |
---|
259 | println "-" |
---|
260 | println "-" |
---|
261 | println "-" |
---|
262 | println "-" |
---|
263 | |
---|
264 | |
---|
265 | |
---|
266 | String msg = "EventGroup ${eventGroup} was deleted" |
---|
267 | |
---|
268 | // remove all samples that originate from this eventGroup |
---|
269 | if (eventGroup.samplingEvents.size()) { |
---|
270 | // find all samples related to this eventGroup |
---|
271 | // - subject comparison is relatively straightforward and |
---|
272 | // behaves as expected |
---|
273 | // - event comparison behaves strange, so now we compare |
---|
274 | // 1. database id's or, |
---|
275 | // 2. object identifiers or, |
---|
276 | // 3. objects itself |
---|
277 | // this seems now to work as expected |
---|
278 | this.samples.findAll { sample -> |
---|
279 | ( |
---|
280 | (eventGroup.subjects.findAll { |
---|
281 | it.equals(sample.parentSubject) |
---|
282 | }) |
---|
283 | && |
---|
284 | (eventGroup.samplingEvents.findAll { |
---|
285 | ( |
---|
286 | (it.id && sample.parentEvent.id && it.id==sample.parentEvent.id) |
---|
287 | || |
---|
288 | (it.getIdentifier() == sample.parentEvent.getIdentifier()) |
---|
289 | || |
---|
290 | it.equals(sample.parentEvent) |
---|
291 | ) |
---|
292 | }) |
---|
293 | ) |
---|
294 | }.each() { |
---|
295 | // remove sample from study |
---|
296 | println ".removing sample "+it |
---|
297 | this.removeFromSamples( it ) |
---|
298 | } |
---|
299 | } |
---|
300 | |
---|
301 | // remove all samplingEvents from this eventGroup |
---|
302 | eventGroup.samplingEvents.findAll{}.each() { eventGroup.removeFromSamplingEvents(it) } |
---|
303 | |
---|
304 | // remove all subject from this eventGroup |
---|
305 | eventGroup.subjects.findAll{}.each() { eventGroup.removeFromSubjects(it) } |
---|
306 | |
---|
307 | // remove the eventGroup from the study |
---|
308 | this.removeFromEventGroups(eventGroup) |
---|
309 | |
---|
310 | return msg |
---|
311 | } |
---|
312 | } |
---|