37
DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER GATEWAY CD263 Exercises / Solutions André Fischer, Jens Hüsken, Thomas Brüggemann / SAP AG Contents Gateway System M17 ..................................................................................................................................................... 2 The OData Service........................................................................................................................................................... 2 EXERCISE 1 - CHECKing prerequisites ................................................................................................................................. 4 ABAP Model Provider and Data Provider Classes ........................................................................................................... 4 CHECK Availability of your service Show the Service Document ................................................................................. 5 Exercise 2: Navigation Properties ..................................................................................................................................... 10 Goal of this exercise...................................................................................................................................................... 10 Implement data retrieval for the “DepartingFlights” navigation property .................................................................. 11 Exercise 3: Filter System Query Option ($filter) .............................................................................................................. 18 Goal of this exercise...................................................................................................................................................... 18 Implementation of filtering for “Arrival/Airport” ......................................................................................................... 19 Exercise 4: ASP.NET Frontend for the Flight Service ..................................................................................................... 22 Goal of this exercise...................................................................................................................................................... 22 Start the ASP.NET solution............................................................................................................................................ 22 Exercise 5: How to add a (complex) property to the Airport Entity ................................................................................. 25 Goal of this exercise...................................................................................................................................................... 25 Add a complex type and property “GeoCoordinates” to the model ............................................................................ 25 Exercise 6: Implement the data-retrieval of Geo-Coordinates......................................................................................... 29 Optional Exercise 7: Flight Bookings ................................................................................................................................. 33 Installation of the REST-Client for the “FireFox” browser ............................................................................................ 33 Booking a flight ............................................................................................................................................................. 34 Cancelling a booking ..................................................................................................................................................... 36

DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

Embed Size (px)

Citation preview

Page 1: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER GATEWAY CD263

Exercises / Solutions André Fischer, Jens Hüsken, Thomas Brüggemann / SAP AG

Contents Gateway System M17 ..................................................................................................................................................... 2

The OData Service ........................................................................................................................................................... 2

EXERCISE 1 - CHECKing prerequisites ................................................................................................................................. 4

ABAP Model Provider and Data Provider Classes ........................................................................................................... 4

CHECK Availability of your service – Show the Service Document ................................................................................. 5

Exercise 2: Navigation Properties ..................................................................................................................................... 10

Goal of this exercise ...................................................................................................................................................... 10

Implement data retrieval for the “DepartingFlights” navigation property .................................................................. 11

Exercise 3: Filter System Query Option ($filter) .............................................................................................................. 18

Goal of this exercise ...................................................................................................................................................... 18

Implementation of filtering for “Arrival/Airport” ......................................................................................................... 19

Exercise 4: ASP.NET Frontend for the Flight Service ..................................................................................................... 22

Goal of this exercise ...................................................................................................................................................... 22

Start the ASP.NET solution............................................................................................................................................ 22

Exercise 5: How to add a (complex) property to the Airport Entity ................................................................................. 25

Goal of this exercise ...................................................................................................................................................... 25

Add a complex type and property “GeoCoordinates” to the model ............................................................................ 25

Exercise 6: Implement the data-retrieval of Geo-Coordinates......................................................................................... 29

Optional Exercise 7: Flight Bookings ................................................................................................................................. 33

Installation of the REST-Client for the “FireFox” browser ............................................................................................ 33

Booking a flight ............................................................................................................................................................. 34

Cancelling a booking ..................................................................................................................................................... 36

Page 2: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

INTRODUCTION

SAP NetWeaver Gateway (Gateway) gives you the ability to expose your SAP business data as OData-Services. Gateway allows you to work with SAP interfaces in a way that their usage is possible without any SAP expert knowledge.

In this session you will expose a service via SAP NetWeaver Gateway using the REST-based Open Data Protocol. The

focus of this session is on the backend implementation of this service.

Furthermore, the consumption of this service from a given Rich Internet Application (RIA) will be demonstrated.

Gateway System M17 The Gateway system used in this session is a standalone Gateway system. This means that the Gateway server components, as well as the Gateway backend components have been deployed to this ABAP Application Server. System M17 is an AS ABAP 7.0 EhP2 SP8 server with SAP NetWeaver Gateway 2.0 SP1 installed. How to logon to M17:

