Changeset 270 for trunk/grails-app/controllers
- Timestamp:
- Mar 16, 2010, 9:20:40 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/dbnp/studycapturing/EventController.groovy
r247 r270 15 15 params.max = Math.min(params.max ? params.int('max') : 10, 100) 16 16 [eventInstanceList: Event.list(params), eventInstanceTotal: Event.count()] 17 }18 19 def create = {20 def eventInstance = new Event()21 eventInstance.properties = params22 //chain( view: "createForEventDescription", params:params )23 return [eventInstance:eventInstance]24 17 } 25 18 … … 69 62 def save = { 70 63 71 def event = Event.get(params["id"]) 72 73 if( event==null ) { // this is an entirely new event 74 render(action: "list", total:Event.count() ) 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 103 params.each{ key,value -> 104 def pattern =/(parameterValue\.)([\d]+)/ 105 def matcher = key=~pattern 106 if(matcher) { 107 def id = key.replaceAll(pattern,'$2') 108 def parameter = ProtocolParameter.get(id) 109 110 switch(parameter.type) 111 { 112 case dbnp.studycapturing.ProtocolParameterType.STRING: 113 event.parameterStringValues[parameter.name]=value 114 break 115 case dbnp.studycapturing.ProtocolParameterType.FLOAT: 116 event.parameterFloatValues[parameter.name]=value.toFloat() 117 break 118 case dbnp.studycapturing.ProtocolParameterType.INTEGER: 119 event.parameterFloatValues[parameter.name]=value.toInteger() 120 break 121 case dbnp.studycapturing.ProtocolParameterType.STRINGLIST: 122 def item = ParameterStringListItem.get(value) 123 event.parameterStringListValues[''+parameter.id]=item 124 } 125 } 126 } 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 } 155 } 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 } 162 } 163 } 164 165 75 166 } 76 167 77 params["startTime"] = parseDate(params["startTime"]) // parse the date strings 78 params["endTime"] = parseDate(params["endTime"]) 79 80 81 // the Subject is automatically parsed 82 // update Event Description 83 def oldProtocol=event.eventDescription.protocol.id.toString() 84 def newProtocol=params["protocol.id"] 85 def protocolParameters = params["protocolParameter"] 86 87 println "\n\nparams" 88 params.each{ println it } 89 90 if(oldProtocol<=>newProtocol) { // protocol id changed 91 event.eventDescription=EventDescription.get(newProtocol) 92 event.parameterStringValues.clear() // this does not propagate orphened parameters 93 def protocol=Protocol.get(newProtocol) 94 protocolParameters.each{ key, value -> 95 def parameter=ProtocolParameter.get(key).name 96 event.parameterStringValues[parameter] = value 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'] 179 description.description = (params['description']==null || params['description'].replaceAll(/\w/,'').size()==0 ) ? '[no description]' : params['description'] 180 description.protocol = Protocol.get( params['protocol'] ) 181 description.isSamplingEvent = params['isSamplingEvent']=='on' ? true : false 182 183 event.startTime = new Date(params["startTime"]) 184 event.endTime = new Date(params["endTime"]) 185 event.parameterStringValues = new HashMap() 186 event.parameterFloatValues = new HashMap() 187 event.parameterIntegerValues = new HashMap() 188 event.parameterStringListValues = new HashMap() 189 190 if (event.save(flush: true)) { 191 flash.message = "${message(code: 'default.created.message', args: [message(code: 'event.label', default: 'Event'), event.id])}" 192 } 193 else { 194 event.errors.each{ println it } 97 195 } 98 println event.parameterStringValues 99 event.eventDescription.protocol=protocol 196 197 100 198 } 101 else // protocol is the same, values changed 102 { 103 protocolParameters.each{ key, value -> 104 def parameter=ProtocolParameter.get(key) 105 event.parameterStringValues[parameter.name]=value // changed from key to id 106 } 107 108 } 109 110 111 if (event.save(flush: true)) { 112 flash.message = "${message(code: 'default.created.message', args: [message(code: 'event.label', default: 'Event'), event.id])}" 113 redirect(action: "show", id: event.id) 114 } 199 200 115 201 116 202 render(action: "list", total:Event.count() ) … … 163 249 164 250 def edit = { 251 println "sdfadfs" + params 165 252 166 253 if( params["id"]==null) 167 254 { 168 255 def eventInstance = new Event() 169 def sDate = new Date( params["startTime"])170 def eDate = new Date( params["endTime"])171 def description = EventDescription.findById((params["eventDescription"])["id"])172 return [eventInstance:eventInstance, testo:params.clone(), sDate:sDate, eDate:eDate, description:description ]256 def sDate = new Date() 257 def eDate = new Date() 258 def description = new EventDescription() 259 return [eventInstance:eventInstance, testo:params.clone(), sDate:sDate, eDate:eDate, description:description, showSample:true, samples:null, createNew:true ] 173 260 } 174 261 else … … 179 266 redirect(action: "list") 180 267 } 181 return [eventInstance:eventInstance, testo:params.clone(), sDate:eventInstance.startTime, eDate:eventInstance.endTime, description:eventInstance.eventDescription] 182 } 183 184 } 268 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 */ 278 279 return [eventInstance:eventInstance, testo:params.clone(), sDate:eventInstance.startTime, eDate:eventInstance.endTime, description:eventInstance.eventDescription, showSample:showSample, samples:samples, createNew:false ] 280 } 281 282 } 283 284 285 def create = { 286 println("jhalkds;lasjf;ldjasdklfja;slkdfja;sdjfklasdj;flkasdf") 287 redirect(action:"edit") 288 } 289 185 290 186 291 … … 234 339 235 340 341 236 342 def showSample = { 343 344 println params 345 println "\n\nin showSample" 346 params.each{ x -> println x} 237 347 def samples = null 238 348 def event = Event.get(params.id) 349 if(event!=null) 350 { 351 def wantSample = params['wantSample'] 239 352 // user wants this Event to be a SamplingEvent? 240 def wantSample = params.wantSample <=>'no'?true:false 241 print wantSample 353 if( wantSample==null && event.isSamplingEvent() ) 354 { 355 println "want sample is null" 356 wantSample = true 357 } 358 else { println "want sample is " + params['wantSample'] 359 wantSample = params.wantSample <=>'no'?true:false } 360 361 362 242 363 if( event.isSamplingEvent() ) { 243 samples=event.samples 364 samples = Sample.findAll("from Sample as s where s.parentEvent.id = ${event.id}" ) 365 samples.each{ println it.class } 366 samples.collect{ it.name } 244 367 println "yes ${event.id}" 245 368 } 246 369 else println "no ${event.id}" 247 370 248 render( view:"showSample", model:[samples:samples,wantSample:wantSample] ) 249 } 250 251 371 372 render( view:"showSample", model:[samples:samples,wantSample:wantSample,id:event.id] ) 373 } 374 } 375 376 377 def deleteSample = { 378 // saves the samples from the page, then repaint the samples 379 println "in deleteSample" 380 println params 381 382 def event = Event.get(params['id']) 383 384 redirect( action:showSample, samples:newSample, wantSample:true,id:params['id'] ) 385 } 386 387 388 def showEventDescription = { 389 def event = Event.get( params['id'] ) 390 def description = EventDescription.get( params['eventDescriptionId'] ) 391 render( view:"showEventDescription", model:[description:description] ) 392 } 393 394 395 def deleteAllSamples = { 396 println "in deleteSamples" 397 println params 398 def event = Event.get(params['id']) 399 event.samples.each{ 400 event.removeFromSamples(it) 401 it.delete() 402 } 403 404 redirect( action:showSample, id:params['id'] ) 405 } 406 407 408 def combobox = { 409 def event = Event.get(1) 410 def parameters = event.parameterStringValues 411 render( view:"combobox", model:[event:event,parameters:parameters] ) 412 } 252 413 }
Note: See TracChangeset
for help on using the changeset viewer.