Changeset 248
- Timestamp:
- Mar 8, 2010, 11:58:23 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/dbnp/studycapturing/EventDescriptionController.groovy
r247 r248 17 17 def create = { 18 18 def eventDescriptionInstance = new EventDescription() 19 eventDescriptionInstance.properties = params 20 return [eventDescriptionInstance: eventDescriptionInstance] 21 } 19 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 */ 30 } 31 22 32 23 33 def save = { 24 def eventDescriptionInstance = new EventDescription(params) 25 if (eventDescriptionInstance.save(flush: true)) { 26 flash.message = "${message(code: 'default.created.message', args: [message(code: 'eventDescription.label', default: 'EventDescription'), eventDescriptionInstance.id])}" 27 redirect(action: "show", id: eventDescriptionInstance.id) 28 } 29 else { 30 render(view: "create", model: [eventDescriptionInstance: eventDescriptionInstance]) 31 } 32 } 34 println "save" 35 println params 36 params.each { println it } 37 38 39 40 def description = null // the variable to be updated 41 42 43 /* update an existing description */ 44 45 if(params['id']!='') { 46 47 48 // STEP 0 - set variables 49 50 description = new EventDescription() 51 description.name=params['name'] 52 // description.description=params['description'] // has to be a Term 53 // description.classification=params['classification'] // has to be a Term 54 description.isSamplingEvent= params['isSample']=='true'?true:false 55 56 def protocol = Protocol.get(params['protocol']) // the protocol 57 58 def parameters = [:] // parameters given by the user (in the view) 59 // key: id (as string), value: non-persistant ProtocolParameter object 60 61 def options = [:] // store options, i.e. ParameterStringListItems given by the user 62 // use ids of parameter as key, store hash of id and name 63 64 65 66 67 // STEP 1 parse params and fill hashes 68 69 70 // collect parameters from form 71 params.each { key, value -> 72 if(key=~/row_(.*)__(.*)/) { 73 def matcher = (key=~/row_(.*?)__(.*?)$/) 74 println matcher[0][1]+' '+matcher[0][2] 75 def id = matcher[0][1] 76 def member = matcher[0][2] 77 78 println 'member: '+ member 79 if(member=~/parameterStringValue__(.*?)$/) { 80 matcher = member=~/parameterStringValue__(.*?)$/ 81 def psv = matcher[0][1] 82 println "${id}\t${psv}:\tvalue:${value}" 83 if(options[id]==null) { options[id]=[:] } // store paramter string value's id and value 84 (options[id])[psv]=value 85 } 86 else if(member!='reference') { 87 if(parameters[id]==null) { parameters[id]=new ProtocolParameter() } 88 89 if(member=~/^type.*/) { 90 value= ProtocolParameterType.valueOf(value) 91 member='type' 92 } 93 parameters[id].setProperty(member,value) 94 } 95 } 96 } 97 98 println "here 1" 99 100 // collect options (i.e., ParameterStringListItem from parameters) 101 parameters.each{ key, value -> 102 if(value.type==ProtocolParameterType.STRINGLIST) { 103 def parameter = parameters[key] 104 options[key].each{k,v-> println "k: ${k}\t v:${v}" } // debug 105 } 106 } 107 108 println "here 2" 109 110 111 112 113 // STEP 2 - remove deleted parameters (from persistent protocol description) 114 115 protocol.parameters.each{ 116 if( parameters[it.id.toString()]==null ) 117 { protocol.removeFromParameters(it.id) } 118 } 119 120 println "here 3" 121 122 123 // STEP 3 - update altered parameters 124 125 protocol.parameters.each{ inDb -> 126 127 def found = parameters[inDb.id.toString()] 128 //['name','type','description','reference','unit'].each { 129 // debugging: ignoring the reference !! 130 ['name','type','description','unit'].each { 131 inDb.setProperty(it,found.getProperty(it)) 132 } 133 134 135 // update options (i.e. ParameterStringListItem objects) of parameter inDb 136 if(inDb.type==ProtocolParameterType.STRINGLIST ) { 137 138 if(found.type==ProtocolParameterType.STRINGLIST ) { 139 140 // add or modifiy options for existing parameters 141 options[inDb.id.toString()].each{ id, name -> 142 def item = inDb.listEntries.find{ it.id.toString()==id } 143 if(!item) { // add as new option to persistant parameter 144 item = new ParameterStringListItem() 145 item.name = name 146 inDb.addToListEntries(item) 147 } 148 else { // update persistant paramter 149 item.name = name 150 } 151 } 152 153 // remove options that have been deleted by the user 154 def itemsToBeRemoved = [] 155 inDb.listEntries.each { item -> 156 if( ! ((options[inDb.id.toString()])[item.id.toString()]) ) 157 { itemsToBeRemoved.push item } 158 } 159 itemsToBeRemoved.each { inDb.removeFromListEntries(it) } 160 } 161 162 } 163 else { 164 inDb.listEntries.collect{it}.each{ inDb.removeFromListEntries(it) } 165 } 166 167 } 168 169 170 171 172 // STEP 4 - add new parameters 173 174 println "here 4" 175 // find new parameters, added by user 176 // remove all other parameters from paramters list 177 def newParameters = [:] 178 parameters.each{ key, value -> 179 if( ! protocol.parameters.find {it.id.toString()==key} ) { 180 newParameters[key]=value 181 } 182 } 183 parameters=newParameters 184 185 186 187 println "here 5" 188 // add new parameters (to persistent protocolDescription) 189 parameters.each { id, parameter-> 190 def newParameter = new ProtocolParameter() // update properties 191 ['name','type','description','unit'].each { 192 newParameter.setProperty( it, parameter.getProperty(it) ) 193 } 194 195 if(parameter.type==ProtocolParameterType.STRINGLIST) { // update options 196 options[id].each { someKey, name -> 197 if(item==null) item = new ParameterStringListItem() 198 item.name=name 199 parameter.addToListEntries(item) 200 } 201 } 202 description.addToListEntries(parameter) 203 } 204 205 206 207 println "here 6" 208 209 // STEP 5 - make changes persitant 210 211 212 // compare paramters for protocol 213 def parametersFromUser = [] 214 215 // check whether all parameters are still part of the protocol 216 protocol.parameters 217 218 } 219 220 if (description.save(flush: true)) { 221 flash.message = "${message(code: 'default.created.message', args: [message(code: 'description.label', default: 'EventDescription'), description.id])}" 222 redirect(action: "show", id: description.id) 223 } 224 else { 225 render(view: "create", model: [description: description]) 226 } 227 228 229 render( action: 'list' ) 230 } 231 33 232 34 233 def show = { … … 46 245 47 246 247 def edit = { 248 def eventDescriptionInstance = EventDescription.get(params.id) 249 if (!eventDescriptionInstance) { 250 flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'eventDescription.label', default: 'EventDescription'), params.id])}" 251 redirect(action: "list") 252 } 253 else { 254 return [eventDescriptionInstance: eventDescriptionInstance] 255 } 256 } 257 258 259 48 260 def showMyProtocol = { 49 def description = EventDescription.get(params.id) 50 if( description.protocol==null ) { 51 protocol = new Protocol() 52 render( view:"showMyProtocolEmpty", model:[protocol:protocol,description:description] ) 53 } 54 else { 261 println "in showMyProtocol" 262 println params 263 if( EventDescription.get(params.id)==null || EventDescription.get(params.id).protocol==null ) { 264 println "in 1a" 265 def protocol = Protocol.find("from Protocol p where id>=0") 266 println "protocol: ${protocol}" 267 //def description = EventDescription.find("from EventDescription e where id>=0") 268 //println "description: ${description}" 269 def description=new EventDescription(); 270 render( view:"showMyProtocolFilled", model:[protocol:protocol,description:description] ) 271 } 272 else { 273 def description = EventDescription.get(params.id) 55 274 render( view: "showMyProtocolFilled", model:[protocol:description.protocol,description:description] ) 56 275 } … … 71 290 72 291 73 def edit = {74 def eventDescriptionInstance = EventDescription.get(params.id)75 if (!eventDescriptionInstance) {76 flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'eventDescription.label', default: 'EventDescription'), params.id])}"77 redirect(action: "list")78 }79 else {80 return [eventDescriptionInstance: eventDescriptionInstance]81 }82 }83 292 84 293 def update = { 294 println "update" 295 print params 296 85 297 def eventDescriptionInstance = EventDescription.get(params.id) 86 298 if (eventDescriptionInstance) { … … 110 322 111 323 def delete = { 324 flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'eventDescription.label', default: 'EventDescription'), params.id])}" 325 redirect(action: "list") 326 327 /* 328 // old shit 112 329 def eventDescriptionInstance = EventDescription.get(params.id) 113 330 if (eventDescriptionInstance) { … … 126 343 redirect(action: "list") 127 344 } 345 */ 128 346 } 129 347 … … 149 367 150 368 def showProtocolParameters = { 151 println params152 153 369 def description = EventDescription.get(params.id) 154 def protocol = description.protocol==null ? new Protocol() : description.protocol 155 156 if( params['addOneParameter']=='true') { // add a new parameter 157 println "adding" 158 def parameter = new ProtocolParameter() 159 protocol.addToParameters(parameter) 160 } 161 370 def protocol = [] 162 371 def list = [] 372 373 if(description!=null) { // editing an existing EventDescription 374 protocol = description.protocol==null ? new Protocol() : description.protocol 375 } 376 else { // creating a new EventDescription 377 protocol=Protocol.find("from Protocol p where id>=0") 378 } 379 163 380 protocol.parameters.each { 164 381 list.add(it)
Note: See TracChangeset
for help on using the changeset viewer.