1) Open SAP Logon

2) Select system M17

3) Chose button logon 4) Provide the following user credentials:

a. User: CD263-xx with xx being your group name b. Password: “Initial” c. Language: “en” d. Logon

The OData Service In our exercise each participant will use their own OData Service. The services are ready to be consumed and we will only enhance the existing coding to show the concept of how to implement them using the OData Channel API. We start with a basic data model that is shown in the figure on the next page. It contains three Entity types: and two complex types for:

Airports

Flights

Bookings

Location

GeoCoordinates

Navigation properties are based on associations between entity types. Associations are visualized in the figure by light grey lines with a crossing line at the beginning and two diamonds at the end

The entity type “Airport” offers two navigation properties:

o Using the navigation property “AirportsTo” you can retrieve the destination airports that can be reached from a given Airport

o Using a second navigation property “DepartingFlights” to the entity type “Flight” you can determine the list of flights that are departing from a certain airport.

The entity type “Flight” offers an association to the entity type “Booking” to retrieve all bookings that are made for a given flight, already.

Page 3: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

Complex Type references are visualized in the figure by dotted arrows.

The entity type “Flight” references the complex type “Location” two times, because its properties “Departure” and “Arrival” are both of type Location.

The entity type “Airport” has a property “GeoCoordinates” of type “GeoCoordinates”.

Page 4: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

EXERCISE 1 - CHECKING PREREQUISITES

ABAP Model Provider and Data Provider Classes An OData Channel service is basically implemented by two ABAP classes:

Meta Data Provider Class and

Data Provider Class. In this sample we use a Main Data Provider Class that behaves as a dispatcher and calls separate Data Provider Classes for each entity type. 1 Main Metadata Provider Class ZCD263_CL_MODEL_PROVIDER_0xx 1 Main Data Provider Class ZCD263_CL_DATA_PROVIDER_0xx 1 Data Provider Class & Interfaces per Entity Type Airport ZCD263_CL_PROVIDER_AIRPORT_0xx Booking ZCD263_CL_PROVIDER_BOOKING_0xx Flight ZCD263_CL_PROVIDER_FLIGHT_0xx You will find all required classes and interfaces relevant for this session in transaction SE80 under “Local Objects”. Please have a look on your local objects and verify that the following objects are amongst them (xx is your group number):

1. Start transaction SE80

2. From the dropdown box select “Local Objects” and then press the “Display”

button .

Page 5: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

3. Expand the “Class Library” and then the “Classes” and “Interfaces” Node. You will see the Model and Data Provider Classes and the appropriate Interfaces with your group number.

CHECK Availability of your service – Show the Service Document The service “ZCD263_ICF_SERVICE_0xx” (xx is your group number) is already available and activated in the backend which means that the service together with the model provider class and the data provider class is configured to act as a Gateway service. In general, if you are not able to finish one of the following exercises or face any other problems you may refer to the reference implementation classes that you can obtain by taking the according service class from above and leaving the suffix “_0xx” out (xx is your group number). The services that are available in a Gateway system are listed by the Service Registration transaction:

1. Start transaction “/n/IWFND/REG_SERVICE”

2. Enter “LOCAL” as System Alias and press the “Execute” Button or F8.

3. Select the service with your group number from the table at the bottom of the screen

4. Please, press the button “Call Browser”

Page 6: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

in order to call the browser with the service document of this service.

5. When you are asked whether you want to grant access, choose “Always allow” in the dropdown listbox on the popup.

6. Provide user

credentials for the browser. Use the same user/password you used to logon to system M17.

7. Now you should see

the following path in your browser‟s URI field where xx has to be replace by your group number: http://<hostname>:51080/sap/opu/sdata/sap/ZCD263_ICF_SERVICE_0xx/?sap-client=800&$format=xml This URL shows the Service Document of the OData Service. Please note that the Service Document contains the three feeds defined in the service, namely “Airports”, “Flights” and “Bookings”.

Page 7: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

Show the MetaData Document A Service Metadata Document is a self-describing set of metadata that provides an external software system with all the information necessary to consume the particular OData service. The metadata description can be obtained by adding the keyword $metadata to the end of the Service Document URL. http://<hostname>:51080/sap/opu/sdata/sap/ZCD263_ICF_SERVICE_0xx/$metadata

