root/trunk/grails-app/domain/dbnp/studycapturing/Sample.groovy @ 1169

Revision 1169, 3.5 KB (checked in by business@…, 3 years ago)

fix for #128 - parentEvent is now nullable

  • Property svn:keywords set to Author Date Rev
Line 
1package dbnp.studycapturing
2
3import dbnp.data.Term
4
5/**
6 * The Sample class describes an actual sample which results from a SamplingEvent.
7 *
8 * Revision information:
9 * $Rev$
10 * $Author$
11 * $Date$
12 */
13class 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        static belongsTo = [
19                // A Sample always belongs to one study.
20                parent                  : Study,
21
22                // A Sample optionally has a parent Subject from which it was taken, this Subject should be in the same parent study.
23                parentSubject   : Subject,
24
25                // Also, it has a parent SamplingEvent describing the actual sampling, also within the same parent study.
26                parentEvent             : SamplingEvent,
27
28                // And it has a parent EventGroup which tied it to its parent subject and parent event
29                parentEventGroup: EventGroup
30
31                // We can't have parentAssay since a Sample can belong to multiple Assays
32        ]
33
34        String name             // should be unique with respect to the parent study (which can be inferred)
35        Term material           // material of the sample (should normally be bound to the BRENDA ontology)
36
37        /**
38         * return the domain fields for this domain class
39         * @return List
40         */
41        static List<TemplateField> giveDomainFields() { return Sample.domainFields }
42
43        // We have to specify an ontology list for the material property. However, at compile time, this ontology does of course not exist.
44        // Therefore, the ontology is added at runtime in the bootstrap, possibly downloading the ontology properties if it is not present in the database yet.
45        static List<TemplateField> domainFields = [
46                new TemplateField(
47                        name: 'name',
48                        type: TemplateFieldType.STRING,
49                        preferredIdentifier: true,
50                        required: true
51                ),
52                new TemplateField(
53                        name: 'material',
54                        type: TemplateFieldType.ONTOLOGYTERM
55                )
56        ]
57
58        static constraints = {
59                // The parent subject is optional, e.g. in a biobank of samples the subject could be unknown or non-existing.
60                parentSubject(nullable:true)
61
62                // The same holds for parentEvent
63                parentEvent(nullable:true)
64
65                // and for parentEventGroup
66                parentEventGroup(nullable:true)
67
68                // The material domain field is optional
69                material(nullable: true)
70
71                // Check if the externalSampleId (currently defined as name) is really unique within each parent study of this sample.
72                // This feature is tested by integration test SampleTests.testSampleUniqueNameConstraint
73                name(unique:['parent'])
74
75                // Same, but also when the other sample is not even in the database
76                // This feature is tested by integration test SampleTests.testSampleUniqueNameConstraintAtValidate
77                name(validator: { field, obj, errors ->
78                        // 'obj' refers to the actual Sample object
79
80                        // define a boolean
81                        def error = false
82
83                        if (obj.parent) {
84
85                                // check if there is exactly one sample with this name in the study (this one)
86                                if (obj.parent.samples.findAll{ it.name == obj.name}.size() > 1) {
87                                        error = true
88                                        errors.rejectValue(
89                                                'name',
90                                                'sample.UniqueNameViolation',
91                                                [obj.name, obj.parent] as Object[],
92                                                'Sample name {0} appears multiple times in study {1}'
93                                                )
94                                }
95                        }
96                        else {
97                                // if there is no parent study defined, fail immediately
98                                error = true
99                        }
100
101                        // got an error, or not?
102                        return (!error)
103                })
104        }
105
106    static mapping = {
107        sort "name"
108    }
109
110        static getSamplesFor( event ) {
111                return  Sample.findAll( 'from Sample s where s.parentEvent =:event', [event:event] )
112        }
113
114        def String toString() {
115                return name
116        }
117}
Note: See TracBrowser for help on using the browser.