Changeset 1257 for trunk/grails-app/domain
- Timestamp:
- Dec 9, 2010, 9:12:10 PM (10 years ago)
- Location:
- trunk/grails-app/domain/dbnp/studycapturing
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/domain/dbnp/studycapturing/Template.groovy
r1027 r1257 123 123 124 124 /** 125 * Check whether the contents of the other template and the current template are equal. 126 * For this check the name, description and owner don't matter. Also, the order of 127 * template fields doesn't matter 128 * 129 * @return true iff this template and the other template are used for the same entity and 130 * the template contain the same template fields 131 */ 132 public boolean contentEquals( Template otherTemplate ) { 133 if( otherTemplate == this ) 134 return true 135 136 if( otherTemplate == null ) 137 return false 138 139 if( otherTemplate.entity != this.entity ) 140 return false 141 142 // Check all template fields 143 if( otherTemplate.fields?.size() != this.fields?.size() ) 144 return false 145 146 if( otherTemplate.fields != null && this.fields != null ) { 147 for( def field in this.fields ) { 148 def fieldFound = false; 149 for( def otherField in otherTemplate.fields ) { 150 if( otherField.contentEquals( field ) ) { 151 fieldFound = true; 152 break 153 } 154 } 155 156 if( !fieldFound ) { 157 return false 158 } 159 } 160 } 161 162 // If all tests pass, the objects are content-equal 163 return true 164 } 165 166 /** 125 167 * Look up the type of a certain template subject field 126 168 * @param String fieldName The name of the template field … … 210 252 return results 211 253 } 254 255 /** 256 * Create a new template based on the parsed XML object. 257 * 258 * @see grails.converters.XML#parse(java.lang.String) 259 * @throws IllegalArgumentException 260 * @throws ClassNotFoundException 261 * 262 */ 263 public static parse(Object xmlObject, SecUser loggedInUser) { 264 def t = new Template(); 265 t.name = xmlObject?.name?.text() 266 t.description = xmlObject?.description?.text() 267 268 // Check whether a correct entity is given. The parseEntity method might 269 // throw a ClassNotFoundException, but that is OK, it should be thrown if the class 270 // doesn't exist. 271 def entity = TemplateEntity.parseEntity( xmlObject.entity?.text() ); 272 if( !entity ) { 273 throw new Exception( "Incorrect entity given" ); 274 } 275 276 t.entity = entity 277 278 // Set the owner to the currently logged in user 279 t.owner = loggedInUser 280 281 // Set all template fields 282 xmlObject.templateFields.templateField.each { 283 def field = TemplateField.parse( it, entity ); 284 285 // Check whether a similar field already exists. For that, we search in all 286 // template fields with the same name, in order to have as little comparisons 287 // as possible 288 for( def otherField in TemplateField.findAllByName( field.name ) ) { 289 if( field.contentEquals( otherField ) ) { 290 field = otherField; 291 break; 292 } 293 } 294 295 t.addToFields( field ); 296 } 297 298 return t 299 } 212 300 } -
trunk/grails-app/domain/dbnp/studycapturing/TemplateEntity.groovy
r1245 r1257 781 781 } 782 782 } 783 784 /** 785 * Returns a Class object given by the entityname, but only if it is a subclass of TemplateEntity 786 * 787 * @return A class object of the given entity, null if the entity is not a subclass of TemplateEntity 788 * @throws ClassNotFoundException 789 */ 790 static Class parseEntity( String entityName ) { 791 if( entityName == null ) 792 return null 793 794 // Find the templates 795 def entity = Class.forName(entityName, true, Thread.currentThread().getContextClassLoader()) 796 797 // succes, is entity an instance of TemplateEntity? 798 if (entity?.superclass =~ /TemplateEntity$/ || entity?.superclass?.superclass =~ /TemplateEntity$/) { 799 return entity; 800 } else { 801 return null; 802 } 803 804 } 783 805 } -
trunk/grails-app/domain/dbnp/studycapturing/TemplateField.groovy
r1213 r1257 366 366 } 367 367 368 369 /** 370 * Check whether the contents of the other templatefield and the current templatefield are equal. 371 * For this check the comments field doesn't matter. 372 * 373 * @return true iff this template field equals the other template field 374 * (the comments field may be different) 375 */ 376 public boolean contentEquals( Object otherObject ) { 377 if( !( otherObject instanceof TemplateField ) ) 378 return false 379 380 TemplateField otherField = (TemplateField) otherObject; 381 382 if( otherField == this ) 383 return true 384 385 if( otherField == null ) 386 return false 387 388 if( otherField.entity != this.entity ) { 389 return false 390 } 391 if( otherField.name != this.name ) { 392 return false 393 } 394 if( otherField.type != this.type ) { 395 return false 396 } 397 if( otherField.unit != this.unit ) { 398 return false 399 } 400 if( otherField.required != this.required ) { 401 return false 402 } 403 404 if( otherField.preferredIdentifier != this.preferredIdentifier ) { 405 return false 406 } 407 408 // Check whether the list entries are equal (except for the order) 409 def size1 = otherField.listEntries?.size() ?: 0 410 def size2 = this.listEntries?.size() ?: 0 411 if( size1 != size2 ) { 412 return false 413 } 414 415 if( otherField.listEntries != null && this.listEntries != null ) { 416 for( def entry in this.listEntries ) { 417 def entryFound = false; 418 for( def otherEntry in otherField.listEntries ) { 419 if( otherEntry.name == entry.name ) { 420 entryFound = true; 421 break 422 } 423 } 424 425 if( !entryFound ) { 426 return false 427 } 428 } 429 } 430 431 // Check whether the ontologies are equal (except for the order) 432 size1 = otherField.ontologies?.size() ?: 0 433 size2 = this.ontologies?.size() ?: 0 434 if( size1 != size2 ) { 435 return false 436 } 437 if( this.ontologies != null && otherField.ontologies != null ) { 438 for( def ontology in this.ontologies ) { 439 if( !otherField.ontologies.contains( ontology ) ) { 440 return false 441 } 442 } 443 } 444 445 // If all tests pass, the objects are content-equal 446 return true 447 } 448 449 /** 450 * Create a new template field based on the parsed XML object. 451 * 452 * @see grails.converters.XML#parse(java.lang.String) 453 * @throws IllegalArgumentException 454 */ 455 public static parse(Object xmlObject, Class entity) { 456 def t = new TemplateField(); 457 458 t.name = xmlObject?.name?.text() 459 t.unit = xmlObject?.unit?.text() == "" ? null : xmlObject?.unit?.text() 460 t.comment = xmlObject?.comment?.text() 461 t.required = xmlObject?.required?.text() == 'true' ? true : false 462 t.preferredIdentifier = xmlObject?.preferredIdentifier?.text() == 'true' ? true : false 463 464 t.entity = entity 465 466 t.type = TemplateFieldType.valueOf( xmlObject?.type?.text() ) 467 468 // Search for ontologies 469 xmlObject.ontologies?.ontology.each { 470 def ncboId = it.ncboId?.text(); 471 t.addToOntologies( Ontology.getOrCreateOntologyByNcboId( ncboId ) ); 472 } 473 474 // Search for list entries 475 xmlObject.listItems?.listItem.each { 476 t.addToListEntries( new TemplateFieldListItem( name: it.name?.text() ) ); 477 } 478 return t; 479 } 480 481 368 482 }
Note: See TracChangeset
for help on using the changeset viewer.