Changeset 390 for trunk/grails-app/controllers
- Timestamp:
- May 3, 2010, 3:55:29 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/dbnp/studycapturing/WizardController.groovy
r389 r390 242 242 flow.page = 4 243 243 244 /* 244 245 if (!flow.event) flow.event = new Event() 245 246 if (!flow.events) flow.events = [] … … 247 248 flow.eventGroups = [] 248 249 flow.eventGroups[0] = new EventGroup(name: 'Group 1') // 1 group by default 250 } 251 */ 252 253 if (!flow.event) { 254 flow.event = new Event() 255 flow.events = [] 256 flow.eventGroups = [] 257 flow.eventGroups[0] = new EventGroup(name: 'Group 1') // 1 group by default 258 flow.eventTemplates = [:] 249 259 } 250 260 } … … 303 313 } 304 314 */ 305 flash.values = params 315 flash.values = params 316 def eventTemplateName = params.get('template') 317 def eventTemplate = Template.findByName(eventTemplateName) 318 319 // add this event template to the event template array 320 if (!flow.eventTemplates[ eventTemplateName ]) { 321 flow.eventTemplates[ eventTemplateName ] = [ 322 name: eventTemplateName, 323 template: eventTemplate, 324 events: [] 325 ] 326 } 306 327 307 328 // handle study data … … 310 331 // validate event object 311 332 if (flow.event.validate()) { 312 //flow.events[ flow.events.size() ] = flow.event 333 334 flow.event.template.fields.each() { 335 println "["+it.name+"] = "+flow.event.getFieldValue(it.name) 336 } 337 // it validated! Duplicate the event object... 338 def newEvent = flow.event 339 def increment = flow.events.size() 340 341 // ...store it in the events map in the flow scope... 342 flow.events[ increment ] = newEvent 343 344 // ...and 'reset' the event object in the flow scope 345 flow.event = new Event(template: newEvent.template) 346 347 // remember the event id with the template 348 def eventSize = flow.eventTemplates[ eventTemplateName ]['events'].size() 349 flow.eventTemplates[ eventTemplateName ]['events'][ eventSize ] = increment 350 313 351 success() 314 352 } else { 353 // it does not validate, show error feedback 315 354 flash.errors = [:] 316 355 this.appendErrors(flow.event, flash.errors) … … 382 421 flash.values = params 383 422 flash.errors = [:] 423 /* 384 424 385 425 // handle event groupings … … 394 434 error() 395 435 } 396 }.to "confirm" 436 */ 437 }.to "events" 397 438 } 398 439 … … 524 565 } 525 566 567 /** 568 * re-usable code for handling study form data in a web flow 569 * @param Map LocalAttributeMap (the flow scope) 570 * @param Map localAttributeMap (the flash scope) 571 * @param Map GrailsParameterMap (the flow parameters = form data) 572 * @returns boolean 573 */ 574 def handleStudy(flow, flash, params) { 575 // create study instance if we have none 576 if (!flow.study) flow.study = new Study(); 577 578 // create date instance from date string? 579 // @see WizardTagLibrary::dateElement{...} 580 if (params.get('startDate')) { 581 params.startDate = new Date().parse("d/M/yyyy", params.get('startDate').toString()) 582 } else { 583 params.remove('startDate') 584 } 585 586 // if a template is selected, get template instance 587 def template = params.remove('template') 588 if (template instanceof String && template.size() > 0) { 589 params.template = Template.findByName(template) 590 } else if (template instanceof Template) { 591 params.template = template 592 } 593 594 // update study instance with parameters 595 params.each() { key, value -> 596 if (flow.study.hasProperty(key)) { 597 flow.study.setProperty(key, value); 598 } 599 } 600 601 // walk through template fields 602 if (params.template) { 603 params.template.fields.each() { field -> 604 flow.study.setFieldValue(field.name, params.get(field.escapedName())) 605 } 606 } 607 608 // validate study 609 if (flow.study.validate()) { 610 return true 611 } else { 612 // validation failed, feedback errors 613 flash.errors = [:] 614 this.appendErrors(flow.study, flash.errors) 615 return false 616 } 617 } 618 619 /** 620 * re-usable code for handling subject form data in a web flow 621 * @param Map LocalAttributeMap (the flow scope) 622 * @param Map localAttributeMap (the flash scope) 623 * @param Map GrailsParameterMap (the flow parameters = form data) 624 * @returns boolean 625 */ 626 def handleSubjects(flow, flash, params) { 627 def names = [:]; 628 def errors = false; 629 def id = 0; 630 631 // iterate through subject templates 632 flow.subjectTemplates.each() { 633 def subjectTemplate = it.getValue().template 634 def templateFields = subjectTemplate.fields 635 636 // iterate through subjects 637 it.getValue().subjects.each() { subjectId -> 638 flow.subjects[ subjectId ].name = params.get('subject_' + subjectId + '_name') 639 flow.subjects[ subjectId ].species = Term.findByName(params.get('subject_' + subjectId + '_species')) 640 641 // remember name and check for duplicates 642 if (!names[ flow.subjects[ subjectId ].name ]) { 643 names[ flow.subjects[ subjectId ].name ] = [count: 1, first: 'subject_' + subjectId + '_name', firstId: subjectId] 644 } else { 645 // duplicate name found, set error flag 646 names[ flow.subjects[ subjectId ].name ]['count']++ 647 648 // second occurence? 649 if (names[ flow.subjects[ subjectId ].name ]['count'] == 2) { 650 // yeah, also mention the first 651 // occurrence in the error message 652 this.appendErrorMap(name: 'The subject name needs to be unique!', flash.errors, 'subject_' + names[ flow.subjects[ subjectId ].name ]['firstId'] + '_') 653 } 654 655 // add to error map 656 this.appendErrorMap([name: 'The subject name needs to be unique!'], flash.errors, 'subject_' + subjectId + '_') 657 errors = true 658 } 659 660 // iterate through template fields 661 templateFields.each() { subjectField -> 662 flow.subjects[ subjectId ].setFieldValue( 663 subjectField.name, 664 params.get( 'subject_' + subjectId + '_' + subjectField.escapedName() ) 665 ) 666 } 667 668 // validate subject 669 if (!flow.subjects[ subjectId ].validate()) { 670 errors = true 671 this.appendErrors(flow.subjects[ subjectId ], flash.errors, 'subject_' + subjectId + '_') 672 } 673 } 674 } 675 676 return !errors 677 } 526 678 527 679 /** … … 548 700 // set template 549 701 if (params.template) flow.event.template = params.template 550 println flow.event.template 551 println params 552 702 553 703 // update event instance with parameters 554 704 params.each() { key, value -> 555 try { 705 // does this event have such a property or (if 706 // a template is set) such a template field? 707 if (flow.event.fieldExists(key)) { 708 // yes, set it 556 709 flow.event.setFieldValue(key, value) 557 println "has "+key 558 } catch (Exception e) { 559 println "does NOT have "+key 560 } 561 /* 562 if (flow.event.hasProperty(key)) { 563 println "has property " + key 564 flow.event.setProperty(key, value); 565 } 566 */ 567 } 568 } 569 570 /** 571 * re-usable code for handling study form data in a web flow 572 * @param Map LocalAttributeMap (the flow scope) 573 * @param Map localAttributeMap (the flash scope) 574 * @param Map GrailsParameterMap (the flow parameters = form data) 575 * @returns boolean 576 */ 577 def handleStudy(flow, flash, params) { 578 // create study instance if we have none 579 if (!flow.study) flow.study = new Study(); 580 581 // create date instance from date string? 582 // @see WizardTagLibrary::dateElement{...} 583 if (params.get('startDate')) { 584 params.startDate = new Date().parse("d/M/yyyy", params.get('startDate').toString()) 585 } else { 586 params.remove('startDate') 587 } 588 589 // if a template is selected, get template instance 590 def template = params.remove('template') 591 if (template instanceof String && template.size() > 0) { 592 params.template = Template.findByName(template) 593 } else if (template instanceof Template) { 594 params.template = template 595 } 596 597 // update study instance with parameters 598 params.each() { key, value -> 599 if (flow.study.hasProperty(key)) { 600 flow.study.setProperty(key, value); 601 } 602 } 603 604 // walk through template fields 605 if (params.template) { 606 params.template.fields.each() { field -> 607 flow.study.setFieldValue(field.name, params.get(field.escapedName())) 608 } 609 } 610 611 // validate study 612 if (flow.study.validate()) { 613 return true 614 } else { 615 // validation failed, feedback errors 616 flash.errors = [:] 617 this.appendErrors(flow.study, flash.errors) 618 return false 619 } 620 } 621 622 /** 623 * re-usable code for handling eventDescription form data in a web flow 624 * @param Map LocalAttributeMap (the flow scope) 625 * @param Map localAttributeMap (the flash scope) 626 * @param Map GrailsParameterMap (the flow parameters = form data) 627 * @returns boolean 628 */ 629 def handleEventDescriptions(flow, flash, params) { 630 def names = [:] 631 def errors = false 632 def id = 0 633 634 flow.eventDescriptions.each() { 635 it.name = params.get('eventDescription_' + id + '_name') 636 it.description = params.get('eventDescription_' + id + '_description') 637 it.protocol = Protocol.findByName(params.get('eventDescription_' + id + '_protocol')) 638 //it.classification = Term.findByName(params.get('eventDescription_' + id + '_classification')) 639 it.isSamplingEvent = (params.containsKey('eventDescription_' + id + '_isSamplingEvent')) 640 641 // validate eventDescription 642 if (!it.validate()) { 643 errors = true 644 this.appendErrors(it, flash.errors, 'eventDescription_' + id + '_') 645 } 646 647 id++ 648 } 710 } 711 } 712 713 // handle event objects 714 flow.eventTemplates.each() { 715 def eventTemplate = it.getValue().template 716 def templateFields = eventTemplate.fields 717 718 // iterate through events 719 it.getValue().events.each() { eventId -> 720 // iterate through template fields 721 templateFields.each() { eventField -> 722 flow.events[ eventId ].setFieldValue( 723 eventField.name, 724 params.get( 'event_' + eventId + '_' + eventField.escapedName() ) 725 ) 726 } 727 728 // validate event 729 if (!flow.events[ eventId ].validate()) { 730 errors = true 731 this.appendErrors(flow.events[ eventId ], flash.errors, 'event_' + eventId + '_') 732 } 733 734 735 } 736 } 737 738 // handle event grouping 739 handleEventGrouping(flow, flash, params) 740 741 println flow.event 649 742 650 743 return !errors … … 680 773 681 774 /** 682 * re-usable code for handling subject form data in a web flow683 * @param Map LocalAttributeMap (the flow scope)684 * @param Map localAttributeMap (the flash scope)685 * @param Map GrailsParameterMap (the flow parameters = form data)686 * @returns boolean687 */688 def handleSubjects(flow, flash, params) {689 def names = [:];690 def errors = false;691 def id = 0;692 693 // iterate through subject templates694 flow.subjectTemplates.each() {695 def subjectTemplate = it.getValue().template696 def templateFields = subjectTemplate.fields697 698 // iterate through subjects699 it.getValue().subjects.each() { subjectId ->700 flow.subjects[ subjectId ].name = params.get('subject_' + subjectId + '_name')701 flow.subjects[ subjectId ].species = Term.findByName(params.get('subject_' + subjectId + '_species'))702 703 // remember name and check for duplicates704 if (!names[ flow.subjects[ subjectId ].name ]) {705 names[ flow.subjects[ subjectId ].name ] = [count: 1, first: 'subject_' + subjectId + '_name', firstId: subjectId]706 } else {707 // duplicate name found, set error flag708 names[ flow.subjects[ subjectId ].name ]['count']++709 710 // second occurence?711 if (names[ flow.subjects[ subjectId ].name ]['count'] == 2) {712 // yeah, also mention the first713 // occurrence in the error message714 this.appendErrorMap(name: 'The subject name needs to be unique!', flash.errors, 'subject_' + names[ flow.subjects[ subjectId ].name ]['firstId'] + '_')715 }716 717 // add to error map718 this.appendErrorMap([name: 'The subject name needs to be unique!'], flash.errors, 'subject_' + subjectId + '_')719 errors = true720 }721 722 // iterate through template fields723 templateFields.each() { subjectField ->724 flow.subjects[ subjectId ].setFieldValue(725 subjectField.name,726 params.get( 'subject_' + subjectId + '_' + subjectField.escapedName() )727 )728 }729 730 // validate subject731 if (!flow.subjects[ subjectId ].validate()) {732 errors = true733 this.appendErrors(flow.subjects[ subjectId ], flash.errors, 'subject_' + subjectId + '_')734 }735 }736 }737 738 return !errors739 }740 741 /**742 775 * return the object from a map of objects by searching for a name 743 776 * @param String name
Note: See TracChangeset
for help on using the changeset viewer.