- Timestamp:
- Jun 1, 2010, 2:45:21 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/domain/dbnp/studycapturing/TemplateEntity.groovy
r506 r507 22 22 Map templateDateFields = [:] 23 23 Map templateRelTimeFields = [:] // Contains relative times in seconds 24 Map templateFileFields = [:] // Contains filenames 24 25 Map templateTermFields = [:] 25 26 … … 34 35 templateTermFields: Term, 35 36 templateRelTimeFields: long, 37 templateFileFields: String, 36 38 systemFields: TemplateField 37 39 ] … … 42 44 templateTextFields type: 'text' 43 45 } 46 47 def fileService 44 48 45 49 /** … … 238 242 } 239 243 } 244 return (!error) 245 }) 246 templateFileFields(validator: { fields, obj, errors -> 247 // note that we only use 'fields' and 'errors', 'obj' is 248 // merely here because it's the way the closure is called 249 // by the validator... 250 251 // define a boolean 252 def error = false 253 254 // iterate through fields 255 fields.each { key, value -> 256 // check if the value is of proper type 257 if (value && value.class != String) { 258 // it's of some other type 259 try { 260 // try to cast it to the proper type 261 fields[key] = (value as String) 262 263 // Find the file on the system 264 // if it does not exist, the filename can 265 // not be entered 266 267 } catch (Exception e) { 268 // could not typecast properly, value is of improper type 269 // add error message 270 error = true 271 errors.rejectValue( 272 'templateFileFields', 273 'templateEntity.typeMismatch.string', 274 [key, value.class] as Object[], 275 'Property {0} must be of type String and is currently of type {1}' 276 ) 277 } 278 } 279 } 280 281 // got an error, or not? 240 282 return (!error) 241 283 }) … … 263 305 case TemplateFieldType.RELTIME: 264 306 return templateRelTimeFields 307 case TemplateFieldType.FILE: 308 return templateFileFields 265 309 case TemplateFieldType.FLOAT: 266 310 return templateFloatFields … … 383 427 } 384 428 429 // Sometimes the fileService is not created yet 430 if( !fileService ) { 431 fileService = new FileService(); 432 } 433 434 // Magic setter for files: handle values for file fields 435 // 436 // If NULL is given, the field value is emptied and the old file is removed 437 // If an empty string is given, the field value is kept as was 438 // If a file is given, it is moved to the right directory. Old files are deleted. If 439 // the file does not exist, the field is kept 440 // If a string is given, it is supposed to be a file in the upload directory. If 441 // it is different from the old one, the old one is deleted. If the file does not 442 // exist, the old one is kept. 443 if (field.type == TemplateFieldType.FILE) { 444 def currentFile = getFieldValue( field.name ); 445 446 if( value == null ) { 447 // If NULL is given, the field value is emptied and the old file is removed 448 value = ""; 449 if( currentFile ) 450 { 451 fileService.delete( currentFile ) 452 } 453 } else if( value.class == File ) { 454 // a file was given. Attempt to move it to the upload directory, and 455 // afterwards, store the filename. If the file doesn't exist 456 // or can't be moved, "" is returned 457 value = fileService.moveFileToUploadDir( value ); 458 459 if( value ) { 460 if( currentFile ) 461 { 462 fileService.delete( currentFile ) 463 } 464 } else { 465 value = currentFile; 466 } 467 } else if ( value == "" ) { 468 value = currentFile; 469 } else { 470 if( value != currentFile ) { 471 if( fileService.fileExists( value ) ) { 472 // When a FILE field is filled, and a new file is set 473 // the existing file should be deleted 474 if( currentFile ) 475 { 476 fileService.delete( currentFile ) 477 } 478 } else { 479 // If the file does not exist, the field is kept 480 value = currentFile; 481 } 482 } 483 } 484 } 485 486 385 487 // Magic setter for ontology terms: handle string values 386 488 if (field.type == TemplateFieldType.ONTOLOGYTERM && value && value.class == String) {
Note: See TracChangeset
for help on using the changeset viewer.