Search in sources :

Example 1 with EdmEntityType

use of org.apache.olingo.commons.api.edm.EdmEntityType in project cxf by apache.

the class DemoEntityCollectionProcessor method readEntityCollection.

// CHECKSTYLE:ON
// the only method that is declared in the EntityCollectionProcessor interface
// this method is called, when the user fires a request to an EntitySet
// in our example, the URL would be:
// http://localhost:8080/ExampleService1/ExampleServlet1.svc/Products
public void readEntityCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat) throws ODataApplicationException, SerializerException {
    // 1st we have retrieve the requested EntitySet from the uriInfo object
    // (representation of the parsed service URI)
    List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
    // In our example, the first segment is the EntitySet
    UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
    EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
    // 2nd: fetch the data from backend for this requested EntitySetName
    // it has to be delivered as EntitySet object
    EntityCollection entitySet = getData(edmEntitySet);
    // 3rd: create a serializer based on the requested format (json)
    ODataSerializer serializer = odata.createSerializer(responseFormat);
    // 4th: Now serialize the content: transform from the EntitySet object to InputStream
    EdmEntityType edmEntityType = edmEntitySet.getEntityType();
    ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build();
    final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName();
    EntityCollectionSerializerOptions opts = EntityCollectionSerializerOptions.with().id(id).contextURL(contextUrl).build();
    SerializerResult serializedContent = serializer.entityCollection(serviceMetadata, edmEntityType, entitySet, opts);
    // Finally: configure the response object: set the body, headers and status code
    response.setContent(serializedContent.getContent());
    response.setStatusCode(HttpStatusCode.OK.getStatusCode());
    response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
Also used : UriResource(org.apache.olingo.server.api.uri.UriResource) ODataSerializer(org.apache.olingo.server.api.serializer.ODataSerializer) ContextURL(org.apache.olingo.commons.api.data.ContextURL) EntityCollection(org.apache.olingo.commons.api.data.EntityCollection) EdmEntityType(org.apache.olingo.commons.api.edm.EdmEntityType) EdmEntitySet(org.apache.olingo.commons.api.edm.EdmEntitySet) SerializerResult(org.apache.olingo.server.api.serializer.SerializerResult) EntityCollectionSerializerOptions(org.apache.olingo.server.api.serializer.EntityCollectionSerializerOptions) UriResourceEntitySet(org.apache.olingo.server.api.uri.UriResourceEntitySet)

Example 2 with EdmEntityType

use of org.apache.olingo.commons.api.edm.EdmEntityType in project teiid by teiid.

the class ODataSQLBuilder method processExpandOption.

private void processExpandOption(ExpandOption option, DocumentNode node, Query outerQuery, int expandLevel, Integer cyclicLevel) throws TeiidException {
    checkExpandLevel(expandLevel);
    int starLevels = 0;
    HashSet<String> seen = new HashSet<String>();
    for (ExpandItem ei : option.getExpandItems()) {
        if (ei.getSearchOption() != null) {
            throw new TeiidNotImplementedException(ODataPlugin.Event.TEIID16035, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16035));
        }
        Integer levels = null;
        if (cyclicLevel != null) {
            levels = cyclicLevel - 1;
        } else if (ei.getLevelsOption() != null) {
            if (ei.getLevelsOption().isMax()) {
                levels = MAX_EXPAND_LEVEL - expandLevel + 1;
            } else {
                levels = ei.getLevelsOption().getValue();
                checkExpandLevel(expandLevel + levels - 1);
            }
        }
        ExpandSQLBuilder esb = new ExpandSQLBuilder(ei);
        EdmNavigationProperty property = esb.getNavigationProperty();
        if (property == null) {
            if (ei.isStar()) {
                if (starLevels > 0) {
                    throw new TeiidProcessingException(ODataPlugin.Event.TEIID16058, // $NON-NLS-1$
                    ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16058, "*"));
                }
                if (levels != null) {
                    starLevels = levels;
                } else {
                    starLevels = 1;
                }
                continue;
            }
            throw new TeiidNotImplementedException(ODataPlugin.Event.TEIID16057, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16057));
        }
        if (!seen.add(property.getName())) {
            throw new TeiidProcessingException(ODataPlugin.Event.TEIID16058, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16058, property.getName()));
        }
        // always pass in the root as the parent as that seems to be the definition of the current context
        // if instead it should refer to the parent expands, then we would pass in the node instead
        ExpandDocumentNode expandResource = ExpandDocumentNode.buildExpand(property, this.metadata, this.odata, this.nameGenerator, true, getUriInfo(), this.parseService, this.context);
        node.addExpand(expandResource);
        // process $filter
        if (ei.getFilterOption() != null) {
            Expression expandCriteria = processFilterOption(ei.getFilterOption(), expandResource);
            expandResource.addCriteria(expandCriteria);
        }
        OrderBy expandOrder = null;
        if (ei.getOrderByOption() != null) {
            expandOrder = new OrderBy();
            processOrderBy(expandOrder, ei.getOrderByOption().getOrders(), expandResource);
        } else {
            expandOrder = expandResource.addDefaultOrderBy();
        }
        // process $select
        processSelectOption(ei.getSelectOption(), expandResource, this.reference);
        if (ei.getSkipOption() != null) {
            expandResource.setSkip(ei.getSkipOption().getValue());
        }
        if (ei.getTopOption() != null) {
            expandResource.setTop(ei.getTopOption().getValue());
        }
        Query query = expandResource.buildQuery();
        if (ei.getExpandOption() != null) {
            processExpandOption(ei.getExpandOption(), expandResource, query, expandLevel + 1, null);
        } else if (levels != null) {
            // self reference check
            if (!property.getType().getFullQualifiedName().equals(node.getEdmEntityType().getFullQualifiedName())) {
                throw new TeiidProcessingException(ODataPlugin.Event.TEIID16060, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16060, node.getEdmEntityType().getFullQualifiedName(), property.getType().getFullQualifiedName()));
            }
            if (levels > 1) {
                ExpandOptionImpl eoi = new ExpandOptionImpl();
                eoi.addExpandItem(ei);
                processExpandOption(eoi, expandResource, query, expandLevel + 1, levels);
            }
        }
        buildAggregateQuery(node, outerQuery, expandResource, expandOrder, query, property);
    }
    if (starLevels > 0) {
        List<ExpandNode> starExpand = new ArrayList<TeiidServiceHandler.ExpandNode>();
        EdmEntityType edmEntityType = node.getEdmEntityType();
        buildExpandGraph(seen, starExpand, edmEntityType, starLevels - 1);
        if (!starExpand.isEmpty()) {
            processExpand(starExpand, node, outerQuery, expandLevel);
        }
    }
}
Also used : ExpandOptionImpl(org.apache.olingo.server.core.uri.queryoption.ExpandOptionImpl) ExpandNode(org.teiid.olingo.service.TeiidServiceHandler.ExpandNode) ArrayList(java.util.ArrayList) EdmNavigationProperty(org.apache.olingo.commons.api.edm.EdmNavigationProperty) EdmEntityType(org.apache.olingo.commons.api.edm.EdmEntityType) SubqueryHint(org.teiid.query.sql.lang.ExistsCriteria.SubqueryHint) TeiidProcessingException(org.teiid.core.TeiidProcessingException) Expression(org.teiid.query.sql.symbol.Expression) HashSet(java.util.HashSet)

