Search in sources :

Example 1 with UriResourceEntitySet

use of org.apache.olingo.server.api.uri.UriResourceEntitySet 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 UriResourceEntitySet

use of org.apache.olingo.server.api.uri.UriResourceEntitySet 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)2 UriResourceEntitySet (org.apache.olingo.server.api.uri.UriResourceEntitySet)2 URISyntaxException (java.net.URISyntaxException)1 SQLException (java.sql.SQLException)1 ContextURL (org.apache.olingo.commons.api.data.ContextURL)1 EntityCollection (org.apache.olingo.commons.api.data.EntityCollection)1 EdmEntitySet (org.apache.olingo.commons.api.edm.EdmEntitySet)1 EdmProperty (org.apache.olingo.commons.api.edm.EdmProperty)1 EntityCollectionSerializerOptions (org.apache.olingo.server.api.serializer.EntityCollectionSerializerOptions)1 ODataSerializer (org.apache.olingo.server.api.serializer.ODataSerializer)1 SerializerResult (org.apache.olingo.server.api.serializer.SerializerResult)1 UriInfo (org.apache.olingo.server.api.uri.UriInfo)1 UriParameter (org.apache.olingo.server.api.uri.UriParameter)1 UriResource (org.apache.olingo.server.api.uri.UriResource)1 UriParserException (org.apache.olingo.server.core.uri.parser.UriParserException)1 UriValidationException (org.apache.olingo.server.core.uri.validator.UriValidationException)1 TeiidException (org.teiid.core.TeiidException)1 Column (org.teiid.metadata.Column)1 Criteria (org.teiid.query.sql.lang.Criteria)1 Update (org.teiid.query.sql.lang.Update)1