FileDocCategorySizeDatePackage
ObjectTypeConverter.javaAPI DocGlassfish v2 API13759Tue May 22 16:54:46 BST 2007oracle.toplink.essentials.mappings.converters

ObjectTypeConverter

public class ObjectTypeConverter extends Object implements Converter
Purpose: Object type converter is used to match a fixed number of database data values to Java object value. It can be used when the values on the database and in the Java differ. To create an object type converter, simply specify the set of conversion value pairs. A default value and one-way conversion are also supported for legacy data situations.
author
James Sutherland
since
Toplink 10

Fields Summary
protected DatabaseMapping
mapping
protected transient Map
fieldToAttributeValues
protected Map
attributeToFieldValues
protected transient Object
defaultAttributeValue
protected transient Class
fieldClassification
protected transient String
fieldClassificationName
Constructors Summary
public ObjectTypeConverter()
PUBLIC: Default constructor.

        this.attributeToFieldValues = new HashMap(10);
        this.fieldToAttributeValues = new HashMap(10);
    
public ObjectTypeConverter(DatabaseMapping mapping)
PUBLIC: Default constructor.

        this();
        this.mapping = mapping;
    
Methods Summary
public voidaddConversionValue(java.lang.Object fieldValue, java.lang.Object attributeValue)
PUBLIC: A type conversion value is a two-way mapping from the database to the object. The database value will be substituted for the object value when read, and the object value will be substituted for database value when written. Note that each field/attribute value must have one and only one attribute/field value to maintain a two-way mapping.

        if (fieldValue == null) {
            fieldValue = Helper.getNullWrapper();
        }

        if (attributeValue == null) {
            attributeValue = Helper.getNullWrapper();
        }

        getFieldToAttributeValues().put(fieldValue, attributeValue);
        getAttributeToFieldValues().put(attributeValue, fieldValue);
    
public voidaddToAttributeOnlyConversionValue(java.lang.Object fieldValue, java.lang.Object attributeValue)
PUBLIC: An attribute only conversion value is a one-way mapping from the database to the object. This can be used if multiple database values are desired to be mapped to the same object value. Note that when written only the default value will be used for the attribute, not this value.

        if (fieldValue == null) {
            fieldValue = Helper.getNullWrapper();
        }

        if (attributeValue == null) {
            attributeValue = Helper.getNullWrapper();
        }

        getFieldToAttributeValues().put(fieldValue, attributeValue);
    
public voidconvertClassNamesToClasses(java.lang.ClassLoader classLoader)
INTERNAL: Convert all the class-name-based settings in this converter to actual class-based settings. This method is used when converting a project that has been built with class names to a project with classes.

param
classLoader

        // Does nothing right now but was implemented since EnumTypeConverter
        // is dependent on this method but we need to avoid JDK 1.5 
        // dependencies. AbstractDirectMapping will call this method.
    
public java.lang.ObjectconvertDataValueToObjectValue(java.lang.Object fieldValue, oracle.toplink.essentials.sessions.Session session)
INTERNAL: Returns the corresponding attribute value for the specified field value.

        Object attributeValue = null;

        if (fieldValue == null) {
            attributeValue = getFieldToAttributeValues().get(Helper.getNullWrapper());
        } else {
            try {
                fieldValue = ((AbstractSession)session).getDatasourcePlatform().getConversionManager().convertObject(fieldValue, getFieldClassification());
            } catch (ConversionException e) {
                throw ConversionException.couldNotBeConverted(mapping, mapping.getDescriptor(), e);
            }

            attributeValue = getFieldToAttributeValues().get(fieldValue);
            if (attributeValue == null) {
                if (getDefaultAttributeValue() != null) {
                    attributeValue = getDefaultAttributeValue();
                } else {
                    // CR#3779
                    throw DescriptorException.noFieldValueConversionToAttributeValueProvided(fieldValue, getMapping().getField(), getMapping());
                }
            }
        }
        return attributeValue;
    
public java.lang.ObjectconvertObjectValueToDataValue(java.lang.Object attributeValue, oracle.toplink.essentials.sessions.Session session)
INTERNAL: Convert to the data value.

        Object fieldValue;
        if (attributeValue == null) {
            fieldValue = getAttributeToFieldValues().get(Helper.getNullWrapper());
        } else {
            fieldValue = getAttributeToFieldValues().get(attributeValue);
            if (fieldValue == null) {
                throw DescriptorException.noAttributeValueConversionToFieldValueProvided(attributeValue, getMapping());
            }
        }
        return fieldValue;
    
public java.util.MapgetAttributeToFieldValues()
INTERNAL: Get the attribute to field mapping.

        return attributeToFieldValues;
    
public java.lang.ObjectgetDefaultAttributeValue()
PUBLIC: The default value can be used if the database can possibly store additional values then those that have been mapped. Any value retreived from the database that is not mapped will be substitued for the default value.

        return defaultAttributeValue;
    
