Search in sources :

Example 1 with DataHelper

use of commonj.sdo.helper.DataHelper in project eclipselink by eclipse-ee4j.

the class XPathEngine method set.

/**
 * Set a property's value.
 *
 * @param lastProperty      the property to queries.
 * @param lastDataObject    the DataObject, owner of the queried property
 * @param numInLastProperty the index number in the value list of the above property
 * @param value             the value to be set as the target property's value
 * @param convertValue     boolean used for set if we should convert the value
 */
private void set(Property lastProperty, DataObject lastDataObject, int numInLastProperty, Object value, boolean convertValue) {
    if (numInLastProperty == -1) {
        if (lastDataObject != null) {
            if (convertValue) {
                DataHelper dataHelper = ((SDODataObject) lastDataObject).getType().getHelperContext().getDataHelper();
                value = dataHelper.convert(lastProperty, value);
            }
            lastDataObject.set(lastProperty, value);
        } else {
            throw new IllegalArgumentException("lastDataObject is null");
        }
    } else {
        // case like set("a/b.1", List) will cause a list be added into a existed list
        List objects = lastDataObject.getList(lastProperty);
        if (convertValue) {
            DataHelper dataHelper = ((SDODataObject) lastDataObject).getType().getHelperContext().getDataHelper();
            value = dataHelper.convert(lastProperty.getType(), value);
        }
        Sequence seq = lastDataObject.getSequence();
        if (seq != null) {
            seq.setValue(numInLastProperty, value);
        } else {
            objects.set(numInLastProperty, value);
        }
    }
}
Also used : List(java.util.List) DataHelper(commonj.sdo.helper.DataHelper) Sequence(commonj.sdo.Sequence) SDODataObject(org.eclipse.persistence.sdo.SDODataObject)

Example 2 with DataHelper

use of commonj.sdo.helper.DataHelper in project eclipselink by eclipse-ee4j.

the class SDODataObject method getDate.

@Override
public Date getDate(Property property) {
    if (null == property) {
        throw SDOException.cannotPerformOperationOnNullArgument("getDate");
    }
    if (property.getType().equals(SDOConstants.SDO_STRING)) {
        DataHelper dHelper = aHelperContext.getDataHelper();
        String dateString = (String) get(property);
        return dHelper.toDate(dateString);
    }
    Date propertyDateValue = (Date) convertObjectToValue(property, Date.class);
    return propertyDateValue;
}
Also used : SDODataHelper(org.eclipse.persistence.sdo.helper.SDODataHelper) DataHelper(commonj.sdo.helper.DataHelper) Date(java.util.Date)

Example 3 with DataHelper

use of commonj.sdo.helper.DataHelper in project eclipselink by eclipse-ee4j.

the class XPathEngine method convertObjectToValueByPath.

// dataobject a's property a has value dataobject b, dataobject b's property b has value dataobject c,
// dataobject c's property c has value boolean,
/**
 * access the wanted values through path and convert it into required java class.
 * If conversion is not supported, exception is thrown.
 *
 * @param path      string representation of accessing path
 * @param cls       the java class that accessed value is to be converted to
 * @param caller    the DataObject that pass the path in
 * @return          values to be accessed
 * @throws ClassCastException
 */
public Object convertObjectToValueByPath(String path, Class<?> cls, DataObject caller) throws ClassCastException {
    if (null == path || XMLConstants.EMPTY_STRING.equals(path)) {
        throw new ClassCastException("Attempting null value conversion.");
    }
    try {
        int lastSlashIndex = path.lastIndexOf('/');
        SDODataObject lastDataObject;
        String lastPropertyName;
        Property lastProperty;
        int numInLastProperty = -1;
        // to do: if "/" or ".." lastDataObject = container or root
        if (-1 < lastSlashIndex) {
            // case 1 "a/b/c"
            String frag = path.substring(lastSlashIndex + 1);
            int indexOfDot = frag.lastIndexOf('.');
            int indexOfOpenBracket = frag.lastIndexOf('[');
            int indexOfCloseBracket = frag.lastIndexOf(']');
            numInLastProperty = getNumberInFrag(frag, indexOfDot, indexOfOpenBracket, indexOfCloseBracket);
            // getPropertyNameFromFragment(path.substring(lastSlashIndex + 1));// get last property name on path for case 1
            lastPropertyName = getPropertyNameInFrag(frag, numInLastProperty, indexOfDot, indexOfOpenBracket);
            // get last dataobject on path
            lastDataObject = (SDODataObject) caller.getDataObject(path.substring(0, lastSlashIndex));
            if (lastDataObject == null) {
                return null;
            }
            // get property of this dataobject
            lastProperty = lastDataObject.getInstanceProperty(lastPropertyName);
        } else {
            // case 2 "a"
            // to do: call eextractPositionAndPropertyName() here
            String frag = path;
            int indexOfDot = frag.lastIndexOf('.');
            int indexOfOpenBracket = frag.lastIndexOf('[');
            int indexOfCloseBracket = frag.lastIndexOf(']');
            numInLastProperty = getNumberInFrag(frag, indexOfDot, indexOfOpenBracket, indexOfCloseBracket);
            // getPropertyNameFromFragment(path);// get last property name on path for case 1
            lastPropertyName = getPropertyNameInFrag(frag, numInLastProperty, indexOfDot, indexOfOpenBracket);
            lastDataObject = (SDODataObject) caller;
            if (lastDataObject == null) {
                return null;
            }
            // get property of this dataobject
            lastProperty = caller.getInstanceProperty(lastPropertyName);
        }
        if ((lastProperty != null) && (cls == Date.class) && lastProperty.getType().equals(SDOConstants.SDO_STRING)) {
            // in case getDate, for string property, use DataHelper
            DataHelper dHelper = HelperProvider.getDefaultContext().getDataHelper();
            String dateString;
            if (numInLastProperty == -1) {
                dateString = (String) lastDataObject.get(lastProperty);
            } else {
                dateString = (String) (lastDataObject.getList(lastProperty)).get(numInLastProperty);
            }
            return dHelper.toDate(dateString);
        }
        return lastDataObject.convertObjectToValue(lastProperty, numInLastProperty, cls);
    } catch (IllegalArgumentException e) {
        // according to exception table 1st row, when get(Property) and get(Index) throw IllegalArgument, get(String) return null
        throw new ClassCastException("Conversion is not supported.");
    // return null;
    }
}
Also used : DataHelper(commonj.sdo.helper.DataHelper) Property(commonj.sdo.Property) Date(java.util.Date) SDODataObject(org.eclipse.persistence.sdo.SDODataObject)

Aggregations

DataHelper (commonj.sdo.helper.DataHelper)3 Date (java.util.Date)2 SDODataObject (org.eclipse.persistence.sdo.SDODataObject)2 Property (commonj.sdo.Property)1 Sequence (commonj.sdo.Sequence)1 List (java.util.List)1 SDODataHelper (org.eclipse.persistence.sdo.helper.SDODataHelper)1