Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Odata V4 : The New way to REST for Your Applications
1. Alok Chhabria & Kritika Goyal (SAP Labs)
July 20, 2018
ODATA V4
The new way to REST
for your applications
2. 2
• What is ODATA?
• Improvements in ODATA V4 over ODATA V2 ?
• SAP UI5 ODATA V4 Model
• Hands-On
- Initial Setup
- Data Access and Client Server Communication
- Automatic Data Type detection
- Batch Groups
- CRUD Enablement
- ODATA Operations (If time permits)
• Q and A
AGENDA
4. 4
WHAT is ODATA?
According to www.odata.org :
OData (Open Data Protocol) is an ISO/IEC approved, OASIS standard that
defines a set of best practices for building and consuming RESTful APIs.
OData helps you focus on your business logic while building RESTful APIs
without having to worry about the various approaches to define request and
response headers, status codes, HTTP methods, URL conventions, media
types, payload formats, query options, etc. OData also provides guidance for
tracking changes, defining functions/actions for reusable procedures, and
sending asynchronous/batch requests.
OData RESTful APIs are easy to consume. The OData metadata, a machine-
readable description of the data model of the APIs, enables the creation of
powerful generic client proxies and tools.
5. Industry-standard protocol for creating and consuming data APIs
Based on core protocols like HTTP and open standards like XML and JSON
Enables provision of data services based on REST principles
Initiated by Microsoft, adopted by SAP, IBM and others
Data handling using URLs constructed with specific rules
Defines data formats representing resources like collections, entities, properties
Optimized for consuming data, also known as “ODBC of the web”
OData URL structure:
http://services.odata.org/Northwind/Northwind.svc/Customers?$top=3
Service Root URI Resource Path Query options
Introduction to ODATA
6. ODATA V4 TripPin Sample Service-
http://services.odata.org/V4/TripPinServiceRW
http://www.odata.org/odata-services/
TripPin provides a service that can manage people's trips.
Key Features :
• Person Entity People Collection of
entity
• Friends is a Navigation Property
• Gender is Enum Property
• Loc is a Spatial Property
• Photo is a media entity
• Create Update Delete
• Batch
• Async Request
• And many more…
Data Model
7. ODATA V4 TripPin Sample Service
http://services.odata.org/V4/TripPinServiceRW
http://www.odata.org/odata-services/
Try it Out ! (use POSTMAN or chrome )
.
we only use the entity type Person of the TripPin service. The entities of type Person are collected in the entity set People. Each Person has a
key property UserName and the properties Age, FirstName, and LastName
Description URL
Request People
Entity
http://services.odata.org/TripPinRESTierService/People
Request Single Entity http://services.odata.org/TripPinRESTierService/People('russellwhyte')
Request Single
Property (Enum)
http://services.odata.org/TripPinRESTierService/People('russellwhyte')/Gender/$value
$filter http://services.odata.org/TripPinRESTierService/People?$filter=FirstName eq 'Scott’
Metadata http://services.odata.org/V4/TripPinService/$metadata
$orderBy http://services.odata.org/V4/TripPinService/People?$orderby=length(FirstName)
desc,UserName
9. What has improved in ODATA V4
Performance
New JSON Format (data): ~ 10% to 60% payload size reduction for lists after compression
No inline metadata
Messages part of payload (compressible) – V2 only header (not compressible)
Lazy loading of huge service models
In ODATA V2 payload had to be retrieved in both XML and JSON. With V4 XML is optional.
Features
Improved datatypes: New data types (esp. in date/time area, e.g. Duration), inheritance, collection
values
$filter with lambda operators ”any” and “all” for conditions on navigation properties
People?$filter=Emails/any(a:endswith(a,'@example.com'))
$expand with query options
People?$expand=Friends($filter=Emails/any(a:endswith(a,'@example.com')))
… and many more
10. New JSON Format - Sample Payload V2 vs. V4
V2 Payload Northwind Order_Details
V4 Payload Northwind Order_Details
11. New Data Types
• Date
• Fiori: V2 only has DateTime;
workaround "ignore time" is
error prone
(time zones, accidental day shift)
• TimeOfDay, Duration
• Replacements for Edm.Time
which is a duration of max. 24
hours
Payload Example:
"DateValue": "2012-12-03"
"DateTimeOffsetValue": "2012-12-03T07:16:23Z“
Edm.Date Date without a time-zone offset
Edm.DateTimeOffset Date and time with a time-zone offset, no leap seconds
Edm.Duration Signed duration in days, hours, minutes, and (sub)seconds
Edm.TimeOfDay Clock time 00:00-23:59:59.999999999999
Payload Example:
"TimeOfDayValue": "07:59:59.999"
"DurationValue": "P12DT23H59M59.999999999999S"
12. • Geo types for geographic data, and corresponding $filter functions
• Ideal fit with HANA Spatial
Payload Example:
"GeographyPoint": {"type": "Point","coordinates":[142.1,64.1]}
Edm.GeographyPoint A point in a round-earth coordinate system
… …
New Data Types
14. The OData Model enables binding of control
properties and aggregations to data from a
remote server
The complete dataset is stored on the server
and only the requested fields are transferred
to the client
Currently supported versions:
sap.ui.model.odata.v2.ODataModel
sap.ui.model.odata.v4.ODataModel
(Limited Feature Scope)
UI5 Component
View
Controllers
Server-side
Model
DB data
Data Engine
ODATA Model in UI5
15. 15
Difference in ODATA V4 and ODATA V2 Models
OData operations executed via binding: Model
does not support the method callFunction.
Simplification: Use an operation binding instead;
it is now much easier to bind operation execution
results to controls.
No CRUD methods on model: Model does not
support the
methods create, read, remove, update.
Simplification: read, update, create and remove o
perations are available implicitly via the bindings,
so that changes are bound to controls.
Minimize APIs required for batch control: Model
does not support the
methodsgetChangeBatchGroups, getChangeGro
ups, getDeferredGroups,setChangeBatchGroups,
setChangeGroups, setDeferredBatchGroups,set
DeferredGroups, setUseBatch (and
corresponding model construction parameters).
Simplification: Batch groups are solely defined via
binding parameters with the corresponding
parameters on the model as default. You just
need the submitBatchmethod on the model to
control execution of the batch.
17. Setup Development Environment
• Setup SAP Cloud Platform HANA Trial Account
(https://account.hanatrial.ondemand.com/)
• After logging in, activate Neo trial.
• Go to Connectivity -> Destinations
• Create a destination with the following configuration:
• Go to Services -> Search WebIDE -> Launch WebIDE
18. Step 1: The Initial App
https://openui5.hana.ondemand.com/#/topic/15d84f36c0594cb2b3295aa1f55c
b961
• Follow the steps mentioned in the tutorial given on the link
• After clicking on the above hyperlink, click on the show source code button as shown below:
• Click on Download button and follow the further steps
19. Step 1: The Initial App
Continued..
• Import the downloaded project
• Run the project using index.html
• Currently the project runs with Mockdata
• Go to index.html and comment the following line of code:
• This causes your requests to go to actual northwind service
• Run the app- You will see data is not loaded.
20. Step 1: The Initial App
Continued..
• Right click on your project. Go to NEW -> HTML5 Application Descriptor
• You have a neo-app.json file added to your project
• Add the destination configuration code to your neo-app file.
• In your manifest file change your service URI to relative URI
Instead of hardcoded (To avoid CORS issue)
• Now the requests are routed to REAL backend service via the destination configured on SCP
• We will use real service for all exercises.
• If you face issues with the real service, feel free to switch to
• Mockserver by uncommenting the code in index.html
21. Step 2: Data Access and Client-Server Communication
https://openui5.hana.ondemand.com/#/topic/e9de002ddf1e478fbb9232bfba83d
cf0
• Add a refresh button and refresh the model if it doesn’t have any pending changes
• Study the calls in Chrome Developer Tools Console
• For more details, follow steps in link
22. Step 3: Automatic datatype detection
https://openui5.hana.ondemand.com/#/topic/96bb6e06fa22491fa65a5040f6da0
99c
• Add handleValidation: true to see UI validations based on metadata information
• You can start the mockserver, update local metadata and see the changes reflect in real time without
additional UI5 coding
• For detailed steps, follow the link given above
Auto-type detection
Derive UI5 type from $metadata
Formatting, parsing, validation between model value and UI value
Override by specifying type in binding information
Binding mode: TwoWay is default Changes to model written to service backend
23. Step 4: Filtering, Sorting and Counting
https://openui5.hana.ondemand.com/#/topic/426ff318051a465191c861b51a74f
00e
• Here we add capabilities of Sort and Filter to our applications.
• Follow the steps in the tutorial
• The REAL TRIP PIN service does not support COUNT yet. To try out the COUNT functionality, switch to
Mockserver.
24. Step 5: Batch Groups
https://openui5.hana.ondemand.com/#/topic/ef2af4957fae469e9203e98006d3e
e75
• Batch Groups are used to group multiple requests into one to improve overall performance.
• Follow the steps in the tutorial
• Check the Batch Request in the Chrome developer tools Console and analyze the batch requests
25. Process Flow for Create Implementation
Attach Controller function for Create
Event in view
Call oBinding.create method to
create a new row with blank values
Refresh the model and set focus on
the newly created row
Step 6: Create and Edit
https://openui5.hana.ondemand.com/#/topic/b4f12660538147f8839b05cb03f1d
478
26. Step 6: Create and Edit
Continued…
Process Flow for UI Update Implementation
Attach OnInputChange event on all
Input fields (on liveChange property)
Set the UIChanges property as true if
the event is fired
If UserName has been changed, set
property UserNameEmpty to false
(for validation purposes)
27. Step 6: Create and Edit
Continued…
Process Flow for Save Implementation
Add SAVE button on footer, attach
event onPress
Define success and function
handlers for the ODATA request
Save the data to your backend by
calling SubmitBatch method with
appropriate batchGroupID
28. Step 6: Create and Edit
Continued…
Process Flow for Reset Implementation
Add RESET button on footer, attach
event onPress
Call method ResetChanges on the
items binding of the list
29. Step 6: Create and Edit
Continued…
Message Handling Process Flow
Initialize MessageManager and
MessageModel
AttachChange on MessageModel
ListBinding (Filter based on technical
Messages)
Get the technical messages and
display in MessageBox
30. Step 7: Delete
https://openui5.hana.ondemand.com/#/topic/12a0d1ef150a42ef81e9f07fe6407
018
Steps to follow for Delete Operation
Add DELETE button on the UI and
attach OnPress event
Get Binding Context for the selected
item and call delete function of the
context
Attach Success and Error handlers
We send parameter “$AUTO” for the delete call to ensure immediate effect of the request
Ensure only one record can be
selected in your list
31. Step 7: OData Operations
https://openui5.hana.ondemand.com/#/topic/a3e7cb6f671b4b839f37eb5f88429
e41
Steps to follow:
Add RESTART button on the UI and attach
OnPress event
The ResetDataSource action of the TripPin
service will reset the changes made in the
model during the complete exercise
Execute the operation using execute() method,
attach relevant success and failure handlers
We use (…) syntax to make the binding DEFERRED. This way the action can be called only on explicitly calling execute() method
Call bindContext method of the model with the
relevant action name
32. QUIZ TIME
• How do we enable BATCH in ODATA V4 model?
• Tell one difference between ODATA V4 Model and ODATA V2 Model?
• Tell one distinct feature of ODATA V4 which wasn’t available in V2?
• How to enable deferred processing for data operations? (Syntax)
• Can we apply sort and filter together on a list using ODATA V4?
• And one more……
33. QUIZ TIME
Did you enjoy the session?
Please give us your valuable feedback!!
Keep RESTing the V4 way ADIOS!