Ignore:
Timestamp:
Mar 22, 2010, 11:38:25 AM (9 years ago)
Author:
keesvb
Message:

debugged EventDescription?.save, added comment field for template fields, add code comments to setFieldValue, added missing events to PPSH demo in BootStrap?, cleaned up home menu, re-added searchable

File:
1 edited

Legend:

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

    r257 r290  
    7070
    7171        /**
    72          * Find a template field by its name and return its value for this subject
    73          * @param fieldName The name of the template subject field
     72         * Find a template field by its name and return its value for this entity
     73         * @param fieldName The name of the template field
    7474         * @return the value of the field (class depends on the field type)
    7575         * @throws NoSuchFieldException If the field is not found or the field type is not supported
     
    8181        }
    8282
     83        /**
     84         * Set a template/entity field value
     85         * @param fieldName The name of the template or entity field
     86         * @param value The value to be set, this should align with the (template) field type, but there are some convenience setters
     87         *
     88         */
    8389        def setFieldValue(String fieldName, value) {
     90
     91                // First, search if there is an entity property with the given name, and if so, set that
    8492                if (this.properties.containsKey(fieldName)) {
    8593                        this[fieldName] = value
    8694                }
    87                 else
    88                 if (template == null) {
    89                         throw new NoSuchFieldException("Field ${fieldName} not found in class properties")
     95                // If not the found, then it is a template field, so check if there is a template
     96                else if (template == null) {
     97                        throw new NoSuchFieldException("Field ${fieldName} not found in class properties: template not set")
    9098                }
     99                // If there is a template, check the template fields
    91100                else {
     101                        // Find the target template field, if not found, throw an error
    92102                        TemplateField field = this.template.fields.find { it.name == fieldName}
    93103                        if (field == null) {
    94104                                throw new NoSuchFieldException("Field ${fieldName} not found in class properties or template fields")
    95105                        }
     106                        // Set the value of the found template field
    96107                        else {
     108                                // Convenience setter for template string list fields: find TemplateFieldListItem by name
    97109                                if (field.type == TemplateFieldType.STRINGLIST && value.class == String) {
    98                                         // Convenience setter: find template item by name
    99110                                        value = field.listEntries.find { it.name == value }
    100111                                }
    101112
    102                                 // handle string values for date fields
     113                                // Convenience setter for dates: handle string values for date fields
    103114                                if (field.type == TemplateFieldType.DATE && value.class == String) {
    104115                                        // a string was given, attempt to transform it into a date instance
     
    119130                                }
    120131
    121                                 // Caution: this assumes that all template...Field Maps are already initialized
    122                                 // Otherwise, the results are pretty much unpredictable!
     132                                // Set the field value
     133                                // Caution: this assumes that all template...Field Maps are already initialized (as is done now above as [:])
     134                                // If that is ever changed, the results are pretty much unpredictable (random Java object pointers?)!
    123135                                getStore(field.type)[fieldName] = value
    124136                                return this
Note: See TracChangeset for help on using the changeset viewer.