-
Notifications
You must be signed in to change notification settings - Fork 21
Usage
Francesco Chicchiriccò edited this page May 23, 2013
·
18 revisions
This page shows some sample usage of ODataJClient, either at high (e.g. Proxy) and low (e.g. Engine) level.
The snippets below refer to the example presented in the design document.
DemoService demoService = entityContainerFactory.getEntityContainer(DemoService.class);
// Take the product with key value 3
Product product3 = demoService.getProducts().get(3);
AsyncDemoService demoService = entityContainerFactory.getEntityContainer(AsyncDemoService.class);
// Take the product with key value 3 asynchronously
Future<Product> product3 = demoService.getProducts().get(3);
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();
DemoService demoService = entityContainerFactory.getEntityContainer(DemoService.class);
// invoke GetProductsByRating
Collection<Product> products = demoService.getProductsByRating(15);
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();
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
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
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
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
// 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();
// 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();
// 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();
// 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();
// 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()) {
// .................
}
// 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);
// .....
// 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);
// .....