1. Add the keyword

$metadata to the end of the Service Document URL to retrieve the MetaData Document of the Service

http://<hostname>:51080/sap/opu/sdata/sap/ZCD263_ICF_SERVICE_0xx/$metadata

Page 8: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

Retrieve Data from the Service

Since we know the Service Document URL and since we know the names of the feeds being part of the service it is very easy to retrieve data from the service by simply adding the name of the feed to the Service Document URL.

http://<hostname>:51080/sap/opu/sdata/sap/ZCD263_ICF_SERVICE_001/Airports

Single entries in a collection (e.g. one airport) can easily be retrieved using the URL listed as the id.

<atom:id>

http://<hostname>:51080

/sap/opu/sdata/sap/ZCD263_ICF_SERVICE_001/Airports(IATACode='FRA')

</atom:id>

1. Add “Airports” to the the end of the Service Document URL to retrieve a list of airports,

http://<hostname>:51080/sap/opu/sdata/sap/ZCD263_ICF_SERVICE_0xx/Airports

Page 9: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

2. Select the URL from the <atom:id> entry to retrieve the data of a single airport (here „FRA‟) that is marked with a red rectangular in the screen shot above

http://<hostname>:51080 /sap/opu/sdata/sap/ZCD263_ICF_SERVICE_001/Airports(IATACode='FRA')

Add “Flights” to the the end of the Service Document URL to retrieve a list of flights.

http://<hostname>:51080/sap/opu/sdata/sap/ZCD263_ICF_SERVICE_0xx/Flights

Select the URL from the <atom:id> entry to retrieve the data of a single flight

Page 10: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

EXERCISE 2: NAVIGATION PROPERTIES

Goal of this exercise In this exercise we want to explain how one of the key features the so called Navigation Properties of the OData protocol are implemented in ABAP

Navigation Properties Navigation Property: A property of an Entry that represents a Link from the Entry to one or more related Entries. A Navigation Property is not a structural part of the Entry it belongs to.

1

In our sample service the navigation property “DepartingFlights” is implemented in the MetaData Provider Class but it has not been implemented in the data provider class yet. This we will do now in our exercise. Using the navigation property “DepartingFlights” the user shall be able to retrieve flights leaving from a given departure airport.

Check your service before implementation Since the Navigation Property Departing is not implemented yet in the Data Provider Class adding the Navigation Property to the URL does not produce any result. If you enter the following URL in your browser:

http://<hostname>:51080/sap/opu/sdata/sap/ZCD263_ICF_SERVICE_0xx/Airports('FRA')/DepartingFlights where “xx” denotes your group number you will notice that no results are shown.

1 http://www.odata.org

Page 11: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

Check the reference service If you use the URL of the already implemented reference service ZCD263_ICF_SERVICE (without your group number) you will see that it only lists flights that depart from Frankfurt „FRA‟. http://<hostname>:51080/sap/opu/sdata/sap/ZCD263_ICF_SERVICE/Airports('FRA')/DepartingFlights

Implement data retrieval for the “DepartingFlights” navigation property In the following we will implement the data retrieval for the navigation property “DepartingFlights” of “Airport” entity “Airport”. This has to be done in the Data Provider Class of the “Flight” entity.

1. Start transaction SE80

2. From the dropdown box select “Local Objects” and then press the “Display”

button .

Page 12: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

3. Expand the “Class Library” and then the “Classes”. Double-Click on the entry of class ZCD263_CL_PROVIDER_FLIGHT_0xx Where xx denotes your group number The screen shot shows this for group number 04. Please use your group number instead.

4. Double-Click on the method GET_ENTITYSET (which is highlighted on the screen shot). As a result the coding of the GET_ENTITYSET method is shown on the right hand side.

Page 13: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

1. Select the code line that contains the following statement: „IF it_navigation_

path IS INITIAL.“ and set an external breakpoint by pressing the “Set/Delete External Breakpoint” button

.

2. Check the hostname of the SAP application server you are working

The hostname is shown in the bottom right hand corner of the SAPGUI window (see screen shot above). In this case we are working on the application server wdflbmt0692

