Saturday, February 15, 2020

Media Handling in SAP ODATA Gateway

               Media Handling in SAP ODATA  Gateway :
If you have a requirement of showing PDF or sending images over the gateway, you can do it seamlessly.
This article should give a fair idea of  handling PDF printing in the gateway.
In our case we are calling Smartform PDF in ODATA.
Step1 :Create a Entity Type and Entity Set  and entity type in Segw(I hope you know how to create a project in SEGW ).





Step 2: Mark BPRINT as Media :
Select this checkbox to specify that this entity type belongs to media collection and has a media resource stream. This is indicated in the data service metadata by the attribute HasStream applied to an entity type that is the media link entry.




To make our media entity type work, we need to have some tweaks in MPC_EXT. Since media is a model attribute we need to explicitly redefine the DEFINE method in MPC_EXT.
Step 3 : Go to MPC_EXT and go to Method DEFINE.
We need to re-define method : DEFINE.




Step 4 : REDEFINE method DEFINE METHOD.

It will be empty


Copy the same logic and redefine DEFINE method and write same logic:
Just write your Entity name and property type should be 'MIMETYPE'.



Step 5 :Now go to  DPC_EXTENSION CLASS and write the logic to fetch the data .
/IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM                Instance Method             Public                         
REDEFINE Method and write your logic to fetch the data from Backend system to frontend system.




 METHOD /iwbep/if_mgw_appl_srv_runtime~get_stream.

    
DATAls_key_tab     TYPE /iwbep/s_mgw_name_value_pair,
          ls_lheader     
TYPE ihttpnvp,
          ls_stream      
TYPE ty_s_media_resource,
          lv_sales       
TYPE vbeln,
          lv_pdf_len     
TYPE i,
          lv_pdf_xstring 
TYPE xstring,
          lt_lines       
TYPE TABLE OF tline,
          ls_lines       
TYPE tline.
    
READ TABLE it_key_tab INTO ls_key_tab WITH KEY name 'Vbeln'.
    
IF sy-subrc 0.
      lv_sales 
ls_key_tab-value.
    
ENDIF.

    
DATA wa_outopt  TYPE ssfcompop.
    
DATA t_otfdata  TYPE ssfcrescl.
    
DATA otf_table  TYPE TABLE OF itcoo.
    
DATA control_parameters TYPE ssfctrlop.
    
DATA :  fm_name TYPE rs38l_fnam.
*    do 3 times.
    wa_outopt
-tdcopies 003.
    
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
      
EXPORTING
        formname           
'ZTEST_ODATA_SF'
*       VARIANT            = ' '
*       DIRECT_CALL        = ' '
      
IMPORTING
        fm_name            
fm_name
      
EXCEPTIONS
        no_form            
1
        no_function_module 
2
        
OTHERS             3.
    
IF sy-subrc 0.
      control_parameters
-device    space.
      control_parameters
-getotf    'X'.
*      control_parameters-no_dialog = 'X'.
    
ENDIF.

    
CALL FUNCTION fm_name
      
EXPORTING
        control_parameters 
control_parameters
        output_options     
wa_outopt
        user_settings      
'X'
      
IMPORTING
*       DOCUMENT_OUTPUT_INFO       =
        job_output_info    
t_otfdata
*       JOB_OUTPUT_OPTIONS =
      
EXCEPTIONS
        formatting_error   
1
        internal_error     
2
        send_error         
3
        user_canceled      
4
        
OTHERS             5.
    
IF sy-subrc <> 0.


      
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    
ELSE.
      
APPEND LINES OF t_otfdata-otfdata TO otf_table.
    
ENDIF.


* * CONVERT TO OTF
    
CALL FUNCTION 'CONVERT_OTF'
      
EXPORTING
        
format                'PDF'
        max_linewidth         
132
      
IMPORTING
        bin_filesize          
lv_pdf_len
        bin_file              
lv_pdf_xstring       "” BINARY FILE
      
TABLES
        otf                   
otf_table
        
lines                 lt_lines
      
EXCEPTIONS
        err_max_linewidth     
1
        err_format            
2
        err_conv_not_possible 
3
        err_bad_otf           
4
        
OTHERS                5.
    
IF sy-subrc <> 0.
*   ERROR HANDLING
*    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    
ENDIF.


    ls_stream
-value  lv_pdf_xstring.
    ls_stream
-mime_type 'application/pdf'.
*
    copy_data_to_ref
EXPORTING is_data ls_stream
                      
CHANGING  cr_data er_stream ).

  
ENDMETHOD.


Step 6: Testing ODATA SERVICE in gateway client
After i executed the service :