Search in sources :

Example 1 with OSimpleObject

use of org.odata4j.core.OSimpleObject in project teiid by teiid.

the class ODataExecutionFactory method retrieveValue.

/**
 * @param value
 * @param expectedType
 * @return
 */
public Object retrieveValue(Object value, Class<?> expectedType) {
    if (value == null) {
        return null;
    }
    if (value instanceof LocalDateTime) {
        DateTime dateTime = ((LocalDateTime) value).toDateTime(DateTimeZone.forTimeZone(this.timeZone));
        return new java.sql.Timestamp(dateTime.getMillis());
    }
    if (value instanceof LocalTime) {
        return new java.sql.Timestamp(((LocalTime) value).toDateTimeToday().getMillis());
    }
    if (value instanceof UnsignedByte) {
        return Short.valueOf(((UnsignedByte) value).shortValue());
    }
    if (expectedType.isArray() && value instanceof OCollection<?>) {
        ArrayList<Object> result = new ArrayList<Object>();
        OCollection<?> arrayValues = (OCollection<?>) value;
        Iterator<?> it = arrayValues.iterator();
        while (it.hasNext()) {
            OSimpleObject<?> item = (OSimpleObject<?>) it.next();
            result.add(retrieveValue(item.getValue(), expectedType.getComponentType()));
        }
        Object target = java.lang.reflect.Array.newInstance(expectedType.getComponentType(), result.size());
        System.arraycopy(result.toArray(), 0, target, 0, result.size());
        value = target;
    }
    return value;
}
Also used : LocalDateTime(org.joda.time.LocalDateTime) LocalTime(org.joda.time.LocalTime) OSimpleObject(org.odata4j.core.OSimpleObject) ArrayList(java.util.ArrayList) UnsignedByte(org.odata4j.core.UnsignedByte) DateTime(org.joda.time.DateTime) LocalDateTime(org.joda.time.LocalDateTime) OCollection(org.odata4j.core.OCollection) OSimpleObject(org.odata4j.core.OSimpleObject)

Example 2 with OSimpleObject

use of org.odata4j.core.OSimpleObject in project teiid by teiid.

the class ODataProcedureExecution method execute.

@Override
public void execute() throws TranslatorException {
    String URI = this.visitor.buildURL();
    Schema schema = visitor.getProcedure().getParent();
    EdmDataServices edm = new TeiidEdmMetadata(schema.getName(), ODataEntitySchemaBuilder.buildMetadata(schema));
    if (this.visitor.hasCollectionReturn()) {
        if (this.visitor.isReturnComplexType()) {
            // complex return
            this.response = executeWithComplexReturn(this.visitor.getMethod(), URI, null, this.visitor.getReturnEntityTypeName(), edm, null, Status.OK, Status.NO_CONTENT);
        } else {
            // entity type return
            this.response = executeWithReturnEntity(this.visitor.getMethod(), URI, null, this.visitor.getTable().getName(), edm, null, Status.OK, Status.NO_CONTENT);
        }
        if (this.response != null && this.response.hasError()) {
            throw this.response.getError();
        }
    } else {
        try {
            BinaryWSProcedureExecution execution = executeDirect(this.visitor.getMethod(), URI, null, getDefaultHeaders());
            if (execution.getResponseCode() != Status.OK.getStatusCode()) {
                throw buildError(execution);
            }
            Blob blob = (Blob) execution.getOutputParameterValues().get(0);
            ODataVersion version = getODataVersion(execution);
            // if the procedure is not void
            if (this.visitor.getReturnType() != null) {
                FormatParser<? extends OObject> parser = FormatParserFactory.getParser(OSimpleObject.class, FormatType.ATOM, new Settings(version, edm, this.visitor.getProcedure().getName(), // entitykey
                null, // isResponse
                true, ODataTypeManager.odataType(this.visitor.getReturnType())));
                OSimpleObject object = (OSimpleObject) parser.parse(new InputStreamReader(blob.getBinaryStream()));
                this.returnValue = this.translator.retrieveValue(object.getValue(), this.visitor.getReturnTypeClass());
            }
        } catch (SQLException e) {
            throw new TranslatorException(e);
        }
    }
}
Also used : Blob(java.sql.Blob) OSimpleObject(org.odata4j.core.OSimpleObject) InputStreamReader(java.io.InputStreamReader) SQLException(java.sql.SQLException) Schema(org.teiid.metadata.Schema) BinaryWSProcedureExecution(org.teiid.translator.ws.BinaryWSProcedureExecution) ODataVersion(org.odata4j.core.ODataVersion) EdmDataServices(org.odata4j.edm.EdmDataServices) TranslatorException(org.teiid.translator.TranslatorException) Settings(org.odata4j.format.Settings)

Aggregations

OSimpleObject (org.odata4j.core.OSimpleObject)2 InputStreamReader (java.io.InputStreamReader)1 Blob (java.sql.Blob)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 DateTime (org.joda.time.DateTime)1 LocalDateTime (org.joda.time.LocalDateTime)1 LocalTime (org.joda.time.LocalTime)1 OCollection (org.odata4j.core.OCollection)1 ODataVersion (org.odata4j.core.ODataVersion)1 UnsignedByte (org.odata4j.core.UnsignedByte)1 EdmDataServices (org.odata4j.edm.EdmDataServices)1 Settings (org.odata4j.format.Settings)1 Schema (org.teiid.metadata.Schema)1 TranslatorException (org.teiid.translator.TranslatorException)1 BinaryWSProcedureExecution (org.teiid.translator.ws.BinaryWSProcedureExecution)1