Changeset 181


Ignore:
Timestamp:
Feb 9, 2010, 4:13:42 PM (14 years ago)
Author:
duh
Message:
  • seperated wizard subject handling logic out of the webflow to make it re-usable for different flow actions
File:
1 edited

Legend:

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

    r180 r181  
    125125                        }.to "subjects"
    126126                        on("next") {
    127                                 def errors = false
    128127                                flash.errors = new LinkedHashMap()
    129128
    130                                 // got one or more subjects?
    131                                 if (flow.subjects.size() < 1) {
    132                                         errors = true;
    133                                 } else {
    134                                         // handle form data
    135                                         def id = 0;
    136                                         flow.subjects.each() {
    137                                                 // store subject properties
    138                                                 it.name = params.get('subject_' + id + '_name')
    139                                                 it.species = Term.findByName(params.get('subject_' + id + '_species'))
    140 
    141                                                 // clear lists
    142                                                 def stringList = new LinkedHashMap();
    143                                                 def intList = new LinkedHashMap();
    144 
    145                                                 // get all template fields
    146                                                 flow.study.template.subjectFields.each() {
    147                                                         // get value
    148                                                         def value = params.get('subject_' + id + '_' + it.name);
    149                                                         if (value) {
    150                                                                 // add to template parameters
    151                                                                 switch (it.type) {
    152                                                                         case 'STRINGLIST':
    153                                                                                 stringList[it.name] = value
    154                                                                                 break;
    155                                                                         case 'INTEGER':
    156                                                                                 intList[ it.name ] = value
    157                                                                                 break;
    158                                                                         default:
    159                                                                                 // unsupported type?
    160                                                                                 println "ERROR: unsupported type: "+it.type
    161                                                                                 break;
    162                                                                 }
    163                                                         }
    164                                                 }
    165 
    166                                                 // set field data
    167                                                 it.templateStringFields         = stringList
    168                                                 it.templateIntegerFields        = intList
    169 
    170                                                 // validate subject
    171                                                 if (!it.validate()) {
    172                                                         errors = true
    173                                                         //println id + ' :: ' + it.errors.getAllErrors()
    174                                                         this.appendErrors(it, flash.errors)
    175                                                 }
    176 
    177                                                 id++;
    178                                         }
    179                                 }
    180 
    181                                 // got errors?
    182                                 if (errors) {
    183                                         println flash.errors
     129                                // handle form data
     130                                if (!this.handleSubjects(flow, flash, params)) {
    184131                                        error()
    185132                                } else {
     
    188135                        }.to "groups"
    189136                        on("previous") {
    190                                 // TODO
     137                                flash.errors = new LinkedHashMap()
     138
     139                                // handle form data
     140                                if (!this.handleSubjects(flow, flash, params)) {
     141                                        error()
     142                                } else {
     143                                        success()
     144                                }
    191145                        }.to "study"
    192146                }
     
    204158                                def increment = flow.groups.size()
    205159                                flow.groups[increment] = new SubjectGroup(params)
    206 
    207                                 println flow.groups
    208160                        }.to "groups"
    209161                        on("next") {
     
    224176                                // TODO
    225177                        }.to "groups"
     178                }
     179        }
     180
     181        /**
     182         * re-usable code for handling subject form data in a web flow
     183         * @param Map   LocalAttributeMap (the flow scope)
     184         * @param Map   localAttributeMap (the flash scope)
     185         * @param Map   GrailsParameterMap (the flow parameters = form data)
     186         * @returns boolean
     187         */
     188        def handleSubjects(flow, flash, params) {
     189                println flow.getClass()
     190                println flash.getClass()
     191                println params.getClass()
     192               
     193                if (flow.subjects.size() < 1) {
     194                        return false
     195                } else {
     196                        def errors = false;
     197                        def id = 0;
     198                        flow.subjects.each() {
     199                                // store subject properties
     200                                it.name = params.get('subject_' + id + '_name')
     201                                it.species = Term.findByName(params.get('subject_' + id + '_species'))
     202
     203                                // clear lists
     204                                def stringList = new LinkedHashMap();
     205                                def intList = new LinkedHashMap();
     206
     207                                // get all template fields
     208                                flow.study.template.subjectFields.each() {
     209                                        // get value
     210                                        def value = params.get('subject_' + id + '_' + it.name);
     211
     212                                        if (value) {
     213                                                // add to template parameters
     214                                                switch (it.type) {
     215                                                        case 'STRINGLIST':
     216                                                                stringList[it.name] = value
     217                                                                break;
     218                                                        case 'INTEGER':
     219                                                                intList[it.name] = value
     220                                                                break;
     221                                                        default:
     222                                                                // unsupported type?
     223                                                                println "ERROR: unsupported type: " + it.type
     224                                                                break;
     225                                                }
     226                                        }
     227                                }
     228
     229                                // set field data
     230                                it.templateStringFields = stringList
     231                                it.templateIntegerFields = intList
     232
     233                                // validate subject
     234                                if (!it.validate()) {
     235                                        errors = true
     236                                        println id + ' :: ' + it.errors.getAllErrors()
     237                                        this.appendErrors(it, flash.errors)
     238                                }
     239
     240                                id++;
     241                        }
     242
     243                        return !errors
    226244                }
    227245        }
Note: See TracChangeset for help on using the changeset viewer.