Search in sources :

Example 11 with Property

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

the class ODataSQLBuilder method insert.

public Insert insert(EdmEntityType entityType, Entity entity, List<UriParameter> keys, boolean prepared) throws TeiidException {
    Table entityTable = findTable(entityType.getName(), this.metadata);
    DocumentNode resource = new DocumentNode(entityTable, new GroupSymbol(entityTable.getFullName()), entityType);
    List<Reference> referenceValues = new ArrayList<Reference>();
    List<Constant> constantValues = new ArrayList<Constant>();
    Insert insert = new Insert();
    insert.setGroup(resource.getGroupSymbol());
    if (keys != null) {
        for (UriParameter key : keys) {
            EdmProperty edmProperty = (EdmProperty) entityType.getProperty(key.getName());
            Column column = entityTable.getColumnByName(edmProperty.getName());
            Object propertyValue = ODataTypeManager.parseLiteral(edmProperty, column.getJavaType(), key.getText());
            Property existing = entity.getProperty(edmProperty.getName());
            if (existing == null || (existing.getValue() == null && propertyValue != null) || (existing.getValue() != null && propertyValue == null) || (existing.getValue() != null && !existing.getValue().equals(propertyValue))) {
                throw new TeiidProcessingException(ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16048, edmProperty.getName()));
            }
        }
    }
    int i = 0;
    for (Property prop : entity.getProperties()) {
        EdmProperty edmProp = (EdmProperty) entityType.getProperty(prop.getName());
        Column column = entityTable.getColumnByName(edmProp.getName());
        insert.addVariable(new ElementSymbol(column.getName(), resource.getGroupSymbol()));
        if (prepared) {
            referenceValues.add(new Reference(i++));
            this.params.add(asParam(edmProp, prop.getValue()));
        } else {
            constantValues.add(new Constant(asParam(edmProp, prop.getValue()).getValue()));
        }
    }
    if (prepared) {
        insert.setValues(referenceValues);
    } else {
        insert.setValues(constantValues);
    }
    return insert;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Table(org.teiid.metadata.Table) Reference(org.teiid.query.sql.symbol.Reference) Constant(org.teiid.query.sql.symbol.Constant) ArrayList(java.util.ArrayList) SubqueryHint(org.teiid.query.sql.lang.ExistsCriteria.SubqueryHint) TeiidProcessingException(org.teiid.core.TeiidProcessingException) Column(org.teiid.metadata.Column) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) EdmProperty(org.apache.olingo.commons.api.edm.EdmProperty) Property(org.apache.olingo.commons.api.data.Property) EdmNavigationProperty(org.apache.olingo.commons.api.edm.EdmNavigationProperty) EdmProperty(org.apache.olingo.commons.api.edm.EdmProperty)

Example 12 with Property

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

the class OperationResponseImpl method getComplexProperty.

private ComplexValue getComplexProperty(ResultSet rs) throws SQLException {
    HashMap<Integer, Property> properties = new HashMap<Integer, Property>();
    for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
        Object value = rs.getObject(i + 1);
        String propName = rs.getMetaData().getColumnLabel(i + 1);
        EdmElement element = ((EdmComplexType) this.procedureReturn.getReturnType().getType()).getProperty(propName);
        if (!(element instanceof EdmProperty) && !((EdmProperty) element).isPrimitive()) {
            throw new SQLException(new TeiidNotImplementedException(ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16024)));
        }
        EdmPropertyImpl edmProperty = (EdmPropertyImpl) element;
        Property property;
        try {
            property = EntityCollectionResponse.buildPropery(propName, (SingletonPrimitiveType) edmProperty.getType(), edmProperty.getPrecision(), edmProperty.getScale(), edmProperty.isCollection(), value);
            properties.put(i, property);
        } catch (IOException e) {
            throw new SQLException(e);
        } catch (TeiidProcessingException e) {
            throw new SQLException(e);
        }
    }
    // filter those columns out.
    return createComplex("result", properties.values());
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) IOException(java.io.IOException) EdmElement(org.apache.olingo.commons.api.edm.EdmElement) EdmComplexType(org.apache.olingo.commons.api.edm.EdmComplexType) EdmPropertyImpl(org.apache.olingo.commons.core.edm.EdmPropertyImpl) TeiidProcessingException(org.teiid.core.TeiidProcessingException) SingletonPrimitiveType(org.apache.olingo.commons.core.edm.primitivetype.SingletonPrimitiveType) EdmProperty(org.apache.olingo.commons.api.edm.EdmProperty) Property(org.apache.olingo.commons.api.data.Property) EdmProperty(org.apache.olingo.commons.api.edm.EdmProperty)

