Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
mohammed_arshad
Explorer

Hi All,

Finally thought of writing blog  after struggling to create simple o-data service to perform basic operations including media and deep entities.

So I don't want you all to struggle and waste your time...

This is for Beginners not for Experts :wink: ,

Create your simple gateway service by following provided steps.

Service is all about Employee Information System.

Tables Used.

   1. Employee Details table[Principle data]Or[Header Information]

   2. Employee Contact details(Ph no on working days and on leave)[Line Item]

3.  Employee Photo Details.

Structures Used

[But U can directly use Database tables to include structure to create Data types]

1. Employee Details.

 

2.  Employee Address Info[Include structure in Emp Details table].


3.  Employee Contact details(Info like Contact no during working days and on leave).

 

4. Employee Photo details.


Lets start rocket science :lol: ,

Here we go,

Create your simple gateway service, following provided steps.

using table and structure list below with Project Details.

     1. Log onto the SAP NetWeaver Gateway system.

     2. Open transaction SEGW.

     3. The SAP NetWeaver Gateway Service Builder opens.

     4. Create a new project.

   

            5.  Enter project name and meaningful description.

              

               Your project is created,


Entity Type

      6.  Right click on the Data Model folder and select  Import DDIC  Structure .

7. Following window will appear, In ABAP Structure field

          You can import the following DDIC structures

          into the Service Builder:

        • Views
        • Database tables
        • Structures

     8.  Press enter below window will appear,

     9.  Select at-least one key or more than one keys according to requirement

         in Usage column.

     10. You can directly create complex types by adding

         Deep structure (Structure within structure).

              

Entity Set

      11. Create Entity set for each Entity type.

Right click on Entity sets->select create


     12. Press F4 on Entity type field and select entity type,

                 Enter Entity Set name click okay,

         

     13.     Create Entity Sets for each entity type.

    

Association.

     14.     If you have Header Entity[Principal or Parent]

             and LineItem Entity[Dependent Information]

      Or

      If you want to create Relationship between 2 entities

          Ex: Purchase Order header and Line Items.

              Or

          Ex: Employee Details[Main] like id and name and Employee

              Contatct details[Dependent] many.

      15.      Right click on Association->create

         

     16. Enter the Association name,

     17. Press F4 on Entity Type name and select Parent[Header Info]

         Entity type.

     18. Specify proper cardinality between entity types.

     19. Specify Navigation Property name[This will be used in URL to navigate from parent to child and child           to parent, In our case its only from parent to child] .

    

     20. Click next.

     21. Press F4 on Dependent property select the key field which is

         common among both.

     22. Click next.

     23. Check Entries are proper.

     24. Click Finish,

         

Once after creating Entity Types, Entity Sets, Associations, Navigation Properties.

Your Project View look like this.

    

Project Details


     1. Click on Generate Runtime objects.

    

     2. Expand Runtime artifacts.

     3. Right click on Class which ends with DPC_EXT and select Go to ABAP Workbench.

       

     4. Redefine the methods which mentioned below.

     5. Paste the code[Put break-point if u want to check behavior of methods called    based on URL].

     6. Expand Service maintenance.

     7. Right click on system in which you created these objects.

     8. Click on  Register and select proper System in RFC System alias.

     9. Right click again on Selected system and click maintain.

    10. Pop up will appear asking for redirection Click Yes.

    11. Click on Gateway Client.

    12. Click Execute to test.

    13. Status 200 shows service is created correctly.

    14. Paste URL in Requested URI field and Click Execute. Check respective methods by keeping breakpoints               as given below.

     15. Click Execute to test.

     Status 200 shows service is created correctly.

     16.Paste URL in Requested URI field and Click Execute.

     17.Check respective methods by keeping breakpoints as given below.

 

READ.

             

URL :

To get single record.

------------------------------/sap/ZEMPLOYEE_INFO_SRV/EmpDetailsSet(EMP_ID='1')

METHOD empdetailsset_get_entity.
DATA: ls_key_tab TYPE /iwbep/s_mgw_name_value_pair,
ls_emp_tab
TYPE zxx_emp_det.

