You are on page 1of 26

Class 6

Developing OData ABAP Service using BAPI with Mapping Technique:


________________________________________________________________

Note : This is just one of the Approach for Develop OData Service

Note : Use This Technique if the Complete Database Functionality is


Available inside BAPI

step by step procedure:


____________________________________________________________________

Step1 : Analyze BAPI ( BAPI_FLIGHT_GETLIST )


--------------------------------------------------------------------

open SE37 Tcode ( Function Builder )


Analyse BAPI_FLIGHT_GETLIST
Recognize The Input Options of BAPI ( Airline )
Recognize The Output Options of BAPI ( Flight_List )

Note : Select Query is already Available in BAPI


____________________________________________________________________

Step2 : Create OData Service Project


----------------------------------------------------------
open SEGW Tcode ( Gateway Service Builder )
Click on Create icon
Provide Project Name = Z6AMODATAPROJECTBAPI
Provide Description = OData with BAPI using Mapping
Save in package/local
Continue
____________________________________________________________________

Step3 : Create an Entity Type and Entity Set


--------------------------------------------------------------------
------
Right Click DataModel -> Import -> DDIC Structure
Provide Entity Type Name = Flight
provide ABAP Structure = BAPISFLDAT
( Structure refered inside BAPI also )
Select checkbox to Create EntitySet ( FlightSet )
Next
Select The Required Fields
Next
Select Atleast one Field as Key Field ( for Ex : Airlineid )
Finish
____________________________________________________________________
Step4 : Maintain Input Mapping & Output Mapping between OData
Service and BAPI_FLIGHT_GETLIST
--------------------------------------------------------------------

Expand Service Implementation Folder


Expand EntitySet ( FlightSet )
Right Click GetEntitySet( Query )
Select "Map to DataSource"

Provide Data Source Type = Remote Function Call


Provide Data Source Name = BAPI_FLIGHT_GETLIST
Continue

Note : At This level : it opens "Mapping Editor"

Maintain Input Mapping


-------------------------------------------
Click (+) icon left Side -> Select Airlineid (for Input Purpose )

Drag and Drop Airline ( BAPI ) to Airlineid ( OData )

Maintain Output Mapping


-------------------------------------------

Drag Drop Each Field from BAPI ( Flight_List ) to Each Field of


OData Service

Repeat this for Every Output Field of Flight_List


Save

____________________________________________________________________
_

Step5 : Generate Runtime Artifacts:


-----------------------------------------------------------------
Select Project FOlder -> Click on Red Color icon
Continue
save in package/local
Finish

Note : internally the Complete Metadata is Registered inside


MPC Class Automatically
Note : internally OData Service will access BAPI via DPC
Class GetEntitySet Method
____________________________________________________________________
Step6 : Register the OData Service
------------------------------------------------------------

Double click on "Service Maintainence" Folder


click on "Register"
Click YES
Save in Package/Local
Continue

Note : The OData Service will be registered inside SAP


Netweaver Gateway
& The OData Service will be Activated inside
SICF Tcode automatically
____________________________________________________________________
___________

Step7 : Test the OData Service


-------------------------------------------------
open Service maintainence Folder
open "SAP Gateway Client"
Select EntitySets ( FlightSet )

Select "Get" and Execute the Service

To Fetch All Records:


------------------------------------------

/sap/opu/odata/SAP/Z6AMODATAPROJECTBAPI_SRV/FlightSet

To Fetch Records based on Filter Input


-----------------------------------------------------------------

/sap/opu/odata/SAP/Z6AMODATAPROJECTBAPI_SRV/FlightSet?$filter=Airlin
eid eq 'JL'

/sap/opu/odata/SAP/Z6AMODATAPROJECTBAPI_SRV/FlightSet?$filter=Airlin
eid eq 'DL'

/sap/opu/odata/SAP/Z6AMODATAPROJECTBAPI_SRV/FlightSet?$filter=Airlin
eid eq 'AA'

/sap/opu/odata/SAP/Z6AMODATAPROJECTBAPI_SRV/FlightSet?$filter=Airlin
eid eq 'LH'

/sap/opu/odata/SAP/Z6AMODATAPROJECTBAPI_SRV/FlightSet?$filter=Airlin
eid eq 'SQ'
/sap/opu/odata/SAP/Z6AMODATAPROJECTBAPI_SRV/FlightSet?$filter=Airlin
eid eq 'UA'