public java.lang.ClassgetFieldClassification()
INTERNAL: Get the type of the field value to allow conversion from the database.

        return fieldClassification;
    
public java.lang.ClassgetFieldClassification(oracle.toplink.essentials.internal.helper.DatabaseField fieldToClassify)
INTERNAL: Return the classifiction for the field contained in the mapping. This is used to convert the row value to a consistent java value. By default this is null which means unknown.

        return getFieldClassification();
    
public java.lang.StringgetFieldClassificationName()

        if ((fieldClassificationName == null) && (fieldClassification != null)) {
            fieldClassificationName = fieldClassification.getName();
        }
        return fieldClassificationName;
    
public java.util.MapgetFieldToAttributeValues()
INTERNAL: Get the field to attribute mapping.

        return fieldToAttributeValues;
    
protected oracle.toplink.essentials.mappings.DatabaseMappinggetMapping()
INTERNAL: Return the mapping.

        return mapping;
    
public voidinitialize(oracle.toplink.essentials.mappings.DatabaseMapping mapping, oracle.toplink.essentials.sessions.Session session)
INTERNAL: Set the mapping.

        this.mapping = mapping;
        initializeFieldClassification(session);
    
public voidinitializeFieldClassification(oracle.toplink.essentials.sessions.Session session)
INTERNAL: Set the field classification through searching the fields hashtable.

        if (getFieldToAttributeValues().isEmpty()) {
            return;
        }
        Class type = null;
        Iterator fieldValuesEnum = getFieldToAttributeValues().keySet().iterator();
        while (fieldValuesEnum.hasNext() && (type == null)) {
            Object value = fieldValuesEnum.next();
            if (value != Helper.getNullWrapper()) {
                type = value.getClass();
            }
        }

        setFieldClassification(type);
        // CR#... Mapping must also have the field classification.
        if (getMapping().isDirectToFieldMapping()) {
            AbstractDirectMapping directMapping = (AbstractDirectMapping)getMapping();

            // Allow user to specify field type to override computed value. (i.e. blob, nchar)
            if (directMapping.getFieldClassification() == null) {
                directMapping.setFieldClassification(type);
            }
        }
    
public booleanisMutable()
INTERNAL: If the converter converts the value to a non-atomic value, i.e. a value that can have its' parts changed without being replaced, then it must return false, serialization can be non-atomic.

        return false;
    
public booleanisObjectTypeMapping()
INTERNAL:

        return true;
    
public voidmapBooleans()
PUBLIC: This is a very specific protocol which maps fieldValues "T" and "F" to true and false respectively.

        addConversionValue("F", new Boolean(false));
        addConversionValue("T", new Boolean(true));
    
public voidmapGenders()
PUBLIC: This is a very specific protocol which maps fieldValues "F" and "M" to "Female" and "Male" respectively.

        addConversionValue("F", "Female");
        addConversionValue("M", "Male");
    
public voidmapResponses()
PUBLIC: This is a very specific protocol which maps fieldValues "Y" and "N" to "Yes" and "No" respectively.

        addConversionValue("Y", "Yes");
        addConversionValue("N", "No");
    
public voidsetAttributeToFieldValues(java.util.Map attributeToFieldValues)
INTERNAL: Set the attribute to field mapping.

        this.attributeToFieldValues = attributeToFieldValues;
    
public voidsetDefaultAttributeValue(java.lang.Object defaultAttributeValue)
PUBLIC: The default value can be used if the database can possibly store additional values then those that have been mapped. Any value retreived from the database that is not mapped will be substitued for the default value.

        this.defaultAttributeValue = defaultAttributeValue;
    
public voidsetFieldClassification(java.lang.Class fieldClassification)
INTERNAL: Set the type of the field value to allow conversion from the database.

        this.fieldClassification = fieldClassification;
    
public voidsetFieldClassificationName(java.lang.String fieldClassificationName)

        this.fieldClassificationName = fieldClassificationName;
    
public voidsetFieldToAttributeValueAssociations(java.util.Vector fieldToAttributeValueAssociations)
INTERNAL: Set a collection of the field to attribute value associations.

        setFieldToAttributeValues(new Hashtable(fieldToAttributeValueAssociations.size() + 1));
        setAttributeToFieldValues(new Hashtable(fieldToAttributeValueAssociations.size() + 1));
        for (Enumeration associationsEnum = fieldToAttributeValueAssociations.elements();
                 associationsEnum.hasMoreElements();) {
            Association association = (Association)associationsEnum.nextElement();
            addConversionValue(association.getKey(), association.getValue());
        }
    
public voidsetFieldToAttributeValues(java.util.Map fieldToAttributeValues)
INTERNAL: Set the field to attribute mapping.

        this.fieldToAttributeValues = fieldToAttributeValues;
    
protected voidsetMapping(oracle.toplink.essentials.mappings.DatabaseMapping mapping)
INTERNAL: Set the mapping.

        this.mapping = mapping;