READ TABLE it_key_tab INTO ls_key_tab WITH KEY name = 'EMP_ID'.
SELECT SINGLE * FROM zxx_emp_det INTO ls_emp_tab WHERE emp_id = ls_key_tab-value.
IF sy-subrc = 0.
MOVE-CORRESPONDING ls_emp_tab TO er_entity.
MOVE-CORRESPONDING ls_emp_tab TO er_entity-details.
ENDIF.
ENDMETHOD.

SUCCESS CODE : 200

URL:

To get all records from table respect to EntitySet.

-------------------------------/sap/ZEMPLOYEE_INFO_SRV/EmpDetailsSet

To get records based on where condition.

------------------------------/sap/ZEMPLOYEE_INFO_SRV/EmpDetailsSet?$filter=EMP_ID eq '1'

We can add more conditions using & Operator in between fields.

METHOD empdetailsset_get_entityset.

DATA: lt_emp_tab    TYPE TABLE OF zxx_emp_det,
ls_emp_tab   
TYPE          zxx_emp_det,
es_entityset 
TYPE          zcl_zemployee_info_mpc=>ts_empdetails.

SELECT * FROM zxx_emp_det INTO TABLE lt_emp_tab WHERE (iv_filter_string).
IF sy-subrc = 0.
LOOP AT lt_emp_tab INTO ls_emp_tab.
MOVE-CORRESPONDING  ls_emp_tab    TO es_entityset.
MOVE-CORRESPONDING  ls_emp_tab    TO es_entityset-details.
APPEND              es_entityset  TO et_entityset.
CLEAR ls_emp_tab.
ENDLOOP.
ENDIF.
ENDMETHOD.

SUCCESS CODE : 200




CREATE


Steps :

  1. 1.Get any record from GET_ENTITY ..
  2. 2.After getting click on Use as request.
  3. 3.Change details what you want to create in left window.
  4. 4.Check POST method of HTTP.
  5. 5.Use URL..

---------------------------/sap/ZEMPLOYEE_INFO_SRV/EmpDetailsSet

method ZEMPDETSTRSET_CREATE_ENTITY.
DATA: ls_emp_details TYPE zxx_emp_det.
"Get the created entity here
io_data_provider
->read_entry_data( IMPORTING es_data = er_entity ).
MOVE-CORRESPONDING er_entity TO ls_emp_details.
MOVE-CORRESPONDING er_entity-details TO ls_emp_details.
INSERT zxx_emp_det FROM ls_emp_details
endmethod.

SUCCESS CODE : 201



UPDATE

To Update the Record.

Follow same steps as for CREATE_ENTITY.

Just Change URL and Check HTTP  PUT Method.

  1. URL.

---------------------------/sap/ZEMPLOYEE_INFO_SRV/EmpDetailsSet(EMP_ID='1')

But on response   body nothing will be displayed..

For that we need to call get entity internally.

method EMPDETAILSSET_UPDATE_ENTITY.
DATA: ls_emp_details TYPE zxx_emp_det.

"Get the created entity here
io_data_provider
->read_entry_data( IMPORTING es_data = er_entity ).
MOVE-CORRESPONDING er_entity TO ls_emp_details.
MOVE-CORRESPONDING er_entity-details TO ls_emp_details.
MODIFY zxx_emp_det FROM ls_emp_details.

endmethod.

SUCCESS CODE : 204



DELETE


To delete record Follow same steps for create.

Check http method DELETE.

URL .

--------------------/sap/ZEMPLOYEE_INFO_SRV/EmpDetailsSet(EMP_ID='1')

method EMPDETAILSSET_DELETE_ENTITY.
DATA: ls_key_tab TYPE /iwbep/s_mgw_name_value_pair,
ls_emp_details
TYPE zxx_emp_det.

READ TABLE it_key_tab INTO ls_key_tab WITH KEY name = 'EMP_ID'.
IF sy-subrc = 0.
ls_emp_details
-emp_id   = ls_key_tab-value.
DELETE zxx_emp_det FROM ls_emp_details.
ENDIF.
endmethod.

SUCCESS CODE : 204

 

DEEP READ


To get data of dependent with parent entity[Header with Line Items]

  1. URL.

-----------------------------ZEMPLOYEE_INFO_SRV/EmpDetailsSet(EMP_ID='1')/?$expand=EmpContDetailsNavig