To Fetch Records with Specific Fields in Output


--------------------------------------------------------------------
------

/sap/opu/odata/SAP/Z6AMODATAPROJECTBAPI_SRV/FlightSet?$select=Airlin
e,Airlineid,Price

/sap/opu/odata/SAP/Z6AMODATAPROJECTBAPI_SRV/FlightSet?$select=Airlin
e,Airlineid,Cityfrom,Cityto,Price

To Fetch Records with Filter Input and Output with Specific Fields
--------------------------------------------------------------------
------------------------------------

/sap/opu/odata/SAP/Z6AMODATAPROJECTBAPI_SRV/FlightSet?$filter=Airlin
eid eq 'UA'&$select=Airline,Airlineid,Cityfrom,Cityto,Price

To Fetch Record Count


-------------------------------------------

/sap/opu/odata/SAP/Z6AMODATAPROJECTBAPI_SRV/FlightSet/$count

To Fetch MetaData of OData Service


----------------------------------------------------------

/sap/opu/odata/SAP/Z6AMODATAPROJECTBAPI_SRV/$metadata

To Fetch Records ( json Format )


---------------------------------------------------------

/sap/opu/odata/SAP/Z6AMODATAPROJECTBAPI_SRV/FlightSet?$format=json

____________________________________________________________________
Note :

For Testing OData ABAP Services ,


The common keywords
filter
select
count
metadata
format
skip
top

Class 7
---------------------------------------------------------------------
Developing OData ABAP Service using Get_Entity( ) method
__________________________________________________________

Get_Entity( ) method is used to Fetch Single Record based on Input Key


Field Value(s)

Get_Entity( ) method is Suitable if Front-end App (SAP Ui5/Fiori )

Input with SimpleForm


Output with SimpleForm

Get_Entity( ) method contains two Important Parameters

it_Key_Tab : it will Accept Input Key Field Value(s) from Front-end


App (SAP Ui5/Fiori )

er_Entity :it will Send Output Record to Front-end App (SAP Ui5/Fiori )

Get_Entity( ) is Triggered from Front-end App using bindElement( )


Method
_______________________________________________________________________
__

Note :

To Deal with Key Field Name and Value within the code
Then we can use Standard STructure = /IWBEP/S_MGW_NAME_VALUE_PAIR

/IWBEP/S_MGW_NAME_VALUE_PAIR contains two options

1. Name ( Name of The Key Field )


2. Value ( Value of that Key Field )
_______________________________________________________________________

Step By STep Procedure:


_____________________________________________

step1 : Create New OData Project


--------------------------------------------------
Open SEGW Tcode
Click create
ProjectName = Z6AMODATAPROJECT2
description = OData with CRUDQ
save in package/local
______________________________________________________

Step2 : Create Entity Type and EntitySet


-------------------------------------------------------------------
Right Click DataModel -> Import -> DDIC Structure4
provide Entity Type name = EMP
provide ABAP Structure = ZEMPDBTABLE
Select Checkbox to Create EntitySet ( EMPSet )
Next
Select All The Fields
next
Select Key Fields ( Userid, Firstname )
Finish

Save
________________________________________________________

Step3 : Generate Runtime Artifacts


-----------------------------------------------------------

Select Your Project Folder


Click Red Color Icon
Continue
Finish
_______________________________________________________

Step4 : Implement the Functionality


--------------------------------------------------------------

Goto Runtime Artifacts Folder


Double Click DPC_EXT Class
Click Change
place Cursor EMPSet_Get_Entity( ) Method
Click on "Redefine Method" icon
Implement below code:
-----------------------------------------------------------------------
---------------------------------

Data : Wa_key_tab1 Type /iwbep/s_mgw_name_value_pair.


Data : Wa_key_tab2 Type /iwbep/s_mgw_name_value_pair.

Read Table it_key_tab into wa_key_tab1 index 1.


Read Table it_key_tab into wa_key_tab2 index 2.

Select Single * from zempdbtable


into
CORRESPONDING FIELDS OF er_Entity
where Userid = wa_key_tab1-value
and
Firstname = wa_key_tab2-value.
____________________________________________________________
save and Activate

Step5 : Register the Service


----------------------------------------------------

