Changeset 239 for trunk/grails-app/domain/dbnp
- Timestamp:
- Mar 5, 2010, 4:46:29 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/domain/dbnp/studycapturing/TemplateEntity.groovy
r238 r239 2 2 3 3 import dbnp.data.Term 4 import org.codehaus.groovy.runtime.NullObject5 4 6 5 /** … … 91 90 } 92 91 else { 93 if (templateStringFields && templateStringFields.containsKey(fieldName) && value.class == String) { 94 this.templateStringFields[fieldName] = value 95 } 96 if (templateStringFields && templateStringListFields.containsKey(fieldName) && value.class == TemplateFieldListItem) { 97 // TODO: check if item really belongs to the list under fieldName 98 this.templateStringListFields[fieldName] = value 99 } 100 if (templateTextFields.containsKey(fieldName) && value.class == String) { 101 this.templateTextFields[fieldName] = value 102 } 103 else if (templateIntegerFields && templateIntegerFields.containsKey(fieldName) && value.class == Integer) { 104 this.templateIntegerFields[fieldName] = value 105 } 106 else if (templateFloatFields && templateFloatFields.containsKey(fieldName) && value.class == Float) { 107 this.templateFloatFields[fieldName] = value 108 } 109 else if (templateDoubleFields && templateDoubleFields.containsKey(fieldName) && value.class == Double) { 110 this.templateDoubleFields[fieldName] = value 111 } 112 else if (templateDateFields && templateDateFields.containsKey(fieldName) && value.class == Date) { 113 this.templateDateFields[fieldName] = value 114 } 115 else if (templateTermFields && templateTermFields.containsKey(fieldName) && value.class == Term) { 116 this.templateTermFields[fieldName] = value 92 TemplateField field = this.template.fields.find { it.name == fieldName} 93 if (field == null) { 94 throw new NoSuchFieldException("Field ${fieldName} not found in class properties or template fields") 117 95 } 118 96 else { 119 throw new NoSuchFieldException("Field ${fieldName} not found in class properties or template fields") 97 if (field.type == TemplateFieldType.STRINGLIST && value.class == String) { 98 // Convenience setter: find template item by name 99 value = field.listEntries.find { it.name == value } 100 } 101 // Caution: this assumes that all template...Field Maps are already initialized 102 // Otherwise, the results are pretty much unpredictable! 103 getStore(field.type)[fieldName] = value 104 return this 120 105 } 121 106 } … … 128 113 } 129 114 130 @Override131 void setTemplate(Template newTemplate) {132 115 133 // Contrary to expectation, this method does not cause an infinite loop but calls the super method 134 // whereas super.setTemplate(newTemplate) leads to errors concerning NullObject values 135 this.template = newTemplate 136 137 // TODO: initialize all template fields with the necessary keys and null values 138 139 //println "Setting template " + newTemplate 140 if (template != null) { 141 142 // Loop over all template field types and 143 // That is inpossible in Java if the Maps are not yet set because the pointer is evaluated here 144 145 // So this code is quite dangerous stuff: 146 147 /*TemplateFieldType.list().each() { fieldType -> 148 Set<TemplateFieldType> fields = template.getFieldsByType(fieldType) 149 println fieldType 150 println "before: " + getStore(fieldType) 151 initFields(getStore(fieldType),fieldType.getDefaultValue(),fields) 152 println "after: " + getStore(fieldType) 153 154 } 155 println "SF:" + templateStringListFields*/ 156 157 158 /*def type = TemplateFieldType.STRING 159 //<T extends Annotation> T = type.getTypeClass().class 160 def stringFields = template.getFieldsByType(TemplateFieldType.STRING) 161 if (stringFields.size() > 0) { 162 templateStringFields = new HashMap<String,String>(stringFields.size()) 163 stringFields.each { 164 templateStringFields.put(it.name,TemplateFieldType.STRING.getDefaultValue()) 165 } 166 println templateStringFields 167 } 168 stringFields = template.getFieldsByType(TemplateFieldType.INTEGER) 169 println stringFields*.name 170 if (stringFields.size() > 0) { 171 templateIntegerFields = new HashMap<String,Integer>(stringFields.size()) 172 stringFields.each { 173 templateIntegerFields.put(it.name,TemplateFieldType.INTEGER.getDefaultValue()) 174 } 175 }*/ 176 } 177 } 178 179 // Private function to initialize template field collections 180 private <T> void initFields(Map fieldCollection, T defaultValue, Set<TemplateFieldType> fields) { 181 if (fields.size() > 0) { 182 fieldCollection = new HashMap<String,T>(fields.size()); 183 fields.each { 184 fieldCollection.put(it.name,defaultValue); 185 } 186 println fieldCollection 187 } 188 } 189 116 // See revision 237 for ideas about initializing the different templateField Maps 117 // with tailored Maps that already contain the neccessary keys 190 118 /** 191 119 * Convenience method. Returns all unique templates used within a collection of TemplateEntities.
Note: See TracChangeset
for help on using the changeset viewer.