Changeset 288


Ignore:
Timestamp:
Mar 21, 2010, 2:17:51 PM (13 years ago)
Author:
jahn
Message:

Event's create and edit action have been tested and are now working.

Also, the old problem of adding Samples has been addressed. When a new event is created as a SamplingEvent?, no new samples can be added directly. The reason for this is, that the event is not yet assigned to any Subject, and thus the non nullable subject field of a new sample (belonging to the new event) cannot be filled.
Similarly, when a SamplingEvent? event is edited, new samples can only be added, if the event already is assigned to belong to a subject. New samples are always added to the same subject as the first (random) sample belonging to the SamplingEvent?.

However, there are still open questions:

(1) Should the sample also be assigned to the samples of its subject's Study?

(2) Should a sample also be assigned, if there are no samples assigned yet, but there is a subject assigned to this event?

(3) Should the user be allowed to chose to assign a subject if the event is assigned to a Study that contains multiple subjects?

Location:
trunk/grails-app
Files:
6 edited

Legend:

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

    r270 r288  
    6060
    6161
    62     def save = {
    63 
    64         params.each{ println it }
    65 
    66         if( params['id']==null ) {                                                        // this is an entirely new event
    67 
    68             def description = new EventDescription()
    69             description.name = (params['name']==null || params['name'].replaceAll(/\w/,'').size()==0 ) ? '[no Name]' : params['name']
    70             description.description = (params['description']==null || params['description'].replaceAll(/\w/,'').size()==0 ) ? '[no description]' : params['description']
    71             description.protocol = Protocol.get( params['protocol'] )
    72             description.isSamplingEvent = params['isSamplingEvent']=='on' ? true : false
    73 
    74             if (description.save(flush: true)) {
    75                 flash.message = "${message(code: 'default.created.message', args: [message(code: 'description.label', default: 'Event'), description.id])}"
    76             }
    77             else {
    78                 description.errors.each{ println it }
    79             }
    80 
    81             def event = description.isSamplingEvent ? new SamplingEvent() : new Event();
    82 
    83             event.startTime = new Date(params["startTime"])                   // parse the date strings
    84             event.endTime = new Date(params["endTime"])                       // parse the date strings
    85             event.parameterStringValues = new HashMap()
    86             event.parameterFloatValues = new HashMap()
    87             event.parameterIntegerValues = new HashMap()
    88             event.parameterStringListValues = new HashMap()
    89             event.eventDescription = description
    90 
    91 
    92             if (event.save(flush:true, validate:false)) {
    93                 flash.message = "${message(code: 'default.created.message', args: [message(code: 'event.label', default: 'Event'), event.id])}"
    94             }
    95             else {
    96                     event.errors.each{ println it }
    97             }
    98 
    99 
    100 
    101             // parse the parameter values
    102             // and read them into the event's maps
     62
     63    // helper function for save()
     64    // parses the params from the edit view
     65    // and saves paramters as new entries in the events value lists
     66    def parseParamsForParameterValues( params, event ) {
     67
    10368            params.each{ key,value ->
    10469                 def pattern =/(parameterValue\.)([\d]+)/
     
    12590                 }
    12691            }
    127 
    128             if (event.save(flush: true)) {
    129                 flash.message = "${message(code: 'default.created.message', args: [message(code: 'event.label', default: 'Event'), event.id])}"
    130             }
    131             else {
    132                     event.errors.each{ println it }
    133             }
    134 
    135 
    136 
    137 
    138 
    139             // parse the samples added by the user
    140             // and add them to the sample list
    141             if( description.isSamplingEvent ) {
    142                 def samples = []
    143                 params.each{ k,v ->
    144                      def pattern = /^(sampleName)([\d]+)/
    145                      def matcher =  k=~pattern
    146                      if(matcher) {
    147                          def id = k.replaceAll(pattern,'$2')
    148                          def sample = new Sample()
    149                          //sample.parentEvent = (SamplingEvent) event
    150                          sample.parentSubject = null
    151                          sample.name = v
    152                          sample.material= Term.getTerm( params['sampleMaterial'+id] )
    153                          samples.push(sample)
    154                      }
     92    }
     93
     94
     95
     96    // assuming that an event has a sample
     97    // return the first sample's subject
     98    def getSubjectForEvent( event ) {
     99        def samples =  Sample.getSamplesFor(event)
     100        return samples[0].parentSubject
     101    }
     102
     103
     104
     105
     106    // helper function for save()
     107    // parse params from the edit view
     108    // and save all samples returned as a list
     109    def parseParamsForNewSamples( params, event ) {
     110
     111        def subject = getSubjectForEvent( event )
     112
     113        def samples=[]
     114            params.each{ k,v ->
     115                 def pattern = /^(sampleName)([\d]+)/
     116                 def matcher =  k=~pattern
     117                 if(matcher) {
     118                     def id = k.replaceAll(pattern,'$2')
     119                     def sample = new Sample()
     120                     sample.parentEvent = event
     121                     sample.parentSubject = subject
     122                     sample.name = v
     123                     sample.material= Term.getTerm( params['sampleMaterial'+id] )
     124                     saveSample(sample)
     125                     samples.push(sample)
     126                 }
     127            }
     128        return samples
     129    }
     130
     131
     132
     133    // save a samle or handle errors
     134    def saveSample(sample) {
     135           if (sample.save(flush: true)) {
     136                       flash.message = "${message(code: 'default.created.message', args: [message(code: 'event.label', default: 'Sample'), sample.id])}"
     137            }
     138            else {
     139                    sample.errors.each{ println it }
     140            }
     141    }
     142
     143
     144    // helper function for save()
     145    // parse params from the edit view and save changes.
     146    // Return all updated samples as a list
     147    def parseParamsForOldSamples( params ) {
     148        def samples=[]
     149            params.each{ k,v ->
     150                 def pattern = /^(sampleName_existing_)([\d]+)/
     151                 def matcher =  k=~pattern
     152                 if(matcher) {
     153                     def id = k.replaceAll(pattern,'$2')
     154                     def sample = Sample.get(id)
     155                     sample.name = v
     156                     sample.material= Term.getTerm( params['sampleMaterial_existing_'+id] )
     157                     samples.push(sample)
     158                     saveSample(sample)
     159                 }
     160            }
     161        return samples
     162    }
     163
     164
     165    // helper function for save()
     166    // delete a sample removed by the user
     167    // Note: we completely delete this sample! It is also removed from the Study and the Assay!
     168    def deleteSampelsRemovedByUser( originalSamples, remainingSamples) {
     169
     170        def toDelete = []
     171        originalSamples.each { original ->
     172            if( !remainingSamples.contains(original) )
     173            {
     174                toDelete.push( original )
     175            }
     176        }
     177
     178        toDelete.each {
     179            Assay.list().each{ assay ->
     180                if( assay.samples.contains((Sample)it) )
     181                {
     182                    assay.removeFromSamples(it)
    155183                }
    156 
    157                 if (event.save(flush: true)) {
    158                     flash.message = "${message(code: 'default.created.message', args: [message(code: 'event.label', default: 'Event'), event.id])}"
    159                 }
    160                 else {
    161                     event.errors.each{ println it }
     184            }
     185            Study.list().each{ study ->
     186                if( study.samples.contains((Sample)it) )
     187                {
     188                    study.removeFromSamples(it)
    162189                }
    163190            }
    164 
    165 
     191            ((Sample)it).delete()
    166192        }
    167 
    168 
    169 
    170         else {                                                                          // we only modify an element
    171 
    172             def event = Event.get(params['id'])
    173 
    174             // save basic changes in event and event description
    175 
    176             def description = event.eventDescription
    177 
    178             description.name = (params['name']==null || params['name'].replaceAll(/\w/,'').size()==0 ) ? '[no Name]' : params['name']
     193    }
     194
     195
     196
     197
     198
     199
     200    def save = {
     201
     202        // create a new event from scratch
     203
     204        if( !(params['id']=~/^[\d]+$/) ) {
     205
     206            def description = new EventDescription()
     207            description.name = (params['name']==null || params['name'].replaceAll(/\S/,'').size()==0 ) ? '[no Name]' : params['name']
    179208            description.description = (params['description']==null || params['description'].replaceAll(/\w/,'').size()==0 ) ? '[no description]' : params['description']
    180209            description.protocol = Protocol.get( params['protocol'] )
    181210            description.isSamplingEvent = params['isSamplingEvent']=='on' ? true : false
    182211
    183             event.startTime = new Date(params["startTime"])
    184             event.endTime = new Date(params["endTime"])
     212            if (description.save(flush: true)) {
     213                flash.message = "${message(code: 'default.created.message', args: [message(code: 'description.label', default: 'Event'), description.id])}"
     214            }
     215            else {
     216                description.errors.each{ println it }
     217            }
     218
     219            def event = description.isSamplingEvent ? new SamplingEvent() : new Event();
     220
     221            event.startTime = new Date(params["startTime"])                   // parse the date strings
     222            event.endTime = new Date(params["endTime"])                       // parse the date strings
    185223            event.parameterStringValues = new HashMap()
    186224            event.parameterFloatValues = new HashMap()
    187225            event.parameterIntegerValues = new HashMap()
    188226            event.parameterStringListValues = new HashMap()
     227            event.eventDescription = description
     228
     229
     230            if (event.save(flush:true, validate:false)) {
     231                flash.message = "${message(code: 'default.created.message', args: [message(code: 'event.label', default: 'Event'), event.id])}"
     232            }
     233            else {
     234                    event.errors.each{ println it }
     235            }
     236
     237            // read params and add parameter values to event.
     238            // (such as ParameterStringListValues, etc.
     239            parseParamsForParameterValues( params, event )
     240
     241
     242            if (event.save(flush: true)) {
     243                flash.message = "${message(code: 'default.created.message', args: [message(code: 'event.label', default: 'Event'), event.id])}"
     244            }
     245            else {
     246                    event.errors.each{ println it }
     247            }
     248
     249
     250        }
     251
     252
     253        // modify an existing event
     254
     255        else { 
     256
     257            def event = Event.get(params['id'])
     258
     259            // save basic changes in event and event description
     260
     261            def description = event.eventDescription
     262            def oldProtocol = description.protocol
     263
     264            def name = params['name']
     265            description.name = ( name==null || name.replaceAll(/\S/,'').size()==0 ) ? '[no Name]' : name
     266            description.description = (params['description']==null || params['description'].replaceAll(/\w/,'').size()==0 ) ? '[no description]' : params['description']
     267            description.isSamplingEvent = params['isSamplingEvent']=='on' ? true : false
     268            event.startTime = new Date(params["startTime"])
     269            event.endTime   = new Date(params["endTime"])
     270
     271
     272            // save changed parameters
     273            description.protocol = Protocol.get( params['protocol'] )
     274
     275            // get the protocol
     276            if(description.protocol!=oldProtocol)  {          // protocol changed
     277
     278                // remove all old parameter values
     279
     280                def removeAll = { values, memberName ->
     281                    def list = values.getProperty(memberName)
     282                }
     283
     284                removeAll(event, 'parameterStringValues' )
     285                removeAll(event, 'parameterIntegerValues' )
     286                removeAll(event, 'parameterFloatValues' )
     287                removeAll(event, 'parameterStringListValues')
     288
     289
     290                // add all new parameter values
     291                parseParamsForParameterValues( params, event )
     292            }
     293
     294
     295            // update samples
     296
     297            if( event.isSamplingEvent() ) {
     298
     299                // remove deleted samples
     300                // update existing samples
     301
     302                // add new samples
     303
     304                def originalSamples = Sample.getSamplesFor(event)               // samples that have been in this form before the edit
     305
     306                def newSamples = parseParamsForNewSamples( params, event )       //  get list of new samples as persistent sample objects
     307                                                                                 //  also add all the samples to this event already
     308                                                                                 //  by assigning event as parentEvent
     309
     310                def remainingSamples = parseParamsForOldSamples( params )        // samples, that have been in the form, and not deleted by the user
     311                                                                                 // remainigSamples is subset of originalSamples
     312
     313                deleteSampelsRemovedByUser( originalSamples, remainingSamples)   // delete sample and remove it from parentSubject and the
     314                                                                                 // associated study.
     315
     316            }
     317
     318            ((Event)event).eventDescription=description
     319
    189320
    190321            if (event.save(flush: true)) {
     
    195326            }
    196327
    197 
    198328        }
    199 
    200329
    201330
     
    249378
    250379    def edit = {
    251         println "sdfadfs" + params
    252 
    253         if( params["id"]==null)
     380
     381        // create entirely new Event
     382
     383        if( params["id"]==null || params['id']=='' )
    254384        {
     385            // New events cannot deal with Samples because there is not subject
     386            // to assign samples to. Therefore, samples cannot be added to the a new
     387            // Event, event if the user makes it a SamplingEvent by ticking a box.
     388            // Therefore, showSample is set to false.
     389
    255390            def eventInstance = new Event()
    256391            def sDate = new Date()
    257392            def eDate = new Date()
    258393            def description = new EventDescription()
    259             return [eventInstance:eventInstance, testo:params.clone(), sDate:sDate, eDate:eDate, description:description, showSample:true, samples:null, createNew:true ]
     394            return [eventInstance:eventInstance, testo:params.clone(), sDate:sDate, eDate:eDate, description:description, showSample:false, samples:null, createNew:true ]
    260395        }
     396
     397
     398        // edit an existing Event
     399
    261400        else
    262401        {
     
    267406            }
    268407            def samples = []
    269             def showSample = eventInstance.isSamplingEvent()
    270             if(showSample) { samples = ((SamplingEvent) eventInstance).getSamples() }
    271             /*
    272             println "\n--------"
    273             samples.each{ println it.class }
    274             samples.each{ println it.name}
    275             println samples.class
    276             println "-------\n"
    277             */
     408            def showSample = false
     409            if(eventInstance.isSamplingEvent() ) {
     410                samples = ((SamplingEvent) eventInstance).getSamples()
     411                if( samples.size() > 0 ) { showSample = true }
     412                // later, also check of eventInstance's study contains any subjects, if so, show them as list to chose from
     413            }
    278414
    279415            return [eventInstance:eventInstance, testo:params.clone(), sDate:eventInstance.startTime, eDate:eventInstance.endTime, description:eventInstance.eventDescription, showSample:showSample, samples:samples, createNew:false ]
     
    284420
    285421    def create = {
    286         println("jhalkds;lasjf;ldjasdklfja;slkdfja;sdjfklasdj;flkasdf")
    287         redirect(action:"edit")
     422        redirect(action:"edit", id:'')
    288423    }
    289424
     
    399534        event.samples.each{
    400535            event.removeFromSamples(it)
    401             it.delete()
     536            it.delete(flush:true)
    402537        }
    403538
    404539        redirect( action:showSample, id:params['id'] )
    405540   }
     541
    406542
    407543
  • trunk/grails-app/controllers/dbnp/studycapturing/EventDescriptionController.groovy

    r248 r288  
    1818        def eventDescriptionInstance = new EventDescription()
    1919            render(view:'edit', model:[eventDescriptionInstance: eventDescriptionInstance] )
    20 
    21                 /*
    22         if (!eventDescriptionInstance) {
    23             flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'eventDescription.label', default: 'EventDescription'), params.id])}"
    24             redirect(action: "list")
    25         }
    26         else {
    27             render(view:'edit', model:[eventDescriptionInstance: eventDescriptionInstance] )
    28         }
    29         */
    3020    }
    3121
     
    169159
    170160
     161println "here 4"
    171162
    172163           // STEP 4  - add new parameters
    173164
    174 println "here 4"
    175165            // find new parameters, added by user
    176166            // remove all other parameters from paramters list
     
    198188                         item.name=name
    199189                         parameter.addToListEntries(item)
     190
     191                         if (item.save(flush: true)) {
     192                             flash.message = "${message(code: 'default.created.message', args: [message(code: 'item.label', default: 'EventDescription'), item.id])}"
     193                             redirect(action: "show", id: item.id)
     194                         }
     195                         else {
     196                             render(view: "create", model: [item:item])
     197                         }
     198
     199
     200
    200201                     }
    201202                }
     
    226227        }
    227228
    228 
    229229        render( action: 'list' )
    230230    }
     231
    231232
    232233
     
    261262        println "in showMyProtocol"
    262263        println params
     264
    263265        if( EventDescription.get(params.id)==null || EventDescription.get(params.id).protocol==null ) {
    264             println "in 1a"
    265266            def protocol = Protocol.find("from Protocol p where id>=0")
    266267            println "protocol: ${protocol}"
     
    274275            render( view: "showMyProtocolFilled", model:[protocol:description.protocol,description:description] )
    275276        }
     277    }
     278
     279
     280
     281    def showPartial = {
     282        def description = EventDescription.get(params['protocolid'])
     283        def event       = Event.get(params['id'])
     284        render( view: "showPartial", model:[description:description,event:event] )    // error handling missing
    276285    }
    277286
  • trunk/grails-app/domain/dbnp/studycapturing/Event.groovy

    r247 r288  
    2020        Map parameterIntegerValues
    2121        Map parameterFloatValues
     22        Map parameterStringListValues
    2223
    2324        static hasMany = [
    24                 parameterStringValues: String, // stores both STRING and STRINGLIST items (latter should be checked against the list)
     25                parameterStringValues: String, // stores STRINGLIST items
     26                parameterStringListValues: ParameterStringListItem,
    2527                parameterIntegerValues: int,
    2628                parameterFloatValues: float,
     
    100102                return eventDescription ? eventDescription.name : ""
    101103        }
     104
    102105}
  • trunk/grails-app/domain/dbnp/studycapturing/Sample.groovy

    r247 r288  
    1717
    1818        static constraints = {
     19                parentSubject(nullable:true)
     20        }
     21
     22        static getSamplesFor( event ) {
     23            return  Sample.findAll( 'from Sample s where s.parentEvent =:event', [event:event] )
    1924        }
    2025
  • trunk/grails-app/views/event/edit.gsp

    r264 r288  
    178178
    179179
    180                             <!-- this part changes dynamically on select -->
     180
     181
    181182
    182183                            <tbody id="preview">
     184
     185                                <!-- this part changes dynamically on select -->
    183186                                <g:include action='showPartial' controller='eventDescription' params="[protocolid:protocols[0].id]" id="${eventInstance.id}" />
     187
     188
     189                                <!-- show checkbox when creating events -->
     190                                <g:if test="${createNew}">
     191                                <tr class="prop">
     192                                <td valign="top" class="name">
     193                                    <label for="endTime"><g:message code="event.endTime.label" default="This is a Sampling event" /></label>
     194                                </td>
     195                                <td valign="top" class="name">
     196                                    <g:checkBox name="isSamplingEvent" value="${false}" />
     197                                </td>
     198                                </tr>
     199                                </g:if>
     200
    184201                            </tbody>
     202
     203
     204
    185205
    186206
  • trunk/grails-app/views/eventDescription/showPartial.gsp

    r272 r288  
    4545
    4646
    47                             <% showSample=true %>
    48                             <tr class="prop">
    49                                 <td valign="top" class="name">
    50                                     <label for="endTime"><g:message code="event.endTime.label" default="This is a sampling event" /></label>
    51                                 </td>
    52                                 <td valign="top" class="name">
    53                                      <g:checkBox name="isSamplingEvent" value="${false}" />
    54                                 </td>
    55                             </tr>
    5647
    57 
    58 
    59 
    60 
    61 
    62 
    63 
Note: See TracChangeset for help on using the changeset viewer.