Example 3 with EdmEntityType

use of org.apache.olingo.commons.api.edm.EdmEntityType in project teiid by teiid.

the class ODataSQLBuilder method visit.

@Override
public void visit(UriInfoCrossjoin info) {
    for (String name : info.getEntitySetNames()) {
        EdmEntitySet entitySet = this.serviceMetadata.getEdm().getEntityContainer().getEntitySet(name);
        EdmEntityType entityType = entitySet.getEntityType();
        CrossJoinNode resource = null;
        try {
            boolean hasExpand = hasExpand(entitySet.getName(), info.getExpandOption());
            resource = CrossJoinNode.buildCrossJoin(entityType, null, this.metadata, this.odata, this.nameGenerator, this.aliasedGroups, getUriInfo(), this.parseService, hasExpand);
            resource.addAllColumns(!hasExpand);
            if (this.context == null) {
                this.context = resource;
                this.orderBy = this.context.addDefaultOrderBy();
            } else {
                this.context.addSibiling(resource);
                OrderBy orderby = resource.addDefaultOrderBy();
                int index = orderby.getVariableCount();
                for (int i = 0; i < index; i++) {
                    this.orderBy.addVariable(orderby.getVariable(i));
                }
            }
        } catch (TeiidException e) {
            this.exceptions.add(e);
        }
    }
    super.visit(info);
    // the expand behavior is handled above with selection of the columns
    this.expandOption = null;
}
Also used : EdmEntityType(org.apache.olingo.commons.api.edm.EdmEntityType) EdmEntitySet(org.apache.olingo.commons.api.edm.EdmEntitySet) SubqueryHint(org.teiid.query.sql.lang.ExistsCriteria.SubqueryHint) TeiidException(org.teiid.core.TeiidException)

Example 4 with EdmEntityType

use of org.apache.olingo.commons.api.edm.EdmEntityType in project teiid by teiid.

the class ReferenceUpdateSQLBuilder method visit.

@Override
public void visit(UriResourceEntitySet info) {
    Table table = DocumentNode.findTable(info.getEntitySet(), this.metadata);
    EdmEntityType type = info.getEntitySet().getEntityType();
    List<UriParameter> keys = info.getKeyPredicates();
    this.updateTable = new ScopedTable(table, type, keys);
}
Also used : Table(org.teiid.metadata.Table) EdmEntityType(org.apache.olingo.commons.api.edm.EdmEntityType) UriParameter(org.apache.olingo.server.api.uri.UriParameter)

