Search in sources :

Example 6 with Property

use of org.apache.olingo.commons.api.data.Property in project teiid by teiid.

the class ODataProcedureExecution method handleResponse.

private void handleResponse(final Procedure procedure, final String baseUri, final InputStream payload) throws TranslatorException, ODataDeserializerException {
    if (procedure.getResultSet() != null) {
        ODataType type = ODataType.valueOf(procedure.getResultSet().getProperty(ODataMetadataProcessor.ODATA_TYPE, false));
        this.response = new ODataResponse(payload, type, new DocumentNode()) {

            @Override
            public InputStream nextBatch(java.net.URI uri) throws TranslatorException {
                return executeSkipToken(uri, baseUri, new HttpStatusCode[] { HttpStatusCode.OK });
            }
        };
    } else if (getReturnParameter() != null) {
        // this is scalar result
        JsonDeserializer parser = new JsonDeserializer(false);
        Property property = parser.toProperty(payload).getPayload();
        if (property.isCollection()) {
            this.returnValue = property.asCollection();
        } else {
            this.returnValue = property.asPrimitive();
        }
    }
}
Also used : DocumentNode(org.teiid.translator.document.DocumentNode) InputStream(java.io.InputStream) ODataType(org.teiid.translator.odata4.ODataMetadataProcessor.ODataType) HttpStatusCode(org.apache.olingo.commons.api.http.HttpStatusCode) TranslatorException(org.teiid.translator.TranslatorException) JsonDeserializer(org.apache.olingo.client.core.serialization.JsonDeserializer) Property(org.apache.olingo.commons.api.data.Property)

Example 7 with Property

use of org.apache.olingo.commons.api.data.Property in project teiid by teiid.

the class ODataResponse method parsePayload.

private Iterator<ODataDocument> parsePayload(InputStream payload) throws TranslatorException {
    try {
        JsonDeserializer parser = new JsonDeserializer(false);
        if (this.resultsType == ODataType.ENTITY) {
            Entity entity = parser.toEntity(payload).getPayload();
            ODataDocument document = ODataDocument.createDocument(entity);
            return Arrays.asList(document).iterator();
        } else if (this.resultsType == ODataType.ENTITY_COLLECTION) {
            EntityCollection entityCollection = parser.toEntitySet(payload).getPayload();
            this.nextUri = entityCollection.getNext();
            ArrayList<ODataDocument> documents = new ArrayList<ODataDocument>();
            for (Entity entity : entityCollection.getEntities()) {
                documents.add(ODataDocument.createDocument(entity));
            }
            return documents.iterator();
        } else {
            // complex
            Property property = parser.toProperty(payload).getPayload();
            if (property.isCollection()) {
                ArrayList<ODataDocument> documents = new ArrayList<ODataDocument>();
                for (Object obj : property.asCollection()) {
                    ComplexValue complexValue = (ComplexValue) obj;
                    documents.add(ODataDocument.createDocument(complexValue));
                }
                return documents.iterator();
            } else {
                ODataDocument document = ODataDocument.createDocument(property.asComplex());
                return Arrays.asList(document).iterator();
            }
        }
    } catch (ODataDeserializerException e) {
        throw new TranslatorException(e);
    }
}
Also used : Entity(org.apache.olingo.commons.api.data.Entity) ComplexValue(org.apache.olingo.commons.api.data.ComplexValue) EntityCollection(org.apache.olingo.commons.api.data.EntityCollection) ArrayList(java.util.ArrayList) TranslatorException(org.teiid.translator.TranslatorException) ODataDeserializerException(org.apache.olingo.client.api.serialization.ODataDeserializerException) JsonDeserializer(org.apache.olingo.client.core.serialization.JsonDeserializer) Property(org.apache.olingo.commons.api.data.Property)

Example 8 with Property

use of org.apache.olingo.commons.api.data.Property in project teiid by teiid.

the class ODataUpdateQuery method getPayload.