It should be one of the following: wdflbmt0691.wdf.sap.corp wdflbmt0692.wdf.sap.corp

3. Run the following URL

in your browser

http://<hostname>:5

1080/sap/opu/sdata/s

ap/ZCD263_ICF_SE

RVICE_0xx/Flights

If the external breakpoint works you will see a „Connecting…“ in the browser tab

and a „Waiting for http://...“ in the status bar of your browser

4. Notice the blinking SAPGUI icon. A new window with the ABAP Debugger session has opened.

5. Open the ABAP

Debugger Window Double-click on the variable

“it_navigation_path”.

You will notice that the table IT_NAVIGATION_PATH is empty.

[0x6(48)] means that there are no entries in the table that has 6

columns.

As a result the select statement will be performed and the service will

Page 14: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

provide a result set of flights.

6. Press the Continue

(F8) button.

You will observe

that a result set for

flights is shown in

the browser.

Page 15: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

7. Check again with a URL that contains the navigation property The following URL shall retrieve all flights departing from Frankfurt 'FRA'. http://<hostname>:51080/sap/opu/sdata/sap/ZCD263_ICF_SERVICE_0xx/Airports('FRA')/DepartingFlights

You will notice that the table IT_NAVIGATION_PATH is this time

NOT empty.

[1x6(48)] means that there is one entry in the table that has 6 columns.

As a result the select statement will NOT be performed and the service

will NOT provide a result set of flights.

8. Press the Continue

(F8) button.

You will observe

that no results are

shown in the

browser.

Page 16: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

9. Comment out the original select statement. To do so first press the “Display <-> Change”

Button . Then select the coding you want to comment out. From the context menu (right mouse click) select “Format Comment Out Lines” as shown in the screen shot.

* IF it_navigation_path IS INITIAL.

* SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_f

lights UP TO is_paging-top ROWS

* FROM ( spfli AS c

* INNER JOIN sflight AS f ON f~carrid =

c~carrid

* AND f~connid =

c~connid ).

* Endif.

3. Un-comment the next block of coding in the GET_ENTITYSET method of class ZCD263_CL_PROVIDER_FLIGHT_0xx To do so select the coding you want to un-comment. From the context menu (right mouse click) select “Format Uncomment Lines” as shown in the screen shot. If the navigation path is not initial the coding after the ELSE statement is used and the WHERE clause is populated from the

Page 17: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

values retrieved from the navigation path

IF it_navigation_path IS INITIAL.

SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_flights

UP TO is_paging-top ROWS

FROM ( spfli AS c

INNER JOIN sflight AS f ON f~carrid = c~carr

id

AND f~connid = c~conn

id ).

ELSE.

READ TABLE it_key_tab ASSIGNING <key> INDEX 1.

SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_flights

UP TO is_paging-top ROWS

FROM ( spfli AS c

INNER JOIN sflight AS f ON f~carrid = c~carr

id

AND f~connid = c~conn

id )

WHERE c~airpfrom = <key>-value.

ENDIF.

4. Save and

activate your

changes

5. Check again with a URL that contains the navigation property http://<hostname>:51080/sap/opu/sdata/sap/ZCD263_ICF_SERVICE_0xx/Airports('FRA')/DepartingFlights This time you should get a result set.

Page 18: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

EXERCISE 3: FILTER SYSTEM QUERY OPTION ($FILTER)

Goal of this exercise In this exercise we want to explain how one of the key features called Filter System Query Option ($filter) of the OData protocol is implemented in ABAP using the OData Channel.

Filter System Query Option ($filter) A URI with a $filter System Query Option identifies a subset of the Entries from the Collection of Entries identified by the Resource Path section of the URI.

2

In our service we have to implement a filtering option for the arrival airport in order to be able to limit the query result to those flights that have specific departure airports (selected via the navigation property) and a specific arrival airport. Let‟s have a look at the structure of the “Flight” entity. Please note that “Airport” is not a simple property of the entity “Flight”. Flight has however a complex property “Arrival” of type “Location”. Flight Entity:

<EntityType Name="Flight" >

<Key> <PropertyRef Name="AirlineId" />

<PropertyRef Name="ConnectionNo" />

