Get employee image through SAP Netweaver Gateway

1
7633

Hello everyone, in this SAP Netweaver Gateway tutorial, we will learn how to read/get images through SAP Netweaver Gateway from backend system using OData service. These images can be of employee photo or product images etc..

Before proceeding further we assume you know how to build OData service, if not please check out our step-by-step tutorials here,which will give you good understanding of SAP OData service. So lets get started.

Supported System Version

SAP NetWeaver Gateway Release 2.0 Support Package >=05

Business Example

In SAPUI5 application, you want to display employee image (or) display product image.

Step-by-Step procedure to implement the service

In this example i am using Enterprise Procurement Model(EPM) model data to demonstrate the scenario.

We are going to display the photo of employee from the backend system. BAPI  BAPI_EPM_EMPLOYEE_GET_LIST is used to get the employee data.

1. Go to Gateway Service Builder, transaction code SEGW. Create a new project by clicking on the create button in the application toolbar, provide the project details and hit OK button.

Create New Project2. Expand the project, Right click on the Data Model → Create → Entity Type to create an entity type in the OData service.

Create Entity Type3. In the Create Entity Type popup window, enter the Entity Type Name and select the check box Create Related Entity Set to create a default entity set. Hit OK button to create entity type and entity set.

Entity Type Name and Entity Set4. In the Entity Type detail screen, enter the ABAP structure type name as ZDEMO_S_IMAGE and select the check box under Media column.

Mandatory Note : A normal entity type will act as media entity type when you flag it is media by selecting the check box.

ABAP Structure

Set Entity Type as Media5. Save the service till now.Now add the properties to the  entity type.To do so right click on entity type → Import → Properties.

Add Properties6. In the Import Properties window, select EMPLOYEE_ID, FIRST_NAME, LAST_NAME and EMPLOYEE_PIC_URL and PICMIMETYPE fields and click on Next button.

Import Properties 1 of 27. In the next window, select EMPLOYEE_ID as key and click Finish.Save and click on generate button to generate DPC and MPC classes.

Import Properties 2 of 28. Setting Entity Type as Media in the step-4 is not sufficient we also need to set manually via coding.To do so, go to the MPC extension class and redefine the method DEFINE and write the below code inside it.

DATA:
      lo_entity_type TYPE REF TO /iwbep/if_mgw_odata_entity_typ,
      lo_property    TYPE REF TO /iwbep/if_mgw_odata_property.

    super->define( ).

    lo_entity_type = model->get_entity_type( 
                            iv_entity_name = 'EmployeeData' ).
    lo_entity_type->set_is_media( ).

    IF lo_entity_type IS BOUND.
*      Set Content Source
      lo_property = lo_entity_type->get_property( 
                                    iv_property_name = 'EmployeePicUrl' ).
      lo_property->set_as_content_source( ).

*      Set Content Type
      lo_property = lo_entity_type->get_property( 
                                    iv_property_name = 'Picmimetype' ).
      lo_property->set_as_content_type( ).
    ENDIF.

 

8.Now we need to implement the method EMPLOYEEDATASET_GET_ENTITYSET in DPC extension class.Expand the Service Implementation node and right click on GetEntitySet(Query) → Go to Workbench to implement the method.

Service Implementaion9.Write the below code in EMPLOYEEDATASET_GET_ENTITYSET to get the list of employees using BAPI with employee’s picture URL and mime type.

DATA: ls_maxrows       TYPE bapi_epm_max_rows,
      lt_employee_data TYPE STANDARD TABLE OF  bapi_epm_employee,
      ls_employee_data TYPE bapi_epm_employee,
      ls_entityset     TYPE zcl_zdemo_read_image_mpc=>ts_employeedata,
      lt_return        TYPE STANDARD TABLE OF  bapiret2,
      lo_mr_api        TYPE REF TO if_mr_api.

    ls_maxrows-bapimaxrow = 20.
    CALL FUNCTION 'BAPI_EPM_EMPLOYEE_GET_LIST'
      EXPORTING
        max_rows      = ls_maxrows
      TABLES
        employee_data = lt_employee_data
        return        = lt_return.

    lo_mr_api = cl_mime_repository_api=>if_mr_api~get_api( ).

    LOOP AT lt_employee_data INTO ls_employee_data.
      MOVE-CORRESPONDING ls_employee_data TO ls_entityset.
      IF ls_entityset-employee_pic_url IS NOT INITIAL.
        CALL METHOD lo_mr_api->get
          EXPORTING
            i_url              = ls_entityset-employee_pic_url
          IMPORTING
            e_mime_type        = ls_entityset-picmimetype
          EXCEPTIONS
            parameter_missing  = 1
            error_occured      = 2
            not_found          = 3
            permission_failure = 4
            OTHERS             = 5.
        IF sy-subrc <> 0.
        ENDIF.
      ENDIF.
      APPEND ls_entityset TO et_entityset.
    ENDLOOP.

10. As of now we are ready to test the service.Save and generate the service and make sure that DPC and MPC extension classes are active. Go to transaction SAP Netweaver Gateway Client – /IWFND/GW_CLIENT to test the service.You should see the output like below.

Output11. We have successfully got the image URL in the output, but to get the raw data of the image we need to implement method GET_STREAM so that we can use $value in the output URI to get the raw data.Redefine the method in DPC extension class and write the below code.

    DATA:  ls_key_tab  TYPE /iwbep/s_mgw_name_value_pair,
           lv_emp_id   TYPE bapi_epm_employee_id,
           ls_emp_data TYPE bapi_epm_employee,
           lt_return   TYPE STANDARD TABLE OF bapiret2,
           lo_mr_api   TYPE REF TO if_mr_api,
           ls_stream   TYPE ty_s_media_resource.

    lo_mr_api = cl_mime_repository_api=>if_mr_api~get_api( ).

*   Get Employee Id
    READ TABLE it_key_tab INTO ls_key_tab WITH KEY name = 'EmployeeId'.
    IF sy-subrc IS INITIAL.
      lv_emp_id  = ls_key_tab-value.
    ENDIF.

    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input  = lv_emp_id
      IMPORTING
        output = lv_emp_id.

    CALL FUNCTION 'BAPI_EPM_EMPLOYEE_GET_DETAIL'
      EXPORTING
        employee_id   = lv_emp_id
      IMPORTING
        employee_data = ls_emp_data
      TABLES
        return        = lt_return.

    IF ls_emp_data-employee_pic_url IS NOT INITIAL.
      CALL METHOD lo_mr_api->get
        EXPORTING
          i_url              = ls_emp_data-employee_pic_url
        IMPORTING
          e_mime_type        = ls_stream-mime_type
          e_content          = ls_stream-value
        EXCEPTIONS
          parameter_missing  = 1
          error_occured      = 2
          not_found          = 3
          permission_failure = 4
          OTHERS             = 5.
      IF sy-subrc <> 0.
      ENDIF.
    ENDIF.
    copy_data_to_ref( EXPORTING is_data = ls_stream
                      CHANGING cr_data  = er_stream ).

12. Now test the service again by providing the URI as below

/sap/opu/odata/sap/ZDEMO_READ_IMAGE_SRV/EmployeeDataSet(‘2’)/$value

Image in Output

You have successfully created and implemented service by which you get employee image through SAP Netweaver Gateway. Stay tuned to us for more SAP Netweaver Gateway tutorials.

Please feel free to comment and let us know your feedback.

Thank you. 🙂

like-128