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

Revision 654, 2.2 KB (checked in by keesvb, 4 years ago)

updated sample and study tests, added belongsTo to Sample (which gives it its parent), tried to write the unique name constraint of Sample and wrote a test for it

  • Property svn:keywords set to Date Rev Author
Line 
1package dbnp.studycapturing
2
3import dbnp.data.Term
4import dbnp.data.Ontology
5
6/**
7 * The Sample class describes an actual sample which results from a SamplingEvent.
8 */
9class Sample extends TemplateEntity {
10        static searchable = { [only: ['name']] }
11
12        static belongsTo = [ parent : Study]
13
14        Subject parentSubject
15        SamplingEvent parentEvent
16
17        String name             // should be unique with respect to the parent study (which can be inferred)
18        Term material           // material of the sample (should normally be bound to the BRENDA ontology)
19
20        def getExternalSampleId() { name }
21
22        /**
23         * return the domain fields for this domain class
24         * @return List
25         */
26        static List<TemplateField> giveDomainFields() { return Sample.domainFields }
27        static List<TemplateField> domainFields = [
28                new TemplateField(
29                        name: 'name',
30                        type: TemplateFieldType.STRING,
31                        preferredIdentifier: true,
32                        required: true
33                ),
34                new TemplateField(
35                        name: 'material',
36                        type: TemplateFieldType.ONTOLOGYTERM
37                )
38        ]
39
40        static constraints = {
41                parentSubject(nullable:true)
42                material(nullable: true)
43
44                // TODO: Write a validation method that checks if the externalSampleId (currently defined as name)
45                // is really unique within each parent study of this sample.
46                // Maybe this could also be a constraint, but we might run into trouble creating new Sample objects in e.g. the create wizard.
47                // To be checked.
48                // This feature is tested by integration test SampleTests.testSampleUniqueNameConstraint
49
50                /*name(validator: { fields, obj, errors ->
51                        def error = false
52
53                        if (fields) {
54                                // Search through all study
55                                if (obj.parent.samples.findAll({ it.name == fields}).size() != 1) {
56                                        errors.rejectValue(
57                                                'name',
58                                                'sample.name.NotUnique',
59                                                ['name',fields] as Object[],
60                                                'The sample name is not unique within the study'
61                                        )
62                                }
63                        }
64                        else {
65                                // If no value for name is specified, the sample should not validate
66                                error = true
67                        }
68
69                        return (!error)
70                })*/
71        }
72
73        /*static def getParentStudies() {
74                Study.findAll {
75                        it.samples.findAll {
76                                it.name == this.name
77                        }
78                }
79        }*/
80
81        static getSamplesFor( event ) {
82                return  Sample.findAll( 'from Sample s where s.parentEvent =:event', [event:event] )
83        }
84
85}
Note: See TracBrowser for help on using the browser.