<PropertyRef Name="FlightDate" />

</Key>

<Property Name="AirlineId" Type="Edm.String" … />

<Property Name="ConnectionNo" Type="Edm.String" … />

<Property Name="FlightDate" Type="Edm.DateTime" … />

<Property Name="Price" Type="Edm.Decimal" … />

… <Property Name="Departure" Type="Location" … />

<Property Name="Arrival" Type=" Location" … />

<NavigationProperty Name="Bookings" … />

</EntityType> “Airport” is a property of the complex type location. Complex Type Location:

<ComplexType Name="Location"> <Property Name="Country" Type="Edm.String" sap:filterable="false" />

<Property Name="City" Type="Edm.String" sap:filterable="false" />

<Property Name="Airport" Type="Edm.String" sap:filterable="true" />

<Property Name="Time" Type="Edm.Time" sap:label="Departure" sap:filterable="false" />

</ComplexType> The query option that is passed as an URL parameter therefore looks like follows. $filter=Arrival/Airport eq 'JFK'

2 http://www.odata.org

Page 19: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

As a result the following URL should work for you after having completed this exercise:

http://<hostname>:51080 /sap/opu/sdata/sap/ZCD263_ICF_SERVICE_0xx/Airports('FRA')/DepartingFlights/? $filter=Arrival/Airport%20eq%20'JFK It actually works already for the reference service http://wdflbmt0691.wdf.sap.corp:51080 /sap/opu/sdata/sap/ZCD263_ICF_SERVICE_0xx/Airports('FRA')/DepartingFlights/? $filter=Arrival/Airport%20eq%20'JFK

Implementation of filtering for “Arrival/Airport” In the following we will implement a filtering option for the Flight collection for the property “Arrival/Location”

1. Start transaction SE80

2. From the dropdown box select “Local Objects” and then press the “Display”

button .

3. Expand the folder “Class Library” and the folder then the “Classes”. Double-Click on the entry of class ZCD263_CL_PROVIDER_FLIGHT_0xx Where xx denotes your group number

4. Double-Click on the method GET_ENTITYSET (which is highlighted on the screen shot). As a result the coding of the GET_ENTITYSET method is shown on the right hand side.

Page 20: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

5. The code that is currently active is highlighted. It shall be replaced with the third code block that is currently un-commented. To do so first press the “Display <-> Change”

Button .

6. Comment out the coding that you un-commented in the previous exercise. To do so select the coding you want to comment out. From the context menue (right mouse click) select “Format Comment Out Lines” as shown in the screen shot.

* IF it_navigation_path IS INITIAL.

* SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_f

lights UP TO is_paging-top ROWS

* FROM ( spfli AS c

* INNER JOIN sflight AS f ON f~carrid =

c~carrid

* AND f~connid =

c~connid ).

* ELSE.

* READ TABLE it_key_tab ASSIGNING <key> INDEX 1.

*

* SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_f

lights UP TO is_paging-top ROWS

* FROM ( spfli AS c

* INNER JOIN sflight AS f ON f~carrid =

c~carrid

* AND f~connid =

c~connid )

* WHERE c~airpfrom = <key>-value.

* ENDIF.

7. Un-comment the following coding in method GET_ENTITYSET of class ZCD263_CL_PROVIDER_FLIGHT_0xx as shown on the right side. To do so select the coding you want to un-comment.

IF it_navigation_path IS INITIAL.

SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_fl

ights UP TO is_paging-top ROWS

FROM ( spfli AS c

INNER JOIN sflight AS f ON f~carrid = c

~carrid

AND f~connid = c

~connid ).

ELSE.

READ TABLE it_key_tab ASSIGNING <key> INDEX 1.

READ TABLE it_filter_select_options ASSIGNING <so

_airpto> WITH KEY property = 'Arrival/Airport'.

READ TABLE it_filter_select_options ASSIGNING <so

Page 21: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

From the context menu (right mouse click) select “Format Uncomment Lines” as shown in the screen shot.

_fldate> WITH KEY property = 'FlightDate'.

SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_fl

ights UP TO is_paging-top ROWS

FROM ( spfli AS c

INNER JOIN sflight AS f ON f~carrid = c

~carrid

AND f~connid = c

~connid )