Goto Service Maintainence Folder


click Register
Click YES
save in package
Continue
_________________________________________________

Step6 : Test the OData Service


------------------------------------------------------------

Click on "Gateway Client"

Select "EntitySets" -> EMPSet

Select "Get" option and Execute the Service with Below URL's

To Fetch Output Record based on Input Key Field Value(s)


-----------------------------------------------------------------------
------------------------

/sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/EMPSet(Userid='1001',Firstname
='VIJAY')

/sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/EMPSet(Userid='1002',Firstname
='VARUN')

/sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/EMPSet(Userid='1003',Firstname
='AJAY')

To Fetch Output Record ( jsonFormat )


--------------------------------------------------------------

/sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/EMPSet(Userid='1001',Firstname
='VIJAY')?$format=json

To Fetch the Metadata of OData Service


---------------------------------------------------------

/sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/$metadata

To Fetch Output Record with Specific Fields


-------------------------------------------------------------------

/sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/EMPSet(Userid='1001',Firstname
='VIJAY')?$select=Userid,Firstname,Emailid,Salary

_______________________________________________________________________
Note :

To Test OData Service

==============================

SAP Gateway Client = From within SAP Server

Rest Client = From Outside SAP Server

___________________________________________________

The Service URL's syntax remains same for SAP Gateway and Rest Client
,But use protocal , IP Address , PortNumber additionally when we test
from RestClient(or) Third party plugins

Class 8
Developing OData ABAP Service using Get_EntitySet( ) Method:
_________________________________________________________________

Get_EntitySet( ) Method: is used to Fetch Collection of Records

Get_EntitySet( ) Method: is used if Front-end App Contains

Input with SimpleForm

Output with Table / List / Chart / MicroChart / MultiComboBox etc..

Get_EntitySet( ) Method: Contains two important Parameters

iv_filter_string : Will Accept Input Filter Value(s) from Front-end


App(SAP Ui5/Fiori )

et_EntitySet :Will Send Output Records to Front-end App(SAP Ui5/Fiori )

Get_EntitySet( ) Method: is Triggered from Front-End App using


bindRows( ) Method (or ) items="{/EntitySet}"
_______________________________________________________________________

Requirement 1:
--------------------------------

/ServiceURL/EMPSet

/ServiceURL/EMPSet?$filter=Userid eq '1001'

/ServiceURL/EMPSet?$filter=Country eq 'INDIA'

/ServiceURL/EMPSet?$filter=Salary gt 70000

/ServiceURL/EMPSet?$filter=Country eq 'INDIA' and Salary gt 70000


Requirement 2:
-----------------------------------
Add New Fields ( Virtual Fields ) at OData Service level inside Entity
Type

Status with Data Type Edm.String


Currency with Data Type Edm.String

Populate Values to New Fields based on Condition

if Salary > 80000

populate Status = 'Experience EMP'.

else.
populate Status = 'Fresher EMP'.

if Country = 'INDIA'

populate Currency = 'Indian Rupee'.

else
populate Currency = 'US Dollar'.
________________________________________________________________

Step by Step procedure:


___________________________________________

stpe1 : open SEGW Tcode


-----------------------------------------------------

open Your OData Project ( which is created Yesterday Session )


i,e
Project Name = Z6AMODATAPROJECT2

step2 : Make sure that Entity Type and Entity Set is Available
-----------------------------------------------------------------------

Entity Type = EMP (Fields are imported from DB Table = ZEMPDBTABLE)


Entity Set = EMPSet

step3 : Add New Fields ( Virtual Fields ) inside Entity Type at OData
level
-----------------------------------------------------------------------
-------------------------------------------

Select Entity Type ( EMP ) -> properties


Click on Change icon
Click on "Append Row " icon to Add New Fields
_____________________________________________
Status with DataType Edm.String
Currency with DataType Edm.String
______________________________________________
save

Step4 : Remove Existing ABAP Structure for Entity Type EMP


-----------------------------------------------------------------------
------------------------

Select Entity Type EMP


Remove ABAP Structure ZEMPDBTABLE ( make it blank )
save

Step5 : Re-Generate Runtime Artifacts


-------------------------------------------------------------------
Select your Project Folder -> Click on Red Color icon
continue

Note : Whenever we Add/Remove/Change anything in DataModel ,


