-
Notifications
You must be signed in to change notification settings - Fork 21
Usage
Francesco Chicchiriccò edited this page May 21, 2013
·
18 revisions
This page shows some sample usage of ODataJClient, either at high (e.g. Proxy) and low (e.g. Engine) level.
DemoService demoService = null; // an instance of DemoService is provided by the context
// Take the product with key value 3
Product product3 = demoService.getProducts().get(3);
DemoService demoService = null; // an instance of DemoService is provided by the context
// 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 = null; // an instance of DemoService is provided by the context
// invoke GetProductsByRating
Collection<Product> products = demoService.getProductsByRating(15);
DemoService demoService = null; // an instance of DemoService is provided by the context
// 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
DemoService demoService = null; // an instance of DemoService is provided by the context
// 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
// 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()) {
// .................
}