Skip to content
Francesco Chicchiriccò edited this page May 24, 2013 · 18 revisions

This page shows some sample usage of ODataJClient, either at high (e.g. Proxy) and low (e.g. Engine) level.

Proxy samples

The snippets below refer to the example presented in the design document.

Get Entity

        DemoService demoService = entityContainerFactory.getEntityContainer(DemoService.class);

        // Take the product with key value 3
        Product product3 = demoService.getProducts().get(3);

Asynchronous

        AsyncDemoService demoService = entityContainerFactory.getEntityContainer(AsyncDemoService.class);

        // Take the product with key value 3 asynchronously
        Future<Product> product3 = demoService.getProducts().get(3);

Create Entities

        DemoService demoService = entityContainerFactory.getEntityContainer(DemoService.class);

        // create Category (local)
        Category category = new Category();
        category.setName("a category name");        
        category = demoService.getCategories().save(category);
        
        // create Product (local)
        Product product = new Product();
        product.setName("a name");
        product.setDescription("a description");
        product.setPrice(11F);
        product.setReleaseDate(new Date());        
        product = demoService.getProducts().save(product);
        
        // link product and category
        product.setCategory(category);
        category.setProducts(Collections.singletonList(product));
        
        // any flush() will generate actual operations on the OData service
        demoService.getProducts().flush();

Invoke Operation

        DemoService demoService = entityContainerFactory.getEntityContainer(DemoService.class);

        // invoke GetProductsByRating
        Collection<Product> products = demoService.getProductsByRating(15);

Asynchronous

        AsyncDemoService demoService = entityContainerFactory.getEntityContainer(AsyncDemoService.class);

        // invoke GetProductsByRating asynchronously
        Future<Collection<Product>> productsFuture = demoService.getProductsByRating(15);
        while (!productsFuture.isDone()) {
            Thread.sleep(1000);
        }
        Collection<Product> products = productsFuture.get();

Entity Query

        DemoService demoService = entityContainerFactory.getEntityContainer(DemoService.class);

        // Typed query for products with price < 10.00
        EntityQuery<Product> productQuery = demoService.getProducts().createQuery(Product.class);
        productQuery.setFilter("Price lt 10.00");

        List<Product> matchingProducts = productQuery.getResultList();

        // ... do something with matchingProducts

        // Typed query for categories of products with price < 10.00
        EntityQuery<Category> categoryQuery = demoService.getProducts().createQuery(Category.class);
        productQuery.setFilter("Price lt 10.00");
        productQuery.setSelect("Category");

        List<Category> matchingCategories = categoryQuery.getResultList();
        
        // ... do something with matchingCategories

Asynchronous

        AsyncDemoService demoService = entityContainerFactory.getEntityContainer(AsyncDemoService.class);

        // Typed query for products with price < 10.00, to be execute asynchronously
        AsyncEntityQuery<Product> productQuery = demoService.getProducts().createQuery(Product.class);
        productQuery.setFilter("Price lt 10.00");

        Future<List<Product>> matchingProductsFuture = productQuery.asyncGetResultList();

        // ... do something with matchingProductsFuture

Untyped Query

        DemoService demoService = entityContainerFactory.getEntityContainer(DemoService.class);

        // Untyped query for names of products with price < 10.00
        Query query = demoService.getProducts().createQuery();
        query.setFilter("Price lt 10.00");
        query.setSelect("Name");

        List<? extends Serializable> match = query.getResultList();

        // ... do something with match

Asynchronous

        AsyncDemoService demoService = entityContainerFactory.getEntityContainer(AsyncDemoService.class);

        // Untyped query for names of products with price < 10.00, to be execute asynchronously
        AsyncQuery query = demoService.getProducts().createQuery();
        query.setFilter("Price lt 10.00");
        query.setSelect("Name");

        Future<List<? extends Serializable>> matchFuture = query.asyncGetResultList();

        // ... do something with matchFuture

Engine samples

