Netweaver Gateway Development

How to get image through SAP Netweaver Gateway

How to get image through SAP Netweaver Gateway

System Requirements:  SAP Net weaver Gateway Release 2.0 Support package >= 05

Step By Step Procedure    : In this EPM model we are going to display employee image from the backend of the systemWe are using bapi_employee_get_list to get employee data .



Step 1 : Open SEGW  .

               Create a new project by clicking create button on the application tool bar provide to application details and click ok button.



Step 2: Expand the project , Right click on  DATA MODEL ->  CREATE -> ENTITY TYPE to create a entity type in data model




Step 3 : In the Create Entity Type popup window enter the entity type name and select the check box Create Related Entity Set and Press OK to continue.


Step 4:  In the Entity Type Detail screen, enter the ABAP structure type name as ZDEMO_S_IMAGE and SELECT CHECK BOX UNDER MEDIA (in order to make it a media entity ) column.




Step 5:  Save the service till now. Now add the properties to the entity type . To do so right cclick on entity type  (EmployeeData)->Import->Properties

Step 6:  In the import properties window select the EMPLOYEE_ID,FIRST_NAME,LAST_NAME , EMPLOYEE_PIC_URL and PICMIMETYPE fields and click on NEXT button.


Step 7:  In this window set EMPLOYEE_ID as key and press Finish . SAVE and press on Generate button to create DPC and MPC classes.


Step 8:  Clicking on media check box (in step -4)  is not enough to pass media . we need to redefine DEFINE in MPC extension class

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.


Step 9: Now we need to implement EMPLOYEEDATASET_GET_ENTITYSET in DPC Extension class. Expand the service implementation node and right click on GET_ENTITYSET (Query)àGo To Workbench to implement the method




Step 10: Write the following code in EMPLOYEEDATASET_GET_ENTITYSET to get the details of employees 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.




Step 11: As of now we are ready to test the service. SAVE and GENERATE the service. Now to go to SERVICE MAINTANCE àSAP GATEWAY CLIENTàand press EXECUTE now you should be able to see output like below.



Step 12 : We have successfully got the image URL but to get the raw data we need to implement method GET_STREAM so we can use $value in the output URI to get the RAW data . Redefine the method in the DPC extension and write the 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 ).


Step 13: Now test the service again by calling the service


     /sap/opu/odata/SAP/ZDEMO_READ_IMAGE/EmployeeDataSet('2')/$value



You have successfully created and implemented service to get employee image from through SAP Netweaver Gateway

Please feel free to comment and give feedback









If you enjoyed this post, Please Share!!

6 comments :

  1. Hello Toney,

    thank you for your post. I have a requirement for which i can’t unfortunately find a solution yet.
    I want to provide a ZIP file for downloading based on a complex data selection from the backend with a lot of filter parameters.
    Unfotunately your solution only works in combination with the get_entity method and $value request.
    My requirement is to provide a ZIP file based on the result of the get_entityset method with filter parameters which are not necessarily key attributes of the entity. I would appreciate your help a lot.

    Best Regards,
    Eskender

    ReplyDelete
    Replies
    1. Hi Eskender,
      Have you ever came across this thread scn discussion
      Since get_stream method doesn't allow multiple filters ,passing multiple filters through a string may be a useful workaround.
      hope this helps
      Regards

      Delete
  2. Hello Rajeesh,

    thank you for your reply. Yes, i think you are right. With the current URI convention it is probably the only option to do it.
    I found the method CL_CLB2_TOOLSO=>ODATA_FILTER2SELECT_OPTION for parsing the SearchString. It seems to work with a simple test. Although the method is not used by any other SAP program. Do you know whether there are any oficially released SAP classes for parsing OData strings?

    Best regards,
    Eskender

    ReplyDelete
    Replies
    1. Hi Eskender,
      For parsing query strings we can make use of parameter IO_TECH_REQUEST_CONTEXT.
      for example try below query:
      /sap/opu/odata/SAP/ZDEMO_READ_IMAGE_SRV/EmployeeDataSet('2')/$value?$filter=EmployeeId eq '01'
      you can access the filter query in the form of select options structure by using
      io_tech_request_context->get_filter( )->get_filter_select_options( ) method
      Regards

      Delete
    2. Hello Rajeesh,

      You just made my day. Thank you very much. I didn't expect I would find any other interfaces in the IO_TECH_REQUEST_CONTEXT object rather than the interface /IWBEP/IF_MGW_REQ_ENTITY in the method GET_STREAM. I wouldn't even call this solution a workaround. It is a confusing interface type in the GET_STREAM method but it is exactly what I was looking for.

      Thanks and regards,
      Eskender

      Delete
    3. Hi Eskender,
      You are welcome
      regards

      Delete

Powered by Blogger.