Then we need to re-generate Runtime Artifacts ,
so Latest metadata will be Reflected in MPC Class automatically

Step6 : Implement below Code:


--------------------------------------------------------
Goto Runtime Artifacts
open Data Provider Extension Class (DPC_EXT )
click change
place cursor on EMPSet_Get_EntitySet( ) Method
Click on "Redefine The Method"
Implement below code:
_____________________________________________________

Data : wa Type zcl_z6amodataproject2_mpc=>ts_emp.


Data : itab Type zcl_z6amodataproject2_mpc=>tt_emp.

Select * From ZEMPDBTABLE


into
CORRESPONDING FIELDS OF Table itab
WHERE (iv_filter_string).

Loop at itab into wa.

if Wa-salary > 80000.


wa-status = 'Experience EMP'.
else.
wa-status = 'Fresher EMP'.
endif.

if Wa-country = 'INDIA' .
wa-currency = 'Indian Rupee'.
else.
wa-currency = 'US Dollar'.
endif.
Append Wa To Et_EntitySet.

endLoop.
_______________________________________________________

Save and Activate The Method


Go back ……….Save and Activate The Class
Step 7 : Test OData Service
-----------------------------------------------
Goto Service maintainence Folder
open Gateway Client

Select "MetaData" -> Cleanup cache -> from Both Systems


Continue

Select EntitySets ( EMPSet )

Select "Get" option and Execute the Service with below Service URL's

To Fetch All Records


----------------------------------

/sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/EMPSet

To Fetch Records based on Filter Inputs


------------------------------------------------------------

/sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/EMPSet?$filter=Userid eq
'1004'

/sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/EMPSet?$filter=Country eq
'INDIA'

/sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/EMPSet?$filter=Salary gt 70000

/sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/EMPSet?$filter=Country eq 'US'
and Salary gt 80000

/sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/EMPSet?$filter=Country eq
'INDIA' and Salary gt 70000

To Fetch metadata of OData Service


--------------------------------------------------------

/sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/$metadata

To Fetch Record Count


-----------------------------------------

/sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/EMPSet/$count

To Fetch Records based on Filter Input , & Output in Json Format


-----------------------------------------------------------------------
----------------------------

/sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/EMPSet?$filter=Country eq
'US'&$format=json

_______________________________________________________________________
Difference between Get_Entity( ) Method Vs Get_EntitySet( ) Method
-----------------------------------------------------------------------

Get_Entity( ) Method : To Fetch Single Record always

Get_Entity( ) Method : is used when Front-end App Contains

Input with SimpleForm


Output with SimpleForm

Get_Entity( ) Method is Triggered from Front-end App using bindElement


( ) Method

Get_Entity( ) Method : uses below Service URL Syntax

/ServiceURL/EMPSet('1001')

/ServiceURL/EMPSet(Userid='1001')

/ServiceURL/EMPSet(Userid='1001',Firstname='VIJAY')
_______________________________________________________________________

Get_EntitySet( ) Method : To Fetch Collection of Records

Get_EntitySet( ) Method : is used when Front-end App Contains

Input with SimpleForm


Output with Table/List/Chart/MicroChart/MultiComboBox

Get_EntitySet( ) Method is Triggered from Front-end App using


bindRows( ) method (or) items="{/EntitySet}"

Get_EntitYSet( ) method : uses below Service URL Syntaxes

/ServiceURL/EMPSet

/ServiceURL/EMPSet?$filter=Userid eq '1001'

/ServiceURL/EMPSet?$filter=Country eq 'INDIA'

/ServiceURL/EMPSet?$filter=Salary gt 70000

/ServiceURL/EMPSet?$filter=Country eq 'INDIA' and Salary gt 70000


_______________________________________________________________________

What is purpose of "metaData Clean Up Cache" ?


-----------------------------------------------------------------------

Ans:- Old metadata will be deleted from Cache memory and New Metadata
will be loaded into Cache Memory so Next Time onwards Odata Service is
executed with Latest metadata only so we can expect proper and
consistent results during Service Execution

_______________________________________________________________________
Note :

In case if Methods are Not Visible after opening DPC_EXT Clas ?

Solution

open DPC_EXT Class

goto utilities -> Settings -> ClassBuilder

Select Checkbox i,e Display inherited Components Also


