SAP Netweaver Gateway and OData. Part XII. Media Handling using OData Gateways

Media Handling using OData-Gateways


The conundrum (confusion/difficulty) of handling media through gateway service is gone. If you have a requirement of showing PDF or sending images/music over the gateway, you can do it seamlessly. While in recent times we get a torrent of requirements of Smartforms printing, very fewer opportunities are there to pass image/music files. This article should give a fair idea about the tit-bits of handling PDF printing in the gateway.


Step-1: Create Entity to Handle Media Information

Create a gateway project (Follow the previous blogs for details of creating a project and subsequent actions). Create a separate entity to handle the media operations. Don’t forget to tick the ‘Media’ check box as this indicates the entity as media.

Step-2: Generate the runtime artifacts

Generate the runtime artifacts of the project. All the required methods we will find in Data Provider Class and Model Provider Class. Remember we will start our development with Model Provider Class this time, unlike other CRUD operations. Let’s start with our development.

Step-3: Code in MPC

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.

Basic method coding is explained below. Remember, it is extremely important that you write the statement super->define( ) otherwise we would not be able to access the model object. From the MPC we want the entity “SoPrint” to work with. In my requirement, I need to have the ‘OrderNo’ property of the attribute ‘SoPrint’. You have to set the attribute as content type with set_as_content_type( ) method.

method DEFINE.
DATA: lo_property type REF TO /iwbep/if_mgw_odata_property,
super->define( ).
lo_entity_type = model->get_entity_type(
iv_entity_name = 'SoPrint').
if lo_entity_type is bound.
lo_property = lo_entity_type->get_property('OrderNo').
lo_property->set_as_content_type( ).

Step -4: Create a Smartform to Print

Before we go and code in the DPC_EXT let’s create a Smartform which will simply show a Smartform with some text. Obviously, you will have your own Smartform with complex business logic and various importing parameters. Our simple Smartform looks like below.

Step-5: Code in DPC_EXT

In DPC_EXT we have a separate method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM to handle the GET media operation. I know it is needless to copy the code for the readers of this blog as they have more acumen in doing the coding part compared to me.

DATA: ls_key_tab type /iwbep/s_mgw_name_value_pair,
ls_stream  type ty_s_media_resource,
lv_so_id   type vbeln.
DATA: lv_fname              type rs38l_fnam,
ls_control_parameters type ssfctrlop,
ls_output_options     type ssfcompop,
lv_device_type        type rspoptype,
bin_pdfx              type xstring,
ls_otf_data           type ssfcrescl.
DATA: lt_otf        type standard table of itcoo,
lt_lines      type standard table of tline,
t_otf_from_fm type ssfcrescl.
*  You can pass the order number to the Smartform to generate an order related Smartform 
READ TABLE it_key_tab into ls_key_tab with key name = 'OrderNo'.
*  Get the order 
*  For this sales order we are going to prepare string data for the PDF
lv_so_id = ls_key_tab-value.
*  Call the smartform to get the smartform name. I have not passed the order since *the Smartform I am using is a generic one
FM_NAME    = lv_fname.
*Prepare control parameter
ls_output_options-tddest        = 'LOCL'.
ls_output_options-xdfcmode      = abap_true.
ls_output_options-xsfcmode      = abap_true.
ls_output_options-tdnewid       = abap_true.
ls_output_options-tdimmed       = abap_true.
ls_control_parameters-no_dialog = abap_true.
ls_control_parameters-preview   = space.
ls_control_parameters-getotf    = abap_true. " Extremely Important to get OTF data
*  Call the function module of smartforms
CONTROL_PARAMETERS = ls_control_parameters
OUTPUT_OPTIONS     = ls_output_options
JOB_OUTPUT_INFO    = t_otf_from_fm
formatting_error   = 1
internal_error     = 2
send_error         = 3
user_canceled      = 4
OTHERS             = 5.
*  Pass otf data to otf table
lt_otf[] = t_otf_from_fm-otfdata[].
* Convert OTF to xString
FORMAT                      = 'PDF'
BIN_FILE                    = bin_pdfx
OTF                        = lt_otf
LINES                      = lt_lines
ERR_MAX_LINEWIDTH           = 1
ERR_FORMAT                  = 2
ERR_BAD_OTF                 = 4
OTHERS                      = 5.
*  Pass xstring value
ls_stream-value = bin_pdfx.
ls_stream-mime_type = 'application/pdf'.
*  Copy data to reference
exporting is_data = ls_stream
changing  cr_data = er_stream ).

Voila! You have done the required coding and you should be ready to test the solution.

URI: /sap/opu/odata/SAP/ZDEMO_GW_SRV/SoPrintSet(”)/$value

Note– You can pass any order no in the SoPrintSet attribute or the attribute keys you have used.

Save or print the Smartform!

Tip of the day

In the DPC_EXT programming, you might face an issue of blank JOB_OUTPUT_INFO in Smartform calling function module, i.e. t_otf_from_fm will be blank. I personally faced this problem and after a considerable amount of options found that the actual issue was with the DEFAULT output device configuration associated with my sap account. If you don’t have any output device configured in your account (can be checked through SU3 transaction) you will not get any value in JOB_OUTPUT_INFO table.

It is also important that you pass ls_control_parameters–getotf    = abap_true to get the other text format output.