0% found this document useful (0 votes)
83 views7 pages

Working With Odata Services: Week 2 Unit 6

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

WEEK 2 UNIT 6

WORKING WITH ODATA SERVICES


Please perform the exercises below in your app project as shown in the video.

Table of Contents

1 Disable Batch Processing for Debugging .................................................................................... 2


2 Display Data From a Related Entity Using Expand ..................................................................... 3
3 (Optional) Update Mock Server Metadata ................................................................................... 5

Preview

Figure 1 - Preview of the app after doing this unit’s exercises


openSAP – Developing Web Apps with SAPUI5

1 DISABLE BATCH PROCESSING FOR DEBUGGING


In this step, we manually turn off batch processing to inspect the calls of the OData model.

webapp/Component.js
sap.ui.define([
"sap/ui/core/UIComponent"
], function (UIComponent) {
"use strict";

return UIComponent.extend("opensap.myapp.Component", {

metadata : {
manifest: "json"
},

init : function () {
// call the init function of the parent
UIComponent.prototype.init.apply(this, arguments);

// used only for this lessons to show the request individually...


this.getModel().setUseBatch(false);

// additional initialization can be done here


}

});
});

In the init method of the component, we add a single line that manually turns off batch processing to
be able to debug calls from the ODataModel to the server easily. By default, batch processing is
turned on in SAPUI5, so all calls that would otherwise be sent to a server in a single request are
composed to a batch request that combines multiple requests.

This is a great performance boost, especially when network latency is high, but also makes debugging
hard. That is why we manually turn off batch processing in our app so that you can analyze how
changes in your app - for example adding or removing filters - influence the calls sent to the backend
service.

Note: Only do this for testing!


Do not turn of batch processing in productive applications as it might slow down the application flow
and impact performance negatively!

Copyright/Trademark
openSAP – Developing Web Apps with SAPUI5

2 DISPLAY DATA FROM A RELATED ENTITY USING EXPAND


In this step, we will expand the list binding to show data from another OData entity

webapp/view/App.view.xml
<mvc:View>

<List
id="productsList"
items="{
path: '/ProductSet',
parameters : {
expand: 'ToSupplier'
},
sorter : {
path : 'Category',
group : true
}
}"
growing="true"
growingThreshold="5"
growingScrollToLoad="false">
<headerToolbar>
<Toolbar>
<Title text="{i18n>productListTitle}"/>
<ToolbarSpacer/>
<SearchField width="50%"
search="onFilterProducts"/>
</Toolbar>
</headerToolbar>
<items>
<ObjectListItem
title="{Name}"
number="{Price}"
numberState="{= ${Price} > 500 ? 'Error' :
'Success'}"
intro="{ProductID}"
press="onItemSelected"
type="Active">
<firstStatus>
<ObjectStatus text="{
parts: [
{path: 'WeightUnit'},
{path: 'WeightMeasure'}
],
formatter : '.formatter.delivery'
}"/>
</firstStatus>
<secondStatus>
<ObjectStatus
title="{i18n>statusDeliveryFrom}"
text="{ToSupplier/Address/City}"/>
</secondStatus>
</ObjectListItem>
</items>
</List>

</mvc:View>

In this step we want to use the OData query parameter $expand to be able to display additional
information from a related entity.

Copyright/Trademark
openSAP – Developing Web Apps with SAPUI5

First we have to add the parameter to the list binding. For that, we use the parameters setting in the
binding and pass the ToSupplier navigation property to this parameter. We can now use properties
from this entity using relative paths. We add the secondStatus aggregation to our list item and add
an sap.m.ObjectStatus control into it. We bind its property text to the City property in the
BusinessPartner entity of the service, which is the target type of navigation property ToSupplier.

webapp/i18n/i18n.properties

# Data Binding Content
productListTitle=Products
...
statusDeliveryFrom=From

As we use a new translatable text, we adjust the i18n file as well.

Note: Mock Server and expands


