commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jaco van Niekerk <jac...@incubeta.com>
Subject BasicRowProcessor - Enum to INT mapping
Date Thu, 01 Sep 2005 14:16:24 GMT
Regarding DbUtils,

I've made a small mod to my copy of the DbUtils code. We often map int 
table fields to enum ordinals, where the enum if for example a status, 
type etc. Would it be possible to add this to the current 
BasicRowProcessor? It's a low impact feature, but would require us to 
re-compile the feature in with every new release of dbutils.

    /**
     * Calls the setter method on the target object for the given property.
     * If no setter method exists for the property, this method does 
nothing.
     * @param target The object to set the property on.
     * @param prop The property to set.
     * @param value The value to pass into the setter.
     * @throws SQLException if an error occurs setting the property.
     */
    private void callSetter(
        Object target,
        PropertyDescriptor prop,
        Object value)
        throws SQLException {

        Method setter = prop.getWriteMethod();

        if (setter == null) {
            return;
        }
       
        // ******************************************
        // new line to call new method getEnumValue()
        value = getEnumValue(prop, value);

        Class[] params = setter.getParameterTypes();
        try {
            // Don't call setter if the value object isn't the right type
            if (this.isCompatibleType(value, params[0])) {
                setter.invoke(target, new Object[] { value });
               
            }

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
           
            throw new SQLException(
                "Cannot set " + prop.getName() + ": " + e.getMessage());

        } catch (IllegalAccessException e) {
            e.printStackTrace();
           
            throw new SQLException(
                "Cannot set " + prop.getName() + ": " + e.getMessage());

        } catch (InvocationTargetException e) {
            e.printStackTrace();
           
            throw new SQLException(
                "Cannot set " + prop.getName() + ": " + e.getMessage());
        }
    }
   
    /**
     * If the bean property is an enum, and the data type an int, this 
method
     * return the enum in the integers index position.
     */
    private Object getEnumValue(PropertyDescriptor prop, Object value) {
       
        Class propType = prop.getPropertyType();
        Class superClass = prop.getPropertyType().getSuperclass();
       
        if (superClass == null) {
            // no super class, can't be an enum
            return value;
        }
       
        if (superClass.toString().indexOf("java.lang.Enum") == -1) {
            // not an enum
            return value;
        }
       
        Object[] enums = propType.getEnumConstants();
        if (enums != null) {
            if (value instanceof Integer) {
                try {
                    // replace the current Integer value with the 
relative Enum value
                    value = enums[(Integer) value];
                } catch (ArrayIndexOutOfBoundsException e) {
                    // illegal value
                }
            }
        }
       
        return value;
    }


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message