Example 13 with Property

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

the class TeiidServiceHandler method read.

@Override
public <T extends ServiceResponse> void read(final DataRequest request, T response) throws ODataLibraryException, ODataApplicationException {
    final ODataSQLBuilder visitor = new ODataSQLBuilder(odata, getClient().getMetadataStore(), this.prepared, true, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
    visitor.visit(request.getUriInfo());
    final BaseResponse queryResponse;
    try {
        Query query = visitor.selectQuery();
        queryResponse = executeQuery(request, request.isCountRequest(), visitor, query);
    } catch (Throwable e) {
        throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
    }
    response.accepts(new ServiceResponseVisior() {

        public void visit(CountResponse response) throws ODataLibraryException, ODataApplicationException {
            org.teiid.odata.api.CountResponse cr = (org.teiid.odata.api.CountResponse) queryResponse;
            response.writeCount(cr.getCount());
        }

        public void visit(PrimitiveValueResponse response) throws ODataLibraryException, ODataApplicationException {
            EntityCollection entitySet = (EntityCollection) queryResponse;
            if (!entitySet.getEntities().isEmpty()) {
                Entity entity = entitySet.getEntities().get(0);
                EdmProperty edmProperty = request.getUriResourceProperty().getProperty();
                Property property = entity.getProperty(edmProperty.getName());
                if (property == null) {
                    response.writeNotFound(true);
                } else if (property.getValue() == null) {
                    response.writeNoContent(true);
                } else {
                    response.write(property.getValue());
                }
            } else {
                response.writeNotFound(true);
            }
        }

        public void visit(PropertyResponse response) throws ODataLibraryException, ODataApplicationException {
            EntityCollection entitySet = (EntityCollection) queryResponse;
            if (!entitySet.getEntities().isEmpty()) {
                Entity entity = entitySet.getEntities().get(0);
                EdmProperty edmProperty = request.getUriResourceProperty().getProperty();
                Property property = entity.getProperty(edmProperty.getName());
                response.writeProperty(edmProperty.getType(), property);
            } else {
                response.writeNotFound(true);
            }
        }

        public void visit(StreamResponse response) throws ODataLibraryException, ODataApplicationException {
            EntityCollectionResponse entitySet = (EntityCollectionResponse) queryResponse;
            EdmProperty edmProperty = request.getUriResourceProperty().getProperty();
            Object value = entitySet.getStream(edmProperty.getName());
            if (value == null) {
                response.writeNoContent(true);
            } else {
                try {
                    handleLobResult(getClient().getProperty(Client.CHARSET), value, response);
                } catch (SQLException e) {
                    LogManager.logDetail(LogConstants.CTX_ODATA, e);
                    response.writeServerError(true);
                }
            }
        }

        public void visit(EntityResponse response) throws ODataLibraryException, ODataApplicationException {
            EntityCollection entitySet = (EntityCollection) queryResponse;
            if (entitySet.getEntities().isEmpty()) {
                if (visitor.hasNavigation()) {
                    response.writeNoContent(true);
                } else {
                    response.writeNotFound(true);
                }
            } else {
                response.writeReadEntity(visitor.getContext().getEdmEntityType(), entitySet.getEntities().get(0));
            }
        }

        public void visit(EntitySetResponse response) throws ODataLibraryException, ODataApplicationException {
            sendResults(request, visitor, queryResponse, response);
        }
    });
}
Also used : Entity(org.apache.olingo.commons.api.data.Entity) Query(org.teiid.query.sql.lang.Query) SQLException(java.sql.SQLException) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException) ODataLibraryException(org.apache.olingo.server.api.ODataLibraryException) BaseResponse(org.teiid.odata.api.BaseResponse) EntityCollection(org.apache.olingo.commons.api.data.EntityCollection) EdmProperty(org.apache.olingo.commons.api.edm.EdmProperty) Property(org.apache.olingo.commons.api.data.Property) EdmNavigationProperty(org.apache.olingo.commons.api.edm.EdmNavigationProperty) EdmProperty(org.apache.olingo.commons.api.edm.EdmProperty)