METHOD /iwbep/if_mgw_appl_srv_runtime~get_expanded_entity.
DATA:   ls_key_tab        TYPE /iwbep/s_mgw_name_value_pair,
ls_emp_tab       
TYPE zxx_emp_det,
ls_empadd_tab    
TYPE zot_cont_det,
ls_empadd        
TYPE zot_cont_det.

DATA: ls_expand_tech_clauses LIKE LINE OF et_expanded_tech_clauses.

DATA: BEGIN OF ls_expand.
INCLUDE TYPE zcl_zemployee_info_mpc=>ts_empdetails.
DATA: empcontdetailsnavig TYPE STANDARD TABLE OF zcl_zemployee_info_mpc=>ts_empcontactdetails WITH DEFAULT KEY.
DATA: END OF ls_expand.

READ TABLE it_key_tab INTO ls_key_tab WITH KEY name = 'EMP_ID'.

SELECT SINGLE * FROM zxx_emp_det INTO ls_emp_tab WHERE emp_id = ls_key_tab-value.

SELECT SINGLE * FROM zot_cont_det INTO ls_empadd_tab WHERE emp_id = ls_key_tab-value.

MOVE-CORRESPONDING ls_emp_tab TO ls_expand.
MOVE-CORRESPONDING ls_empadd_tab TO ls_empadd.
APPEND ls_empadd TO ls_expand-empcontdetailsnavig.

"Assign the navigation properties
ls_expand_tech_clauses
= 'empcontdetailsnavig'.
APPEND ls_expand_tech_clauses TO et_expanded_tech_clauses.

copy_data_to_ref
(
EXPORTING
is_data
= ls_expand
CHANGING
cr_data
= er_entity ).

ENDMETHOD.

SUCCESS CODE : 200




DEEP INSERT


To create deep entity.[Parent data with child][Header with Line Items]

Steps :

  1. 1.Read data from GET_EXPANDED_ENTITY
  2. 2.Made HTTP method as POST.
  3. 3.Click User as Request and Edit values You want to Insert.
  4. 4.url

-----------------------ZEMPLOYEE_INFO_SRV/EmpDetailsSet

  1. 5.Execute.

method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_DEEP_ENTITY.

TYPES : ty_t_empadd TYPE STANDARD TABLE OF ZCL_ZEMPLOYEE_INFO_MPC=>TS_EMPCONTACTDETAILS WITH DEFAULT KEY.
TYPES: BEGIN OF ty_expand.
INCLUDE TYPE ZCL_ZEMPLOYEE_INFO_MPC=>TS_EMPDETAILS.
TYPES  :       EmpContDetailsNavig TYPE ty_t_empadd,
END OF ty_expand.
DATA :
*&-------> Local structures.
ls_deep_emp_details
TYPE  ty_expand,
ls_emp_cont_det    
TYPE  ZOT_CONT_DET,
ls_emp_details     
TYPE  zxx_emp_det,
*&-------> Local Itabs.
lt_emp_cont_det    
TYPE TABLE OF ZOT_CONT_DET.

io_data_provider
->read_entry_data( IMPORTING es_data = ls_deep_emp_details ).

copy_data_to_ref
( EXPORTING  is_data = ls_deep_emp_details
CHANGING   cr_data = er_deep_entity ).

MOVE-CORRESPONDING ls_deep_emp_details TO ls_emp_details.
MOVE-CORRESPONDING ls_deep_emp_details-details TO ls_emp_details.

INSERT INTO zxx_emp_det VALUES ls_emp_details.

LOOP AT ls_deep_emp_details-empcontdetailsnavig INTO ls_emp_cont_det.
APPEND ls_emp_cont_det TO lt_emp_cont_det.
ENDLOOP.
MODIFY zot_cont_det FROM TABLE lt_emp_cont_det.

IF sy-subrc EQ 4.
ENDIF.
endmethod.

SUCCESS CODE : 201




CREATE MEDIA


  1. 1.Redefine the DEFINE METHOD OF MPC_EXT CLASS
  2. 2.Paste code.
  3. 3.We need to get key values in SLUG Parameter Of http.
  4. URL.

-------------------------------ZEMP_PRO_SRV/EmpDetailsSet('1')/EmpPhotoNavig

  1. 4.Click on add file select image.
  2. 5.Check Post method of HTTP.
  3. 5.Execute.

