source: trunk/grails-app/domain/dbnp/studycapturing/Subject.groovy @ 146

Last change on this file since 146 was 146, checked in by keesvb, 9 years ago

added helper functions to Subject and Template classes to easily get the template fields, and added a demonstration of use in the sandbox

File size: 1.6 KB
Line 
1package dbnp.studycapturing
2
3import dbnp.data.Term
4
5/**
6 * This domain class describes the subjects in a study.
7 *
8 * Revision information:
9 * $Rev$
10 * $Author$
11 * $Date$
12 */
13class Subject implements Serializable {
14        static searchable = true
15        Template template
16        String name
17        Term species
18        Map templateStringFields
19        Map templateIntegerFields
20        Map templateFloatFields
21        Map templateTermFields
22
23        static hasMany = [
24                templateStringFields: String, // stores both STRING and STRINGLIST items (latter should be checked against the list)
25                templateIntegerFields: int,
26                templateFloatFields: float,
27                templateTermFields: Term
28        ]
29
30        static constraints = {
31        }
32
33        /**
34         * Find a template field by its name and return its value for this subject
35         * @param fieldName The name of the template subject field
36         * @return the value of the field (class depends on the field type)
37         * @throws NoSuchFieldException If the field is not found or the field type is not supported
38         */
39        def getFieldValue(String fieldName) {
40                TemplateFieldType fieldType = template.getSubjectFieldType(fieldName)
41                if (!fieldType) throw new NoSuchFieldException("Field name ${fieldName} not recognized")
42                switch(fieldType) {
43                        case [TemplateFieldType.STRING, TemplateFieldType.STRINGLIST]:
44                                return templateStringFields[fieldName]
45                        case TemplateFieldType.INTEGER:
46                                return templateIntegerFields[fieldName]
47                        case TemplateFieldType.FLOAT:
48                                return templateFloatFields[fieldName]
49                        case TemplateFieldType.ONTOLOGYTERM:
50                                return templateTermFields[fieldName]
51                        default:
52                                throw new NoSuchFieldException("Field type ${fieldType} not recognized")
53                }
54        }
55}
Note: See TracBrowser for help on using the repository browser.