Changeset 487 for trunk/grails-app/domain
- Timestamp:
- May 27, 2010, 4:03:31 PM (13 years ago)
- Location:
- trunk/grails-app/domain/dbnp/studycapturing
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/domain/dbnp/studycapturing/TemplateEntity.groovy
r453 r487 21 21 Map templateDoubleFields = [:] 22 22 Map templateDateFields = [:] 23 Map templateRelTimeFields = [:] // Contains relative times in seconds 23 24 Map templateTermFields = [:] 24 25 … … 32 33 templateDateFields: Date, 33 34 templateTermFields: Term, 35 templateRelTimeFields: long, 34 36 systemFields: TemplateField 35 37 ] … … 200 202 return (!error) 201 203 }) 204 templateRelTimeFields(validator: { fields, obj, errors -> 205 def error = false 206 fields.each { key, value -> 207 if ( value && value.class != long ) { 208 try { 209 fields[key] = (value as long) 210 } catch (Exception e) { 211 error = true 212 errors.rejectValue( 213 'templateRelTimeFields', 214 'templateEntity.typeMismatch.reltime', 215 [key, value.class] as Object[], 216 'Property {0} must be of type long and is currently of type {1}' 217 ) 218 } 219 } 220 } 221 return (!error) 222 }) 202 223 templateTermFields(validator: { fields, obj, errors -> 203 224 def error = false … … 228 249 * @throws NoSuchFieldException 229 250 */ 230 p rivateMap getStore(TemplateFieldType fieldType) {251 public Map getStore(TemplateFieldType fieldType) { 231 252 switch(fieldType) { 232 253 case TemplateFieldType.STRING: … … 240 261 case TemplateFieldType.DATE: 241 262 return templateDateFields 263 case TemplateFieldType.RELTIME: 264 return templateRelTimeFields 242 265 case TemplateFieldType.FLOAT: 243 266 return templateFloatFields … … 353 376 } 354 377 378 // Magic setter for relative times: handle string values for relTime fields 379 // 380 // The relative time may be set as a string, using the following format 381 // 382 // #w #d #h #m #s 383 // 384 // Where w = weeks, d = days, h = hours, m = minutes, s = seconds 385 // 386 // The spaces between the values are optional. Every timespan 387 // (w, d, h, m, s) must appear at most once. You can also omit 388 // timespans if needed or use a different order. 389 // Other characters are disregarded, allthough results may not 390 // always be as expected. 391 // 392 // If an incorrect format is used, which can't be parsed 393 // an IllegalArgumentException is thrown. 394 // 395 // An empty span is treated as zero seconds. 396 // 397 // Examples: 398 // --------- 399 // 5d 3h 20m // 5 days, 3 hours and 20 minutes 400 // 6h 2d // 2 days, 6 hours 401 // 10m 200s // 13 minutes, 20 seconds (200s == 3m + 20s) 402 // 5w4h15m // 5 weeks, 4 hours, 15 minutes 403 // 404 // 16x14w10d // Incorrect. 16x is disregarded, so the 405 // // result is 15 weeks, 3 days 406 // 13days // Incorrect: days should be d, but this is 407 // // parsed as 13d, 0 seconds 408 // 409 if (field.type == TemplateFieldType.RELTIME && value.class == String) { 410 // A string was given, attempt to transform it into a timespan 411 412 // An empty string should be parsed as 0 413 if( value.trim() == "" ) { 414 value = 0; 415 } else { 416 // Find all parts that contain numbers with 417 // a character w, d, h, m or s after it 418 def periodMatch = value =~ /([0-9]+)([wdhms])/ 419 if (periodMatch.size() > 0 ) { 420 def seconds = 0L; 421 422 // Now check if every part contains data for 423 // the time interval 424 periodMatch.each { 425 def partValue 426 427 println it 428 429 if( it[1].isLong() ) { 430 partValue = Long.parseLong( it[1] ); 431 } else { 432 partValue = 0; 433 } 434 435 switch( it[ 2 ] ) { 436 case 'w': 437 seconds += 7L * 24 * 60 * 60 * partValue; 438 break; 439 case 'd': 440 seconds += 24L * 60 * 60 * partValue; 441 break; 442 case 'h': 443 seconds += 60L * 60 * partValue; 444 break; 445 case 'm': 446 seconds += 60L * partValue; 447 break; 448 case 's': 449 seconds += partValue; 450 break; 451 default: 452 adf.error.warn( 'Parsing relative time: ' + it[0] + it[1] + ' is not understood and disregarded' ); 453 break; 454 } 455 } 456 457 // Continue with the computed value 458 value = seconds; 459 } else { 460 throw new IllegalArgumentException( "String " + value + " cannot be parsed as a relative time. Use format #w #d #h #m #s." ); 461 } 462 } 463 } 464 355 465 // Magic setter for ontology terms: handle string values 356 466 if (field.type == TemplateFieldType.ONTOLOGYTERM && value && value.class == String) { … … 384 494 // If that is ever changed, the results are pretty much unpredictable (random Java object pointers?)! 385 495 def store = getStore(field.type) 386 if (!value && store[fieldName]) { 387 println ".unsetting [" + ((super) ? super.class : '??') + "] template field: [" + fieldName + "]" 388 389 // remove the item from the Map (if present) 390 store.remove(fieldName) 391 } else if (value) { 392 println ".setting [" + ((super) ? super.class : '??') + "] template field: [" + fieldName + "] ([" + value.toString() + "] of type [" + value.class + "])" 393 394 // set value 395 store[fieldName] = value 396 } 496 497 // If some value is entered (or 0), then save the value 498 // otherwise, it should not be present in the store, so 499 // it is unset if it is. 500 if ( value || value == 0 ) { 501 println ".setting [" + ((super) ? super.class : '??') + "] template field: [" + fieldName + "] ([" + value.toString() + "] of type [" + value.class + "])" 502 503 // set value 504 store[fieldName] = value 505 } else if ( store[fieldName] ) { 506 println ".unsetting [" + ((super) ? super.class : '??') + "] template field: [" + fieldName + "]" 507 508 // remove the item from the Map (if present) 509 store.remove(fieldName) 510 } 397 511 } 398 512 … … 420 534 /** 421 535 * Return all fields defined in the underlying template and the built-in 422 * domain fields of this entity536 * domain fields of this entity 423 537 */ 424 538 def List<TemplateField> giveFields() { -
trunk/grails-app/domain/dbnp/studycapturing/TemplateFieldType.groovy
r482 r487 17 17 ONTOLOGYTERM('Ontology Reference'), 18 18 DATE('Date'), 19 RELDATE('Relative date') // relative date, e.g. days since start of study19 RELTIME('Relative time') // relative date, e.g. days since start of study 20 20 21 21 String name … … 26 26 27 27 static list() { 28 [STRING, TEXT, INTEGER, FLOAT, DOUBLE, STRINGLIST, ONTOLOGYTERM, DATE, REL DATE]28 [STRING, TEXT, INTEGER, FLOAT, DOUBLE, STRINGLIST, ONTOLOGYTERM, DATE, RELTIME] 29 29 } 30 30 … … 45 45 case DATE: 46 46 return null 47 case RELDATE:48 return null47 case RELTIME: 48 return null 49 49 default: 50 50 throw new NoSuchFieldException("Field type ${fieldType} not recognized")
Note: See TracChangeset
for help on using the changeset viewer.