Monday, September 13, 2010

SDO: Wrapping Simple Types as DataObjects

  <xsd:element name="value" type="xsd:anyType" minOccurs="0"/>
The SDO specs specifies that an XSD element with type "anyType" is map to a DataObject SDO type. This means that you cannot assign simple types like String, Integer, Long, and Boolean among others into this element, because if you do so, you will receive a java.lang.ClassCastException in runtime.

To be able to do that therefore, you need to wrap those simple types into SDO DataObjects. Luckily the current org.eclipse.persistense_1.0.0.0_2-0.jar that ships with JDeveloper already contains simple type wrappers with type "org.eclipse.persistence.sdo.types.SDOWrapperType" as follows:

  • String - org.eclipse.persistence.sdo.StringWrapperImpl
  • Integer - org.eclipse.persistence.sdo.IntObjectWrapperImpl
  • Long - org.eclipse.persistence.sdo.LongObjectWrapperImpl
  • Double - org.eclipse.persistence.sdo.DoubleObjectWrapperImpl
  • BigDecimal - org.eclipse.persistence.sdo.DecimalWrapperImpl
  • Boolean - org.eclipse.persistence.sdo.BooleanObjectWrapperImpl

How do we wrap our simple types into SDO DataObjects?
Below is a utility class that I have devised to handle wrapping and unwrapping of simple types.

package soadev.sdo.utils;

import commonj.sdo.DataObject;
import commonj.sdo.Type;
import commonj.sdo.helper.DataFactory;

import java.math.BigDecimal;

import oracle.jbo.common.sdo.SDOTypeHandler;

import org.eclipse.persistence.sdo.types.SDOWrapperType;


/**@author Rommel Pino http://soadev.blogspot.com
 */
public class SimpleTypeWrapperUtils {

    public static DataObject wrap(String value) {
        return wrap(value, "java.lang.String");
    }

    public static DataObject wrap(Long value) {
        return wrap(value, "java.lang.Long");
    }

    public static DataObject wrap(Integer value) {
        return wrap(value, "java.lang.Integer");
    }

    public static DataObject wrap(BigDecimal value) {
        return wrap(value, "java.math.BigDecimal");
    }

    public static DataObject wrap(Short value) {
        return wrap(value, "java.lang.Short");
    }

    public static DataObject wrap(Boolean value) {
        return wrap(value, "java.lang.Boolean");
    }

    public static DataObject wrap(Byte value) {
        return wrap(value, "java.lang.Byte");
    }
    
    public static DataObject wrap(Double value) {
        return wrap(value, "java.lang.Double");
    }

    public static DataObject wrap(Float value) {
        return wrap(value, "java.lang.Float");
    }


    private static DataObject wrap(Object value,
                                   String fullyQualifiedSimleTypeClassName) {
        Type type =
            SDOTypeHandler.getSDOType(fullyQualifiedSimleTypeClassName);
        DataObject wrapper = DataFactory.INSTANCE.create(type);
        wrapper.set("value", value);
        return wrapper;
    }

    public static Object unwrap(DataObject wrapper) {
        if (wrapper.getType() instanceof SDOWrapperType) {
            return wrapper.get("value");
        }
        //if not wrapper return the same object
        return wrapper;
    }

}

In subsequent post, I will describe how to implement some sort of SDO HashMap.

No comments:

Post a Comment