Changeset 289


Ignore:
Timestamp:
Mar 22, 2010, 2:56:15 AM (14 years ago)
Author:
jahn
Message:

Views for EventDescription? (edit and create) have been cleaned, tested, debugged, and improved.

Location:
trunk/grails-app
Files:
2 edited

Legend:

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

    r288 r289  
    2121
    2222
    23     def save = {
    24         println "save"
    25         println params
    26         params.each { println it }
    27 
    28 
    29 
    30         def description = null      // the variable to be updated
    31 
    32 
    33         /*  update an existing description */
    34 
    35         if(params['id']!='')  {             
    36 
    37 
    38             // STEP 0  - set variables
    39 
    40             description = new EventDescription()
    41             description.name=params['name']
    42             // description.description=params['description']   // has to be a Term
    43             // description.classification=params['classification']  // has to be a Term
    44             description.isSamplingEvent= params['isSample']=='true'?true:false
    45 
    46             def protocol = Protocol.get(params['protocol'])   // the protocol
    47 
    48             def parameters = [:]                   // parameters given by the user (in the view)
    49                                                    // key: id (as string), value: non-persistant ProtocolParameter object
    50 
    51             def options = [:]                      // store options, i.e. ParameterStringListItems given by the user
    52                                                    // use ids of parameter as key, store hash of id and name
    53 
    54 
    55 
    56 
    57             // STEP 1 parse params and fill hashes
    58 
    59 
    60             // collect parameters from form
     23
     24
     25
     26    def collectParametersFromForm( params ) {
     27
     28            def parameters = [:]
     29            def options    = [:]
     30
    6131            params.each { key, value ->
    6232                 if(key=~/row_(.*)__(.*)/) {
    6333                      def matcher = (key=~/row_(.*?)__(.*?)$/)
    64                       println matcher[0][1]+'  '+matcher[0][2]
    6534                      def id = matcher[0][1]
    6635                      def member = matcher[0][2]
    6736
    68                       println 'member: '+ member
    6937                      if(member=~/parameterStringValue__(.*?)$/) {
    7038                          matcher = member=~/parameterStringValue__(.*?)$/
    7139                          def psv = matcher[0][1]
    72                           println "${id}\t${psv}:\tvalue:${value}"
    7340                          if(options[id]==null) { options[id]=[:] }  // store paramter string value's id and value
    7441                          (options[id])[psv]=value
    7542                      }
    76                       else if(member!='reference')  { 
     43                      else if(member!='reference')  {
    7744                          if(parameters[id]==null) { parameters[id]=new ProtocolParameter() }
    7845
     
    8451                      }
    8552                 }
    86             }
    87 
    88 println "here 1"
     53           }
     54
    8955
    9056            // collect options (i.e., ParameterStringListItem from parameters)
    91             parameters.each{ key, value ->
    92                 if(value.type==ProtocolParameterType.STRINGLIST) {
     57
     58           parameters.each{ key, value ->
     59               if(value.type==ProtocolParameterType.STRINGLIST) {
    9360                    def parameter = parameters[key]
    94                     options[key].each{k,v-> println "k: ${k}\t v:${v}" } // debug
    95                 }
    96             }
    97 
    98 println "here 2"
     61               }
     62           }
     63
     64
     65           return [parameters,options]
     66    }
     67
     68
     69
     70    // convenience method for save()
     71    def saveParameterStringListItem(item) {
     72
     73        if (item.save(flush: true)) {
     74            flash.message = "${message(code: 'default.created.message', args: [message(code: 'description.label', default: 'Item'), item.id])}"
     75        }
     76        else {
     77            item.errors.each { println it }
     78        }
     79
     80    }
     81
     82
     83    // convenience method for save()
     84    def saveParameter(item) {
     85
     86        if (item.save(flush: true)) {
     87            flash.message = "${message(code: 'default.created.message', args: [message(code: 'description.label', default: 'Parameter'), item.id])}"
     88        }
     89        else {
     90            item.errors.each { println it }
     91        }
     92
     93    }
     94
     95
     96
     97
     98
     99
     100    def save = {
     101       
     102
     103        def description = null      // the variable to be updated
     104
     105
     106
     107        // create a new event from scratch
     108
     109        if( !(params['id']=~/^[\d]+$/) ) {
     110            description = new EventDescription()
     111        }
     112        else {
     113            description = EventDescription.get(params['id'])
     114        }
     115
     116            description.name=params['name']
     117
     118            // description.description=params['description']   // has to be a Term
     119            // description.classification=params['classification']  // has to be a Term
     120            description.isSamplingEvent= params['isSample']=='true'?true:false
     121
     122            def protocol = Protocol.get(params['protocol'])   // the protocol
     123
     124
     125
     126
     127            // STEP 1 parse params and fill hashes
     128            def para_opt = collectParametersFromForm(params)
     129
     130            def parameters = para_opt[0]      // parameters given by the user (in the view)
     131                                              // key: id (as string), value: non-persistant ProtocolParameter object
     132
     133            def options = para_opt[1]         // store options, i.e. ParameterStringListItems given by the user.
     134                                              // use ids of parameter as key, store hash of id and name.
     135
     136            def originalParameters
    99137
    100138
     
    102140
    103141           // STEP 2  -  remove deleted parameters (from persistent protocol description)
    104 
     142           protocol.parameters.each{
     143               def toRemove = []
     144               if( parameters[it.id.toString()]==null ) { toRemove.push(it) }
     145               toRemove.each{ protocol.removeFromParameters(it) }
     146           }
     147
     148
     149
     150           // STEP 3  -  update altered parameters
    105151           protocol.parameters.each{
    106                if( parameters[it.id.toString()]==null  )
    107                    { protocol.removeFromParameters(it.id) }
    108            }
    109 
    110 println "here 3"
    111 
    112 
    113            // STEP 3  -  update altered parameters
    114 
    115            protocol.parameters.each{ inDb ->
    116 
    117                def found = parameters[inDb.id.toString()]
    118                //['name','type','description','reference','unit'].each {
    119                // debugging: ignoring the reference !!
    120                ['name','type','description','unit'].each {
    121                    inDb.setProperty(it,found.getProperty(it))
     152
     153               def inDb = ProtocolParameter.get(it.id)
     154               def originalListEntries = inDb.listEntries.collect{it}
     155               def itemsToBeRemoved = []
     156
     157               if(  parameters[inDb.id.toString()] != null )
     158               {
     159                   def found = parameters[inDb.id.toString()]
     160
     161                   // update options (i.e. ParameterStringListItem objects) of parameter inDb
     162                   if(inDb.type==ProtocolParameterType.STRINGLIST ) {
     163
     164                           if(found.type==ProtocolParameterType.STRINGLIST ) {
     165
     166                                // add or modifiy options for existing parameters
     167                                options[inDb.id.toString()].each{ id, name ->
     168
     169                                    def item = inDb.listEntries.find{ it.id.toString()==id }
     170                                    if(item==null) {  // add as new option to persistant parameter
     171                                        item = new ParameterStringListItem()
     172                                        item.name = name
     173                                        saveParameterStringListItem(item)
     174                                        inDb.addToListEntries(item)
     175                                        inDb.save(flush: true)     // needed??
     176                                    }
     177                                    else {       // update persistant paramter
     178                                        item.name = name
     179                                    }
     180                                }
     181
     182                           }
     183
     184                           else {                                                       // remove all options because the parameter type has changed
     185                               inDb.listEntries.each{ itemsToBeRemoved.push( it ) }
     186                           }
     187                   }
     188
     189
     190                   ['name','type','description','unit' ].each {           // references are missing
     191                       inDb.setProperty(it,found.getProperty(it))         // type has to be set after checking for
     192                   }                                                      // STRINGLIST above.
     193               }
     194
     195               // delete all options removed by the user
     196               originalListEntries.each { original ->
     197                       // if the original is not found in the user's modifications
     198                       def allOptionsForParameters = options[inDb.id.toString()]
     199                       if( allOptionsForParameters==null || allOptionsForParameters[original.id.toString()]==null )
     200                           { itemsToBeRemoved.push( original ) }
    122201               }
    123 
    124 
    125                // update options (i.e. ParameterStringListItem objects) of parameter inDb
    126                if(inDb.type==ProtocolParameterType.STRINGLIST ) {
    127 
    128                        if(found.type==ProtocolParameterType.STRINGLIST ) {
    129 
    130                             // add or modifiy options for existing parameters
    131                             options[inDb.id.toString()].each{ id, name ->
    132                                 def item = inDb.listEntries.find{ it.id.toString()==id }
    133                                 if(!item) {  // add as new option to persistant parameter
    134                                     item = new ParameterStringListItem()
    135                                     item.name = name
    136                                     inDb.addToListEntries(item)
    137                                 }
    138                                 else {       // update persistant paramter
    139                                     item.name = name
    140                                 }
    141                             }
    142 
    143                             // remove options that have been deleted by the user
    144                             def itemsToBeRemoved = []
    145                             inDb.listEntries.each { item ->
    146                                  if( ! ((options[inDb.id.toString()])[item.id.toString()]) )
    147                                      { itemsToBeRemoved.push item }
    148                             }
    149                             itemsToBeRemoved.each { inDb.removeFromListEntries(it) }
    150                        }
    151 
    152                }
    153                else {
    154                         inDb.listEntries.collect{it}.each{ inDb.removeFromListEntries(it) }
    155                }
    156 
    157           }
    158 
    159 
    160 
    161 println "here 4"
     202               itemsToBeRemoved.each {
     203                   inDb.removeFromListEntries(it)
     204               }
     205          }
     206
    162207
    163208           // STEP 4  - add new parameters
     
    175220
    176221
    177 println "here 5"
     222
    178223            //  add new parameters (to persistent protocolDescription)
    179224            parameters.each { id, parameter->
    180                 def newParameter = new ProtocolParameter()                           // update properties
    181                 ['name','type','description','unit'].each {
    182                     newParameter.setProperty( it, parameter.getProperty(it) )
    183                 }
    184 
    185                 if(parameter.type==ProtocolParameterType.STRINGLIST) {               // update options
    186                      options[id].each { someKey, name ->
    187                          if(item==null) item = new ParameterStringListItem()
    188                          item.name=name
    189                          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 
    201                      }
    202                 }
    203                 description.addToListEntries(parameter)
     225                if( id=~/new/ )
     226                {
     227                    def newParameter = new ProtocolParameter()                           // update properties
     228                    ['name','type','description','unit'].each {
     229                        newParameter.setProperty( it, parameter.getProperty(it) )
     230                    }
     231
     232                    if(parameter.type==ProtocolParameterType.STRINGLIST) {               // update options
     233                         options[id].each { someKey, name ->
     234                             def item = new ParameterStringListItem()
     235                             item.name=name
     236                             parameter.addToListEntries(item)
     237                             saveParameterStringListItem(item)
     238                         }
     239                    }
     240                    saveParameter(newParameter)
     241                    protocol.addToParameters(parameter)
     242                }
    204243            }
    205244
    206245
    207 
    208 println "here 6"
    209 
    210            // STEP 5  - make changes persitant
    211 
    212 
    213             // compare paramters for protocol
    214             def parametersFromUser = []
    215 
    216             // check whether all parameters are still part of the protocol
    217             protocol.parameters
    218 
    219         }
     246        // make changes persitant
    220247
    221248        if (description.save(flush: true)) {
     
    260287
    261288    def showMyProtocol = {
    262         println "in showMyProtocol"
    263         println params
    264289
    265290        if( EventDescription.get(params.id)==null || EventDescription.get(params.id).protocol==null ) {
    266             def protocol = Protocol.find("from Protocol p where id>=0")
    267             println "protocol: ${protocol}"
    268             //def description = EventDescription.find("from EventDescription e where id>=0")
    269             //println "description: ${description}"
    270291            def description=new EventDescription();
    271             render( view:"showMyProtocolFilled", model:[protocol:protocol,description:description] )
     292            render( view:"showMyProtocolFilled", model:[protocol:null,description:description] )
    272293        }
    273294        else {
     
    286307
    287308
     309
     310
    288311    def showMyProtocolEmpty = {
    289        println "in showMyProtocolEmpty"
    290312    }
    291313
    292314
    293315    def showMyProtocolFilled = {
    294        println "in showMyProtocolFilled"
    295316    }
    296317
     
    301322
    302323    def update = {
    303         println "update"
    304         print params
    305 
    306         def eventDescriptionInstance = EventDescription.get(params.id)
    307         if (eventDescriptionInstance) {
    308             if (params.version) {
    309                 def version = params.version.toLong()
    310                 if (eventDescriptionInstance.version > version) {
    311                    
    312                     eventDescriptionInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'eventDescription.label', default: 'EventDescription')] as Object[], "Another user has updated this EventDescription while you were editing")
    313                     render(view: "edit", model: [eventDescriptionInstance: eventDescriptionInstance])
    314                     return
    315                 }
    316             }
    317             eventDescriptionInstance.properties = params
    318             if (!eventDescriptionInstance.hasErrors() && eventDescriptionInstance.save(flush: true)) {
    319                 flash.message = "${message(code: 'default.updated.message', args: [message(code: 'eventDescription.label', default: 'EventDescription'), eventDescriptionInstance.id])}"
    320                 redirect(action: "show", id: eventDescriptionInstance.id)
    321             }
    322             else {
    323                 render(view: "edit", model: [eventDescriptionInstance: eventDescriptionInstance])
    324             }
    325         }
    326         else {
    327             flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'eventDescription.label', default: 'EventDescription'), params.id])}"
    328             redirect(action: "list")
    329         }
    330     }
    331 
    332     def delete = {
    333             flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'eventDescription.label', default: 'EventDescription'), params.id])}"
    334             redirect(action: "list")
    335 
    336 /*
    337         // old shit
    338324        def eventDescriptionInstance = EventDescription.get(params.id)
    339325        if (eventDescriptionInstance) {
     
    352338            redirect(action: "list")
    353339        }
    354         */
    355     }
    356 
    357 
    358     def test = { render(params) }
    359 
    360     def test2 = {
    361         def eventDescriptionInstance = EventDescription.get(params.id)
    362         if (!eventDescriptionInstance) {
    363             flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'eventDescription.label', default: 'EventDescription'), params.id])}"
    364             redirect(action: "list")
    365         }
    366         else {
    367             return [eventDescriptionInstance: eventDescriptionInstance]
    368         }
    369     }
     340    }
     341
    370342
    371343
     
    377349    def showProtocolParameters = {
    378350        def description = EventDescription.get(params.id)
    379         def protocol = []
     351        def protocol = null
    380352        def list = []
    381353
    382354        if(description!=null) {                           // editing an existing EventDescription
    383355            protocol = description.protocol==null ? new Protocol() : description.protocol
    384         }
    385         else {                                            // creating a new EventDescription
    386            protocol=Protocol.find("from Protocol p where id>=0")
    387         }
    388 
    389         protocol.parameters.each {
    390             list.add(it)
    391             list.sort{ a,b -> a.name <=> b.name }
    392         }
    393 
    394         render( view:"showProtocolParameters", model:[protocol:protocol,description:description,list:list] )
     356                protocol.parameters.each {
     357                    list.add(it)
     358                    list.sort{ a,b -> a.name <=> b.name }
     359                }
     360        }
     361
     362        render( view:"showProtocolParameters", model:[protocol:null,description:description,list:list] )
    395363    }
    396364
  • trunk/grails-app/views/eventDescription/showMyProtocolFilled.gsp

    r249 r289  
    251251
    252252
    253 
    254 
    255 
    256 
    257 
    258 
    259 
    260 
    261 
    262 
    263 
    264253<tr class="prop">
    265     <td id='test'>  Protocol </td>
    266     <td> <g:select name="protocol" from="${dbnp.studycapturing.Protocol.list()}" value="${protocol}" optionKey="id"   optionValue="name"
     254    <td id='test'> Protocol </td>
     255
     256    <% def protocols = dbnp.studycapturing.Protocol.list() %>
     257    <td>
     258           <g:if test="${protocol==null}">
     259               <g:select name="protocol" from="${protocols}" optionKey="id"   optionValue="name"  noSelection="['':'-- Select Protocol -- ']"
    267260                   onchange= "${remoteFunction( action:'showProtocolParameters', update:'showProtocolParameters', params:'\'id=\'+this.value' )}; deleteHiddenDialogs();" />
     261           </g:if>
     262
     263           <g:else>
     264               <g:select name="protocol" from="${protocols}" optionKey="id"   optionValue="name" value='${protocol.id}'
     265                   onchange= "${remoteFunction( action:'showProtocolParameters', update:'showProtocolParameters', params:'\'id=\'+this.value' )}; deleteHiddenDialogs();" />
     266           </g:else>
    268267    </td>
     268
     269
    269270</tr>
     271
    270272
    271273
Note: See TracChangeset for help on using the changeset viewer.