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

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

added first version of importer data model, added helper methods to domain class for getting all entity fields, added study template fields

  • Property svn:keywords set to
    Date
    Author
    Rev
File size: 1.9 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: 176 $
10 * $Author: keesvb $
11 * $Date: 2010-02-08 11:42:46 +0000 (ma, 08 feb 2010) $
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
56        def setFieldValue(String fieldName, value) {
57                this.properties.each { println it}
58                if (this.properties.containsKey(fieldName)) {
59                        this[fieldName] = value
60                }
61                else if (templateStringFields.containsKey(fieldName) && value.class == String) {
62                        this.templateStringFields[fieldName] = value
63                }
64                else {
65                        println "Field ${fieldName} not found"
66                }
67        }
68}
Note: See TracBrowser for help on using the repository browser.