Example 14 with Property

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

the class EntityCollectionResponse method createEntity.

static Entity createEntity(Row row, DocumentNode node, String baseURL, EntityCollectionResponse response) throws SQLException {
    List<ProjectedColumn> projected = node.getAllProjectedColumns();
    EdmEntityType entityType = node.getEdmEntityType();
    LinkedHashMap<String, Link> streamProperties = new LinkedHashMap<String, Link>();
    Entity entity = new Entity();
    entity.setType(entityType.getFullQualifiedName().getFullQualifiedNameAsString());
    boolean allNulls = true;
    for (ProjectedColumn column : projected) {
        /*
            if (!column.isVisible()) {
                continue;
            }*/
        String propertyName = Symbol.getShortName(column.getExpression());
        Object value = row.getObject(column.getOrdinal());
        if (value != null) {
            allNulls = false;
        }
        try {
            SingletonPrimitiveType type = (SingletonPrimitiveType) column.getEdmType();
            if (type instanceof EdmStream) {
                buildStreamLink(streamProperties, value, propertyName);
                if (response != null) {
                    // this will only be used for a stream response off of the first entity. In all other scenarios it will be ignored.
                    response.setStream(propertyName, value);
                }
            } else {
                Property property = buildPropery(propertyName, type, column.getPrecision(), column.getScale(), column.isCollection(), value);
                entity.addProperty(property);
            }
        } catch (IOException e) {
            throw new SQLException(e);
        } catch (TeiidProcessingException e) {
            throw new SQLException(e);
        }
    }
    if (allNulls) {
        return null;
    }
    // Build the navigation and Stream Links
    try {
        String id = EntityResponse.buildLocation(baseURL, entity, entityType.getName(), entityType);
        entity.setId(new URI(id));
        // build stream properties
        for (String name : streamProperties.keySet()) {
            Link link = streamProperties.get(name);
            link.setHref(id + "/" + name);
            entity.getMediaEditLinks().add(link);
            entity.addProperty(createPrimitive(name, EdmStream.getInstance(), new URI(link.getHref())));
        }
        // build navigations
        for (String name : entityType.getNavigationPropertyNames()) {
            Link navLink = new Link();
            navLink.setTitle(name);
            navLink.setHref(id + "/" + name);
            navLink.setRel("http://docs.oasis-open.org/odata/ns/related/" + name);
            entity.getNavigationLinks().add(navLink);
            Link assosiationLink = new Link();
            assosiationLink.setTitle(name);
            assosiationLink.setHref(id + "/" + name + "/$ref");
            assosiationLink.setRel("http://docs.oasis-open.org/odata/ns/relatedlinks/" + name);
            entity.getAssociationLinks().add(assosiationLink);
        }
    } catch (URISyntaxException e) {
        throw new SQLException(e);
    } catch (EdmPrimitiveTypeException e) {
        throw new SQLException(e);
    }
    return entity;
}
Also used : Entity(org.apache.olingo.commons.api.data.Entity) SQLException(java.sql.SQLException) EdmStream(org.apache.olingo.commons.core.edm.primitivetype.EdmStream) EdmEntityType(org.apache.olingo.commons.api.edm.EdmEntityType) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) EdmPrimitiveTypeException(org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException) URI(java.net.URI) ProjectedColumn(org.teiid.olingo.ProjectedColumn) LinkedHashMap(java.util.LinkedHashMap) TeiidProcessingException(org.teiid.core.TeiidProcessingException) SingletonPrimitiveType(org.apache.olingo.commons.core.edm.primitivetype.SingletonPrimitiveType) Property(org.apache.olingo.commons.api.data.Property) Link(org.apache.olingo.commons.api.data.Link)