continue
________________________________________________________________

Class 9
Developing OData ABAP Service using Create_Entity( ) method :
__________________________________________________________

Create_Entity( ) method : is used to Insert Data ( Single Record )

Create_Entity( ) method Contains Two Parameters

io_Data_Provider :To Accept Input Record Values from Front-end App(SAP


Ui5/Fiori )

er_entity: To Send Output Record back to Front-end App( SAP Ui5/Fiori )

( optional )

Create_Entity( ) method : is suitable if Front-end App Contains

Input with SimpleForm

Create_Entity( ) method is Triggered from Front-end App using create( )


Method
_______________________________________________________________________

Step by Step Procedure:


----------------------------------------------

step1 : open your SEGW TCode

Open your OData Project ( Which is created for CRUDQ Operations)


Project Name = Z6AMODATAPROJECT2

Step2 : Make sure that Entity Type and Entity Set is Available

Entity Type = EMP


Entity Set = EMPSet

Step3 : Make sure That Runtime Artifacts are Generated

Step4 : open Data Provider Extension Clas ( DPC_EXT )


click change
place cursor on EMPSet_Create_Entity( ) method
"Redefine that Method"
Implement below code:
______________________________________________________________

Data : Wa Type ZEMPDBTABLE.

io_data_provider->read_entry_data( IMPORTING es_data = Wa ).

if wa-userid is not INITIAL.

INSERT into ZEMPDBTABLE values Wa.

commit WORK.

endif.

______________________________________________________________

save and Activate

Step5 : Test OData Service


----------------------------------------------------------------
Open Service maintainence Folder
open SAP Gateway Client

Select EntitySets ( EMPSet )

Testing Get_Entity( ) Method:


--------------------------------------------------

/sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/EMPSet(Userid='1002',Firstname
='VARUN')

Select "Get" option and Execute the Service

Note : At This Level : "Get" option Executed Get_Entity( ) Method

Testing Create_Entity( ) Method:


--------------------------------------------------

click on "Use as Request" option


( To Copy XML Format from HTTP Response to HTTP Request )

Provide New Record Values inside HTTP Request Section


___________________________________________________
2007
Satish
Kumar
satish@ca.com
INDIA
79000
9030252696
___________________________________________________
From Service URL -> remove this ->
(Userid='1002',Firstname='VARUN')

Select "Post" option and Execute The Service

Note: At This level : "Post" option executes Create_Entity( ) Method

_______________________________________________________________________

For Data Verification:


-----------------------------------

open SE11 Tcode


open Database Table = ZEMPDBTABLE
Check the contents
Check whether New Record is Inserted or Not
______________________________________________________________________

Note : in Today' Scenario

Get option -> Executes Get_Entity( ) Method


Post option -> Executes Create_Entity( ) Method
_______________________________________________________________________

VVV important Note :


_______________________

What is the Purpose of "Use as Request" option?

Ans It is to Copy XML Format from HTTP Response to HTTP Request


so that we can pass Record values as Input to OData Service via HTTP
Request

Class 10
Developing OData ABAP Service using
Update_Entity( ) Method
Delete_Entity( ) Method
_________________________________________________________

Update_Entity( ) Method :
--------------------------------------

Update_Entity( ) Method is used to Update Data.

Update_Entity( ) Method Contains Two Parameters

io_data_Provider: Will Accept Updated Record from Front-end App


(SAP Ui5/Fiori )

er_Entity: Will Send Output Record back to Front-end App(SAP Ui5/Fiori)

( optional )

Update_Entity( ) Method is Triggered from Front-end App Using Update( )


Front-end Method
_______________________________________________________________________

Delete_Entity( ) Method :
---------------------------------------

Delete_Entity( ) Method is used to Delete Data.

Delete_Entity( ) Method Contains one Parameter

it_key_tab : Will Accept Input key Field Value(S) from Front-end


App(SAP Ui5/Fiori )

Delete_Entity( ) Method is Triggered from Front-end App using remove( )


Front-end Method
_______________________________________________________________________

Step by Step Procedure:


_____________________

step1 : open SEGW Tcode


open Your OData Service Project

( Which is created in previous Sessions )


Project Name = Z6AMODATAPROJECT2

step2 : Make sure that Entity Type and Entity Set is available
Entity Type = EMP
Entity Set = EMPSet

