Changeset 406 for trunk/grails-app/domain
- Timestamp:
- May 11, 2010, 3:35:21 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/domain/dbnp/studycapturing/TemplateEntity.groovy
r397 r406 252 252 253 253 /** 254 * Find a field domain or template field by its name and return its description 255 * @param fieldsCollection the set of fields to search in, usually something like this.giveFields() 256 * @param fieldName The name of the domain or template field 257 * @return the TemplateField description of the field 258 * @throws NoSuchFieldException If the field is not found or the field type is not supported 259 */ 260 private static def getField(Collection fieldsCollection, String fieldName) { 261 // escape the fieldName for easy matching 262 // (such escaped names are commonly used 263 // in the HTTP forms of this application) 264 String escapedLowerCaseFieldName = fieldName.toLowerCase().replaceAll("([^a-z0-9])","_") 265 266 // Find the target template field, if not found, throw an error 267 def field = fieldsCollection.find { it.name.toLowerCase().replaceAll("([^a-z0-9])", "_") == escapedLowerCaseFieldName } 268 269 if (field) { 270 return field 271 } 272 else { 273 throw new NoSuchFieldException("Field ${fieldName} not recognized") 274 } 275 } 276 277 /** 254 278 * Find a domain or template field by its name and return its value for this entity 255 279 * @param fieldName The name of the domain or template field … … 258 282 */ 259 283 def getFieldValue(String fieldName) { 260 // escape the fieldName for easy matching 261 // (such escaped names are commonly used 262 // in the HTTP forms of this application) 263 def escapedLowerCaseFieldName = fieldName.toLowerCase().replaceAll("([^a-z0-9])","_") 264 265 // Find the target template field, if not found, throw an error 266 TemplateField field = this.giveFields().find { it.name.toLowerCase().replaceAll("([^a-z0-9])", "_") == escapedLowerCaseFieldName } 267 268 // field found? 269 if (field == null) { 270 // no such field 271 throw new NoSuchFieldException("Field ${fieldName} not recognized") 272 } else { 284 TemplateField field = getField(this.giveFields(),fieldName) 285 if (isDomainField(field)) { 286 return this[field.name] 287 } 288 else { 273 289 return getStore(field.type)[fieldName] 274 290 } … … 280 296 * @return true if the given field exists and false otherwise 281 297 */ 282 def fieldExists(String fieldName) { 283 // escape the fieldName for easy matching 284 // (such escaped names are commonly used 285 // in the HTTP forms of this application) 286 def escapedLowerCaseFieldName = fieldName.toLowerCase().replaceAll("([^a-z0-9])","_") 287 288 // check if this domain class has got this property 289 if (this.properties.containsKey(fieldName)) { 290 // domain class contains this property 298 boolean fieldExists(String fieldName) { 299 // getField should throw a NoSuchFieldException if the field does not exist 300 try { 301 TemplateField field = getField(this.giveFields(),fieldName) 302 } 303 // so return false in that case 304 catch(NoSuchFieldException e) { 305 return false 306 } 307 // otherwise, return true (but double check if field really is not null) 308 if (field) { 291 309 return true 292 } else if (template == null) {293 // no, and we haven't got a template set either310 } 311 else { 294 312 return false 295 } else {296 // the domain class doesn't have this property but297 // it has a template defined. Check the template298 // fields to see if such a template field exists299 TemplateField field = this.template.fields.find { it.name.toLowerCase().replaceAll("([^a-z0-9])","_") == escapedLowerCaseFieldName }300 301 // does the template field exist?302 if (field == null) {303 // no such template field304 return false305 } else {306 // found!307 return true308 }309 313 } 310 314 } … … 316 320 */ 317 321 def setFieldValue(String fieldName, value) { 318 // escape the fieldName for easy matching 319 // (such escaped names are commonly used 320 // in the HTTP forms of this application) 321 def escapedLowerCaseFieldName = fieldName.toLowerCase().replaceAll("([^a-z0-9])","_") 322 323 // Find the target template field, if not found, throw an error 324 TemplateField field = this.giveFields().find { it.name.toLowerCase().replaceAll("([^a-z0-9])", "_") == escapedLowerCaseFieldName } 325 326 // field found? 327 if (field == null) { 328 // no such field 329 throw new NoSuchFieldException("Field ${fieldName} not found in class template fields") 330 } else { 331 // Set the value of the found template field 332 // Convenience setter for template string list fields: find TemplateFieldListItem by name 333 if (field.type == TemplateFieldType.STRINGLIST && value && value.class == String) { 334 // Kees insensitive pattern matching ;) 335 def escapedLowerCaseValue = value.toLowerCase().replaceAll("([^a-z0-9])", "_") 336 value = field.listEntries.find { 337 it.name.toLowerCase().replaceAll("([^a-z0-9])", "_") == escapedLowerCaseValue 338 } 339 } 340 341 // Convenience setter for dates: handle string values for date fields 342 if (field.type == TemplateFieldType.DATE && value && value.class == String) { 343 // a string was given, attempt to transform it into a date instance 344 // and -for now- assume the dd/mm/yyyy format 345 def dateMatch = value =~ /^([0-9]{1,})([^0-9]{1,})([0-9]{1,})([^0-9]{1,})([0-9]{1,})((([^0-9]{1,})([0-9]{1,2}):([0-9]{1,2})){0,})/ 346 if (dateMatch.matches()) { 347 // create limited 'autosensing' datetime parser 348 // assume dd mm yyyy or dd mm yy 349 def parser = 'd' + dateMatch[0][2] + 'M' + dateMatch[0][4] + (((dateMatch[0][5] as int) > 999) ? 'yyyy' : 'yy') 350 351 // add time as well? 352 if (dateMatch[0][7] != null) { 353 parser += dateMatch[0][6] + 'HH:mm' 354 } 355 356 value = new Date().parse(parser, value) 357 } 358 } 359 360 // Set the field value 322 323 TemplateField field = getField(this.giveFields(),fieldName) 324 325 // Convenience setter for template string list fields: find TemplateFieldListItem by name 326 if (field.type == TemplateFieldType.STRINGLIST && value && value.class == String) { 327 value = getField(field.listEntries,value).name 328 println value 329 } 330 331 // Convenience setter for dates: handle string values for date fields 332 if (field.type == TemplateFieldType.DATE && value && value.class == String) { 333 // a string was given, attempt to transform it into a date instance 334 // and -for now- assume the dd/mm/yyyy format 335 def dateMatch = value =~ /^([0-9]{1,})([^0-9]{1,})([0-9]{1,})([^0-9]{1,})([0-9]{1,})((([^0-9]{1,})([0-9]{1,2}):([0-9]{1,2})){0,})/ 336 if (dateMatch.matches()) { 337 // create limited 'autosensing' datetime parser 338 // assume dd mm yyyy or dd mm yy 339 def parser = 'd' + dateMatch[0][2] + 'M' + dateMatch[0][4] + (((dateMatch[0][5] as int) > 999) ? 'yyyy' : 'yy') 340 341 // add time as well? 342 if (dateMatch[0][7] != null) { 343 parser += dateMatch[0][6] + 'HH:mm' 344 } 345 346 value = new Date().parse(parser, value) 347 } 348 } 349 350 // Set the field value 351 if (isDomainField(field)) { 352 this[field.name] = value 353 } 354 else { 355 // Caution: this assumes that all template...Field Maps are already initialized (as is done now above as [:]) 356 // If that is ever changed, the results are pretty much unpredictable (random Java object pointers?)! 361 357 def store = getStore(field.type) 362 358 if (!value && store[fieldName]) { … … 372 368 } 373 369 } 370 374 371 return this 372 } 373 374 boolean isDomainField(TemplateField field) { 375 return this.giveDomainFields()*.name.contains(field.name) 376 } 377 378 boolean isDomainField(String fieldName) { 379 return this.giveDomainFields()*.name.contains(fieldName) 375 380 } 376 381
Note: See TracChangeset
for help on using the changeset viewer.