Create entity

        // provide the target URI
        final ODataURI targetURI = new ODataURI("http://services.odata.org/OData/Odata.svc");
        targetURI.append("Products");

        // build the new object
        final ODataEntity newEntity = EntityFactory.newEntity("Java Code");
        newEntity.setSummary("OData client Java framework");
        // entity.set......

        // create your request
        final ODataRequest request = ODataRequestFactory.getCreateRequest(targetURI, newEntity);

        // execute the request
        ODataResponse res = client.execute(request);

        // retrieve and process execution results
        int statusCode = res.getStatusCode();
        String statusMessage = res.getStatusMessage();

Create link

        // provide the source URI
        final ODataURI sourceURI = new ODataURI("http://services.odata.org/OData/Odata.svc");
        sourceURI.append("Products(1)");

        // provide the target URI
        final ODataURI targetURI = new ODataURI("http://services.odata.org/OData/Odata.svc");
        targetURI.append("Suppliers(5)");

        // build the new link
        final ODataLink newLink = EntityFactory.newLink("Supplier", targetURI);

        // create your request
        final ODataRequest request = ODataRequestFactory.getCreateRequest(sourceURI, newLink);

        // execute the request
        ODataResponse res = client.execute(request);

        // retrieve and process execution results
        int statusCode = res.getStatusCode();
        String statusMessage = res.getStatusMessage();

Update entity (PATCH)

        // provide the target URI
        final ODataURI targetURI = new ODataURI("http://services.odata.org/OData/Odata.svc");
        targetURI.append("Products(2)");

        // build the new object to change Rating value
        final ODataEntity changes = EntityFactory.newEntity("Java Code");
        ODataProperty rating = new ODataProperty("Rating", "3");

        // create your request
        final ODataRequest request = ODataRequestFactory.getUpdateRequest(targetURI, changes, UpdateType.PATCH);

        // execute the request
        ODataResponse res = client.execute(request);

        // retrieve and process execution results
        int statusCode = res.getStatusCode();
        String statusMessage = res.getStatusMessage();

Delete entity

        // provide the target URI
        final ODataURI targetURI = new ODataURI("http://services.odata.org/OData/Odata.svc");
        targetURI.append("Products(2)");

        // create your request
        final ODataRequest request = ODataRequestFactory.getDeleteRequest(targetURI);

        // execute the request
        ODataResponse res = client.execute(request);

        // retrieve and process execution results
        int statusCode = res.getStatusCode();
        String statusMessage = res.getStatusMessage();

Query

        // prepare URI
        final ODataURI uri = new ODataURI("http://services.odata.org/OData/Odata.svc");
        uri.append("Products(0)").expand("Supplier").select("Rating,Supplier/Name");

        // create new request
        final ODataRequest request = ODataRequestFactory.getQueryRequest(uri);

        // execute request
        final ODataResponse res = client.execute(request);

        // retrieve and process the query result
        for (ODataEntity entity : (Iterable<ODataEntity>) res.getBody()) {
            // .................
        }

Invoke Function

        // provide the target URI
        final ODataURI targetURI = new ODataURI("http://services.odata.org/OData/Odata.svc");
        targetURI.append("GetProductsByRating?rating=4");

        // create your request
        final ODataRequest request = ODataRequestFactory.getInvokeFunctionRequest(targetURI);

        // execute the request
        ODataResponse res = client.execute(request);

        // .....

Invoke Bindable Action

        // provide the target URI
        final ODataURI targetURI = new ODataURI("http://services.odata.org/OData/Odata.svc");
        targetURI.append("Product(0)/UpdateProductRating");

        Map<String, Object> parameters = Collections.<String, Object>singletonMap("rating", 2);

        // create your request
        final ODataRequest request = ODataRequestFactory.getInvokeActionRequest(targetURI, parameters);

        // execute the request
        ODataResponse res = client.execute(request);

        // .....

Metadata Query

        // create new request
        final ODataMetadataRequest request =
                ODataRequestFactory.getMetadataRequest("http://services.odata.org/OData/Odata.svc");

        // execute request
        final ODataMetadataResponse res = client.<ODataMetadataResponse>execute(request);

        // get access to metadata object
        ODataMetadata metadata = res.getBody();

        // (sample) access EntityType
        EntityType entityType = metadata.getSchema(0).getEntityTypes().get(1);

        // (sample) access EntityContainer
        EntityContainer entityContainer = metadata.getSchema(0).getEntityContainers().get(0);

Clone this wiki locally