WHERE c~airpfrom = <key>-

value AND c~airpto IN <so_airpto>-select_options

AND f~fldate IN

<so_fldate>-select_options.

ENDIF.

8. Save and

activate your

changes

9. The following URL should only retrieve flights that depart from Frankfurt (FRA) and arrive in New York (JFK) Please note the $filter query option that has been added to the URL as a parameter:

?$filter=Arrival/Airport eq 'JFK'

http://<hostname>:51080/sap/opu/sdata/sap/ZCD263_ICF_SERVICE_0xx/Airports('FRA')/DepartingFlights/?$filter=Arrival/Airport%20eq%20'JFK' where xx denotes your group number.

Page 22: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

EXERCISE 4: ASP.NET FRONTEND FOR THE FLIGHT SERVICE

Goal of this exercise

In this exercise we want to show the consumption of our OData service published through SAP NetWeaver Gateway through an ASP.NET application.

Since the focus of this session is the service implementation in the backend and not the development of frontends we have prepared a starter application using our SAP NetWeaver Consumption Tools for ASP.NET.

See more in http://www.sdn.sap.com/irj/sdn/gateway --> Developer Tools

Start the ASP.NET solution

1. Open the folder

“Session (Local folder)” on your desktop. Navigate to the subfolder CD263.

2.

Open folder

SAPWebApplication49

and double-click on the file

SAPWebApplication49.sln.

Now Visualstudio2010 should immediately start and load the project “SAPWebApplication49”.

3.

Start the application by pressing F5 or choose the green start button

Now the application should run in a new browser window.

Page 23: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

4.

On the first screen “From” you can select the departure airport. In each row you have a clickable link “To” that directs to the next page showing all matching destination airports for the selected departure airport. Please select the link in the “Frankfurt” row.

5.

On the second screen “To” you see all airports that can be reached from the airport you selected on the first page. This leverages the “AirportTo” navigation property of the Airport Entityset.

In each row you have the possibility to be directed to a page showing you a list of all possible flights for the selected combination of departure and destination airport using the “Flights” link.

This leverages the navigation property “DepartingFlights” and the filtering option you have implemented.

Please select the link in the “New York” row.

Page 24: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

6.

In each row you have the possibility to be directed to a page showing you a list of all bookings that have been made for this flight until now.

Please select an entry.

For example:

UA – 3517 – 2012-02-20 and click on the “Bookings” link.

Please scroll down to the bottom of this page.

7.

Please realize that we could have retrieved the same results as presented in the list above by querying the following URI directly in the browser:

~/Flights(AirlineId='UA',ConnectionNo='3517',FlightDate='20120220')/Bookings?$format=xml

Page 25: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

EXERCISE 5: HOW TO ADD A (COMPLEX) PROPERTY TO THE AIRPORT ENTITY

Goal of this exercise

In this exercise you will learn how the metadata provider class works. You will add programmatically a new property to the Airport entity. In the next exercise you will learn how data is retrieved for this new property by implementing the data provider class.

Add a complex type and property “GeoCoordinates” to the model Geographical information is often beneficial to show a map for an address. As we always need latitude and longitude to define a single point on a map, it makes sense to introduce a complex type for these two properties and call it GeoCoordinates. We change the Metadata document of the airport collection so that for each airport its geo-coordinates can be added in a structured way. A complex type with two properties “Longitude” and “Latitude” is defined and afterwards a property “GeoCoordinates” of this new type is added to the entity type definition of the entity “Airport”.

1. Start transaction SE80

2. From the dropdown box select “Local Objects” and then press the “Display”

button .

3. Expand the folder “Class Library” and then the folder “Classes”. Double-Click on the entry of the model provider class

ZCD263_CL_MOD

EL_PROVIDER_0x

x

Where xx denotes your group number

Page 26: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

1. Double-click on the method DEFINE. As a result the coding of the DEFINE method is shown on the right hand side. We have highlighted the two sections of out-commented coding that you have to activate.

To do so first press the “Display <-> Change”

Button .

2. Un-comment the following coding in method DEFINE of class

ZCD263_CL_MOD

EL_PROVIDER_0x

x

as shown on the right side. To do so select the coding you want to un-comment. From the context menu (right mouse click) select “Format Uncomment Lines” as shown in the screen shot.

