1 21 package oracle.toplink.essentials.mappings.converters; 23 24 import java.util.*; 25 import oracle.toplink.essentials.mappings.*; 26 import oracle.toplink.essentials.exceptions.*; 27 import oracle.toplink.essentials.internal.helper.*; 28 import oracle.toplink.essentials.mappings.foundation.AbstractDirectMapping; 29 import oracle.toplink.essentials.sessions.*; 30 import oracle.toplink.essentials.internal.sessions.AbstractSession; 31 32 41 public class ObjectTypeConverter implements Converter { 42 protected DatabaseMapping mapping; 43 protected transient Map fieldToAttributeValues; 44 protected Map attributeToFieldValues; 45 protected transient Object defaultAttributeValue; 46 protected transient Class fieldClassification; 47 protected transient String fieldClassificationName; 48 49 53 public ObjectTypeConverter() { 54 this.attributeToFieldValues = new HashMap(10); 55 this.fieldToAttributeValues = new HashMap(10); 56 } 57 58 62 public ObjectTypeConverter(DatabaseMapping mapping) { 63 this(); 64 this.mapping = mapping; 65 } 66 67 74 public void addConversionValue(Object fieldValue, Object attributeValue) { 75 if (fieldValue == null) { 76 fieldValue = Helper.getNullWrapper(); 77 } 78 79 if (attributeValue == null) { 80 attributeValue = Helper.getNullWrapper(); 81 } 82 83 getFieldToAttributeValues().put(fieldValue, attributeValue); 84 getAttributeToFieldValues().put(attributeValue, fieldValue); 85 } 86 87 93 public void addToAttributeOnlyConversionValue(Object fieldValue, Object attributeValue) { 94 if (fieldValue == null) { 95 fieldValue = Helper.getNullWrapper(); 96 } 97 98 if (attributeValue == null) { 99 attributeValue = Helper.getNullWrapper(); 100 } 101 102 getFieldToAttributeValues().put(fieldValue, attributeValue); 103 } 104 105 109 public Map getAttributeToFieldValues() { 110 return attributeToFieldValues; 111 } 112 113 120 public void convertClassNamesToClasses(ClassLoader classLoader){ 121 } 125 126 130 public Object convertDataValueToObjectValue(Object fieldValue, Session session) { 131 Object attributeValue = null; 132 133 if (fieldValue == null) { 134 attributeValue = getFieldToAttributeValues().get(Helper.getNullWrapper()); 135 } else { 136 try { 137 fieldValue = ((AbstractSession)session).getDatasourcePlatform().getConversionManager().convertObject(fieldValue, getFieldClassification()); 138 } catch (ConversionException e) { 139 throw ConversionException.couldNotBeConverted(mapping, mapping.getDescriptor(), e); 140 } 141 142 attributeValue = getFieldToAttributeValues().get(fieldValue); 143 if (attributeValue == null) { 144 if (getDefaultAttributeValue() != null) { 145 attributeValue = getDefaultAttributeValue(); 146 } else { 147 throw DescriptorException.noFieldValueConversionToAttributeValueProvided(fieldValue, getMapping().getField(), getMapping()); 149 } 150 } 151 } 152 return attributeValue; 153 } 154 155 160 public Object getDefaultAttributeValue() { 161 return defaultAttributeValue; 162 } 163 164 168 protected DatabaseMapping getMapping() { 169 return mapping; 170 } 171 172 176 protected void setMapping(DatabaseMapping mapping) { 177 this.mapping = mapping; 178 } 179 180 184 public Class getFieldClassification() { 185 return fieldClassification; 186 } 187 188 public String getFieldClassificationName() { 189 if ((fieldClassificationName == null) && (fieldClassification != null)) { 190 fieldClassificationName = fieldClassification.getName(); 191 } 192 return fieldClassificationName; 193 } 194 195 201 public Class getFieldClassification(DatabaseField fieldToClassify) { 202 return getFieldClassification(); 203 } 204 205 209 public Map getFieldToAttributeValues() { 210 return fieldToAttributeValues; 211 } 212 213 217 public Object convertObjectValueToDataValue(Object attributeValue, Session session) { 218 Object fieldValue; 219 if (attributeValue == null) { 220 fieldValue = getAttributeToFieldValues().get(Helper.getNullWrapper()); 221 } else { 222 fieldValue = getAttributeToFieldValues().get(attributeValue); 223 if (fieldValue == null) { 224 throw DescriptorException.noAttributeValueConversionToFieldValueProvided(attributeValue, getMapping()); 225 } 226 } 227 return fieldValue; 228 } 229 230 233 public boolean isObjectTypeMapping() { 234 return true; 235 } 236 237 242 public void mapBooleans() { 243 addConversionValue("F", new Boolean (false)); 244 addConversionValue("T", new Boolean (true)); 245 } 246 247 252 public void mapGenders() { 253 addConversionValue("F", "Female"); 254 addConversionValue("M", "Male"); 255 } 256 257 262 public void mapResponses() { 263 addConversionValue("Y", "Yes"); 264 addConversionValue("N", "No"); 265 } 266 267 271 public void initializeFieldClassification(Session session) throws DescriptorException { 272 if (getFieldToAttributeValues().isEmpty()) { 273 return; 274 } 275 Class type = null; 276 Iterator fieldValuesEnum = getFieldToAttributeValues().keySet().iterator(); 277 while (fieldValuesEnum.hasNext() && (type == null)) { 278 Object value = fieldValuesEnum.next(); 279 if (value != Helper.getNullWrapper()) { 280 type = value.getClass(); 281 } 282 } 283 284 setFieldClassification(type); 285 if (getMapping().isDirectToFieldMapping()) { 287 AbstractDirectMapping directMapping = (AbstractDirectMapping)getMapping(); 288 289 if (directMapping.getFieldClassification() == null) { 291 directMapping.setFieldClassification(type); 292 } 293 } 294 } 295 296 300 public void initialize(DatabaseMapping mapping, Session session) { 301 this.mapping = mapping; 302 initializeFieldClassification(session); 303 } 304 305 309 public void setAttributeToFieldValues(Map attributeToFieldValues) { 310 this.attributeToFieldValues = attributeToFieldValues; 311 } 312 313 318 public void setDefaultAttributeValue(Object defaultAttributeValue) { 319 this.defaultAttributeValue = defaultAttributeValue; 320 } 321 322 326 public void setFieldClassification(Class fieldClassification) { 327 this.fieldClassification = fieldClassification; 328 } 329 330 public void setFieldClassificationName(String fieldClassificationName) { 331 this.fieldClassificationName = fieldClassificationName; 332 } 333 334 338 public void setFieldToAttributeValueAssociations(Vector fieldToAttributeValueAssociations) { 339 setFieldToAttributeValues(new Hashtable(fieldToAttributeValueAssociations.size() + 1)); 340 setAttributeToFieldValues(new Hashtable(fieldToAttributeValueAssociations.size() + 1)); 341 for (Enumeration associationsEnum = fieldToAttributeValueAssociations.elements(); 342 associationsEnum.hasMoreElements();) { 343 Association association = (Association)associationsEnum.nextElement(); 344 addConversionValue(association.getKey(), association.getValue()); 345 } 346 } 347 348 352 public void setFieldToAttributeValues(Map fieldToAttributeValues) { 353 this.fieldToAttributeValues = fieldToAttributeValues; 354 } 355 356 362 public boolean isMutable() { 363 return false; 364 } 365 } 366 | Popular Tags |