Changeset 335
- Timestamp:
- Apr 9, 2010, 3:11:35 PM (13 years ago)
- Location:
- trunk/grails-app
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/dbnp/studycapturing/WizardController.groovy
r332 r335 591 591 if (params.template) { 592 592 params.template.fields.each() { field -> 593 def value = params.get(field.escapedName()) 594 595 if (value) { 596 flow.study.setFieldValue(field.name, value) 597 } 593 flow.study.setFieldValue(field.name, params.get(field.escapedName())) 598 594 } 599 595 } … … 712 708 // iterate through template fields 713 709 templateFields.each() { subjectField -> 714 def value = params.get('subject_' + subjectId + '_' + subjectField.escapedName()) 715 716 if (value) { 717 flow.subjects[ subjectId ].setFieldValue(subjectField.name, value) 718 } 710 flow.subjects[ subjectId ].setFieldValue( 711 subjectField.name, 712 params.get( 'subject_' + subjectId + '_' + subjectField.escapedName() ) 713 ) 719 714 } 720 715 -
trunk/grails-app/domain/dbnp/studycapturing/TemplateEntity.groovy
r333 r335 41 41 42 42 /** 43 * Con traints43 * Constraints 44 44 * 45 45 * All template fields have their own custom validator. Note that there 46 * currently is a lot of code repitition. Ideally we don't want this, but 47 * unfortunately due to scope issues we cannot re-use the code. So make sure 48 * to replicate any changes to all pieces of logic! 46 * currently is a lot of code repetition. Ideally we don't want this, but 47 * unfortunately due to scope issues we cannot re-use the code. So make 48 * sure to replicate any changes to all pieces of logic! Only commented 49 * the first occurrence of the logic, please refer to the templateStringFields 50 * validator if you require information about the validation logic... 49 51 */ 50 52 static constraints = { 51 53 template(nullable: true, blank: true) 52 54 templateStringFields(validator: { fields, obj, errors -> 55 // note that we only use 'fields' and 'errors', 'obj' is 56 // merely here because it's the way the closure is called 57 // by the validator... 58 59 // define a boolean 53 60 def error = false 54 61 55 62 // iterate through fields 56 63 fields.each { key, value -> 57 // check if value is of proper type 58 if (value.class != String) { 59 // it's not, try to cast it to the proper type 60 try { 64 // check if the value is of proper type 65 if ( value && value.class != String ) { 66 // it's of some other type 67 try { 68 // try to cast it to the proper type 61 69 fields[key] = (value as String) 62 70 } catch (Exception e) { 63 // could not typecast properly, value is of inproper type 71 // could not typecast properly, value is of improper type 72 // add error message 64 73 error = true 65 74 errors.rejectValue( … … 72 81 } 73 82 } 83 84 // got an error, or not? 74 85 return (!error) 75 86 }) … … 77 88 def error = false 78 89 fields.each { key, value -> 79 if ( value.class != String) {90 if ( value && value.class != String ) { 80 91 try { 81 92 fields[key] = (value as String) … … 96 107 def error = false 97 108 fields.each { key, value -> 98 if ( value.class != TemplateFieldListItem) {109 if ( value && value.class != TemplateFieldListItem ) { 99 110 try { 100 111 fields[key] = (value as TemplateFieldListItem) … … 115 126 def error = false 116 127 fields.each { key, value -> 117 if (value .class != Integer) {128 if (value && value.class != Integer ) { 118 129 try { 119 130 fields[key] = (value as Integer) … … 134 145 def error = false 135 146 fields.each { key, value -> 136 if ( value.class != Float) {147 if ( value && value.class != Float ) { 137 148 try { 138 149 fields[key] = (value as Float) … … 153 164 def error = false 154 165 fields.each { key, value -> 155 if ( value.class != Double) {166 if ( value && value.class != Double ) { 156 167 try { 157 168 fields[key] = (value as Double) … … 172 183 def error = false 173 184 fields.each { key, value -> 174 if ( value.class != Date) {185 if ( value && value.class != Date ) { 175 186 try { 176 187 fields[key] = (value as Date) … … 191 202 def error = false 192 203 fields.each { key, value -> 193 if ( value.class != Term) {204 if ( value && value.class != Term ) { 194 205 try { 195 206 fields[key] = (value as Term) … … 209 220 } 210 221 222 /** 223 * Get the proper templateFields Map for a specific field type 224 * @param TemplateFieldType 225 * @return pointer 226 * @visibility private 227 * @throws NoSuchFieldException 228 */ 211 229 private Map getStore(TemplateFieldType fieldType) { 212 230 switch(fieldType) { … … 248 266 * @param fieldName The name of the template or entity field 249 267 * @param value The value to be set, this should align with the (template) field type, but there are some convenience setters 250 *251 268 */ 252 269 def setFieldValue(String fieldName, value) { 253 254 270 // First, search if there is an entity property with the given name, and if so, set that 255 271 if (this.properties.containsKey(fieldName)) { 256 272 this[fieldName] = value 257 } 258 // If not the found, then it is a template field, so check if there is a template 259 else if (template == null) { 273 } else if (template == null) { 274 // not the found, then it is a template field, so check if there is a template 260 275 throw new NoSuchFieldException("Field ${fieldName} not found in class properties: template not set") 261 } 262 // If there is a template, check the template fields 263 else { 276 } else { 277 // there is a template, check the template fields 264 278 // Find the target template field, if not found, throw an error 265 279 TemplateField field = this.template.fields.find { it.name == fieldName } 280 266 281 if (field == null) { 267 throw new NoSuchFieldException("Field ${fieldName} not found in class properties or template fields")268 }269 // Set the value of the found template field270 else {282 // no such field 283 throw new NoSuchFieldException("Field ${fieldName} not found in class template fields") 284 } else { 285 // Set the value of the found template field 271 286 // Convenience setter for template string list fields: find TemplateFieldListItem by name 272 287 if (field.type == TemplateFieldType.STRINGLIST && value.class == String) { 288 // Kees insensitive pattern matching ;) 273 289 value = field.listEntries.find { it.name ==~ /(?i)($value)/ } 274 290 } … … 296 312 // Caution: this assumes that all template...Field Maps are already initialized (as is done now above as [:]) 297 313 // If that is ever changed, the results are pretty much unpredictable (random Java object pointers?)! 298 getStore(field.type)[fieldName] = value 314 def store = getStore(field.type) 315 if (!value && store[ fieldName ]) { 316 println "removing " + super.class + " template field: " + fieldName 317 318 // remove the item from the Map (if present) 319 store.remove( fieldName ) 320 } else { 321 println "setting " + super.class + " template field: " + fieldName + " ([" + value.toString() + "] of type [" + value.class + "])" 322 323 // set value 324 store[ fieldName ] = value 325 } 299 326 return this 300 327 } 301 328 } 302 329 } 303 304 330 305 331 /**
Note: See TracChangeset
for help on using the changeset viewer.