Changeset 192 for trunk/grails-app/controllers
- Timestamp:
- Feb 11, 2010, 5:47:49 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/dbnp/studycapturing/WizardController.groovy
r189 r192 127 127 flash.errors = new LinkedHashMap() 128 128 129 // handle form data 130 if (!this.handleSubjects(flow, flash, params)) { 129 // check if we have at least one subject 130 // and check form data 131 if (flow.subjects.size() < 1) { 132 // append error map 133 this.appendErrorMap(['subjects': 'You need at least to create one subject for your study'], flash.errors) 134 error() 135 } else if (!this.handleSubjects(flow, flash, params)) { 131 136 error() 132 137 } else { … … 182 187 /** 183 188 * re-usable code for handling subject form data in a web flow 184 * @param Map 185 * @param Map 186 * @param Map 189 * @param Map LocalAttributeMap (the flow scope) 190 * @param Map localAttributeMap (the flash scope) 191 * @param Map GrailsParameterMap (the flow parameters = form data) 187 192 * @returns boolean 188 193 */ 189 194 def handleSubjects(flow, flash, params) { 190 if (flow.subjects.size() < 1) { 191 return false 192 } else { 193 def names = new LinkedHashMap(); 194 def errors = false; 195 def id = 0; 196 197 // iterate through subjects 198 flow.subjects.each() { 199 // store subject properties 200 def name = params.get('subject_' + id + '_name') 201 it.name = params.get('subject_' + id + '_name') 202 it.species = Term.findByName(params.get('subject_' + id + '_species')) 203 204 // remember name and check for duplicates 205 if (!names[ it.name ]) { 206 names[ it.name ] = [count: 1, first: 'subject_' + id + '_name'] 207 } else { 208 // duplicate name found, set error flag 209 names[ it.name ]['count']++ 210 211 // second occurence? 212 if (names[ it.name ]['count'] == 2) { 213 // yeah, also mention the first 214 // occurrence in the error message 215 this.appendErrorMap([[names[ it.name ]['first']]: 'The subject name needs to be unique!'], flash.errors) 195 def names = new LinkedHashMap(); 196 def errors = false; 197 def id = 0; 198 199 // iterate through subjects 200 flow.subjects.each() { 201 // store subject properties 202 def name = params.get('subject_' + id + '_name') 203 it.name = params.get('subject_' + id + '_name') 204 it.species = Term.findByName(params.get('subject_' + id + '_species')) 205 206 // remember name and check for duplicates 207 if (!names[it.name]) { 208 names[it.name] = [count: 1, first: 'subject_' + id + '_name'] 209 } else { 210 // duplicate name found, set error flag 211 names[it.name]['count']++ 212 213 // second occurence? 214 if (names[it.name]['count'] == 2) { 215 // yeah, also mention the first 216 // occurrence in the error message 217 this.appendErrorMap([[names[it.name]['first']]: 'The subject name needs to be unique!'], flash.errors) 218 } 219 220 // add to error map 221 this.appendErrorMap([['subject_' + id + '_name']: 'The subject name needs to be unique!'], flash.errors) 222 errors = true 223 } 224 225 // clear lists 226 def stringList = new LinkedHashMap(); 227 def intList = new LinkedHashMap(); 228 def floatList = new LinkedHashMap(); 229 def termList = new LinkedHashMap(); 230 231 // get all template fields 232 flow.study.template.subjectFields.each() { 233 // valid type? 234 if (!it.type) throw new NoSuchFieldException("Field name ${fieldName} not recognized") 235 236 // get value 237 def value = params.get('subject_' + id + '_' + it.name); 238 if (value) { 239 // add to template parameters 240 switch (it.type) { 241 case 'STRINGLIST': 242 stringList[it.name] = value 243 break; 244 case 'INTEGER': 245 intList[it.name] = value 246 break; 247 case 'FLOAT': 248 floatList[it.name] = value 249 break; 250 default: 251 // unsupported type? 252 throw new NoSuchFieldException("Field type ${it.type} not recognized") 253 break; 216 254 } 217 218 // add to error map 219 this.appendErrorMap([['subject_' + id + '_name']: 'The subject name needs to be unique!'], flash.errors) 220 errors = true 221 } 222 223 // clear lists 224 def stringList = new LinkedHashMap(); 225 def intList = new LinkedHashMap(); 226 def floatList = new LinkedHashMap(); 227 def termList = new LinkedHashMap(); 228 229 // get all template fields 230 flow.study.template.subjectFields.each() { 231 // valid type? 232 if (!it.type) throw new NoSuchFieldException("Field name ${fieldName} not recognized") 233 234 // get value 235 def value = params.get('subject_' + id + '_' + it.name); 236 if (value) { 237 // add to template parameters 238 switch (it.type) { 239 case 'STRINGLIST': 240 stringList[it.name] = value 241 break; 242 case 'INTEGER': 243 intList[it.name] = value 244 break; 245 case 'FLOAT': 246 floatList[it.name] = value 247 break; 248 default: 249 // unsupported type? 250 throw new NoSuchFieldException("Field type ${it.type} not recognized") 251 break; 252 } 253 } 254 } 255 256 // set field data 257 it.templateStringFields = stringList 258 it.templateIntegerFields = intList 259 it.templateFloatFields = floatList 260 it.templateTermFields = termList 261 262 // validate subject 263 if (!it.validate()) { 264 errors = true 265 println id + ' :: ' + it.errors.getAllErrors() 266 this.appendErrors(it, flash.errors) 267 } 268 269 id++; 270 } 271 272 return !errors 273 } 255 } 256 } 257 258 // set field data 259 it.templateStringFields = stringList 260 it.templateIntegerFields = intList 261 it.templateFloatFields = floatList 262 it.templateTermFields = termList 263 264 // validate subject 265 if (!it.validate()) { 266 errors = true 267 println id + ' :: ' + it.errors.getAllErrors() 268 this.appendErrors(it, flash.errors) 269 } 270 271 id++; 272 } 273 274 return !errors 274 275 } 275 276
Note: See TracChangeset
for help on using the changeset viewer.