- Timestamp:
- Jun 11, 2010, 4:00:10 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/dbnp/studycapturing/TemplateEditorController.groovy
r556 r558 18 18 import cr.co.arquetipos.crypto.Blowfish 19 19 import grails.converters.* 20 import java.lang.reflect.*; 20 21 21 22 class TemplateEditorController { … … 59 60 def selectedTemplate = params.template; 60 61 def template = null; 62 def domainFields = null; 61 63 62 64 if( selectedTemplate ) { 63 65 template = Template.get( selectedTemplate ); 66 67 // Find the domain classes for this template/entity 68 // See http://www.javaworld.com/javaworld/javaqa/1999-07/06-qa-invoke.html 69 Method m = template.entity.getDeclaredMethod("giveDomainFields", null); 70 domainFields = m.invoke( null, null ); 64 71 } else { 65 72 redirect(action:"index",params:[entity:params.entity]) … … 84 91 encryptedEntity: params.entity, 85 92 fieldTypes: TemplateFieldType.list(), 93 ontologies: Ontology.list(), 86 94 humanReadableEntity: humanReadableEntity, 87 95 88 96 template: template, 89 allFields: allFields 97 allFields: allFields, 98 domainFields: domainFields 90 99 ]; 91 100 … … 124 133 if (template.save(flush: true)) { 125 134 126 def html = g.render( template: 'elements/liTemplate Editable', model: [template: template] );135 def html = g.render( template: 'elements/liTemplate', model: [template: template] ); 127 136 def output = [ id: template.id, html: html ]; 128 137 render output as JSON; … … 166 175 template.properties = params 167 176 if (!template.hasErrors() && template.save(flush: true)) { 168 def html = g.render( template: 'elements/liTemplate Editable', model: [template: template] );177 def html = g.render( template: 'elements/liTemplate', model: [template: template] ); 169 178 def output = [ id: template.id, html: html ]; 170 179 render output as JSON; … … 206 215 } 207 216 208 209 210 211 217 /** 212 218 * Creates a new template field using a AJAX call … … 246 252 } 247 253 254 // See whether this exists as domain field. If it does, raise an error 255 Method m = template.entity.getDeclaredMethod("giveDomainFields", null); 256 def domainFields = m.invoke( null, null ); 257 if( domainFields.find { it.name.toLowerCase() == params.name.toLowerCase() } ) { 258 response.status = 500; 259 render "All templates for entity " + template.entity + " contain a domain field with name " + params.name + ". You can not create a field with this name.";; 260 return; 261 } 262 263 // If this field is type stringlist, we have to prepare the parameters 264 if( params.type.toString() == 'STRINGLIST' ) { 265 def listEntries = []; 266 params.listEntries.eachLine { 267 // Search whether a listitem with this name already exists. 268 // if it does, use that one to prevent pollution of the database 269 def name = it.trim(); 270 def listitem = TemplateFieldListItem.findByName( name );7 271 if( !listitem ) { 272 listitem = new TemplateFieldListItem( name: name ) 273 } 274 listEntries.add( listitem ); 275 } 276 277 params.listEntries = listEntries; 278 } else { 279 params.remove( 'listEntries' ); 280 } 281 282 // If this field isnot a ontologyterm, we should remove the ontologies 283 if( params.type.toString() != 'ONTOLOGYTERM' ) { 284 params.remove( 'ontologies' ); 285 } 286 248 287 // Create the template field and add it to the template 249 288 def templateField = new TemplateField( params ); 250 289 if (templateField.save(flush: true)) { 251 290 252 def html = g.render( template: 'elements/ liFieldNotInUse', model: [templateField: templateField, fieldTypes: TemplateFieldType.list()] );291 def html = g.render( template: 'elements/available', model: [templateField: templateField, ontologies: Ontology.list(), fieldTypes: TemplateFieldType.list()] ); 253 292 def output = [ id: templateField.id, html: html ]; 254 293 render output as JSON; … … 289 328 } 290 329 } 291 templateField.properties = params 330 331 // If this field is type stringlist or ontology, we have to prepare the parameters 332 if( params.type.toString() == 'STRINGLIST' ) { 333 def listEntries = []; 334 params.listEntries.eachLine { 335 // Search whether a listitem with this name already exists. 336 // if it does, use that one to prevent pollution of the database 337 def name = it.trim(); 338 def listitem = TemplateFieldListItem.findByName( name );7 339 if( !listitem ) { 340 listitem = new TemplateFieldListItem( name: name ) 341 } 342 listEntries.add( listitem ); 343 } 344 345 params.listEntries = listEntries; 346 } else { 347 params.remove( 'listEntries' ); 348 } 349 350 // If this field is a ontologyterm, we add ontology objects 351 if( params.type.toString() == 'ONTOLOGYTERM' && params.ontologies ) { 352 params.ontologies = Ontology.getAll( params.ontologies.collect { Integer.parseInt( it ) } ); 353 } else { 354 params.remove( 'ontologies' ); 355 } 356 357 // Set all parameters 358 templateField.properties = params 292 359 if (!templateField.hasErrors() && templateField.save(flush: true)) { 293 def html = g.render( template: 'elements/ liField', model: [templateField: templateField, fieldTypes: TemplateFieldType.list()] );360 def html = g.render( template: 'elements/available', model: [templateField: templateField, ontologies: Ontology.list(), fieldTypes: TemplateFieldType.list()] ); 294 361 def output = [ id: templateField.id, html: html ]; 295 362 render output as JSON; … … 364 431 return; 365 432 } 433 434 // If the template is in use, only non-required fields can be added 435 if( template.inUse() && templateField.required ) { 436 response.status = 500; 437 render 'Only non-required fields can be added to templates that are in use.' 438 return; 439 } 440 441 // All field names within a template should be unique 442 if( template.fields.find { it.name.toLowerCase() == templateField.name.toLowerCase() } ) { 443 response.status = 500; 444 render 'This template already contains a field with name ' + templateField.name + '. Field names should be unique within a template.' 445 return; 446 } 447 366 448 if( !params.position || Integer.parseInt( params.position ) == -1) { 367 449 template.fields.add( templateField ) … … 369 451 template.fields.add( Integer.parseInt( params.position ), templateField ) 370 452 } 371 372 def html = g.render( template: 'elements/liFieldSelected', model: [templateField: templateField, template: template, fieldTypes: TemplateFieldType.list()] ); 453 template.save(flush:true); 454 455 def html = g.render( template: 'elements/selected', model: [templateField: templateField, template: template, ontologies: Ontology.list(), fieldTypes: TemplateFieldType.list()] ); 373 456 def output = [ id: templateField.id, html: html ]; 374 457 render output as JSON; 375 458 } 376 377 459 378 460 /** … … 411 493 } 412 494 495 // If the template is in use, field can not be removed 496 if( template.inUse() ) { 497 response.status = 500; 498 render 'No fields can be removed from templates that are in use.' 499 return; 500 } 501 413 502 // Delete the field from this template 414 503 def currentIndex = template.fields.indexOf( templateField ); 415 504 template.fields.remove( currentIndex ); 416 template.save( );417 418 419 def html = g.render( template: 'elements/ liField', model: [templateField: templateField, fieldTypes: TemplateFieldType.list()] );505 template.save(flush:true); 506 507 508 def html = g.render( template: 'elements/available', model: [templateField: templateField, ontologies: Ontology.list(), fieldTypes: TemplateFieldType.list()] ); 420 509 def output = [ id: templateField.id, html: html ]; 421 510 render output as JSON; … … 462 551 def moveField = template.fields.remove( currentIndex ); 463 552 template.fields.add( Integer.parseInt( params.position ), moveField ); 464 465 def html = g.render( template: 'elements/liFieldSelected', model: [templateField: templateField, template: template, fieldTypes: TemplateFieldType.list()] ); 553 template.save(flush:true); 554 555 def html = g.render( template: 'elements/selected', model: [templateField: templateField, template: template, fieldTypes: TemplateFieldType.list()] ); 466 556 def output = [ id: templateField.id, html: html ]; 467 557 render output as JSON; … … 485 575 render templateField.numUses(); 486 576 } 577 578 /** 579 * Adds a ontolgy based on the ID given 580 * 581 * @param ncboID 582 * @return JSON Ontology object 583 */ 584 def addOntologyById = { 585 def id = params.ncboID; 586 587 if( !id ) { 588 response.status = 500; 589 render 'No ID given' 590 return; 591 } 592 593 if( Ontology.findByNcboId( Integer.parseInt( id ) ) ) { 594 response.status = 500; 595 render 'This ontology was already added to the system'; 596 return; 597 } 598 599 def ontology = null; 600 601 try { 602 ontology = dbnp.data.Ontology.getBioPortalOntology( id ); 603 } catch( Exception e ) { 604 response.status = 500; 605 render e.getMessage(); 606 return; 607 } 608 609 if( !ontology ) { 610 response.status = 404; 611 render 'Ontology with ID ' + id + ' not found'; 612 return; 613 } 614 615 // Validate ontology 616 if (!ontology.validate()) { 617 response.status = 500; 618 render ontology.errors.join( '; ' ); 619 return; 620 } 621 622 // Save ontology and render the object as JSON 623 ontology.save(flush: true) 624 render ontology as JSON 625 } 626 627 /** 628 * Adds a ontolgy based on the ID given 629 * 630 * @param ncboID 631 * @return JSON Ontology object 632 */ 633 def addOntologyByTerm = { 634 def id = params.termID; 635 636 if( !id ) { 637 response.status = 500; 638 render 'No ID given' 639 return; 640 } 641 642 def ontology = null; 643 644 try { 645 ontology = dbnp.data.Ontology.getBioPortalOntologyByTerm( id ); 646 } catch( Exception e ) { 647 response.status = 500; 648 render e.getMessage(); 649 return; 650 } 651 652 if( !ontology ) { 653 response.status = 404; 654 render 'Ontology form term ' + id + ' not found'; 655 return; 656 } 657 658 // Validate ontology 659 if (!ontology.validate()) { 660 response.status = 500; 661 render ontology.errors.join( '; ' ); 662 return; 663 } 664 665 // Save ontology and render the object as JSON 666 ontology.save(flush: true) 667 render ontology as JSON 668 } 669 487 670 488 671 /**
Note: See TracChangeset
for help on using the changeset viewer.