Changeset 238 for trunk/grails-app/controllers
- Timestamp:
- Mar 5, 2010, 3:21:52 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/dbnp/studycapturing/WizardController.groovy
r217 r238 47 47 flow.page = 0 48 48 flow.pages = [ 49 [title: 'Templates'], // templates 49 //[title: 'Templates'], // templates 50 [title: 'Start'], // load or create a study 50 51 [title: 'Study'], // study 51 52 [title: 'Subjects'], // subjects … … 67 68 flow.page = 1 68 69 } 69 on("next").to " templates"70 on("next").to "start" 70 71 } 71 72 … … 104 105 } 105 106 107 // create or modify a study 108 start { 109 render(view: "_start") 110 onRender { 111 flow.page = 1 112 } 113 on("next") { 114 115 }.to "study" 116 } 117 106 118 // render and handle the study page 119 // TODO: make sure both template as well as logic will 120 // handle Study templates as well!!! 107 121 study { 108 122 render(view: "_study") … … 118 132 error() 119 133 } 120 }.to " templates"134 }.to "start" 121 135 on("next") { 122 136 flash.errors = new LinkedHashMap() … … 138 152 if (!flow.subjects) { 139 153 flow.subjects = [] 154 flow.subjectTemplates = new LinkedHashMap() 140 155 } 141 156 } … … 143 158 // fetch species by name (as posted by the form) 144 159 def speciesTerm = Term.findByName(params.addSpecies) 160 def subjectTemplateName = params.get('template') 161 def subjectTemplate = Template.findByName(subjectTemplateName) 162 163 // add this subject template to the subject template array 164 if (!flow.subjectTemplates[ subjectTemplateName ]) { 165 flow.subjectTemplates[ subjectTemplateName ] = [ 166 name: subjectTemplateName, 167 template: subjectTemplate, 168 subjects: [] 169 ] 170 } 145 171 146 172 // add x subject of species y 147 173 (params.addNumber as int).times { 148 174 def increment = flow.subjects.size() 149 flow.subjects[increment]= new Subject(175 def subject = new Subject( 150 176 name: 'Subject ' + (increment + 1), 151 177 species: speciesTerm, 152 template: flow.study.template178 template: subjectTemplate 153 179 ) 180 181 // instantiate a new Subject 182 flow.subjects[ increment ] = subject 183 184 // and remember the subject id with the template 185 def subjectsSize = flow.subjectTemplates[ subjectTemplateName ]['subjects'].size() 186 flow.subjectTemplates[ subjectTemplateName ]['subjects'][ subjectsSize ] = increment 154 187 } 155 188 }.to "subjects" 156 189 on("next") { 190 println flow.subjectTemplates 191 println flow.subjects 157 192 flash.errors = new LinkedHashMap() 158 193 … … 193 228 on("add") { 194 229 // fetch classification by name (as posted by the form) 195 params.classification = Term.findByName(params.classification)230 //params.classification = Term.findByName(params.classification) 196 231 197 232 // transform checkbox form value to boolean … … 427 462 428 463 /** 429 * re-usable code for handling event grouping in a web flow430 * @param Map LocalAttributeMap (the flow scope)431 * @param Map localAttributeMap (the flash scope)432 * @param Map GrailsParameterMap (the flow parameters = form data)433 * @returns boolean434 */435 def handleEventGrouping(flow, flash, params) {436 // walk through eventGroups437 def g = 0438 flow.eventGroups.each() {439 def e = 0440 def eventGroup = it441 442 // reset events443 eventGroup.events = new HashSet()444 445 // walk through events446 flow.events.each() {447 if (params.get('event_' + e + '_group_' + g) == 'on') {448 eventGroup.addToEvents(it)449 }450 e++451 }452 g++453 }454 }455 456 /**457 464 * re-usable code for handling study form data in a web flow 458 465 * @param Map LocalAttributeMap (the flow scope) … … 511 518 it.name = params.get('eventDescription_' + id + '_name') 512 519 it.description = params.get('eventDescription_' + id + '_description') 513 it.classification = Term.findByName(params.get('eventDescription_' + id + '_classification'))520 //it.classification = Term.findByName(params.get('eventDescription_' + id + '_classification')) 514 521 it.isSamplingEvent = (params.containsKey('eventDescription_' + id + '_isSamplingEvent')) 515 522 … … 523 530 } 524 531 525 return !(errors) 532 return !errors 533 } 534 535 /** 536 * re-usable code for handling event grouping in a web flow 537 * @param Map LocalAttributeMap (the flow scope) 538 * @param Map localAttributeMap (the flash scope) 539 * @param Map GrailsParameterMap (the flow parameters = form data) 540 * @returns boolean 541 */ 542 def handleEventGrouping(flow, flash, params) { 543 // walk through eventGroups 544 def g = 0 545 flow.eventGroups.each() { 546 def e = 0 547 def eventGroup = it 548 549 // reset events 550 eventGroup.events = new HashSet() 551 552 // walk through events 553 flow.events.each() { 554 if (params.get('event_' + e + '_group_' + g) == 'on') { 555 eventGroup.addToEvents(it) 556 } 557 e++ 558 } 559 g++ 560 } 526 561 } 527 562 … … 538 573 def id = 0; 539 574 540 // iterate through subjects 541 flow.subjects.each() { 542 // store subject properties 543 def name = params.get('subject_' + id + '_name') 544 it.name = params.get('subject_' + id + '_name') 545 it.species = Term.findByName(params.get('subject_' + id + '_species')) 546 547 // remember name and check for duplicates 548 if (!names[it.name]) { 549 names[it.name] = [count: 1, first: 'subject_' + id + '_name', firstId: id] 550 } else { 551 // duplicate name found, set error flag 552 names[it.name]['count']++ 553 554 // second occurence? 555 if (names[it.name]['count'] == 2) { 556 // yeah, also mention the first 557 // occurrence in the error message 558 this.appendErrorMap(name: 'The subject name needs to be unique!', flash.errors, 'subject_' + names[it.name]['firstId'] + '_') 559 } 560 561 // add to error map 562 this.appendErrorMap([name: 'The subject name needs to be unique!'], flash.errors, 'subject_' + id + '_') 563 errors = true 564 } 565 566 // clear lists 567 def stringList = new LinkedHashMap(); 568 def intList = new LinkedHashMap(); 569 def floatList = new LinkedHashMap(); 570 def termList = new LinkedHashMap(); 571 572 // get all template fields 573 flow.study.template.subjectFields.each() { 574 // valid type? 575 if (!it.type) throw new NoSuchFieldException("Field name ${fieldName} not recognized") 576 577 // get value 578 def value = params.get('subject_' + id + '_' + it.name); 579 if (value) { 580 // add to template parameters 581 switch (it.type) { 582 case 'STRINGLIST': 583 stringList[it.name] = value 584 break; 585 case 'INTEGER': 586 intList[it.name] = value 587 break; 588 case 'FLOAT': 589 floatList[it.name] = value 590 break; 591 default: 592 // unsupported type? 593 throw new NoSuchFieldException("Field type ${it.type} not recognized") 594 break; 575 // iterate through subject templates 576 flow.subjectTemplates.each() { 577 def subjectTemplate = it.getValue().template 578 def templateFields = subjectTemplate.fields 579 580 // iterate through subjects 581 it.getValue().subjects.each() { subjectId -> 582 flow.subjects[ subjectId ].name = params.get('subject_' + subjectId + '_name') 583 flow.subjects[ subjectId ].species = Term.findByName(params.get('subject_' + subjectId + '_species')) 584 585 // remember name and check for duplicates 586 if (!names[ flow.subjects[ subjectId ].name ]) { 587 names[ flow.subjects[ subjectId ].name ] = [count: 1, first: 'subject_' + subjectId + '_name', firstId: subjectId] 588 } else { 589 // duplicate name found, set error flag 590 names[ flow.subjects[ subjectId ].name ]['count']++ 591 592 // second occurence? 593 if (names[ flow.subjects[ subjectId ].name ]['count'] == 2) { 594 // yeah, also mention the first 595 // occurrence in the error message 596 this.appendErrorMap(name: 'The subject name needs to be unique!', flash.errors, 'subject_' + names[ flow.subjects[ subjectId ].name ]['firstId'] + '_') 595 597 } 596 } 597 } 598 599 // set field data 600 it.templateStringFields = stringList 601 it.templateIntegerFields = intList 602 it.templateFloatFields = floatList 603 it.templateTermFields = termList 604 605 // validate subject 606 if (!it.validate()) { 607 errors = true 608 this.appendErrors(it, flash.errors) 609 } 610 611 id++; 598 599 // add to error map 600 this.appendErrorMap([name: 'The subject name needs to be unique!'], flash.errors, 'subject_' + subjectId + '_') 601 errors = true 602 } 603 604 // iterate through template fields 605 templateFields.each() { subjectField -> 606 def value = params.get('subject_' + subjectId + '_' + subjectField.name) 607 608 // TODO: UNCOMMENT THIS if (value) flow.subjects[ subjectId ].setFieldValue(subjectField.name, value) 609 } 610 611 // validate subject 612 if (!flow.subjects[ subjectId ].validate()) { 613 errors = true 614 this.appendErrors(flow.subjects[ subjectId ], flash.errors) 615 } 616 } 612 617 } 613 618
Note: See TracChangeset
for help on using the changeset viewer.