Step3: open Data Provider Extension Class ( DPC_EXT )


click change
place cursor on EMPSet_Update_Entity( ) Method
Click on "Redefine that Method"
implement below code:
_______________________________________________________________

Data : WA Type ZEMPDBTABLE.

io_data_provider->read_entry_data( IMPORTING es_data = WA ).

Update ZEMPDBTABLE set Lastname = WA-lastname


Emailid = WA-emailid
Country = WA-country
Salary = WA-salary
Phone = WA-phone
where Userid = WA-userid .

commit work.

____________________________________________________________
save and activate

Step4 : Place cursor on EMPSet_Delete_Entity( ) Method


click on "Redefine that Method"
implement below code:
__________________________________________________________

Data : WA_KEY_TAB Type /iwbep/s_mgw_name_value_pair.

Read Table it_key_tab into wa_key_tab index 1.

Delete from ZEMPDBTABLE where Userid = wa_key_tab-value.

_________________________________________________________
save and activate

Step5 : Test the OData Service


-----------------------------------------------------------

open Service maintainnence Folder


open Gateway Client

Testing Update_Entity( ) Method


________________________________

Select EntitySets ( EMPSet )

/sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/EMPSet(Userid='1003',Firstname
='AJAY')

Select "Get" Option and Execute the Service

Note : At This Level : Get option Executs Get_Entity( ) Method

Now Select "Use As Request" option ( To Copy XML Format from HTTP
Response to HTTP Request )

Update the Record Values in HTTP Request Section


___________________________

Lastname -> Kumar


Emailid -> ajay@gmail.com
Country -> INDIA
Salary -> 83000
Phone -> 9040252676
___________________________________

Select "Put" option and Execute The Service

Note : At This Level : Put option Executes Update_Entity( ) Method


_______________________________________________________________________
Testing Delete_Entity( ) Method

Select EntitySets ( EMPSet )

/sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/EMPSet(Userid='1012',Firstname
='Sachin')

Select "Delete" option and Execute the Service

Note : At this Level : Delete option Executes Delete_Entity( ) Method


_______________________________________________________________________

For Data Verification:


____________________

Open SE11 Tcode


open Database Table = ZEMPDBTABLE
Check the Contents
Check Whether Record is Updated or Not
Check Whether Record is Deleted or Not
_______________________________________________________________________

Note :

OData Service Method HTTP Success Status Codes


______________________________________________________________________

Get_Entity( ) 200

Get_EntitySet( ) 200

Create_Entity( ) 201

Update_Entity( ) 204

Delete_Entity( ) 204
_____________________________________________________________________

Note : Every OData Service runs with HTTP Protocal

HTTP 1XX Information Messages

HTTP 2XX Successful Messages

HTTP 3XX Redirectional Messages

HTTP 4XX Client Side Error Messages

HTTP 5XX Server Side Error Messages


_______________________________________________________________________

For More Information on HTTP Status Codes:


________________________________________

https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
Class 11
Developing OData ABAP Service using
Update_Stream( ) Method -> For File Upload Functionality
Get_Stream( ) Method -> For File Download Functionality
& For File Preview Functionality

_______________________________________________________________________

Update_Stream( ) Method :
----------------------------------------

Update_Stream( ) Method is to Deal with File Upload Functionality

Update_Stream( ) Method contains 2 Parameters

it_key_Tab = To Accept Input Key Field Value(s) From Front-end


App(SAP Ui5/ Fiori )

is_media_resource = To Accept Input File Type and Input File Content


from Front-end App(SAP Ui5/ Fiori )
_______________________________________________________________________

Get_Stream( ) Method :
------------------------------------------

Get_Stream( ) Method is to Deal with File Download Functionality

Get_Stream( ) Method Contains 2 Parameters

it_key_tab = To Accept Input Key Field Value(s) From Front-end App


(SAP Ui5/ Fiori )

er_Stream = To Send Output File Type and Output File Content To Front-
end App(SAP Ui5/Fiori)
_______________________________________________________________________

VVV Important Structure


------------------------------------------

To deal with File Type and File Content, we can use Standard Structure

TY_S_MEDIA_RESOURCE

TY_S_MEDIA_RESOURCE Standard STructure with two options

1. Mime_Type : Refers to File Type

2. Value : Refers to File Content