method DEFINE.
super
->DEFINE( ).
DATA:
lo_entity  
type REF TO /IWBEP/IF_MGW_ODATA_ENTITY_TYP,
lo_property
type REF TO /IWBEP/IF_MGW_ODATA_PROPERTY.
lo_entity
= model->GET_ENTITY_TYPE( IV_ENTITY_NAME = 'EmpPhoto' ).

IF lo_entity is BOUND.
lo_property
= lo_entity->GET_PROPERTY( IV_PROPERTY_NAME = 'MIMETYPE' ).
lo_property
->SET_AS_CONTENT_TYPE( ).
ENDIF.
endmethod.

method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_STREAM.

   DATA: ls_key_tab                   TYPE /iwbep/s_mgw_name_value_pair,

         lt_key_tab                   TYPE /iwbep/t_mgw_name_value_pair,

         ls_photo                     TYPE zemp_photos,

         lv_email                     TYPE char100.

   CASE iv_entity_name.

     WHEN 'EmpPhoto'.

       READ TABLE it_key_tab WITH KEY name = 'EMP_ID' INTO ls_key_tab.

       IF SY-SUBRC EQ 0.

         ls_photo-emp_id = ls_key_tab-value.

       ENDIF.

       ls_photo-mimetype = is_media_resource-mime_type.

       ls_photo-filename = iv_slug.

       ls_photo-content = is_media_resource-value.

       DELETE FROM zemp_photos WHERE emp_id = ls_photo-emp_id.

       INSERT INTO zemp_photos VALUES ls_photo.

       empphotoset_get_entity(

         EXPORTING

           iv_entity_name     = iv_entity_name

           iv_entity_set_name = iv_entity_set_name

           iv_source_name     = iv_source_name

           it_key_tab         = it_key_tab

           it_navigation_path = it_navigation_path

         IMPORTING

           er_entity          = ls_photo ).

       copy_data_to_ref( EXPORTING is_data = ls_photo

                         CHANGING  cr_data = er_entity ).

   ENDCASE.

endmethod.

 

READ MEDIA


Steps:

URL:

---------------------/ZEMPLOYEE_INFO_SRV/EmpPhotoSet('1')/$value

  1. 1.Redefine GET_STREAM Method Paste code.
  2. 2.Select GET Method of HTTP.
  3. 3.Execute.

method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM.

    DATA: ls_key      TYPE /iwbep/s_mgw_name_value_pair,
lv_email   
TYPE char100,
ls_photo   
TYPE ZEMP_PHOTOS,
ls_lheader 
TYPE ihttpnvp,
ls_stream  
TYPE ty_s_media_resource,
lv_filename
TYPE w3conttype.

CASE iv_entity_name.
WHEN 'EmpPhoto'.

READ TABLE it_key_tab WITH KEY name = 'EMP_ID' INTO ls_key.
lv_email
= ls_key-value.

SELECT SINGLE * FROM ZEMP_PHOTOS INTO CORRESPONDING FIELDS OF ls_photo WHERE EMP_ID = lv_email.
ls_stream
-value = ls_photo-content.
ls_stream
-mime_type = ls_photo-mimetype.

lv_filename
= ls_photo-filename.
*        lv_filename = escape( val = lv_filename
*                              format = cl_abap_format=>e_url ).
*        ls_lheader-name = 'Content-Disposition'.
*        ls_lheader-value = |inline; filename="{ lv_filename }"|.
*        set_header( is_header = ls_lheader ).


ls_stream
-value = ls_photo-content.
ls_stream
-mime_type = lv_filename.

copy_data_to_ref
( EXPORTING is_data = ls_stream
CHANGING  cr_data = er_stream ).

*        copy_data_to_ref( EXPORTING is_data = ls_stream
*                          CHANGING  cr_data = er_stream ).
ENDCASE.

 

***** SOME DUMMY REFERENCE CODE FOR SAME METHOD [CAN BE USEFUL]  ****


