Changeset 335


Ignore:
Timestamp:
Apr 9, 2010, 3:11:35 PM (13 years ago)
Author:
duh
Message:
  • templated fields are now properly set as well as unset
  • showing setting and unsetting debugging lines in the terminal
Location:
trunk/grails-app
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/controllers/dbnp/studycapturing/WizardController.groovy

    r332 r335  
    591591                if (params.template) {
    592592                        params.template.fields.each() { field ->
    593                                 def value = params.get(field.escapedName())
    594 
    595                                 if (value) {
    596                                         flow.study.setFieldValue(field.name, value)
    597                                 }
     593                                flow.study.setFieldValue(field.name, params.get(field.escapedName()))
    598594                        }
    599595                }
     
    712708                                // iterate through template fields
    713709                                templateFields.each() { subjectField ->
    714                                         def value = params.get('subject_' + subjectId + '_' + subjectField.escapedName())
    715 
    716                                         if (value) {
    717                                                 flow.subjects[ subjectId ].setFieldValue(subjectField.name, value)
    718                                         }
     710                                        flow.subjects[ subjectId ].setFieldValue(
     711                                                subjectField.name,
     712                                                params.get( 'subject_' + subjectId + '_' + subjectField.escapedName() )
     713                                        )
    719714                                }
    720715
  • trunk/grails-app/domain/dbnp/studycapturing/TemplateEntity.groovy

    r333 r335  
    4141
    4242        /**
    43          * Contraints
     43         * Constraints
    4444         *
    4545         * All template fields have their own custom validator. Note that there
    46          * currently is a lot of code repitition. Ideally we don't want this, but
    47          * unfortunately due to scope issues we cannot re-use the code. So make sure
    48          * to replicate any changes to all pieces of logic!
     46         * currently is a lot of code repetition. Ideally we don't want this, but
     47         * unfortunately due to scope issues we cannot re-use the code. So make
     48         * sure to replicate any changes to all pieces of logic! Only commented
     49         * the first occurrence of the logic, please refer to the templateStringFields
     50         * validator if you require information about the validation logic...
    4951         */
    5052        static constraints = {
    5153                template(nullable: true, blank: true)
    5254                templateStringFields(validator: { fields, obj, errors ->
     55                        // note that we only use 'fields' and 'errors', 'obj' is
     56                        // merely here because it's the way the closure is called
     57                        // by the validator...
     58
     59                        // define a boolean
    5360                        def error = false
    5461
    5562                        // iterate through fields
    5663                        fields.each { key, value ->
    57                                 // check if value is of proper type
    58                                 if (value.class != String) {
    59                                         // it's not, try to cast it to the proper type
    60                                         try {
     64                                // check if the value is of proper type
     65                                if ( value && value.class != String ) {
     66                                        // it's of some other type
     67                                        try {
     68                                                // try to cast it to the proper type
    6169                                                fields[key] = (value as String)
    6270                                        } catch (Exception e) {
    63                                                 // could not typecast properly, value is of inproper type
     71                                                // could not typecast properly, value is of improper type
     72                                                // add error message
    6473                                                error = true
    6574                                                errors.rejectValue(
     
    7281                                }
    7382                        }
     83
     84                        // got an error, or not?
    7485                        return (!error)
    7586                })
     
    7788                        def error = false
    7889                        fields.each { key, value ->
    79                                 if (value.class != String) {
     90                                if ( value && value.class != String ) {
    8091                                        try {
    8192                                                fields[key] = (value as String)
     
    96107                        def error = false
    97108                        fields.each { key, value ->
    98                                 if (value.class != TemplateFieldListItem) {
     109                                if ( value && value.class != TemplateFieldListItem ) {
    99110                                        try {
    100111                                                fields[key] = (value as TemplateFieldListItem)
     
    115126                        def error = false
    116127                        fields.each { key, value ->
    117                                 if (value.class != Integer) {
     128                                if (value && value.class != Integer ) {
    118129                                        try {
    119130                                                fields[key] = (value as Integer)
     
    134145                        def error = false
    135146                        fields.each { key, value ->
    136                                 if (value.class != Float) {
     147                                if ( value && value.class != Float ) {
    137148                                        try {
    138149                                                fields[key] = (value as Float)
     
    153164                        def error = false
    154165                        fields.each { key, value ->
    155                                 if (value.class != Double) {
     166                                if ( value && value.class != Double ) {
    156167                                        try {
    157168                                                fields[key] = (value as Double)
     
    172183                        def error = false
    173184                        fields.each { key, value ->
    174                                 if (value.class != Date) {
     185                                if ( value && value.class != Date ) {
    175186                                        try {
    176187                                                fields[key] = (value as Date)
     
    191202                        def error = false
    192203                        fields.each { key, value ->
    193                                 if (value.class != Term) {
     204                                if ( value && value.class != Term ) {
    194205                                        try {
    195206                                                fields[key] = (value as Term)
     
    209220        }
    210221
     222        /**
     223         * Get the proper templateFields Map for a specific field type
     224         * @param TemplateFieldType
     225         * @return pointer
     226         * @visibility private
     227         * @throws NoSuchFieldException
     228         */
    211229        private Map getStore(TemplateFieldType fieldType) {
    212230                switch(fieldType) {
     
    248266         * @param fieldName The name of the template or entity field
    249267         * @param value The value to be set, this should align with the (template) field type, but there are some convenience setters
    250          *
    251268         */
    252269        def setFieldValue(String fieldName, value) {
    253 
    254270                // First, search if there is an entity property with the given name, and if so, set that
    255271                if (this.properties.containsKey(fieldName)) {
    256272                        this[fieldName] = value                 
    257                 }
    258                 // If not the found, then it is a template field, so check if there is a template
    259                 else if (template == null) {
     273                } else if (template == null) {
     274                        // not the found, then it is a template field, so check if there is a template
    260275                        throw new NoSuchFieldException("Field ${fieldName} not found in class properties: template not set")
    261                 }
    262                 // If there is a template, check the template fields
    263                 else {
     276                } else {
     277                        // there is a template, check the template fields
    264278                        // Find the target template field, if not found, throw an error
    265279                        TemplateField field = this.template.fields.find { it.name == fieldName }
     280
    266281                        if (field == null) {
    267                                 throw new NoSuchFieldException("Field ${fieldName} not found in class properties or template fields")
    268                         }
    269                         // Set the value of the found template field
    270                         else {
     282                                // no such field
     283                                throw new NoSuchFieldException("Field ${fieldName} not found in class template fields")
     284                        } else {
     285                                // Set the value of the found template field
    271286                                // Convenience setter for template string list fields: find TemplateFieldListItem by name
    272287                                if (field.type == TemplateFieldType.STRINGLIST && value.class == String) {
     288                                        // Kees insensitive pattern matching ;)
    273289                                        value = field.listEntries.find { it.name ==~ /(?i)($value)/ }
    274290                                }
     
    296312                                // Caution: this assumes that all template...Field Maps are already initialized (as is done now above as [:])
    297313                                // If that is ever changed, the results are pretty much unpredictable (random Java object pointers?)!
    298                                 getStore(field.type)[fieldName] = value
     314                                def store = getStore(field.type)
     315                                if (!value && store[ fieldName ]) {
     316                                        println "removing " + super.class + " template field: " + fieldName
     317
     318                                        // remove the item from the Map (if present)
     319                                        store.remove( fieldName )
     320                                } else {
     321                                        println "setting " + super.class + " template field: " + fieldName + " ([" + value.toString() + "] of type [" + value.class + "])"
     322
     323                                        // set value
     324                                        store[ fieldName ] = value
     325                                }
    299326                                return this
    300327                        }
    301328                }
    302329        }
    303 
    304330
    305331        /**
Note: See TracChangeset for help on using the changeset viewer.