Note : In Today's Scenario . We use Database Table = ZFILESTABLE

Note : ZFILESTABLE ( DB Table ) Contains Field ( FileContent ) with


RawString DataType

Note : Rawstring Data Type can Store any Type of File Content
( Image Files, Text Files, PDF Files, Audio Files , Video File
, Excel Files etc )
_______________________________________________________________________

Step by STep Procedure:


-------------------------------------------

step1 : open Your OData Project = Z6AMODATAPROJECT2

step2 : Create An Entity Type and Entity Set


i,e
Right Click DataModel -> Import -> DDIC Structure

Provide Entity Type = File


Provide ABAP STrcuture = ZFILESTABLE ( Database Table )
Select CheckBox to Create Entity Set ( FileSet )
Next
Select Fields
next
Select Key Field ( For ex : Userid )
Finish

Step3 : Enable Entity Type with "Media" option


i,e
Select Entity Type ( File )
Select Checkbox to Enable "Media" option for Entity Type
save

Step4 : Re-Generate Runtime Artifacts


i,e
Select Project Folder -> Click red color icon
continue

Step5 : Open DataProvider Extension Class (DPC_EXT )


click change
place Cursor on Update_Stream( ) Method
Click "Redefine method"
Implement below code
_______________________________________________________________________

Data : wa_key_tab Type /iwbep/s_mgw_name_value_pair.

Data : WA TYPE zcl_z6amodataproject2_mpc=>ts_file.

Read Table it_key_tab into wa_key_tab index 1.

WA-userid = wa_key_tab-value.
WA-sydate = Sy-datum.
WA-sytime = Sy-uzeit.
WA-filetype = is_media_resource-mime_type.
WA-filecontent = is_media_resource-value.

INSERT into ZFILESTABLE values WA.

Commit Work.

_______________________________________________________________________
save and Activate

Step6 : Open DataProvider Extension Class (DPC_EXT )


click change
place Cursor on Get_Stream( ) Method
Click "Redefine method"
Implement below code
_______________________________________________________________________

Data : wa_key_tab Type /iwbep/s_mgw_name_value_pair.

Data : WA TYPE zcl_z6amodataproject2_mpc=>ts_file.

Data : WA_STREAM TYPE TY_S_MEDIA_RESOURCE.

Read Table it_key_tab into wa_key_tab index 1.

Select Single * from ZFILESTABLE into

CORRESPONDING FIELDS OF WA where Userid = wa_key_tab-value.


wa_stream-mime_type = WA-filetype.
wa_stream-value = WA-filecontent.

copy_data_to_ref( EXPORTING is_data = WA_Stream )

CHANGING cr_data = ER_STREAM).

_______________________________________________________________________
save and Activate

Step7 : Test the Odata Service


-------------------------------------------------

open Service Maintainence Folder


open Gateway Client

Select "MetaData" cleanup Cache from Both Systems


Continue
Testing Update_Stream( ) method:
---------------------------------------------------------

Select Entity Sets ( FileSet )

Add File -> Pick File

/sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/FileSet(Userid='2001')/$value

Select "Put" option and Execute Service

________________________________________________________________
Add File -> Pick File

/sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/FileSet(Userid='2002')/$value

Select "Put" option and Execute Service


______________________________________________________________________

Add File -> Pick File

/sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/FileSet(Userid='2003')/$value

Select "Put" option and Execute Service


_______________________________________________________________________

Add File -> Pick File

/sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/FileSet(Userid='2004')/$value

Select "Put" option and Execute Service

_______________________________________________________________________

Testing Get_Stream( ) method :


------------------------------------------------------

open SAP Gateway Client

Select Entity Sets ( FileSet )

sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/FileSet(Userid='2001')/$value

Select "Get" option and Execute The Service


_________________________________________________________________

sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/FileSet(Userid='2002')/$value

Select "Get" option and Execute The Service


____________________________________________________________________

sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/FileSet(Userid='2003')/$value

Select "Get" option and Execute The Service


___________________________________________________________________

sap/opu/odata/SAP/Z6AMODATAPROJECT2_SRV/FileSet(Userid='2004')/$value
Select "Get" option and Execute The Service
_______________________________________________________________________

Note : In today scenario

Put option -> Executes Update_Stream( ) Method bcoz we used


