Ignore:
Timestamp:
Jan 28, 2010, 6:42:30 PM (10 years ago)
Author:
keesvb
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/domain/dbnp/studycapturing/Subject.groovy

    r145 r146  
    1313class Subject implements Serializable {
    1414        static searchable = true
     15        Template template
    1516        String name
    1617        Term species
     
    2930        static constraints = {
    3031        }
     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        }
    3155}
Note: See TracChangeset for help on using the changeset viewer.