Example 15 with Property

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

the class TeiidServiceHandler method invokeOperation.

private <T extends ServiceResponse> void invokeOperation(final OperationRequest request, OperationParameterValueProvider parameters, T response) throws ODataApplicationException, ODataLibraryException {
    checkExpand(request.getUriInfo().asUriInfoResource());
    final ODataSQLBuilder visitor = new ODataSQLBuilder(odata, getClient().getMetadataStore(), this.prepared, true, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
    visitor.setOperationParameterValueProvider(parameters);
    visitor.visit(request.getUriInfo());
    final OperationResponseImpl queryResponse;
    try {
        if (visitor.getContext() instanceof NoDocumentNode) {
            NoDocumentNode cdn = (NoDocumentNode) visitor.getContext();
            ProcedureReturn procReturn = cdn.getProcedureReturn();
            queryResponse = new OperationResponseImpl(procReturn);
            getClient().executeCall(cdn.getQuery(), visitor.getParameters(), procReturn, queryResponse);
        } else {
            Query query = visitor.selectQuery();
            queryResponse = (OperationResponseImpl) executeQuery(request, request.isCountRequest(), visitor, query);
        }
    } catch (Throwable e) {
        throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
    }
    /*
        try {
            MetadataStore store = getClient().getMetadataStore();
            ProcedureSQLBuilder builder = new ProcedureSQLBuilder(store.getSchema(schemaName), request);
            ProcedureReturn procedureReturn = builder.getReturn();
            result = new OperationResponseImpl(procedureReturn);
            
            getClient().executeCall(builder.buildProcedureSQL(), builder.getSqlParameters(), procedureReturn, result);
        } catch (SQLException e) {
            throw new ODataApplicationException(e.getMessage(),
                    HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(),
                    Locale.getDefault(), e);
        } catch (TeiidException e) {
            throw new ODataApplicationException(e.getMessage(),
                    HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(),
                    Locale.getDefault(), e);
        } 
        */
    final OperationResponseImpl operationResult = queryResponse;
    response.accepts(new ServiceResponseVisior() {

        @Override
        public void visit(PropertyResponse response) throws ODataLibraryException, ODataApplicationException {
            Property property = (Property) operationResult.getResult();
            Object value = property.getValue();
            if (value instanceof SQLXML || value instanceof Blob || value instanceof Clob) {
                try {
                    handleLobResult(getClient().getProperty(Client.CHARSET), value, response);
                } catch (SQLException e) {
                    LogManager.logDetail(LogConstants.CTX_ODATA, e);
                    response.writeServerError(true);
                }
            } else {
                response.writeProperty(request.getReturnType().getType(), property);
            }
        }
    });
}
Also used : Blob(java.sql.Blob) Query(org.teiid.query.sql.lang.Query) SQLException(java.sql.SQLException) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException) ODataLibraryException(org.apache.olingo.server.api.ODataLibraryException) SQLXML(java.sql.SQLXML) ProcedureReturn(org.teiid.olingo.service.ProcedureSQLBuilder.ProcedureReturn) Clob(java.sql.Clob) Property(org.apache.olingo.commons.api.data.Property) EdmNavigationProperty(org.apache.olingo.commons.api.edm.EdmNavigationProperty) EdmProperty(org.apache.olingo.commons.api.edm.EdmProperty)

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