/$value + EntityType with Media option
Get option -> Executes Get_Stream( ) Method
bcoz we used /$value + Entity Type with Media option
_______________________________________________________________________

For FileContent Verification:


------------------------------------------

open SE11 Tcode


open Database Table = ZFILESTABLE
check the Contents
Check Whether File Content is stored in Rawstring Format in Database
table or not

Class 12
Today Theory ( Next Session will be Practical )
________________________________________________

Principle Entity Type


Dependent Entity Type

Principle Entity Set


Dependent Entity Set

Association
Association Set

Referential Constraint
NavigationProperty

Deep Structure Format

Get_Expanded_Entity( ) Method
Create_Deep_Entity( ) Method
_________________________________________________________

Note : All the Above Topics are Interrelated Topics


These Topics are Used to Deal with Multiple Records
These Topics are Used to Deal with Header + Items Records
Simultaneously
_______________________________________________________________________

Principle Entity Type :


---------------------------------------

Entity Type which Maintains Collection of "Header Data Fields" with


Data Type and length
This Entity Type is Technically Considered as "Principle Entity Type"
Dependent Entity Type :
---------------------------------------

Entity Type which Maintains Collection of "Item Data Fields" with Data
Type and length
This Entity Type is Technically Considered as "Dependent Entity Type"

Principle Entity Set :


--------------------------------------

Entity Set which holds Collection of "Header Records" at Runtime


This Entity Set is Technically Considered as "Principle Entity Set"

Dependent Entity Set:


---------------------------------------

Entity Set which holds Collection of "Item Records" at Runtime


This Entity Set is Technically Considered as "Dependent Entity Set"

Association :
----------------------------

Association is the Name of the Relationship between Two Entity Types


i,e
Association is the Name of the Relationship between Principle Entity
Type and Dependent Entity Type

Association Set:
-----------------------------

Association Set is the Name of the Relationship between Two Entity Sets
i,e
Assocation Set is the Name of the Relationship between Principle Entity
Set and Dependent Entity Set

Referential Constraint:
----------------------------------------

Referential Constraint is the Common Field between Two Entity Types


i,e
Referential Constraint is the Common Field between Principle Entity
Type and Dependent Entity Type

Note : There can be Multiple Common Field(s)

Navigation Property:
-------------------------------------
Navigation Property is an "InterLink" between Header Record and its
Corresponding Item Records at Runtime
Navigation Property is used as "InterLink" in two Situations
1. To Fetch Output Data -> Header Record + its Item Records
Simultaneously

2. To Insert Input Data -> Header Record + its Item Records


Simultaneously
Multiple Record(s)
_______________________________________________________________________

Get_Expanded_Entity( ) Method :
---------------------------------------------------

This Method is used to Fetch Output Data ( Header Record + its Items
Records Simultaneously )
This Method is used to "Send" Output Data ( Header Record + its Items
Records Simultaneously ) to Front-end App(SAP Ui5/Fiori )

Create_Deep_Entity( ) Method:
-------------------------------------------------
This Method is used to Insert Header + item Records Simultaneously
This Method can "Receive" Header + item Records Simultaneously From
Front-end App(SAP Ui5/Fiori )

Note : This Method can also Insert Plain Multiple Record(s)


This Method can "Receive" Plain Multiple Record(s) from Front-
end App(SAP Ui5/Fiori )
_______________________________________________________________________

Deep Structure Format :


--------------------------------------

Deep Structure Format is used in Two situations

1. While Implementing Get_Expanded_Entity( ) method i,e


DeepStructure Format is required to Send Output ( Header + items )
2. While Implementing Create_Deep_Entity( ) Method i,e
DeepStructure Format is required to Receive Input ( Header + items )

Syntax for Deep Structure Format:


-------------------------------------------------------------

Data : begin of DeepStructure,

Header Type MPCClass=> TS_PrincipleEntityType,


Data : items/NP Type MPCClass=>TT_DependentEntityType,

end of DeepStructure.
_______________________________________________________________
Note :
DeepStructure-FieldName -> Refers to Accomodation of HeaderRecord
DeepStructure-NavigationProperty -> Refers to Accomodation of Item
Record(S)
Note :
Deep Structure Format can be used to Send Output Header + item Records
Simultaneously & to Accept Input Header + item Records Simulataneously

You might also like