Example 5 with EdmEntityType

use of org.apache.olingo.commons.api.edm.EdmEntityType in project teiid by teiid.

the class ReferenceUpdateSQLBuilder method updateReference.

public Update updateReference(URI referenceId, boolean prepared, boolean delete) throws SQLException {
    try {
        if (referenceId != null) {
            UriInfo uriInfo = ODataSQLBuilder.buildUriInfo(referenceId, this.baseURI, this.serviceMetadata, this.odata);
            UriResourceEntitySet uriEnitytSet = (UriResourceEntitySet) uriInfo.asUriInfoResource().getUriResourceParts().get(0);
            if (this.collection) {
                this.updateTable.setKeyPredicates(uriEnitytSet.getKeyPredicates());
            } else {
                this.referenceTable.setKeyPredicates(uriEnitytSet.getKeyPredicates());
            }
        }
    } catch (UriParserException e) {
        throw new SQLException(e);
    } catch (URISyntaxException e) {
        throw new SQLException(e);
    } catch (UriValidationException e) {
        throw new SQLException(e);
    }
    try {
        Update update = new Update();
        update.setGroup(this.updateTable.getGroupSymbol());
        List<String> columnNames = DocumentNode.getColumnNames(this.updateTable.getFk().getColumns());
        for (int i = 0; i < columnNames.size(); i++) {
            Column column = this.updateTable.getFk().getColumns().get(i);
            String columnName = columnNames.get(i);
            ElementSymbol symbol = new ElementSymbol(columnName, this.updateTable.getGroupSymbol());
            EdmEntityType entityType = this.updateTable.getEdmEntityType();
            EdmProperty edmProperty = (EdmProperty) entityType.getProperty(columnName);
            // reference table keys will be null for delete scenario
            Object value = null;
            if (!delete) {
                UriParameter parameter = getParameter(this.updateTable.getFk().getReferenceColumns().get(i), this.referenceTable.getKeyPredicates());
                value = ODataTypeManager.parseLiteral(edmProperty, column.getJavaType(), parameter.getText());
            }
            if (prepared) {
                update.addChange(symbol, new Reference(i++));
                this.params.add(ODataSQLBuilder.asParam(edmProperty, value));
            } else {
                update.addChange(symbol, new Constant(ODataSQLBuilder.asParam(edmProperty, value).getValue()));
            }
        }
        Criteria criteria = DocumentNode.buildEntityKeyCriteria(this.updateTable, null, this.metadata, this.odata, null, null);
        update.setCriteria(criteria);
        return update;
    } catch (TeiidException e) {
        throw new SQLException(e);
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) SQLException(java.sql.SQLException) Reference(org.teiid.query.sql.symbol.Reference) Constant(org.teiid.query.sql.symbol.Constant) EdmEntityType(org.apache.olingo.commons.api.edm.EdmEntityType) URISyntaxException(java.net.URISyntaxException) Criteria(org.teiid.query.sql.lang.Criteria) Update(org.teiid.query.sql.lang.Update) TeiidException(org.teiid.core.TeiidException) UriValidationException(org.apache.olingo.server.core.uri.validator.UriValidationException) Column(org.teiid.metadata.Column) EdmProperty(org.apache.olingo.commons.api.edm.EdmProperty) UriResourceEntitySet(org.apache.olingo.server.api.uri.UriResourceEntitySet) UriInfo(org.apache.olingo.server.api.uri.UriInfo) UriParserException(org.apache.olingo.server.core.uri.parser.UriParserException) UriParameter(org.apache.olingo.server.api.uri.UriParameter)

Aggregations

EdmEntityType (org.apache.olingo.commons.api.edm.EdmEntityType)9 SQLException (java.sql.SQLException)4 TeiidException (org.teiid.core.TeiidException)4 URISyntaxException (java.net.URISyntaxException)3 UriParameter (org.apache.olingo.server.api.uri.UriParameter)3 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 EdmEntitySet (org.apache.olingo.commons.api.edm.EdmEntitySet)2 EdmPrimitiveTypeException (org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException)2 ODataApplicationException (org.apache.olingo.server.api.ODataApplicationException)2 UriResourceEntitySet (org.apache.olingo.server.api.uri.UriResourceEntitySet)2 TeiidProcessingException (org.teiid.core.TeiidProcessingException)2 UpdateResponse (org.teiid.odata.api.UpdateResponse)2 SubqueryHint (org.teiid.query.sql.lang.ExistsCriteria.SubqueryHint)2 Update (org.teiid.query.sql.lang.Update)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 ContextURL (org.apache.olingo.commons.api.data.ContextURL)1 Entity (org.apache.olingo.commons.api.data.Entity)1