Working with Data

Feed the label with data from an OData stream.

So far you have assigned a static text to the label. This task feeds the label with data coming from an OData stream, fetched via the Internet from a server.

Add these libraries, which provide connectivity, OData parsing and caching, user authentication, and so on:
  • MAF
    • mafsdmdatasourceadapter-1.2.0.jar
    • sdmdatasource-1.2.0.jar
  • SAP Mobile Platform/SDM
    • SDMCommon-3.0.0.jar
    • SDMConnectivity-3.0.0.jar
    • SDMParser-3.0.0.jar
The Extensibility Framework includes two libraries for working with OData:
  • MAFSDMDataSourceAdapter – a data source adapter that converts between generic data format and OData.
  • SDMDataSource – a facade on top of the SAP Mobile Platform/SDM libraries that acts as a wrapper, simplifying the sequence of fetch and parse calls, and providing additional features, such as caching and demo data support.

This example uses the LeaveRequest endpoint:

http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/GBHCM/LEAVEREQUEST/

The OData feed contains a set of collections; this example uses the ApproverCollection. Add a binding definition at the beginning of the layout_phone.xml. The binding describes the business objects that feed data to the tile's UI. The boType must match the collection name for OData:
<?xml version="1.0" encoding="UTF-8"?>
<MAF xmlns="http://schemas.sap.com/maf/2012/cfg">

	<Binding bindingId="Approvers" type="collection">
		<P pid="boType" value="ApproverCollection"></P>
	</Binding>

	<!-- Define a "tile" - it translates to UIViewController -->
	<Tile tileId="First Extensible Screen">
…
This example displays the approver name from the OData entry identified by the ApproverEmployeeName property, which is part of the ApproverCollection collection. Next, add the binding reference to the tile:
<!-- Define a "tile" - it translates to UIViewController -->
<Tile tileId="First Extensible Screen">

	<!-- This is our root view -->
	<P pid="isRoot" value="true" />
	
	<!-- Reference to the data binding which provides data for this tile -->
	<BindingRef ref="Approvers"/>
Defining a new label is similar to the previous one, but the label's text is fed with data coming from the binding:
<!-- Define 2nd label -->
<UIElement type="label">
	<!-- The label text is showing real data coming from the OData feed -->
	<P pid="text" value="Approved By: {$Approvers.ApproverEmployeeName}"></P>
	<P pid="halign" value="center"></P>
	<P pid="margin_top" value="20pt"></P>
	<P pid="style" value="CustomLabel"></P>
</UIElement>

The {$Approvers.ApproverEmployeeName} syntax refers to the employee name. Curly brackets indicate that the content must be evaluated by the Extensibility Framework. The $ sign denotes a binding (or expression), followed by the collection and the entry name separated by a period.

Create and initialize the datasource in MainActivity’s onCreate method by inserting the following code snippet immediately after building MAFCore:
new Thread(new Runnable() {
	@Override
	public void run() {
		try {
			SDMSource source = null;
			SDMConnectivityParameters connectivityParameters = new SDMConnectivityParameters();
			connectivityParameters.setBaseUrl(BACKEND_BASE_URL_FOR_DIRECT_CONNECTION);
			connectivityParameters.setUserName(USER_NAME_FOR_DIRECT_CONNECTION);
			connectivityParameters.setUserPassword(PASSWORD_FOR_DIRECT_CONNECTION);

			synchronized (SDMSource.class) {
				source = new SDMSource(MainActivity.this.getApplicationContext(), connectivityParameters, "com.sap.mobile.lib.sdmconnectivity.SocketConnectionHandler");
				source.loadModel();
			}
			MAFDataSourceAdapter dataSourceAdapter = new MAFSDMDataSourceAdapter(source);
			mafCore.setDataSourceAdapter(dataSourceAdapter);

		} catch (Exception e) {
			MAFLogger.e("HelloWorld", "SDM data source exception during logon", e);
		}
		mafCore.switchScreen(MainActivity.this);
	}
}).start();
This approach allows passing in a custom data source adapter, but the default implementation works correctly for OData. This setup is all you need to build an extensible app that does not require custom hooks or mixing native UIs with UIs that are defined via metadata.

From this point on, you only have to enhance the layout_phone.xml to define new tiles, UI elements, bindings and so on.