Ignore:
Timestamp:
Mar 11, 2010, 2:23:53 PM (13 years ago)
Author:
duh
Message:
  • added limited auto parsing for DATE template entity values that are set as String instead of Date instances
  • general improvements and bugfixes
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/domain/dbnp/studycapturing/TemplateEntity.groovy

    r247 r257  
    9090                }
    9191                else {
    92                         TemplateField field = this.template.fields.find { it.name == fieldName} 
     92                        TemplateField field = this.template.fields.find { it.name == fieldName}
    9393                        if (field == null) {
    9494                                throw new NoSuchFieldException("Field ${fieldName} not found in class properties or template fields")
     
    9999                                        value = field.listEntries.find { it.name == value }
    100100                                }
     101
     102                                // handle string values for date fields
     103                                if (field.type == TemplateFieldType.DATE && value.class == String) {
     104                                        // a string was given, attempt to transform it into a date instance
     105                                        // and -for now- assume the dd/mm/yyyy format
     106                                        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,})/
     107                                        if (dateMatch.matches()) {
     108                                                // create limited 'autosensing' datetime parser
     109                                                // assume dd mm yyyy  or dd mm yy
     110                                                def parser = 'd' + dateMatch[0][2] + 'M' + dateMatch[0][4] + (((dateMatch[0][5] as int) > 999) ? 'yyyy' : 'yy')
     111
     112                                                // add time as well?
     113                                                if (dateMatch[0][7] != null) {
     114                                                        parser += dateMatch[0][6] + 'HH:mm'
     115                                                }
     116
     117                                                value = new Date().parse(parser, value)
     118                                        }
     119                                }
     120
    101121                                // Caution: this assumes that all template...Field Maps are already initialized
    102122                                // Otherwise, the results are pretty much unpredictable!
Note: See TracChangeset for help on using the changeset viewer.