If you use the mock data workaround, your page might stay blank at this point and there is an error in
the console saying that the ‘ToSupplier’ path could not be resolved. The service metadata does not
contain this navigation property yet, so replace your metadata with the contents in the next step.

Related Information
OData.org

Copyright/Trademark
openSAP – Developing Web Apps with SAPUI5

3 (OPTIONAL) UPDATE MOCK SERVER METADATA


If using the mock server, we need to modify the service metadata so that the supplier expand can be
mocked. The value will remain empty however, as the mock server is not able to automatically
generate mock data for expanded entities.

Note:
This workaround is only applicable if you are using the mock server as described in week 2 unit 1. It
will create random test data in your app if the service connection is too slow during these exercises.
The MockServer will be explained later in the course in more detail and would be set up a bit
differently in a real project.

webapp/metadata.xml
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="1.0"
xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xmlns:sap="http://www.sap.com/Protocols/SAPData">
<edmx:DataServices m:DataServiceVersion="2.0">
<Schema
xmlns="http://schemas.microsoft.com/ado/2008/09/edm"
Namespace="/IWBEP/GWSAMPLE_BASIC"
xml:lang="en"
sap:schema-version="1">
<EntityType Name="BusinessPartner" sap:content-version="1">
<Key>
<PropertyRef Name="BusinessPartnerID"/>
</Key>
<Property Name="Address" Type="/IWBEP/GWSAMPLE_BASIC.CT_Address"
Nullable="false"/>
<Property Name="BusinessPartnerID" Type="Edm.String"
Nullable="false" MaxLength="10" sap:label="Geschäftspartner-ID"
sap:creatable="false" sap:updatable="false"/>
<Property Name="CompanyName" Type="Edm.String" Nullable="false"
MaxLength="80" sap:label="Firma"/>
<Property Name="WebAddress" Type="Edm.String" sap:sortable="false"
sap:filterable="false" sap:semantics="url"/>
<Property Name="EmailAddress" Type="Edm.String" Nullable="false"
MaxLength="255" sap:label="E-Mail" sap:semantics="email"/>
<Property Name="PhoneNumber" Type="Edm.String" MaxLength="30"
sap:label="Telefonnr." sap:semantics="tel"/>
<Property Name="FaxNumber" Type="Edm.String" MaxLength="30"/>
<Property Name="LegalForm" Type="Edm.String" MaxLength="10"
sap:label="Rechtsform"/>
<Property Name="CurrencyCode" Type="Edm.String" Nullable="false"
MaxLength="5" sap:label="Währungscode" sap:semantics="currency-code"/>
<Property Name="BusinessPartnerRole" Type="Edm.String"
Nullable="false" MaxLength="3" sap:label="GeschPartnRolle"/>
<Property Name="CreatedAt" Type="Edm.DateTime" Precision="7"
sap:label="Zeitstempel" sap:creatable="false" sap:updatable="false"/>
<Property Name="ChangedAt" Type="Edm.DateTime" Precision="7"
ConcurrencyMode="Fixed" sap:label="Zeitstempel" sap:creatable="false"
sap:updatable="false"/>
</EntityType>
<EntityType Name="Product">
<Key>
<PropertyRef Name="ProductID"/>
Copyright/Trademark
openSAP – Developing Web Apps with SAPUI5

