- Timestamp:
- Jun 4, 2010, 12:15:24 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/dbnp/studycapturing/WizardController.groovy
r526 r527 682 682 } 683 683 684 // handle Publications 684 // handle Publications and Contacts 685 685 handlePublications(flow, flash, params) 686 handleContacts(flow, flash, params) 686 687 687 688 // validate study … … 739 740 } 740 741 741 742 /** 743 * re-usable code for handling contacts form data in a web flow 744 * @param Map LocalAttributeMap (the flow scope) 745 * @param Map localAttributeMap (the flash scope) 746 * @param Map GrailsParameterMap (the flow parameters = form data) 747 * @returns boolean 748 */ 749 def handleContacts(flow, flash, params) { 750 // create study instance if we have none 751 if (!flow.study) flow.study = new Study(); 752 if (!flow.study.persons ) flow.study.persons = []; 753 754 // Check the ids of the contacts that should be attached 755 // to this study. If they are already attached, keep 'm. If 756 // studies are attached that are not in the selected (i.e. the 757 // user deleted them), remove them 758 759 // Contacts are saved as [person_id]-[role_id] 760 println( params ); 761 def contactIDs = params.get( 'contacts_ids' ); 762 println( contactIDs ); 763 if( contactIDs ) { 764 // Find the individual IDs and make integers 765 contactIDs = contactIDs.split(',').collect { 766 def parts = it.split( '-' ); 767 return [ person: Integer.parseInt( parts[0] ), role: Integer.parseInt( parts[1] ) ]; 768 }; 769 770 // First remove the contacts that are not present in the array 771 flow.study.persons.removeAll { 772 studyperson -> !contactIDs.find { ids -> ( ids.person == studyperson.person.id ) && ( ids.role == studyperson.role.id ) } 773 }; 774 775 // Add those contacts not yet present in the database 776 contactIDs.each { ids -> 777 if( !flow.study.persons.find { studyperson -> ( ids.person == studyperson.person.id ) && ( ids.role == studyperson.role.id ) } ) { 778 def person = Person.get( ids.person ); 779 def role = PersonRole.get( ids.role ); 780 if( person && role ) { 781 // Find a studyperson object with these parameters 782 def studyPerson = StudyPerson.findAll().find { studyperson -> studyperson.person.id == person.id && studyperson.role.id == role.id }; 783 784 // If if does not yet exist, save the example 785 if( !studyPerson ) { 786 studyPerson = new StudyPerson( 787 person: person, 788 role: role 789 ); 790 studyPerson.save( flush: true ); 791 } 792 793 flow.study.addToPersons( studyPerson ); 794 } else { 795 println( 'Person ' + ids.person + ' or Role ' + ids.role + ' not found in database.' ); 796 } 797 } 798 } 799 800 } else { 801 println( 'No persons selected.') 802 flow.study.persons.clear(); 803 } 804 805 } 742 806 /** 743 807 * re-usable code for handling subject form data in a web flow
Note: See TracChangeset
for help on using the changeset viewer.