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

EnumTypeConverter

public class EnumTypeConverter extends ObjectTypeConverter
Purpose: Object type converter is used to match a fixed number of database data values to a Java enum 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
Guy Pelletier
since
Toplink 10.1.4RI

Fields Summary
private Class
m_enumClass
private String
m_enumClassName
Constructors Summary
public EnumTypeConverter(DatabaseMapping mapping, Class enumClass, boolean useOrdinalValues)
PUBLIC: Creating an enum converter this way will create the conversion values for you using ordinal or name values.

        super(mapping);
        m_enumClassName = enumClass.getName();
        
        EnumSet theEnums = EnumSet.allOf(enumClass);
        Iterator<Enum> i = theEnums.iterator();
        
        while (i.hasNext()) {
            Enum theEnum = i.next();
            
            if (useOrdinalValues) {
                addConversionValue(theEnum.ordinal(), theEnum.name());
            } else {
                addConversionValue(theEnum.name(), theEnum.name());
            }
        }
    
public EnumTypeConverter(DatabaseMapping mapping, String enumClassName)
PUBLIC: Creating an enum converter this way expects that you will provide the conversion values separately.

        super(mapping);
        m_enumClassName = enumClassName;
    
Methods Summary
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

        try {
            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
                try {
                    m_enumClass = (Class)AccessController.doPrivileged(new PrivilegedClassForName(m_enumClassName, true, classLoader));
                } catch (PrivilegedActionException exception) {
                    throw ValidationException.classNotFoundWhileConvertingClassNames(m_enumClassName, exception.getException());
                }
            } else {
                m_enumClass = oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.getClassForName(m_enumClassName, true, classLoader);
            }
        } catch (ClassNotFoundException exception){
            throw ValidationException.classNotFoundWhileConvertingClassNames(m_enumClassName, exception);
        }
    
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. Wraps the super method to return an Enum type from the string conversion.

        Object obj = super.convertDataValueToObjectValue(fieldValue, session);
        
        if (fieldValue == null) {
            return obj;
        } else {
            return Enum.valueOf(m_enumClass, (String) obj);
        }
    
public java.lang.ObjectconvertObjectValueToDataValue(java.lang.Object attributeValue, oracle.toplink.essentials.sessions.Session session)
INTERNAL: Convert Enum object to the data value. Internal enums are stored as strings (names) so this method wraps the super method in that if breaks down the enum to a string name before converting it.

        if (attributeValue == null) {
            return super.convertObjectValueToDataValue(null, session);
        } else {
            return super.convertObjectValueToDataValue((Enum.class.cast(attributeValue)).name(), session);
        }