1 | package dbnp.studycapturing |
---|
2 | |
---|
3 | import dbnp.data.Term |
---|
4 | |
---|
5 | /** |
---|
6 | * The Sample class describes an actual sample which results from a SamplingEvent. |
---|
7 | * |
---|
8 | * Revision information: |
---|
9 | * $Rev: 934 $ |
---|
10 | * $Author: j.saito@maastrichtuniversity.nl $ |
---|
11 | * $Date: 2010-10-08 12:47:23 +0000 (vr, 08 okt 2010) $ |
---|
12 | */ |
---|
13 | class Sample extends TemplateEntity { |
---|
14 | // uncommented due to searchable issue |
---|
15 | // @see http://jira.codehaus.org/browse/GRAILSPLUGINS-1577 |
---|
16 | //static searchable = { [only: ['name']] } |
---|
17 | |
---|
18 | // A Sample always belongs to one study. |
---|
19 | static belongsTo = [parent : Study, parentSubject : Subject, parentEvent : SamplingEvent] |
---|
20 | |
---|
21 | // A Sample optionally has a parent Subject from which it was taken, this Subject should be in the same parent study. |
---|
22 | //long parentSubject |
---|
23 | |
---|
24 | // Also, it has a parent SamplingEvent describing the actual sampling, also within the same parent study. |
---|
25 | // Strange enough, we need to define parentEvent as a long here, otherwise the SamplingEvent gets serialized into the database (?!!) |
---|
26 | //long parentEvent |
---|
27 | |
---|
28 | String name // should be unique with respect to the parent study (which can be inferred) |
---|
29 | Term material // material of the sample (should normally be bound to the BRENDA ontology) |
---|
30 | |
---|
31 | |
---|
32 | /** |
---|
33 | * return the domain fields for this domain class |
---|
34 | * @return List |
---|
35 | */ |
---|
36 | static List<TemplateField> giveDomainFields() { return Sample.domainFields } |
---|
37 | |
---|
38 | // We have to specify an ontology list for the material property. However, at compile time, this ontology does of course not exist. |
---|
39 | // Therefore, the ontology is added at runtime in the bootstrap, possibly downloading the ontology properties if it is not present in the database yet. |
---|
40 | static List<TemplateField> domainFields = [ |
---|
41 | new TemplateField( |
---|
42 | name: 'name', |
---|
43 | type: TemplateFieldType.STRING, |
---|
44 | preferredIdentifier: true, |
---|
45 | required: true |
---|
46 | ), |
---|
47 | new TemplateField( |
---|
48 | name: 'material', |
---|
49 | type: TemplateFieldType.ONTOLOGYTERM |
---|
50 | ) |
---|
51 | ] |
---|
52 | |
---|
53 | static constraints = { |
---|
54 | // The parent subject is optional, e.g. in a biobank of samples the subject could be unknown or non-existing. |
---|
55 | parentSubject(nullable:true) |
---|
56 | |
---|
57 | // The material domain field is optional |
---|
58 | material(nullable: true) |
---|
59 | |
---|
60 | // Check if the externalSampleId (currently defined as name) is really unique within each parent study of this sample. |
---|
61 | // This feature is tested by integration test SampleTests.testSampleUniqueNameConstraint |
---|
62 | name(unique:['parent']) |
---|
63 | |
---|
64 | // Same, but also when the other sample is not even in the database |
---|
65 | // This feature is tested by integration test SampleTests.testSampleUniqueNameConstraintAtValidate |
---|
66 | name(validator: { field, obj, errors -> |
---|
67 | // 'obj' refers to the actual Sample object |
---|
68 | |
---|
69 | // define a boolean |
---|
70 | def error = false |
---|
71 | |
---|
72 | if (obj.parent) { |
---|
73 | |
---|
74 | // check if there is exactly one sample with this name in the study (this one) |
---|
75 | if (obj.parent.samples.findAll{ it.name == obj.name}.size() > 1) { |
---|
76 | error = true |
---|
77 | errors.rejectValue( |
---|
78 | 'name', |
---|
79 | 'sample.UniqueNameViolation', |
---|
80 | [obj.name, obj.parent] as Object[], |
---|
81 | 'Sample name {0} appears multiple times in study {1}' |
---|
82 | ) |
---|
83 | } |
---|
84 | } |
---|
85 | else { |
---|
86 | // if there is no parent study defined, fail immediately |
---|
87 | error = true |
---|
88 | } |
---|
89 | |
---|
90 | // got an error, or not? |
---|
91 | return (!error) |
---|
92 | }) |
---|
93 | } |
---|
94 | |
---|
95 | static getSamplesFor( event ) { |
---|
96 | return Sample.findAll( 'from Sample s where s.parentEvent =:event', [event:event] ) |
---|
97 | } |
---|
98 | |
---|
99 | def String toString() { |
---|
100 | return name |
---|
101 | } |
---|
102 | } |
---|