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

Revision 1161, 3.5 KB (checked in by work@…, 3 years ago)

- tried to reproduce issue #128 but it seems resolved, leaving nullable to false

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