"Complex type for GeoCoordinates

lo_complex_type = model-

>create_complex_type( 'GeoCoordinates' ). "#EC NOTEXT

lo_complex_type-

>create_property( iv_property_name = 'Longitude' iv_abap_

fieldname = 'LONGITUDE' ). "#EC NOTEXT

lo_complex_type-

>create_property( iv_property_name = 'Latidude' iv_abap_f

ieldname = 'LATITUDE' ). "#EC NOTEXT

lo_complex_type-

>bind_structure( 'ZCD263_CL_MODEL_PROVIDER_004=>GEOCOORDIN

ATES_S' ). "#EC NOTEXT

and a little bit below

"Complex property for GeoCoordinates

lo_entity_type-

>create_complex_property( iv_property_name = 'GeoCoordinat

es' iv_complex_type_name = 'GeoCoordinates' ). "#EC NOTEXT

Remark: If you decide to use cut and paste to leverage the coding in this document please be aware that it refers to a certain implementation of group ‘04’ and would have to be replaced by your group number

ZCD263_CL_MODEL_PROVIDER_0xx where xx denotes your group number.

3. Save and

activate your

changes

Page 27: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

4. Test the metadata document of the service

You will find a new property “GeoCoordinates” of type “Geocoordinates”. And you will find the definition of the complex type “GeoCoordinates” itself.

Page 28: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

5. Consume the Airport Collection using the following URL: http://<hostname>:51080/sap/opu/sdata/sap/ZCD263_ICF_SERVICE_0xx/Airports You will see the Airports (as before), but with empty geo coordinates (they aren‟t filled with data at the moment). The implementation of the retrieval of the Geo Coordinates data will be done in the next exercise

Page 29: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

EXERCISE 6: IMPLEMENT THE DATA-RETRIEVAL OF GEO-COORDINATES

In the exercise 5 we have only changed the class that creates the metadata file of the service. In order to retrieve the data, we have to change the Data Provider Class of the service or specifically the Data Provider Class for the Airport entity that is called by the Global Data Provider Class as mentioned above. In class ZCD263_CL_PROVIDER_AIRPORT_0xx we have to maintain both methods, GET_ENTIY and GET_ENTITYSET. In order to retrieve the longitude and the latitude of each airport we have provided an extra class ZCL_HELPER. This is necessary since the original data provided by the SFLIGHT sample does not contain geo-coordinates for the airports.

1. Start transaction

SE80.

2. Double-Click on the

folder of the class

ZCD263_CL_PROV

IDER_AIRPORT_0

xx

3. Double-click on the

method

GET_ENTITY and

press the “Display <-

> Change” Button

Activate the code at the highlighted section.

Page 30: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

4. 1. Un-comment the following coding in method GET_ENTITY of class ZCD263_CL_PROVIDER_AIRPORT_0xx As shown on the right side. To do so select the coding you want to un-comment. From the context menu (right mouse click) select “Format Uncomment Lines”

"add code to retrieve longitude and latitude es_airport-geocoordinates-latitude = zcl_helper=>get_latitude_for_airport( airport = ls_airport-airport ). es_airport-geocoordinates-longitude = zcl_helper=>get_longitude_for_airport( airport = ls_airport-airport ).

5. Save and

activate your

changes

6. Double-click on the

method

GET_ENTITYSET

Page 31: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

7. 2

. Un-comment the following coding in method GET_ENTITYSET of class ZCD263_CL_PROVIDER_AIRPORT_0xx As shown on the right side. To do so select the coding you want to un-comment. From the context menu (right mouse click) select “Format Uncomment Lines”

"add code to retrieve longitude and latitude ls_airport-geocoordinates-latitude = zcl_helper=>get_latitude_for_airport( airport = lv_airport ). ls_airport-geocoordinates-longitude = zcl_helper=>get_longitude_for_airport( airport = lv_airport ).

8. 3. Save and

activate your

changes

9. 4. Check whether additional data is retrieved for the Airports entity set

http://<hostname>:51080 /sap/opu/sdata/sap/ZCD263_ICF_SERVICE_0xx/Airports where xx denotes your group number.

Page 32: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

10. The result should look like the following screen shot

Page 33: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

OPTIONAL EXERCISE 7: FLIGHT BOOKINGS

Until now we have read the database content. We typed an URI into the path field of the browser and it performed a “GET” operation via the protocol HTTP in order to make the server execute the service ZCD263_ICF_SERVICE_0xx to retrieve the result. In order to change the database content, we need another HTTP method, namely the “POST” method. It shall create new resources on the server or modify existing ones.

In order to perform a Post HTTP-operation we need to do two things

1. We have to use the FireFox browser.

2. We have to install a special add-on for Firefox

Now, we want to describe the second precondition, in detail. Please start the FireFox browser. It is available via icon on the desktop.

Installation of the REST-Client for the “FireFox” browser

In your FireFox browser please open menu “Tools->Add-ons”.

Click on “Get Add-ons” and type into the search field “REST client”. Type Enter.

Page 34: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

As soon as the browser finds the desired add-on, click on “Add-to Firefox” and confirm all following popups, so that the add-on can be installed. After the installation you are asked to restart FireFox. Please do as recommended.

In your Tools menu there should be a new entry, namely “REST Client”.

Now, please click on it to open the new add-on. And this is how the REST Client tool should appear:

It allows loading, saving and editing of arbitrary REST requests.

The two following exercises are (the last ones and) both updating exercises. Hence, they both rely on the REST-Client that we just installed.

Booking a flight

Page 35: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

In the REST client tool, directly above the word “REST Request” you see a bunch of buttons. The two left-most ones are named “Open” and “Open entire request”. Please click the “Open entire request” button. A file selection dialog opens. Type in or choose “D:\Files\Session\CD263\CD263_BookingRequest.txt” and click ok. The REST Client tool should now look like this:

In the field right beside to the request method you read an URI that is well known to us: http://wdflbmt0692.wdf.sap.corp:51080/sap/opu/sdata/sap/ZCD263_ICF_SERVICE_0xx/Bookings. It means that the entity type Booking is the target of this “Post” request. The request header, a collection of name value pairs, specifies general parameters to be observed by this request. The request body in this case contains all the content and has arbitrary size. The structure of the content is restricted by multiple schemas and has to be compliant to the metadata document (see above).

1. Please change the property “CustomerName” inside of the request body to some arbitrary name of your choice in order to for distinguish a booking that you will create from bookings of other session participants.

2. Now press button “Send” in order to send the HTTP-request.

3. You will be asked for user credentials. Please provide the same credentials you used for logging on at M17.

4. If you inspect the database content via the ASP.NET application as you did in exercise 2.4 you will find this new booking entry (possibly among other similar ones. You can recognize your own entry by the CustomerName that you specified.

Page 36: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

In exercise 2.4 there was no booking entry. Now we find one and it has the same attributes as we entered as the request body in the REST Client above. Observe that the booking id is not provided in the request. It is calculated on the server. Please keep the booking number in mind for the next exercise..

Cancelling a booking

Please click the “Open entire request” button. Type in or choose “D:\Files\Session\CD263\CD263_CancelBookingRequest.txt” and click ok. The REST-Client tool should look like this:

Here we have a Post-request, as in section 2.5.1, because the cancellation is not realized by a deletion of the booking, but by an additional flag in the booking entry denoting that it still exists, but no longer in a valid state. All the properties of the booking-key are specified in the URI in the field right beside to the request method, not in the request body. The booking id belongs to the key.

Page 37: DEVELOPING AND CONSUMING ODATA SERVICES USING SAP ...files.wegbox.com/SAP/TechEd2011/VirtualWorkshop/CD263/CD263... · DEVELOPING AND CONSUMING ODATA SERVICES USING SAP NETWEAVER

1. Please change the attribute “BookingId” inside the URI of this request to the booking number that you received in the preceding exercise 2.5.2.

2. Now press button “Send” in order to send the HTTP-request.

If you inspect the database content via the ASP.NET application as you did in exercise 2.4 you find this booking for the flight with key UA – 3517 – 2012-03-12T00:00:00:

In exercise 2.5.2 this booking entry existed, already. But, now we find a tiny difference: The cancellation flag is now set, in contrast to exercise 2.5.2.