Skip to content

Parameter Construction

Parameters are created in different scenarios when accessing information in the service metadata document and interacting with the OData provider. These scenarios are listed in order of ease of use:

  • Proxy Classes
  • Dynamic API
  • Metadata API

This section illustrates code for proxy classes and the Dynamic API to illustrate the convenience offered by proxy classes. Note that both methods require knowledge of the service metadata with one significant difference: Android Studio can leverage the proxy classes to offer syntax assist and code completion.

Entity Set

// Proxy Class
// Use the generated metadata class and the class variable for the entity set
EntitySet eventSet = EventServiceMetadata.EntitySets.events;

// Dynamic API
// Look up via DataService using the name of the entity set
EntitySet eventSet = dataService.getEntitySet("Events");
// Proxy Class
// Use the generated metadata class and the class variable for the entity set
val eventSet = EventServiceMetadata.EntitySets.events

// Dynamic API
// Look up via DataService using the name of the entity set
val eventSet = dataService.getEntitySet("Events")

Entity Type

// Proxy Class
// Use the generated metadata class and the class variable for the entity type
EntityType eventType = EventServiceMetadata.EntityTypes.event;

// Dynamic API
// Get the entity type via the entity set
EntitySet eventSet = dataService.getEntitySet("Events");
EntityType eventType = eventSet.getEntityType();
// Proxy Class
// Use the generated metadata class and the class variable for the entity type
val eventType = EventServiceMetadata.EntityTypes.event

// Dynamic API
// Get the entity type via the entity set
val eventSet = dataService.getEntitySet("Events")
val eventType = eventSet.entityType

Property

// Proxy Class
// Use the generated strongly-typed class and the class variable for the property
Property eventNameProp = Event.name;

// Dynamic API
EntitySet eventSet = dataService.getEntitySet("Events");
EntityType eventType = eventSet.getEntityType();
// Get the property via the entity type using the property name
Property eventNameProp = eventType.getProperty("Name");
// Proxy Class
// Use the generated strongly-typed class and the class variable for the property
val eventNameProp = Event.name

// Dynamic API
val eventSet = dataService.getEntitySet("Events")
val eventType = eventSet.entityType
// Get the property via the entity type using the property name
val eventNameProp = eventType.getProperty("Name")

We do not have an example for a complex type in our sample OData service. However, properties are retrieved in a similar way. Consider that we have a complex type, Address, with street, city, state, and zipcode, and access the property for city:

// Proxy Class
// Use the generated strongly-typed complex class and the class variable for the property
Property addressCityProp = Address.city;

// Dynamic API
// Get the complex type from data service using namespace and property name
ComplexType addressType = dataService.getMetadata().getComplexType("ODataAPI.Address");
Property addressCityProp = addressType.getProperty("City");
// Proxy Class
// Use the generated strongly-typed complex class and the class variable for the property
val addressCityProp = Address.city

// Dynamic API
// Get the complex type from data service using namespace and property name
val addressType = dataService.metadata.getComplexType("ODataAPI.Address")
val addressCityProp = addressType.getProperty("City")

EntityValue

To create an instance of EntityValue for an entity type:

// Proxy Class
// Use the constructor of the generated strongly-typed class
Event event = new Event();
// Set the property values
event.setName("SAP Sapphire 2016");
event.setDescription("ASUG Annual Conference");
event.setCountry("USA");

// Dynamic API
EntitySet eventSet = dataService.getEntitySet("Events");
EntityType eventType = eventSet.getEntityType();
// Create a new instance of Event type
EntityValue entityValue = EntityValue.ofType(eventType);
// Set the property values
eventType.getProperty("Name").setString(entityValue, "SAP Sapphire 2016");
eventType.getProperty("Description").setString(entityValue, "ASUG Annual Conference");
eventType.getProperty("Country").setString(entityValue, "USA");
// Proxy Class
// Use the constructor of the generated strongly-typed class
val event = Event()
// Set the property values
event.name = "SAP Sapphire 2016"
event.description = "ASUG Annual Conference"
event.country = "USA"

// Dynamic API
val eventSet = dataService.getEntitySet("Events")
val eventType = eventSet.entitySet
// Create a new instance of Event type
val entityValue = EntityValue.ofType(eventType)
// Set the property values
eventType.getProperty("Name").setString(entityValue, "SAP Sapphire 2016")
eventType.getProperty("Description").setString(entityValue, "ASUG Annual Conference")
eventType.getProperty("Country").setString(entityValue, "USA")

Similarly, retrieve the property value from an EntityValue instance:

// Proxy Class
String name = event.getName();
String description = event.getDescription();
String country = event.getCountry();

// Dynamic API
String name = eventType.getProperty("Name").getNullableString(entityValue);
String description = eventType.getProperty("Description").getNullableString(entityValue);
String country = eventType.getProperty("Country").getNullableString(entityValue);
// Proxy Class
val name = event.name
val description = event.description
val country = event.country

// Dynamic API
val name = eventType.getProperty("Name").getNullableString(entityValue)
val description = eventType.getProperty("Description").getNullableString(entityValue)
val country = eventType.getProperty("Country").getNullableString(entityValue)

The generated getter methods of Property class handle the nullability and return null when appropriate. To avoid exceptions, the nullable version of the getter method should be used if the value can be null.

// getString may throw an exception if the value is indeed null
String country = eventType.getProperty("Country").getString(entityValue);
// getString may throw an exception if the value is indeed null
val country = eventType.getProperty("Country").getString(entityValue)

Entity Key

// Proxy Class
// Simple key
EntityKey eventKey = Event.key(1000L);
// Composite key
EntityKey userSessionAgendaKey = UserSessionAgenda.key(1000L, "user");

// Dynamic API
// Simple Key
DataValue keyValue = LongValue.of(1000L);
EntityKey eventKey = new EntityKey().with("EventID", keyValue);
// Composite Key
DataValue sessionIdValue = LongValue.of(1000L);
DataValue userIdValue = StringValue.of("user");
EntityKey key = new EntityKey().with("SessionID", sessionIdValue).with("UserID", userIdValue);
// Proxy Class
// Simple key
va; eventKey = Event.key(1000L)
// Composite key
val userSessionAgendaKey = UserSessionAgenda.key(1000L, "user")

// Dynamic API
// Simple Key
val keyValue = LongValue.of(1000L)
val eventKey = EntityKey().with("EventID", keyValue)
// Composite Key
val sessionIdValue = LongValue.of(1000L)
val userIdValue = StringValue.of("user")
val key = EntityKey().with("SessionID", sessionIdValue).with("UserID", userIdValue)

Last update: August 12, 2020