Search in sources :

Example 1 with EntityCollection

use of org.apache.olingo.commons.api.data.EntityCollection 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 2 with EntityCollection

use of org.apache.olingo.commons.api.data.EntityCollection in project cxf by apache.

the class DemoEntityCollectionProcessor method getData.

/**
 * Helper method for providing some sample data
 * @param edmEntitySet for which the data is requested
 * @return data of requested entity set
 */
private EntityCollection getData(EdmEntitySet edmEntitySet) {
    EntityCollection productsCollection = new EntityCollection();
    // check for which EdmEntitySet the data is requested
    if (DemoEdmProvider.ES_PRODUCTS_NAME.equals(edmEntitySet.getName())) {
        List<Entity> productList = productsCollection.getEntities();
        // add some sample product entities
        final Entity e1 = new Entity().addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 1)).addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Notebook Basic 15")).addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "Notebook Basic, 1.7GHz - 15 XGA - 1024MB DDR2 SDRAM - 40GB"));
        e1.setId(createId("Products", 1));
        productList.add(e1);
        final Entity e2 = new Entity().addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 2)).addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "1UMTS PDA")).addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "Ultrafast 3G UMTS/HSDPA Pocket PC, supports GSM network"));
        e2.setId(createId("Products", 1));
        productList.add(e2);
        final Entity e3 = new Entity().addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 3)).addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Ergo Screen")).addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "19 Optimum Resolution 1024 x 768 @ 85Hz, resolution 1280 x 960"));
        e3.setId(createId("Products", 1));
        productList.add(e3);
    }
    return productsCollection;
}
Also used : Entity(org.apache.olingo.commons.api.data.Entity) EntityCollection(org.apache.olingo.commons.api.data.EntityCollection) Property(org.apache.olingo.commons.api.data.Property)

Example 3 with EntityCollection

use of org.apache.olingo.commons.api.data.EntityCollection 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 4 with EntityCollection

use of org.apache.olingo.commons.api.data.EntityCollection 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)

Aggregations

EntityCollection (org.apache.olingo.commons.api.data.EntityCollection)4 Entity (org.apache.olingo.commons.api.data.Entity)3 Property (org.apache.olingo.commons.api.data.Property)3 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 ODataDeserializerException (org.apache.olingo.client.api.serialization.ODataDeserializerException)1 JsonDeserializer (org.apache.olingo.client.core.serialization.JsonDeserializer)1 ComplexValue (org.apache.olingo.commons.api.data.ComplexValue)1 ContextURL (org.apache.olingo.commons.api.data.ContextURL)1 EdmEntitySet (org.apache.olingo.commons.api.edm.EdmEntitySet)1 EdmEntityType (org.apache.olingo.commons.api.edm.EdmEntityType)1 EdmNavigationProperty (org.apache.olingo.commons.api.edm.EdmNavigationProperty)1 EdmProperty (org.apache.olingo.commons.api.edm.EdmProperty)1 ODataApplicationException (org.apache.olingo.server.api.ODataApplicationException)1 ODataLibraryException (org.apache.olingo.server.api.ODataLibraryException)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 UriResource (org.apache.olingo.server.api.uri.UriResource)1 UriResourceEntitySet (org.apache.olingo.server.api.uri.UriResourceEntitySet)1