public String getPayload(Entity parentEntity) throws TranslatorException {
    JsonSerializer serializer = new JsonSerializer(false, ContentType.APPLICATION_JSON);
    StringWriter writer = new StringWriter();
    try {
        if (!this.complexTables.isEmpty()) {
            Table table = this.complexTables.get(0).getTable();
            Property complexProperty = new Property();
            complexProperty.setName(getName(table));
            complexProperty.setType(table.getProperty(ODataMetadataProcessor.NAME_IN_SCHEMA, false));
            ComplexValue value = new ComplexValue();
            for (Property p : this.payloadProperties) {
                value.getValue().add(p);
            }
            if (this.complexTables.get(0).isCollection()) {
                complexProperty.setValue(ValueType.COLLECTION_COMPLEX, Arrays.asList(value));
            } else {
                complexProperty.setValue(ValueType.COMPLEX, value);
            }
            serializer.write(writer, complexProperty);
        } else if (!this.expandTables.isEmpty()) {
            Table table = this.expandTables.get(0).getTable();
            Entity entity = new Entity();
            entity.setType(table.getProperty(ODataMetadataProcessor.NAME_IN_SCHEMA, false));
            for (Property p : this.payloadProperties) {
                entity.addProperty(p);
            }
            serializer.write(writer, entity);
        } else {
            Entity entity = new Entity();
            entity.setType(this.rootDocument.getTable().getProperty(ODataMetadataProcessor.NAME_IN_SCHEMA, false));
            for (Property p : this.payloadProperties) {
                entity.addProperty(p);
            }
            // for updates
            if (parentEntity != null) {
                // add all the key properties.
                List<Column> keys = this.rootDocument.getTable().getPrimaryKey().getColumns();
                for (Column key : keys) {
                    entity.addProperty(parentEntity.getProperty(key.getName()));
                }
            }
            serializer.write(writer, entity);
        }
    } catch (ODataSerializerException e) {
        throw new TranslatorException(e);
    }
    return writer.toString();
}
Also used : ComplexValue(org.apache.olingo.commons.api.data.ComplexValue) Entity(org.apache.olingo.commons.api.data.Entity) ODataSerializerException(org.apache.olingo.client.api.serialization.ODataSerializerException) Table(org.teiid.metadata.Table) StringWriter(java.io.StringWriter) Column(org.teiid.metadata.Column) ArrayList(java.util.ArrayList) List(java.util.List) TranslatorException(org.teiid.translator.TranslatorException) JsonSerializer(org.apache.olingo.client.core.serialization.JsonSerializer) Property(org.apache.olingo.commons.api.data.Property)

Example 9 with Property

use of org.apache.olingo.commons.api.data.Property in project teiid by teiid.

the class ODataDocument method createDocument.

public static ODataDocument createDocument(Entity entity) {
    ODataDocument document = new ODataDocument();
    List<Property> properties = entity.getProperties();
    for (Property property : properties) {
        populateDocument(property, document);
    }
    return document;
}
Also used : Property(org.apache.olingo.commons.api.data.Property)

Example 10 with Property

use of org.apache.olingo.commons.api.data.Property in project teiid by teiid.

the class ODataUpdateExecution method addAutoGeneretedKeys.

private void addAutoGeneretedKeys(Table table, Entity entity) throws TranslatorException {
    int cols = table.getPrimaryKey().getColumns().size();
    Class<?>[] columnDataTypes = new Class<?>[cols];
    String[] columnNames = new String[cols];
    String[] odataTypes = new String[cols];
    // we may eventual need the type logic off of the metadata importer
    for (int i = 0; i < cols; i++) {
        columnDataTypes[i] = table.getPrimaryKey().getColumns().get(i).getJavaType();
        columnNames[i] = table.getPrimaryKey().getColumns().get(i).getName();
        odataTypes[i] = ODataMetadataProcessor.getNativeType(table.getPrimaryKey().getColumns().get(i));
    }
    GeneratedKeys generatedKeys = this.executionContext.getCommandContext().returnGeneratedKeys(columnNames, columnDataTypes);
    List<Object> vals = new ArrayList<Object>(columnDataTypes.length);
    for (int i = 0; i < columnDataTypes.length; i++) {
        Property prop = entity.getProperty(columnNames[i]);
        Object value;
        try {
            value = ODataTypeManager.convertToTeiidRuntimeType(columnDataTypes[i], prop.getValue(), odataTypes[i]);
        } catch (TeiidException e) {
            throw new TranslatorException(e);
        }
        vals.add(value);
    }
    generatedKeys.addKey(vals);
}
Also used : ArrayList(java.util.ArrayList) TranslatorException(org.teiid.translator.TranslatorException) GeneratedKeys(org.teiid.GeneratedKeys) Property(org.apache.olingo.commons.api.data.Property) TeiidException(org.teiid.core.TeiidException)

Aggregations

Property (org.apache.olingo.commons.api.data.Property)15 EdmProperty (org.apache.olingo.commons.api.edm.EdmProperty)6 Entity (org.apache.olingo.commons.api.data.Entity)5 EdmNavigationProperty (org.apache.olingo.commons.api.edm.EdmNavigationProperty)5 SQLException (java.sql.SQLException)4 ArrayList (java.util.ArrayList)4 TeiidProcessingException (org.teiid.core.TeiidProcessingException)4 Column (org.teiid.metadata.Column)4 TranslatorException (org.teiid.translator.TranslatorException)4 EntityCollection (org.apache.olingo.commons.api.data.EntityCollection)3 Table (org.teiid.metadata.Table)3 IOException (java.io.IOException)2 JsonDeserializer (org.apache.olingo.client.core.serialization.JsonDeserializer)2 ComplexValue (org.apache.olingo.commons.api.data.ComplexValue)2 SingletonPrimitiveType (org.apache.olingo.commons.core.edm.primitivetype.SingletonPrimitiveType)2 ODataApplicationException (org.apache.olingo.server.api.ODataApplicationException)2 ODataLibraryException (org.apache.olingo.server.api.ODataLibraryException)2 Query (org.teiid.query.sql.lang.Query)2 Constant (org.teiid.query.sql.symbol.Constant)2 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)2