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

Revision 960, 3.3 KB (checked in by business@…, 3 years ago)

changed parentEvent in Sample to nullable. Please make sure that any code that depends on the assertion that Sample always has a parentEvent is changed accordingly! In this commit done for getSamples in RestController?.

  • 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        // 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                // The same holds for parentEvent
57                parentEvent(nullable:true)
58               
59                // The material domain field is optional
60                material(nullable: true)
61
62                // Check if the externalSampleId (currently defined as name) is really unique within each parent study of this sample.
63                // This feature is tested by integration test SampleTests.testSampleUniqueNameConstraint
64                name(unique:['parent'])
65
66                // Same, but also when the other sample is not even in the database
67                // This feature is tested by integration test SampleTests.testSampleUniqueNameConstraintAtValidate
68                name(validator: { field, obj, errors ->
69                        // 'obj' refers to the actual Sample object
70
71                        // define a boolean
72                        def error = false
73
74                        if (obj.parent) {
75
76                                // check if there is exactly one sample with this name in the study (this one)
77                                if (obj.parent.samples.findAll{ it.name == obj.name}.size() > 1) {
78                                        error = true
79                                        errors.rejectValue(
80                                                'name',
81                                                'sample.UniqueNameViolation',
82                                                [obj.name, obj.parent] as Object[],
83                                                'Sample name {0} appears multiple times in study {1}'
84                                                )
85                                }
86                        }
87                        else {
88                                // if there is no parent study defined, fail immediately
89                                error = true
90                        }
91
92                        // got an error, or not?
93                        return (!error)
94                })
95        }
96
97        static getSamplesFor( event ) {
98                return  Sample.findAll( 'from Sample s where s.parentEvent =:event', [event:event] )
99        }
100
101        def String toString() {
102                return name
103        }
104}
Note: See TracBrowser for help on using the browser.