Changeset 1426 for trunk/grails-app/controllers
- Timestamp:
- Jan 21, 2011, 7:37:02 PM (10 years ago)
- Location:
- trunk/grails-app/controllers
- Files:
-
- 4 deleted
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/HomeController.groovy
r1397 r1426 1 import dbnp.studycapturing.Study 2 import dbnp.studycapturing.Template 1 import dbnp.studycapturing.* 3 2 import org.codehaus.groovy.grails.commons.GrailsApplication 4 3 -
trunk/grails-app/controllers/dbnp/exporter/ExporterController.groovy
r1222 r1426 210 210 println "----- SUBJECT -----" 211 211 for (u in 0..sample.parentSubject.giveFields().unique().size()-1){ 212 TemplateField tf = sample.parentSubject.giveFields().getAt(u)212 nl.grails.plugins.gdt.TemplateField tf = sample.parentSubject.giveFields().getAt(u) 213 213 println tf.name 214 214 row.createCell((short)9+u).setCellValue(tf.name) … … 221 221 println "----- SAMPLING EVENT -----" 222 222 for (t in 0..sample.parentEvent.giveFields().unique().size()-1){ 223 TemplateField tf =sample.parentEvent.giveFields().getAt(t)223 nl.grails.plugins.gdt.TemplateField tf =sample.parentEvent.giveFields().getAt(t) 224 224 println tf.name 225 225 row.createCell((short)9+sample.parentSubject.giveFields().unique().size()+t).setCellValue("samplingEvent-"+tf.name) … … 237 237 println "----- SAMPLE -----" 238 238 for (v in 0..sample.giveFields().unique().size()-1){ 239 TemplateField tf =sample.giveFields().getAt(v)239 nl.grails.plugins.gdt.TemplateField tf =sample.giveFields().getAt(v) 240 240 println tf.name 241 241 row.createCell((short)9+sample.parentSubject.giveFields().unique().size()+v+sample.parentEvent.giveFields().unique().size()).setCellValue("sample-"+tf.name) -
trunk/grails-app/controllers/dbnp/importer/ImporterController.groovy
r1422 r1426 1 1 package dbnp.importer 2 2 3 import dbnp.studycapturing.Study 4 import dbnp.studycapturing.Subject 5 import dbnp.studycapturing.Sample 6 import dbnp.studycapturing.Event 7 import dbnp.studycapturing.Template 8 3 import dbnp.studycapturing.* 4 import nl.grails.plugins.gdt.* 9 5 import org.apache.poi.ss.usermodel.Workbook 10 11 6 import grails.converters.JSON 12 7 import cr.co.arquetipos.crypto.Blowfish 13 14 8 import org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib 15 16 9 import grails.plugins.springsecurity.Secured 17 18 10 import org.hibernate.SessionFactory 19 11 … … 446 438 entity.giveFields().each { field -> 447 439 // field of type ontology and value "#invalidterm"? 448 if (field.type == dbnp.studycapturing.TemplateFieldType.ONTOLOGYTERM &&440 if (field.type == nl.grails.plugins.gdt.TemplateFieldType.ONTOLOGYTERM && 449 441 params["entity_" + entity.getIdentifier() + "_" + field.escapedName()] == "#invalidterm" 450 442 ) { 451 443 invalidontologies++ 452 444 } else 453 if (field.type == dbnp.studycapturing.TemplateFieldType.ONTOLOGYTERM &&445 if (field.type == nl.grails.plugins.gdt.TemplateFieldType.ONTOLOGYTERM && 454 446 params["entity_" + entity.getIdentifier() + "_" + field.escapedName()] != "#invalidterm") { 455 447 removeFailedCell(flow.importer_failedcells, entity) -
trunk/grails-app/controllers/dbnp/studycapturing/AssayModuleController.groovy
r959 r1426 15 15 package dbnp.studycapturing 16 16 17 import nl.grails.plugins.gdt.* 18 17 19 class AssayModuleController { 18 19 20 // Scaffold this class to circumvent not being able to add assays (0.4) 20 def scaffold = dbnp.studycapturing.AssayModule21 def scaffold = nl.grails.plugins.gdt.AssayModule 21 22 } -
trunk/grails-app/controllers/dbnp/studycapturing/StudyController.groovy
r1261 r1426 313 313 if( value ) { 314 314 if( templateField.type == TemplateFieldType.RELTIME ) 315 value = new RelTime( value ).toString();315 value = new nl.grails.plugins.gdt.RelTime( value ).toString(); 316 316 317 317 def param = templateField.name + " = " + value; -
trunk/grails-app/controllers/dbnp/studycapturing/StudyWizardController.groovy
r1392 r1426 7 7 import dbnp.authentication.SecUser 8 8 import org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib 9 import nl.grails.plugins.gdt.* 9 10 10 11 /** … … 1552 1553 def samplingEventName = ucwords(event.template.name) 1553 1554 def eventGroupName = ucwords(eventGroup.name).replaceAll("([ ]{1,})", "") 1554 def sampleName = (ucwords(subject.name) + '_' + samplingEventName + '_' + eventGroupName + '_' + new RelTime(event.startTime).toString()).replaceAll("([ ]{1,})", "")1555 def sampleName = (ucwords(subject.name) + '_' + samplingEventName + '_' + eventGroupName + '_' + new nl.grails.plugins.gdt.RelTime(event.startTime).toString()).replaceAll("([ ]{1,})", "") 1555 1556 def tempSampleIterator = 0 1556 1557 def tempSampleName = sampleName … … 1648 1649 def samplingEventName = ucwords(samplingEvent.template.name) 1649 1650 def eventGroupName = ucwords(eventGroup.name) 1650 def sampleName = (ucwords(subject.name) + '_' + samplingEventName + '_' + eventGroupName + '_' + new RelTime(samplingEvent.startTime).toString()).replaceAll("([ ]{1,})", "")1651 def sampleName = (ucwords(subject.name) + '_' + samplingEventName + '_' + eventGroupName + '_' + new nl.grails.plugins.gdt.RelTime(samplingEvent.startTime).toString()).replaceAll("([ ]{1,})", "") 1651 1652 def tempSampleIterator = 0 1652 1653 def tempSampleName = sampleName -
trunk/grails-app/controllers/dbnp/studycapturing/TemplateEditorController.groovy
r1312 r1426 4 4 * Webflow driven template editor 5 5 * 6 * @author 7 * @since 8 * @package 6 * @author Jeroen Wesbeek 7 * @since 20100415 8 * @package studycapturing 9 9 * 10 10 * Revision information: … … 14 14 */ 15 15 package dbnp.studycapturing 16 import dbnp.data.* 16 17 import nl.grails.plugins.gdt.* 17 18 import dbnp.studycapturing.* 19 18 20 19 21 import dbnp.authentication.AuthenticationService … … 26 28 @Secured(['IS_AUTHENTICATED_REMEMBERED']) 27 29 class TemplateEditorController { 28 29 30 def entityName; 31 def entity; 30 32 def authenticationService 31 33 32 34 /** 33 34 35 36 if (params['standalone'] && params['standalone'] == 'true') {37 38 model.extraparams = [ 'standalone': 'true' ];39 40 41 model.extraparams = [];42 43 44 45 /** 46 47 */35 * Fires after every action and determines the layout of the page 36 */ 37 def afterInterceptor = { model, modelAndView -> 38 if (params['standalone'] && params['standalone'] == 'true') { 39 model.layout = 'main'; 40 model.extraparams = ['standalone': 'true']; 41 } else { 42 model.layout = 'dialog'; 43 model.extraparams = []; 44 } 45 } 46 47 /** 48 * Study template editor page 49 */ 48 50 def study = { 49 50 51 52 /** 53 54 */51 showEntity('dbnp.studycapturing.Study') 52 } 53 54 /** 55 * Subject template editor page 56 */ 55 57 def subject = { 56 57 58 59 /** 60 61 */58 showEntity('dbnp.studycapturing.Subject') 59 } 60 61 /** 62 * Event template editor page 63 */ 62 64 def event = { 63 64 65 66 /** 67 68 */65 showEntity('dbnp.studycapturing.Event') 66 } 67 68 /** 69 * Sampling Event template editor page 70 */ 69 71 def samplingEvent = { 70 71 72 73 /** 74 75 */72 showEntity('dbnp.studycapturing.SamplingEvent') 73 } 74 75 /** 76 * Event template editor page 77 */ 76 78 def sample = { 77 78 79 80 /** 81 82 */79 showEntity('dbnp.studycapturing.Sample') 80 } 81 82 /** 83 * Assay template editor page 84 */ 83 85 def assay = { 84 85 86 showEntity('dbnp.studycapturing.Assay') 87 } 86 88 87 89 /** … … 94 96 // if a shared secret is defined, encrypt using that 95 97 resultEntity = Blowfish.encryptBase64( 96 97 98 98 targetEntity.toString(), 99 grailsApplication.config.crypto.shared.secret 100 ) 99 101 } 100 102 else { … … 105 107 // redirect to template editor page of the specified entity 106 108 params.entity = resultEntity 107 redirect(action: "index", params: params)108 } 109 110 111 112 113 114 115 if( !_checkEntity()) {109 redirect(action: "index", params: params) 110 } 111 112 /** 113 * index closure 114 */ 115 def index = { 116 // Check whether a right entity is given 117 if (!_checkEntity()) { 116 118 return 117 119 } 118 120 119 120 121 // fetch all templates for this entity 122 def templates = Template.findAllByEntity(entity) 121 123 122 124 // Generate a human readable entity name 123 def parts = entityName.tokenize( '.');124 def humanReadableEntity = parts[ parts.size() - 1];125 126 127 128 129 130 125 def parts = entityName.tokenize('.'); 126 def humanReadableEntity = parts[parts.size() - 1]; 127 128 return [ 129 entity: entity, 130 templates: templates, 131 encryptedEntity: params.entity, 132 humanReadableEntity: humanReadableEntity, 131 133 ontologies: params.ontologies 132 133 134 135 136 137 138 139 140 if( !_checkEntity()) {134 ]; 135 } 136 137 /** 138 * compare two or more templates 139 */ 140 def compare = { 141 // Check whether a right entity is given 142 if (!_checkEntity()) { 141 143 return 142 144 } 143 145 144 145 146 // fetch all templates for this entity 147 def templates = Template.findAllByEntity(entity) 146 148 147 149 // Find all available fields 148 def allFields = TemplateField.findAllByEntity( entity).sort { a, b -> a.name <=> b.name }150 def allFields = TemplateField.findAllByEntity(entity).sort { a, b -> a.name <=> b.name } 149 151 150 152 // Generate a human readable entity name 151 def parts = entityName.tokenize( '.');152 def humanReadableEntity = parts[ parts.size() - 1];153 154 155 156 153 def parts = entityName.tokenize('.'); 154 def humanReadableEntity = parts[parts.size() - 1]; 155 156 return [ 157 entity: entity, 158 templates: templates, 157 159 allFields: allFields, 158 159 160 encryptedEntity: params.entity, 161 humanReadableEntity: humanReadableEntity, 160 162 ontologies: params.ontologies, 161 163 templateEntities: this.templateEntityList() 162 163 164 ]; 165 } 164 166 165 167 /** … … 167 169 */ 168 170 def template = { 169 170 if( !_checkEntity()) {171 // Check whether a right entity is given 172 if (!_checkEntity()) { 171 173 return 172 174 } 173 175 174 175 176 176 // Check whether a template is selected. If not, redirect the user to the index 177 def selectedTemplate = params.template; 178 def template = null; 177 179 def domainFields = null; 178 180 179 if( selectedTemplate) {180 template = Template.get( selectedTemplate);181 if (selectedTemplate) { 182 template = Template.get(selectedTemplate); 181 183 domainFields = template.entity.giveDomainFields(); 182 183 redirect(action: "index",params:[entity:params.entity])184 return; 185 } 186 187 188 184 } else { 185 redirect(action: "index", params: [entity: params.entity]) 186 return; 187 } 188 189 // fetch all templates for this entity 190 def templates = Template.findAllByEntity(entity) 189 191 190 192 // Generate a human readable entity name 191 def parts = entityName.tokenize( '.');192 def humanReadableEntity = parts[ parts.size() - 1];193 def parts = entityName.tokenize('.'); 194 def humanReadableEntity = parts[parts.size() - 1]; 193 195 194 196 // Find all available fields 195 def allFields = TemplateField.findAllByEntity( entity).sort { a, b -> a.name <=> b.name }196 197 198 199 200 201 197 def allFields = TemplateField.findAllByEntity(entity).sort { a, b -> a.name <=> b.name } 198 199 return [ 200 entity: entity, 201 templates: templates, 202 encryptedEntity: params.entity, 203 fieldTypes: TemplateFieldType.list(), 202 204 ontologies: Ontology.list(), 203 204 205 205 humanReadableEntity: humanReadableEntity, 206 207 template: template, 206 208 allFields: allFields, 207 209 domainFields: domainFields 208 ]; 209 210 } 211 212 213 /** 214 * Shows an error page 215 * 216 * TODO: improve the error page 217 */ 218 def error = { 219 render( 'view': 'error' ); 220 } 221 222 /** 223 * Creates a new template using a AJAX call 224 * 225 * @return JSON object with two entries: 226 * id: [id of this object] 227 * html: HTML to replace the contents of the LI-item that was updated. 228 * On error the method gives a HTTP response status 500 and the error 229 */ 230 def createTemplate = { 210 ]; 211 212 } 213 214 /** 215 * Shows an error page 216 * 217 * TODO: improve the error page 218 */ 219 def error = { 220 render('view': 'error'); 221 } 222 223 /** 224 * Creates a new template using a AJAX call 225 * 226 * @return JSON object with two entries: 227 * id: [id of this object] 228 * html: HTML to replace the contents of the LI-item that was updated. 229 * On error the method gives a HTTP response status 500 and the error 230 */ 231 def createTemplate = { 231 232 // Decode the entity 232 if( !_checkEntity()) {233 if (!_checkEntity()) { 233 234 response.status = 500; 234 235 render "Incorrect entity given"; … … 240 241 241 242 // Create the template fields and add it to the template 242 def template = new Template( params);243 244 def html = g.render( template: 'elements/liTemplate', model: [template: template]);245 def output = [ id: template.id, html: html];243 def template = new Template(params); 244 if (template.validate() && template.save(flush: true)) { 245 def html = g.render(template: 'elements/liTemplate', model: [template: template]); 246 def output = [id: template.id, html: html]; 246 247 render output as JSON; 247 248 249 250 251 252 253 254 255 256 * 257 * @return 258 * id: [id of this object]259 * html: HTML of the contents of the LI-item that will be added.260 * On error the method gives a HTTP response status 500 and the error261 262 263 264 def template = Template.get( params.id);265 if( !template) {266 267 268 269 248 } else { 249 response.status = 500; 250 render 'Template could not be created because errors occurred.'; 251 return 252 } 253 } 254 255 /** 256 * Clones a template using a AJAX call 257 * 258 * @return JSON object with two entries: 259 * id: [id of this object] 260 * html: HTML of the contents of the LI-item that will be added. 261 * On error the method gives a HTTP response status 500 and the error 262 */ 263 def cloneTemplate = { 264 // Search for the template field 265 def template = Template.get(params.id); 266 if (!template) { 267 response.status = 404; 268 render 'Template not found'; 269 return; 270 } 270 271 271 272 // Create the template fields and add it to the template 272 def newTemplate = new Template( template, authenticationService.getLoggedInUser());273 274 def html = g.render( template: 'elements/liTemplate', model: [template: newTemplate]);275 def output = [ id: newTemplate.id, html: html];273 def newTemplate = new Template(template, authenticationService.getLoggedInUser()); 274 if (newTemplate.validate() && newTemplate.save(flush: true)) { 275 def html = g.render(template: 'elements/liTemplate', model: [template: newTemplate]); 276 def output = [id: newTemplate.id, html: html]; 276 277 render output as JSON; 277 278 279 280 281 282 283 284 285 286 * 287 * @param id 288 * @return 289 * id: [id of this object]290 * html: HTML to replace the contents of the LI-item that was updated.291 * On error the method gives a HTTP response status 500 and the error292 293 294 295 def template = Template.get( params.id);296 if( !template) {297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 def html = g.render( template: 'elements/liTemplate', model: [template: template]);315 def output = [ id: template.id, html: html];278 } else { 279 response.status = 500; 280 render 'Template could not be cloned because errors occurred.'; 281 return 282 } 283 } 284 285 /** 286 * Updates a selected template using a AJAX call 287 * 288 * @param id ID of the template to update 289 * @return JSON object with two entries: 290 * id: [id of this object] 291 * html: HTML to replace the contents of the LI-item that was updated. 292 * On error the method gives a HTTP response status 500 and the error 293 */ 294 def updateTemplate = { 295 // Search for the template field 296 def template = Template.get(params.id); 297 if (!template) { 298 response.status = 404; 299 render 'Template not found'; 300 return; 301 } 302 303 // Update the field if it is not updated in between 304 if (params.version) { 305 def version = params.version.toLong() 306 if (template.version > version) { 307 response.status = 500; 308 render 'Template was updated while you were working on it. Please reload and try again.'; 309 return 310 } 311 } 312 313 template.properties = params 314 if (!template.hasErrors() && template.save(flush: true)) { 315 def html = g.render(template: 'elements/liTemplate', model: [template: template]); 316 def output = [id: template.id, html: html]; 316 317 render output as JSON; 317 318 319 320 321 322 323 324 325 326 327 * @param template 328 * @return 329 * id: [id of this object]330 * On error the method gives a HTTP response status 500 and the error331 332 333 334 def template = Template.get( params.template);335 if( !template) {336 337 338 339 340 341 318 } else { 319 response.status = 500; 320 render 'Template was not updated because errors occurred.'; 321 return 322 } 323 } 324 325 /** 326 * Deletes a template using a AJAX call 327 * 328 * @param template ID of the template to move 329 * @return JSON object with one entry: 330 * id: [id of this object] 331 * On error the method gives a HTTP response status 500 and the error 332 */ 333 def deleteTemplate = { 334 // Search for the template field 335 def template = Template.get(params.template); 336 if (!template) { 337 response.status = 404; 338 render 'Template not found'; 339 return; 340 } 341 342 // Delete the template field 342 343 try { 343 344 template.delete(flush: true) 344 345 345 def output = [ id: template.id];346 def output = [id: template.id]; 346 347 render output as JSON; 347 348 } 348 349 catch (org.springframework.dao.DataIntegrityViolationException e) { 349 350 351 } 352 353 354 355 356 * 357 * @param template 358 * @return 359 * id: [id of this object]360 * html: HTML to replace the contents of the LI-item that was updated.361 * On error the method gives a HTTP response status 500 and the error362 363 364 365 def template = Template.get( params.template);366 367 if( !template) {368 369 370 371 350 response.status = 500; 351 render 'Template could not be deleted: ' + e.getMessage(); 352 } 353 } 354 355 /** 356 * Creates a new template field using a AJAX call 357 * 358 * @param template ID of the template to add a field to 359 * @return JSON object with two entries: 360 * id: [id of this object] 361 * html: HTML to replace the contents of the LI-item that was updated. 362 * On error the method gives a HTTP response status 500 and the error 363 */ 364 def createField = { 365 // Search for the template 366 def template = Template.get(params.template); 367 368 if (!template) { 369 response.status = 404; 370 render 'Template not found'; 371 return; 372 } 372 373 373 374 // Decode the entity, in order to set a good property 374 if( !_checkEntity()) {375 if (!_checkEntity()) { 375 376 response.status = 500; 376 377 render "Incorrect entity given"; … … 382 383 // See whether this field already exists. It is checked by name, type and unit and entity 383 384 // The search is done using search by example (see http://grails.org/DomainClass+Dynamic+Methods, method find) 384 def uniqueParams = [ name: params.name, type: params.type, unit: params.unit, entity: params.entity];385 if ( TemplateField.find( new TemplateField( uniqueParams ) )) {385 def uniqueParams = [name: params.name, type: params.type, unit: params.unit, entity: params.entity]; 386 if (TemplateField.find(new nl.grails.plugins.gdt.TemplateField(uniqueParams))) { 386 387 response.status = 500; 387 388 render "A field with this name, type and unit already exists."; … … 391 392 // See whether this exists as domain field. If it does, raise an error 392 393 def domainFields = template.entity.giveDomainFields() 393 if ( domainFields.find { it.name.toLowerCase() == params.name.toLowerCase() }) {394 if (domainFields.find { it.name.toLowerCase() == params.name.toLowerCase() }) { 394 395 response.status = 500; 395 396 render "All templates for entity " + template.entity + " contain a domain field with name " + params.name + ". You can not create a field with this name.";; … … 398 399 399 400 // If this field is type stringlist, we have to prepare the parameters 400 if ( params.type.toString() == 'STRINGLIST') {401 if (params.type.toString() == 'STRINGLIST') { 401 402 def listEntries = []; 402 403 params.listEntries.eachLine { … … 406 407 def name = it.trim(); 407 408 408 def listitem = new TemplateFieldListItem( name: name)409 listEntries.add( listitem);409 def listitem = new nl.grails.plugins.gdt.TemplateFieldListItem(name: name) 410 listEntries.add(listitem); 410 411 } 411 412 412 413 params.listEntries = listEntries; 413 414 } else { 414 params.remove( 'listEntries');415 params.remove('listEntries'); 415 416 } 416 417 417 418 // If this field isnot a ontologyterm, we should remove the ontologies 418 if ( params.type.toString() != 'ONTOLOGYTERM') {419 params.remove( 'ontologies');419 if (params.type.toString() != 'ONTOLOGYTERM') { 420 params.remove('ontologies'); 420 421 } 421 422 422 423 // Create the template field and add it to the template 423 def templateField = new TemplateField( params);424 425 426 def html = g.render( template: 'elements/available', model: [templateField: templateField, ontologies: Ontology.list(), fieldTypes: TemplateFieldType.list()]);427 def output = [ id: templateField.id, html: html];424 def templateField = new nl.grails.plugins.gdt.TemplateField(params); 425 if (templateField.save(flush: true)) { 426 427 def html = g.render(template: 'elements/available', model: [templateField: templateField, ontologies: Ontology.list(), fieldTypes: TemplateFieldType.list()]); 428 def output = [id: templateField.id, html: html]; 428 429 render output as JSON; 429 430 430 431 432 433 434 435 436 437 438 439 440 * 441 * @param id 442 * @return 443 * id: [id of this object]444 * html: HTML to replace the contents of the LI-item that was updated.445 * On error the method gives a HTTP response status 500 and the error446 447 448 449 def templateField = TemplateField.get( params.id);450 if( !templateField) {451 452 453 454 455 456 457 458 459 460 461 462 463 464 431 //render ''; 432 } else { 433 response.status = 500; 434 render 'TemplateField could not be created because errors occurred.'; 435 return 436 } 437 } 438 439 /** 440 * Updates a selected template field using a AJAX call 441 * 442 * @param id ID of the field to update 443 * @return JSON object with two entries: 444 * id: [id of this object] 445 * html: HTML to replace the contents of the LI-item that was updated. 446 * On error the method gives a HTTP response status 500 and the error 447 */ 448 def updateField = { 449 // Search for the template field 450 def templateField = TemplateField.get(params.id); 451 if (!templateField) { 452 response.status = 404; 453 render 'TemplateField not found'; 454 return; 455 } 456 457 // Update the field if it is not updated in between 458 if (params.version) { 459 def version = params.version.toLong() 460 if (templateField.version > version) { 461 response.status = 500; 462 render 'TemplateField was updated while you were working on it. Please reload and try again.'; 463 return 464 } 465 } 465 466 466 467 // If this field is type stringlist or ontology, we have to prepare the parameters … … 469 470 // In that case, only never-used items can be removed or changed and items can be added. If that is the case 470 471 // params.is_disabled is true and we should combine listEntries and extraListEntries with the items already in use. 471 if ( params.type.toString() == 'STRINGLIST' || ( templateField.type == TemplateFieldType.STRINGLIST && params.is_disabled)) {472 if (params.type.toString() == 'STRINGLIST' || (templateField.type == TemplateFieldType.STRINGLIST && params.is_disabled)) { 472 473 def listEntryLines = ""; 473 474 def listEntries = []; 474 475 475 if ( params.is_disabled) {476 if (params.is_disabled) { 476 477 listEntries = templateField.getUsedListEntries(); 477 478 } 478 479 479 if ( params.listEntries) {480 if (params.listEntries) { 480 481 listEntryLines = params.listEntries; 481 482 482 483 listEntryLines.eachLine { 483 484 // We don't search for a listitem that might already exist, … … 489 490 def c = TemplateFieldListItem.createCriteria() 490 491 def listitem = c.get { 491 eq( "name", name)492 eq( 492 eq("name", name) 493 eq("parent", templateField) 493 494 } 494 if ( !listitem) {495 listitem = new TemplateFieldListItem( name: name)495 if (!listitem) { 496 listitem = new nl.grails.plugins.gdt.TemplateFieldListItem(name: name) 496 497 } 497 498 498 499 // Prevent using the same list entry twice 499 if ( !listEntries.contains( listitem ))500 listEntries.add( listitem);500 if (!listEntries.contains(listitem)) 501 listEntries.add(listitem); 501 502 } 502 503 } … … 505 506 params.listEntries = listEntries; 506 507 } else { 507 params.remove( 'listEntries');508 params.remove('listEntries'); 508 509 } 509 510 … … 512 513 // In that case, only never-used items can be removed or changed and items can be added. If that is the case 513 514 // params.is_disabled is true and we should combine ontologies with the ontologies already in use. 514 if ( ( params.type.toString() == 'ONTOLOGYTERM' || ( templateField.type == TemplateFieldType.ONTOLOGYTERM && params.is_disabled ) ) && params.ontologies) {515 if ((params.type.toString() == 'ONTOLOGYTERM' || (templateField.type == TemplateFieldType.ONTOLOGYTERM && params.is_disabled)) && params.ontologies) { 515 516 def usedOntologies = []; 516 517 517 if ( params.is_disabled) {518 if (params.is_disabled) { 518 519 usedOntologies = templateField.getUsedOntologies(); 519 520 } 520 521 521 if ( params.ontologies) {522 if (params.ontologies) { 522 523 def ontologies = params.ontologies; 523 524 params.ontologies = usedOntologies + Ontology.getAll( ontologies.collect { Integer.parseInt( it ) });525 } 526 527 } else { 528 params.remove( 'ontologies');524 525 params.ontologies = usedOntologies + Ontology.getAll(ontologies.collect { Integer.parseInt(it) }); 526 } 527 528 } else { 529 params.remove('ontologies'); 529 530 } 530 531 531 532 // A field that is already used in one or more templates, but is not filled everywhere, 532 533 // can not be set to required 533 if ( params.required) {534 if ( !templateField.isFilledInAllObjects()) {534 if (params.required) { 535 if (!templateField.isFilledInAllObjects()) { 535 536 response.status = 500; 536 537 render "A field can only be marked as required if all objects using this field have a value for the field." … … 543 544 544 545 templateField.validate(); 545 546 547 546 547 548 if (!templateField.hasErrors() && templateField.save(flush: true)) { 548 549 549 550 // Remove all orphaned list items, because grails doesn't handle it for us 550 TemplateFieldListItem.findAllByParent( templateField).each {551 if ( !params.listEntries.contains( it )) {552 templateField.removeFromListEntries( it);551 TemplateFieldListItem.findAllByParent(templateField).each { 552 if (!params.listEntries.contains(it)) { 553 templateField.removeFromListEntries(it); 553 554 it.delete(); 554 555 } … … 557 558 // Select the template to use for the HTML output 558 559 def renderTemplate = 'elements/available'; 559 if ( params.renderTemplate == 'selected') {560 if (params.renderTemplate == 'selected') { 560 561 renderTemplate = 'elements/selected'; 561 562 } … … 563 564 // Selected fields should have a template given 564 565 def template = null; 565 if ( params.templateId)566 template = Template.findById( params.templateId);567 568 def html = g.render( template: renderTemplate, model: [template: template, templateField: templateField, ontologies: Ontology.list(), fieldTypes: TemplateFieldType.list()]);569 def output = [ id: templateField.id, html: html];566 if (params.templateId) 567 template = Template.findById(params.templateId); 568 569 def html = g.render(template: renderTemplate, model: [template: template, templateField: templateField, ontologies: Ontology.list(), fieldTypes: TemplateFieldType.list()]); 570 def output = [id: templateField.id, html: html]; 570 571 render output as JSON; 571 572 573 574 575 576 577 578 579 580 581 * @param templateField 582 * @return 583 * id: [id of this object]584 * On error the method gives a HTTP response status 500 and the error585 586 587 588 def templateField = TemplateField.get( params.templateField);589 if( !templateField) {590 591 592 593 594 595 572 } else { 573 response.status = 500; 574 render 'TemplateField was not updated because errors occurred. Please contact the system administrator'; 575 return 576 } 577 } 578 579 /** 580 * Deletes a template field using a AJAX call 581 * 582 * @param templateField ID of the templatefield to move 583 * @return JSON object with one entry: 584 * id: [id of this object] 585 * On error the method gives a HTTP response status 500 and the error 586 */ 587 def deleteField = { 588 // Search for the template field 589 def templateField = TemplateField.get(params.templateField); 590 if (!templateField) { 591 response.status = 404; 592 render 'TemplateField not found'; 593 return; 594 } 595 596 // Delete the template field 596 597 try { 597 598 templateField.delete(flush: true) 598 599 599 def output = [ id: templateField.id];600 def output = [id: templateField.id]; 600 601 render output as JSON; 601 602 } 602 603 catch (org.springframework.dao.DataIntegrityViolationException e) { 603 604 605 } 606 607 608 609 610 * 611 * @param template 612 * @return 613 * id: [id of this object]614 * html: HTML to replace the contents of the LI-item that was updated.615 * On error the method gives a HTTP response status 404 or 500 and the error616 617 618 619 def template = Template.get( params.template);620 621 if( !template) {622 623 624 625 626 627 628 def templateField = TemplateField.get( params.templateField);629 if( !templateField) {630 631 632 633 634 635 636 if( template.fields.contains( templateField )) {637 638 639 640 604 response.status = 500; 605 render 'TemplateField could not be deleted: ' + e.getMessage(); 606 } 607 } 608 609 /** 610 * Adds a new template field to a template using a AJAX call 611 * 612 * @param template ID of the template to add a field to 613 * @return JSON object with two entries: 614 * id: [id of this object] 615 * html: HTML to replace the contents of the LI-item that was updated. 616 * On error the method gives a HTTP response status 404 or 500 and the error 617 */ 618 def addField = { 619 // Search for the template 620 def template = Template.get(params.template); 621 622 if (!template) { 623 response.status = 404; 624 render 'Template not found'; 625 return; 626 } 627 628 // Search for the template field 629 def templateField = TemplateField.get(params.templateField); 630 if (!templateField) { 631 response.status = 404; 632 render 'TemplateField does not exist'; 633 return; 634 } 635 636 // The template field should exist within the template 637 if (template.fields.contains(templateField)) { 638 response.status = 500; 639 render 'TemplateField is already found within template'; 640 return; 641 } 641 642 642 643 // If the template is in use, only non-required fields can be added 643 if ( template.inUse() && templateField.required) {644 if (template.inUse() && templateField.required) { 644 645 response.status = 500; 645 646 render 'Only non-required fields can be added to templates that are in use.' … … 648 649 649 650 // All field names within a template should be unique 650 if ( template.fields.find { it.name.toLowerCase() == templateField.name.toLowerCase() }) {651 if (template.fields.find { it.name.toLowerCase() == templateField.name.toLowerCase() }) { 651 652 response.status = 500; 652 653 render 'This template already contains a field with name ' + templateField.name + '. Field names should be unique within a template.' … … 654 655 } 655 656 656 if ( !params.position || Integer.parseInt( params.position) == -1) {657 template.fields.add( templateField)658 } else { 659 template.fields.add( Integer.parseInt( params.position ), templateField)660 } 661 662 663 664 665 666 template.save(flush:true);667 668 def html = g.render( template: 'elements/selected', model: [templateField: templateField, template: template, ontologies: Ontology.list(), fieldTypes: TemplateFieldType.list()]);669 def output = [ id: templateField.id, html: html];657 if (!params.position || Integer.parseInt(params.position) == -1) { 658 template.fields.add(templateField) 659 } else { 660 template.fields.add(Integer.parseInt(params.position), templateField) 661 } 662 663 if (!template.validate()) { 664 response.status = 500; 665 template.errors.each { render it} 666 } 667 template.save(flush: true); 668 669 def html = g.render(template: 'elements/selected', model: [templateField: templateField, template: template, ontologies: Ontology.list(), fieldTypes: TemplateFieldType.list()]); 670 def output = [id: templateField.id, html: html]; 670 671 render output as JSON; 671 672 673 674 675 * 676 * @param templateField 677 * @param template 678 * @return 679 * id: [id of this object]680 * html: HTML to replace the contents of the LI-item that was updated.681 * On error the method gives a HTTP response status 404 or 500 and the error682 683 684 685 def template = Template.get( params.template);686 687 if( !template) {688 689 690 691 692 693 694 def templateField = TemplateField.get( params.templateField);695 if( !templateField) {696 697 698 699 700 701 702 if( !template.fields.contains( templateField )) {703 704 705 706 672 } 673 674 /** 675 * Removes a selected template field from the template using a AJAX call 676 * 677 * @param templateField ID of the field to update 678 * @param template ID of the template for which the field should be removed 679 * @return JSON object with two entries: 680 * id: [id of this object] 681 * html: HTML to replace the contents of the LI-item that was updated. 682 * On error the method gives a HTTP response status 404 or 500 and the error 683 */ 684 def removeField = { 685 // Search for the template 686 def template = Template.get(params.template); 687 688 if (!template) { 689 response.status = 404; 690 render 'Template not found'; 691 return; 692 } 693 694 // Search for the template field 695 def templateField = TemplateField.get(params.templateField); 696 if (!templateField) { 697 response.status = 404; 698 render 'TemplateField not found'; 699 return; 700 } 701 702 // The template field should exist within the template 703 if (!template.fields.contains(templateField)) { 704 response.status = 404; 705 render 'TemplateField not found within template'; 706 return; 707 } 707 708 708 709 // If the template is in use, field can not be removed 709 if ( templateField.isFilledInTemplate(template)) {710 if (templateField.isFilledInTemplate(template)) { 710 711 response.status = 500; 711 712 render 'Fields can not be removed from a template if it has been filled somewhere.' … … 714 715 715 716 // Delete the field from this template 716 def currentIndex = template.fields.indexOf( templateField);717 template.fields.remove( currentIndex);718 template.save(flush: true);719 720 721 def html = g.render( template: 'elements/available', model: [templateField: templateField, ontologies: Ontology.list(), fieldTypes: TemplateFieldType.list()]);722 def output = [ id: templateField.id, html: html];717 def currentIndex = template.fields.indexOf(templateField); 718 template.fields.remove(currentIndex); 719 template.save(flush: true); 720 721 722 def html = g.render(template: 'elements/available', model: [templateField: templateField, ontologies: Ontology.list(), fieldTypes: TemplateFieldType.list()]); 723 def output = [id: templateField.id, html: html]; 723 724 render output as JSON; 724 725 726 727 728 729 * @param template 730 * @param templateField 731 * @param position 732 * @return 733 * id: [id of this object]734 * html: HTML to replace the contents of the LI-item that was updated.735 * On error the method gives a HTTP response status 500 and the error736 737 738 739 def template = Template.get( params.template);740 741 if( !template) {742 743 744 745 746 747 748 def templateField = TemplateField.get( params.templateField);749 if( !templateField) {750 751 752 753 754 755 756 if( !template.fields.contains( templateField )) {757 758 759 760 761 762 763 def currentIndex = template.fields.indexOf( templateField);764 def moveField = template.fields.remove( currentIndex);765 766 template.fields.add( Integer.parseInt( params.position ), moveField);767 template.save(flush: true);768 769 def html = g.render( template: 'elements/selected', model: [templateField: templateField, template: template, fieldTypes: TemplateFieldType.list()]);770 def output = [ id: templateField.id, html: html];725 } 726 727 /** 728 * Moves a template field using a AJAX call 729 * 730 * @param template ID of the template that contains this field 731 * @param templateField ID of the templatefield to move 732 * @param position New index of the templatefield in the array. The index is 0-based. 733 * @return JSON object with two entries: 734 * id: [id of this object] 735 * html: HTML to replace the contents of the LI-item that was updated. 736 * On error the method gives a HTTP response status 500 and the error 737 */ 738 def moveField = { 739 // Search for the template 740 def template = Template.get(params.template); 741 742 if (!template) { 743 response.status = 404; 744 render 'Template not found'; 745 return; 746 } 747 748 // Search for the template field 749 def templateField = TemplateField.get(params.templateField); 750 if (!templateField) { 751 response.status = 404; 752 render 'TemplateField not found'; 753 return; 754 } 755 756 // The template field should exist within the template 757 if (!template.fields.contains(templateField)) { 758 response.status = 404; 759 render 'TemplateField not found within template'; 760 return; 761 } 762 763 // Move the item 764 def currentIndex = template.fields.indexOf(templateField); 765 def moveField = template.fields.remove(currentIndex); 766 767 template.fields.add(Integer.parseInt(params.position), moveField); 768 template.save(flush: true); 769 770 def html = g.render(template: 'elements/selected', model: [templateField: templateField, template: template, fieldTypes: TemplateFieldType.list()]); 771 def output = [id: templateField.id, html: html]; 771 772 render output as JSON; 772 773 } 773 774 774 775 /** 775 776 * Checks how many template use a specific template field 776 777 * 777 * @param idID of the template field778 * @return 778 * @param id ID of the template field 779 * @return int Number of uses 779 780 */ 780 781 def numFieldUses = { 781 782 def templateField = TemplateField.get( params.id);783 if( !templateField) {784 785 786 787 782 // Search for the template field 783 def templateField = TemplateField.get(params.id); 784 if (!templateField) { 785 response.status = 404; 786 render 'TemplateField not found'; 787 return; 788 } 788 789 789 790 render templateField.numUses(); … … 793 794 * Adds a ontolgy based on the ID given 794 795 * 795 * @param 796 * @return 796 * @param ncboID 797 * @return JSON Ontology object 797 798 */ 798 799 def addOntologyById = { 799 800 def id = params.ncboID; 800 801 801 if ( !id) {802 if (!id) { 802 803 response.status = 500; 803 804 render 'No ID given' … … 805 806 } 806 807 807 if ( Ontology.findByNcboId( Integer.parseInt( id ) )) {808 if (Ontology.findByNcboId(Integer.parseInt(id))) { 808 809 response.status = 500; 809 810 render 'This ontology was already added to the system'; … … 814 815 815 816 try { 816 ontology = dbnp.data.Ontology.getBioPortalOntology( id);817 } catch ( Exception e) {817 ontology = dbnp.data.Ontology.getBioPortalOntology(id); 818 } catch (Exception e) { 818 819 response.status = 500; 819 820 render 'Ontology with ID ' + id + ' not found'; … … 821 822 } 822 823 823 if ( !ontology) {824 if (!ontology) { 824 825 response.status = 404; 825 826 render 'Ontology with ID ' + id + ' not found'; … … 830 831 if (!ontology.validate()) { 831 832 response.status = 500; 832 render ontology.errors.join( '; ');833 render ontology.errors.join('; '); 833 834 return; 834 835 } … … 842 843 * Adds a ontolgy based on the term ID given 843 844 * 844 * @param 845 * @return 846 * @deprecated 845 * @param ncboID 846 * @return JSON Ontology object 847 * @deprecated User addOntologyById instead, using the ncboId given by the JSON call 847 848 */ 848 849 def addOntologyByTerm = { 849 850 def id = params.termID; 850 851 851 if ( !id) {852 if (!id) { 852 853 response.status = 500; 853 854 render 'No ID given' … … 858 859 859 860 try { 860 ontology = dbnp.data.Ontology.getBioPortalOntologyByTerm( id);861 } catch ( Exception e) {862 response.status = 500; 863 864 if ( e.getMessage())865 861 ontology = dbnp.data.Ontology.getBioPortalOntologyByTerm(id); 862 } catch (Exception e) { 863 response.status = 500; 864 865 if (e.getMessage()) 866 render e.getMessage() 866 867 else 867 868 render "Unknown error: " + e.getClass() … … 869 870 } 870 871 871 if ( !ontology) {872 if (!ontology) { 872 873 response.status = 404; 873 874 render 'Ontology form term ' + id + ' not found'; … … 878 879 if (!ontology.validate()) { 879 880 response.status = 500; 880 render ontology.errors.join( '; ');881 render ontology.errors.join('; '); 881 882 return; 882 883 } … … 887 888 } 888 889 889 /** 890 * Checks whether a correct entity is given 891 * 892 * @return boolean True if a correct entity is given. Returns false and raises an error otherwise 893 */ 894 def _checkEntity = { 895 // got a entity get parameter? 896 entityName = _parseEntityType(); 897 898 if( !entityName ) { 899 error(); 900 return false; 901 } 902 903 // Create an object of this type 904 entity = _getEntity( entityName ); 905 906 if( !entity ) { 907 error(); 908 return false 909 } 910 911 return true; 912 } 913 914 915 /** 916 * Checks whether the entity type is given and can be parsed 917 * 918 * @return Name of the entity if parsing is succesful, false otherwise 919 */ 920 def _parseEntityType() { 921 def entityName; 922 if (params.entity) { 923 // decode entity get parameter 924 if (grailsApplication.config.crypto) { 925 // generate a Blowfish encrypted and Base64 encoded string. 926 entityName = Blowfish.decryptBase64( 927 params.entity, 928 grailsApplication.config.crypto.shared.secret 929 ) 930 } else { 931 // base64 only; this is INSECURE! Even though it is not 932 // very likely, it is possible to exploit this and have 933 // Grails dynamically instantiate whatever class you like. 934 // If that constructor does something harmfull this could 935 // be dangerous. Hence, use encryption (above) instead... 936 entityName = new String(params.entity.toString().decodeBase64()) 937 } 938 939 return entityName; 940 } else { 941 return false; 942 } 943 } 944 945 /** 946 * Creates an object of the given entity. 947 * 890 /** 891 * Checks whether a correct entity is given 892 * 893 * @return boolean True if a correct entity is given. Returns false and raises an error otherwise 894 */ 895 def _checkEntity = { 896 // got a entity get parameter? 897 entityName = _parseEntityType(); 898 899 if (!entityName) { 900 error(); 901 return false; 902 } 903 904 // Create an object of this type 905 entity = _getEntity(entityName); 906 907 if (!entity) { 908 error(); 909 return false 910 } 911 912 return true; 913 } 914 915 /** 916 * Checks whether the entity type is given and can be parsed 917 * 918 * @return Name of the entity if parsing is succesful, false otherwise 919 */ 920 def _parseEntityType() { 921 def entityName; 922 if (params.entity) { 923 // decode entity get parameter 924 if (grailsApplication.config.crypto) { 925 // generate a Blowfish encrypted and Base64 encoded string. 926 entityName = Blowfish.decryptBase64( 927 params.entity, 928 grailsApplication.config.crypto.shared.secret 929 ) 930 } else { 931 // base64 only; this is INSECURE! Even though it is not 932 // very likely, it is possible to exploit this and have 933 // Grails dynamically instantiate whatever class you like. 934 // If that constructor does something harmfull this could 935 // be dangerous. Hence, use encryption (above) instead... 936 entityName = new String(params.entity.toString().decodeBase64()) 937 } 938 939 return entityName; 940 } else { 941 return false; 942 } 943 } 944 945 /** 946 * Creates an object of the given entity. 947 * 948 948 * @return False if the entity is not a subclass of TemplateEntity 949 950 def _getEntity( entityName) {951 952 953 954 955 956 957 958 959 960 961 949 */ 950 def _getEntity(entityName) { 951 // Find the templates 952 def entity = Class.forName(entityName, true, this.getClass().getClassLoader()) 953 954 // succes, is entity an instance of TemplateEntity? 955 if (entity.superclass =~ /TemplateEntity$/ || entity.superclass.superclass =~ /TemplateEntity$/) { 956 return entity; 957 } else { 958 return false; 959 } 960 961 } 962 962 963 963 def templateEntityList = { 964 964 def entities = [ 965 [ name: 'Study', entity: 'dbnp.studycapturing.Study'],966 [ name: 'Subject', entity: 'dbnp.studycapturing.Subject'],967 [ name: 'Event', entity: 'dbnp.studycapturing.Event'],968 [ name: 'Sampling Event', entity: 'dbnp.studycapturing.SamplingEvent'],969 [ name: 'Sample', entity: 'dbnp.studycapturing.Sample'],970 [ name: 'Assay', entity: 'dbnp.studycapturing.Assay']965 [name: 'Study', entity: 'dbnp.studycapturing.Study'], 966 [name: 'Subject', entity: 'dbnp.studycapturing.Subject'], 967 [name: 'Event', entity: 'dbnp.studycapturing.Event'], 968 [name: 'Sampling Event', entity: 'dbnp.studycapturing.SamplingEvent'], 969 [name: 'Sample', entity: 'dbnp.studycapturing.Sample'], 970 [name: 'Assay', entity: 'dbnp.studycapturing.Assay'] 971 971 ] 972 972 973 entities.each { 973 entities.each { 974 974 // add the entity class name to the element 975 975 // do we have crypto information available? -
trunk/grails-app/controllers/dbnp/studycapturing/TermEditorController.groovy
r1192 r1426 15 15 package dbnp.studycapturing 16 16 17 import dbnp.data.Term 18 import dbnp.data.Ontology 17 import nl.grails.plugins.gdt.* 19 18 20 19 class TermEditorController {
Note: See TracChangeset
for help on using the changeset viewer.