Ignore:
Timestamp:
Jun 8, 2010, 11:18:27 AM (13 years ago)
Author:
duh
Message:
  • dev commit
File:
1 edited

Legend:

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

    r535 r539  
    293293                                flash.errors = [:]
    294294                                flash.values = params
    295                                 def speciesTerm = Term.findByName(params.species);
    296                                 def subjectTemplateName = params.get('template');
    297                                 def subjectTemplate = Template.findByName(subjectTemplateName);
     295                                def speciesTerm = Term.findByName(params.species)
     296                                def subjectTemplateName = params.get('template')
     297                                def subjectTemplate = Template.findByName(subjectTemplateName)
    298298
    299299                                // add this subject template to the subject template array
     
    330330
    331331                                flash.errors = [:]
    332                                 def delete = params.get('do') as int;
     332                                def delete = params.get('do') as int
    333333
    334334                                // remove subject
     
    456456                        on("deleteEvent") {
    457457                                flash.values = params
    458                                 def delete = params.get('do') as int;
     458                                def delete = params.get('do') as int
    459459
    460460                                // handle event groupings
     
    506506                        on("deleteEventGroup") {
    507507                                flash.values = params
    508                                 def delete = params.get('do') as int;
     508                                def delete = params.get('do') as int
    509509
    510510                                // handle event groupings
     
    589589                                        // iterate through events
    590590                                        flow.events.each() { event ->
    591                                                 println "bla"
    592591                                                if (event instanceof SamplingEvent) {
    593                                                         //println event.getFieldValue('name')
     592                                                        println event.template.name
    594593                                                        println event.startTime
    595594                                                        println event.endTime
     595                                                        def sampleName = (this.ucwords(subject.value.name) + '_' + this.ucwords(event.template.name) + ((event.startTime) ? '_' + event.startTime : '')).replaceAll("([ ]{1,})", "")
     596                                                                //.replaceAll("([^A-Za-z0-9_])", "")
     597                                                        println sampleName
    596598                                                }
    597 
    598599                                        }
    599600                                }
     
    718719        def handleStudy(flow, flash, params) {
    719720                // create study instance if we have none
    720                 if (!flow.study) flow.study = new Study();
     721                if (!flow.study) flow.study = new Study()
    721722
    722723                // create date instance from date string?
     
    743744                }
    744745
    745                 // handle Publications and Contacts
    746                 handlePublications(flow, flash, params)
    747                 handleContacts(flow, flash, params)
     746                // handle Publications and Contacts
     747                handlePublications(flow, flash, params)
     748                handleContacts(flow, flash, params)
    748749
    749750                // validate study
     
    767768        def handlePublications(flow, flash, params) {
    768769                // create study instance if we have none
    769                 if (!flow.study) flow.study = new Study();
    770                 if (!flow.study.publications ) flow.study.publications = [];
    771 
    772                 // Check the ids of the pubblications that should be attached
    773                 // to this study. If they are already attached, keep 'm. If
    774                 // studies are attached that are not in the selected (i.e. the
    775                 // user deleted them), remove them
    776                 def publicationIDs = params.get( 'publication_ids' );
    777                 if( publicationIDs ) {
    778                     // Find the individual IDs and make integers
    779                     publicationIDs = publicationIDs.split(',').collect { Integer.parseInt( it, 10 ) };
    780 
    781                     // First remove the publication that are not present in the array
    782                     flow.study.publications.removeAll { publication -> !publicationIDs.find { id -> id == publication.id } }
    783 
    784                     // Add those publications not yet present in the database
    785                     publicationIDs.each { id ->
    786                         if( !flow.study.publications.find { publication -> id == publication.id } ) {
    787                             def publication = Publication.get( id );
    788                             if( publication ) {
    789                                 flow.study.addToPublications( publication );
    790                             } else {
    791                                 println( 'Publication with ID ' + id + ' not found in database.' );
    792                             }
    793                         }
    794                     }
    795 
    796                 } else {
    797                     println( 'No publications selected.')
    798                     flow.study.publications.clear();
    799                 }
     770                if (!flow.study) flow.study = new Study()
     771                if (!flow.study.publications) flow.study.publications = []
     772
     773                // Check the ids of the pubblications that should be attached
     774                // to this study. If they are already attached, keep 'm. If
     775                // studies are attached that are not in the selected (i.e. the
     776                // user deleted them), remove them
     777                def publicationIDs = params.get('publication_ids')
     778                if (publicationIDs) {
     779                        // Find the individual IDs and make integers
     780                        publicationIDs = publicationIDs.split(',').collect { Integer.parseInt(it, 10) }
     781
     782                        // First remove the publication that are not present in the array
     783                        flow.study.publications.removeAll { publication -> !publicationIDs.find { id -> id == publication.id } }
     784
     785                        // Add those publications not yet present in the database
     786                        publicationIDs.each { id ->
     787                                if (!flow.study.publications.find { publication -> id == publication.id }) {
     788                                        def publication = Publication.get(id)
     789                                        if (publication) {
     790                                                flow.study.addToPublications(publication)
     791                                        } else {
     792                                                println('.publication with ID ' + id + ' not found in database.')
     793                                        }
     794                                }
     795                        }
     796
     797                } else {
     798                        println('.no publications selected.')
     799                        flow.study.publications.clear()
     800                }
    800801
    801802        }
     
    810811        def handleContacts(flow, flash, params) {
    811812                // create study instance if we have none
    812                 if (!flow.study) flow.study = new Study();
    813                 if (!flow.study.persons ) flow.study.persons = [];
    814 
    815                 // Check the ids of the contacts that should be attached
    816                 // to this study. If they are already attached, keep 'm. If
    817                 // studies are attached that are not in the selected (i.e. the
    818                 // user deleted them), remove them
    819 
    820                 // Contacts are saved as [person_id]-[role_id]
    821                 println( params );
    822                 def contactIDs = params.get( 'contacts_ids' );
    823                 println( contactIDs );
    824                 if( contactIDs ) {
    825                     // Find the individual IDs and make integers
    826                     contactIDs = contactIDs.split(',').collect {
    827                         def parts = it.split( '-' );
    828                         return [ person: Integer.parseInt( parts[0] ), role: Integer.parseInt( parts[1] ) ];
    829                     };
    830 
    831                     // First remove the contacts that are not present in the array
    832                     flow.study.persons.removeAll {
    833                         studyperson -> !contactIDs.find { ids -> ( ids.person == studyperson.person.id ) && ( ids.role == studyperson.role.id ) }
    834                     };
    835 
    836                     // Add those contacts not yet present in the database
    837                     contactIDs.each { ids ->
    838                         if( !flow.study.persons.find { studyperson -> ( ids.person == studyperson.person.id ) && ( ids.role == studyperson.role.id ) } ) {
    839                             def person = Person.get( ids.person );
    840                             def role = PersonRole.get( ids.role );
    841                             if( person && role ) {
    842                                 // Find a studyperson object with these parameters
    843                                 def studyPerson = StudyPerson.findAll().find { studyperson -> studyperson.person.id == person.id && studyperson.role.id == role.id };
    844 
    845                                 // If if does not yet exist, save the example
    846                                 if( !studyPerson ) {
    847                                     studyPerson = new StudyPerson(
    848                                         person: person,
    849                                         role: role
    850                                     );
    851                                     studyPerson.save( flush: true );
    852                                 }
    853 
    854                                 flow.study.addToPersons( studyPerson );
    855                             } else {
    856                                 println( 'Person ' + ids.person + ' or Role ' + ids.role + ' not found in database.' );
    857                             }
    858                         }
    859                     }
    860 
    861                 } else {
    862                     println( 'No persons selected.')
    863                     flow.study.persons.clear();
    864                 }
    865 
    866         }
     813                if (!flow.study) flow.study = new Study()
     814                if (!flow.study.persons) flow.study.persons = []
     815
     816                // Check the ids of the contacts that should be attached
     817                // to this study. If they are already attached, keep 'm. If
     818                // studies are attached that are not in the selected (i.e. the
     819                // user deleted them), remove them
     820
     821                // Contacts are saved as [person_id]-[role_id]
     822                def contactIDs = params.get('contacts_ids')
     823                if (contactIDs) {
     824                        // Find the individual IDs and make integers
     825                        contactIDs = contactIDs.split(',').collect {
     826                                def parts = it.split('-')
     827                                return [person: Integer.parseInt(parts[0]), role: Integer.parseInt(parts[1])]
     828                        }
     829
     830                        // First remove the contacts that are not present in the array
     831                        flow.study.persons.removeAll {
     832                                studyperson -> !contactIDs.find { ids -> (ids.person == studyperson.person.id) && (ids.role == studyperson.role.id) }
     833                        }
     834
     835                        // Add those contacts not yet present in the database
     836                        contactIDs.each { ids ->
     837                                if (!flow.study.persons.find { studyperson -> (ids.person == studyperson.person.id) && (ids.role == studyperson.role.id) }) {
     838                                        def person = Person.get(ids.person)
     839                                        def role = PersonRole.get(ids.role)
     840                                        if (person && role) {
     841                                                // Find a studyperson object with these parameters
     842                                                def studyPerson = StudyPerson.findAll().find { studyperson -> studyperson.person.id == person.id && studyperson.role.id == role.id }
     843
     844                                                // If if does not yet exist, save the example
     845                                                if (!studyPerson) {
     846                                                        studyPerson = new StudyPerson(
     847                                                                person: person,
     848                                                                role: role
     849                                                        )
     850                                                        studyPerson.save(flush: true)
     851                                                }
     852
     853                                                flow.study.addToPersons(studyPerson)
     854                                        } else {
     855                                                println('.person ' + ids.person + ' or Role ' + ids.role + ' not found in database.')
     856                                        }
     857                                }
     858                        }
     859                } else {
     860                        println('.no persons selected.')
     861                        flow.study.persons.clear()
     862                }
     863
     864        }
     865
    867866        /**
    868867         * re-usable code for handling subject form data in a web flow
     
    914913                // handle the type of event
    915914                if (params.eventType == 'event') {
    916                         flow.event = new Event();
     915                        flow.event = new Event()
    917916                        template = params.remove('eventTemplate')
    918917                } else if (params.eventType == 'sample') {
    919                         flow.event = new SamplingEvent();
     918                        flow.event = new SamplingEvent()
    920919                        template = params.remove('sampleTemplate')
    921920                }
     
    10141013        }
    10151014
     1015
     1016        /**
     1017         * groovy / java equivalent of php's ucwords function
     1018         *
     1019         * Capitalize all first letters of seperate words
     1020         *
     1021         * @param String
     1022         * @return String
     1023         */
     1024        def ucwords(String text) {
     1025                def newText = ''
     1026
     1027                // change case to lowercase
     1028                text = text.toLowerCase()
     1029
     1030                // iterate through words
     1031                text.split(" ").each() {
     1032                        newText += it[0].toUpperCase() + it.substring(1) + " "
     1033                }
     1034
     1035                return newText.substring(0, newText.size()-1)
     1036        }
     1037
    10161038        /**
    10171039         * return the object from a map of objects by searching for a name
     
    10881110        def ajaxParseRelTime = {
    10891111                if (params.reltime == null) {
    1090                         response.status = 400;
    1091                         render('reltime parameter is expected');
     1112                        response.status = 400
     1113                        render('reltime parameter is expected')
    10921114                }
    10931115
    10941116                try {
    1095                         def reltime = RelTime.parseRelTime(params.reltime);
    1096                         render reltime.toPrettyString();
     1117                        def reltime = RelTime.parseRelTime(params.reltime)
     1118                        render reltime.toPrettyString()
    10971119                } catch (IllegalArgumentException e) {
    1098                         response.status = 400;
    1099                         render(e.getMessage());
     1120                        response.status = 400
     1121                        render(e.getMessage())
    11001122                }
    11011123        }
Note: See TracChangeset for help on using the changeset viewer.