**TRY.
*CALL METHOD SUPER->/IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM
*  EXPORTING
*    IV_ENTITY_NAME          =
*    IV_ENTITY_SET_NAME      =
*    IV_SOURCE_NAME          =
*    IT_KEY_TAB              =
*    IT_NAVIGATION_PATH      =
**    io_tech_request_context =
**  IMPORTING
**    er_stream               =
*    .
** CATCH /iwbep/cx_mgw_busi_exception .
** CATCH /iwbep/cx_mgw_tech_exception .
**ENDTRY.


*DATA: ls_stream    TYPE         ty_s_media_resource
*      ,lo_api    TYPE REF TO  if_mr_api
*      ,lv_entity_name TYPE  /IWBEP/MGW_TECH_NAME
*      ,er_entity type REF TO DATA
*      ,lr_entity TYPE REF TO DATA
*      ,empphotoset_get_entity TYPE ZCL_ZEMP_PRO_MPC=>TS_EMPPHOTO
*      ,ls_content type table of TBL1024 initial size 0
*      ,ls_xstring type xstring
*      ,lt_messg type table of BAPIRET2 initial SIZE 0
*      ,lv_pernr(8) TYPE n
*      ,t_photo type table of TBL1024
*      ,l_photo type XSTRING
*      ,l_line type string
*      ,l_photo1 type string
*      ,ls_ret type BAPIRET2
*      ,ls_photo type ZEMP_PHOTOS
*      ,t_msg type table of BAPIRET2.
*
*lv_entity_name = io_tech_request_context->GET_ENTITY_TYPE_NAME( ).
*
*CASE lv_entity_name.
*
*      WHEN 'ZsempDet'.
*      WHEN 'EmpPhoto'.
*
*     empphotoset_get_entity(
*           EXPORTING iv_entity_name     = iv_entity_name
*                     iv_entity_set_name = iv_entity_set_name
*                     iv_source_name     = iv_source_name
*                     it_key_tab         = it_key_tab
*                     it_navigation_path = it_navigation_path
*                     io_tech_request_context = io_tech_request_context
*           IMPORTING er_entity          = empphotoset_get_entity
*                                 ).
*
*           IF empphotoset_get_entity IS NOT INITIAL.
**     Send specific entity data to the caller interface
*           copy_data_to_ref(
*                     EXPORTING
*                     is_data = empphotoset_get_entity
*                     CHANGING
*                     cr_data = er_entity
*                          ).
*           ELSE.
**         In case of initial values - unbind the entity reference
*           er_entity = lr_entity.
*           ENDIF.
*
*           lv_pernr = empphotoset_get_entity-emp_id.
*
*           CALL FUNCTION 'PAD_PHOTO_UPDATE_GET_DETAIL'
*           EXPORTING
*                IV_EMPLOYEE_NUMBER         = lv_pernr
*
*           TABLES
*                T_PHOTO_ARCHIVE_OUT        = t_photo
*                T_MESSAGES_OUT             = t_msg
*                .
*
**           LOOP AT t_photo into  ls_photo.
**                l_line = ls_photo-line.
**                concatenate l_photo1 l_line into l_photo1.
**           ENDLOOP.
*
*               ls_stream-value = l_photo = l_photo1.
*               ls_stream-mime_type = 'image/jpeg'.
*
*               copy_data_to_ref( EXPORTING is_data = ls_stream
*                                 CHANGING  cr_data = er_stream ).
*
*      WHEN OTHERS.

*ENDCASE.

endmethod.

Entity Types.

https://help.sap.com/saphelp_nw74/helpdata/en/bb/dc22512c312314e10000000a44176d/content.htm

Entity Sets.

https://help.sap.com/saphelp_nw74/helpdata/en/b6/dc22512c312314e10000000a44176d/content.htm

  1. Associations.

https://help.sap.com/saphelp_nw74/helpdata/en/07/dc22512c312314e10000000a44176d/content.htm

Navigation Properties.

http://help.sap.com/saphelp_nw74/helpdata/en/3f/dd22512c312314e10000000a44176d/content.htm



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

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

Guys,


If you observe here we are processing only one entity at a time,But in real time scenario we may have to process multiple entities in an instance. 

so for multiple entity processing we need batch processing, Please refer this blog Simple steps to perform Batch Operations in SAP Gateway Services.

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

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


Thanks,


Arshad Shaikh -- :smile: .

51 Comments
Labels in this area