Search in sources :

Example 1 with EdmDate

use of org.apache.olingo.commons.core.edm.primitivetype.EdmDate in project teiid by teiid.

the class EntityCollectionResponse method getPropertyValueInternal.

private static Object getPropertyValueInternal(SingletonPrimitiveType expectedType, boolean isArray, Object value) throws TransformationException, SQLException, IOException {
    Class<?> sourceType = DataTypeManager.getRuntimeType(value.getClass());
    if (sourceType.isAssignableFrom(expectedType.getDefaultType())) {
        return value;
    }
    if (expectedType instanceof EdmDate && sourceType == Date.class) {
        return value;
    } else if (expectedType instanceof EdmDateTimeOffset && sourceType == Timestamp.class) {
        return value;
    } else if (expectedType instanceof EdmTimeOfDay && sourceType == Time.class) {
        return value;
    } else if (expectedType instanceof EdmBinary) {
        // there could be memory implications here, should have been modeled as EdmStream
        // $NON-NLS-1$
        LogManager.logDetail(LogConstants.CTX_ODATA, "Possible OOM when inlining the stream based values");
        if (sourceType == ClobType.class) {
            return ClobType.getString((Clob) value).getBytes();
        }
        if (sourceType == SQLXML.class) {
            return ((SQLXML) value).getString().getBytes();
        }
        if (sourceType == BlobType.class) {
            return ObjectConverterUtil.convertToByteArray(((Blob) value).getBinaryStream());
        }
        if (value instanceof Serializable) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(value);
            oos.close();
            bos.close();
            return bos.toByteArray();
        }
    }
    Class<?> targetType = DataTypeManager.getDataTypeClass(ODataTypeManager.teiidType(expectedType, isArray));
    if (sourceType != targetType) {
        Transform t = DataTypeManager.getTransform(sourceType, targetType);
        value = t != null ? t.transform(value, targetType) : value;
    }
    return value;
}
Also used : Blob(java.sql.Blob) Serializable(java.io.Serializable) EdmBinary(org.apache.olingo.commons.core.edm.primitivetype.EdmBinary) EdmDateTimeOffset(org.apache.olingo.commons.core.edm.primitivetype.EdmDateTimeOffset) Time(java.sql.Time) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) EdmDate(org.apache.olingo.commons.core.edm.primitivetype.EdmDate) EdmDate(org.apache.olingo.commons.core.edm.primitivetype.EdmDate) Date(java.sql.Date) ClobType(org.teiid.core.types.ClobType) SQLXML(java.sql.SQLXML) BlobType(org.teiid.core.types.BlobType) Transform(org.teiid.core.types.Transform) EdmTimeOfDay(org.apache.olingo.commons.core.edm.primitivetype.EdmTimeOfDay)

Example 2 with EdmDate

use of org.apache.olingo.commons.core.edm.primitivetype.EdmDate in project teiid by teiid.

the class ODataTypeManager method parseLiteral.

public static Object parseLiteral(String odataType, String value) throws TeiidException {
    EdmPrimitiveType primitiveType = EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.valueOf(odataType.substring(4)));
    int maxLength = DataTypeManager.MAX_STRING_LENGTH;
    if (primitiveType instanceof EdmBinary || primitiveType instanceof EdmStream) {
        maxLength = DataTypeManager.MAX_VARBINARY_BYTES;
    }
    int precision = 4;
    int scale = 3;
    if (primitiveType instanceof EdmDecimal) {
        precision = 38;
        scale = 9;
    }
    Class<?> expectedClass = primitiveType.getDefaultType();
    try {
        if (EdmString.getInstance().equals(primitiveType)) {
            value = EdmString.getInstance().fromUriLiteral(value);
        }
        Object converted = primitiveType.valueOfString(value, false, maxLength, precision, scale, true, expectedClass);
        if (primitiveType instanceof EdmTimeOfDay) {
            Calendar ts = (Calendar) converted;
            return new Time(ts.getTimeInMillis());
        } else if (primitiveType instanceof EdmDate) {
            Calendar ts = (Calendar) converted;
            return new Date(ts.getTimeInMillis());
        }
        return converted;
    } catch (EdmPrimitiveTypeException e) {
        throw new TeiidException(e);
    }
}
Also used : EdmPrimitiveType(org.apache.olingo.commons.api.edm.EdmPrimitiveType) EdmBinary(org.apache.olingo.commons.core.edm.primitivetype.EdmBinary) EdmStream(org.apache.olingo.commons.core.edm.primitivetype.EdmStream) Calendar(java.util.Calendar) Time(java.sql.Time) EdmPrimitiveTypeException(org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException) EdmDate(org.apache.olingo.commons.core.edm.primitivetype.EdmDate) EdmDate(org.apache.olingo.commons.core.edm.primitivetype.EdmDate) Date(java.sql.Date) EdmDecimal(org.apache.olingo.commons.core.edm.primitivetype.EdmDecimal) TeiidException(org.teiid.core.TeiidException) EdmTimeOfDay(org.apache.olingo.commons.core.edm.primitivetype.EdmTimeOfDay)

Aggregations

Date (java.sql.Date)2 Time (java.sql.Time)2 EdmBinary (org.apache.olingo.commons.core.edm.primitivetype.EdmBinary)2 EdmDate (org.apache.olingo.commons.core.edm.primitivetype.EdmDate)2 EdmTimeOfDay (org.apache.olingo.commons.core.edm.primitivetype.EdmTimeOfDay)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Serializable (java.io.Serializable)1 Blob (java.sql.Blob)1 SQLXML (java.sql.SQLXML)1 Calendar (java.util.Calendar)1 EdmPrimitiveType (org.apache.olingo.commons.api.edm.EdmPrimitiveType)1 EdmPrimitiveTypeException (org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException)1 EdmDateTimeOffset (org.apache.olingo.commons.core.edm.primitivetype.EdmDateTimeOffset)1 EdmDecimal (org.apache.olingo.commons.core.edm.primitivetype.EdmDecimal)1 EdmStream (org.apache.olingo.commons.core.edm.primitivetype.EdmStream)1 TeiidException (org.teiid.core.TeiidException)1 BlobType (org.teiid.core.types.BlobType)1 ClobType (org.teiid.core.types.ClobType)1 Transform (org.teiid.core.types.Transform)1