</Key>
<Property Name="ProductID" Type="Edm.String" Nullable="false"
MaxLength="10"/>
<Property Name="TypeCode" Type="Edm.String" Nullable="false"
MaxLength="2"/>
<Property Name="Category" Type="Edm.String" Nullable="false"
MaxLength="40"/>
<Property Name="Name" Type="Edm.String" Nullable="false"
MaxLength="255"/>
<Property Name="NameLanguage" Type="Edm.String" MaxLength="2"/>
<Property Name="Description" Type="Edm.String" MaxLength="255"/>
<Property Name="DescriptionLanguage" Type="Edm.String"
MaxLength="2"/>
<Property Name="SupplierID" Type="Edm.String" Nullable="false"
MaxLength="10"/>
<Property Name="SupplierName" Type="Edm.String" MaxLength="80"/>
<Property Name="TaxTarifCode" Type="Edm.Byte" Nullable="false"/>
<Property Name="MeasureUnit" Type="Edm.String" Nullable="false"
MaxLength="3"/>
<Property Name="WeightMeasure" Type="Edm.Decimal" Precision="13"
Scale="3"/>
<Property Name="WeightUnit" Type="Edm.String" MaxLength="3"/>
<Property Name="CurrencyCode" Type="Edm.String" Nullable="false"
MaxLength="5"/>
<Property Name="Price" Type="Edm.Decimal" Precision="16"
Scale="3"/>
<Property Name="Width" Type="Edm.Decimal" Precision="13"
Scale="3"/>
<Property Name="Depth" Type="Edm.Decimal" Precision="13"
Scale="3"/>
<Property Name="Height" Type="Edm.Decimal" Precision="13"
Scale="3"/>
<Property Name="DimUnit" Type="Edm.String" MaxLength="3"
sap:label="Dimension Unit"/>
<Property Name="CreatedAt" Type="Edm.DateTime" Precision="7"/>
<Property Name="ChangedAt" Type="Edm.DateTime" Precision="7"
ConcurrencyMode="Fixed"/>
<NavigationProperty Name="ToSupplier"
Relationship="/IWBEP/GWSAMPLE_BASIC.Assoc_Products_BusinessPartner"
FromRole="FromRole_Assoc_Products_BusinessPartner"
ToRole="ToRole_Assoc_Products_BusinessPartner"/>
</EntityType>
<EntityContainer Name="/IWBEP/GWSAMPLE_BASIC_Entities"
m:IsDefaultEntityContainer="true">
<EntitySet Name="BusinessPartnerSet"
EntityType="/IWBEP/GWSAMPLE_BASIC.BusinessPartner" sap:content-
version="1"/>
<EntitySet Name="ProductSet"
EntityType="/IWBEP/GWSAMPLE_BASIC.Product"/>
<Association Name="Assoc_Products_BusinessPartner" sap:content-
version="1">
<End Type="/IWBEP/GWSAMPLE_BASIC.BusinessPartner"
Multiplicity="1" Role="FromRole_Assoc_BusinessPartner_Products"/>
<End Type="/IWBEP/GWSAMPLE_BASIC.Product" Multiplicity="*"
Role="ToRole_Assoc_BusinessPartner_Products"/>
<ReferentialConstraint>
Copyright/Trademark
openSAP – Developing Web Apps with SAPUI5

<Principal Role="FromRole_Assoc_Products_BusinessPartner">
<PropertyRef Name="SupplierID"/>
</Principal>
<Dependent Role="ToRole_Assoc_Products_BusinessPartner">
<PropertyRef Name="BusinessPartnerID"/>
</Dependent>
</ReferentialConstraint>
</Association>
<AssociationSet Name="Assoc_Products_BusinessPartner_AssocSet"
Association="/IWBEP/GWSAMPLE_BASIC.Assoc_Products_BusinessPartner"
sap:creatable="false" sap:updatable="false" sap:deletable="false"
sap:content-version="1">
<End EntitySet="ProductSet"
Role="FromRole_Assoc_Products_BusinessPartner"/>
<End EntitySet="BusinessPartnerSet"
Role="ToRole_Assoc_Products_BusinessPartner"/>
</AssociationSet>
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>

Replace the contents of the file metadata.xml with the updated service interface. The entity
BusinessPartner has been added.

Coding Samples
Any software coding or code lines/strings (“Code”) provided in this documentation are only examples
and are not intended for use in a productive system environment. The Code is only intended to better
explain and visualize the syntax and phrasing rules for certain SAP coding. SAP does not warrant the
correctness or completeness of the Code provided herein and SAP shall not be liable for errors or
damages cause by use of the Code, except where such damages were caused by SAP with intent or
with gross negligence.

Copyright/Trademark

You might also like