Media Handling using OData-Gateways
Introduction
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.
Steps
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,
lo_entity_type TYPE REF TO /IWBEP/IF_MGW_ODATA_ENTITY_TYP.
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( ).
endif.
endmethod.
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.
method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM.
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
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
EXPORTING
FORMNAME = 'ZTEST61'
IMPORTING
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
CALL FUNCTION lv_fname
EXPORTING
CONTROL_PARAMETERS = ls_control_parameters
OUTPUT_OPTIONS = ls_output_options
IMPORTING
JOB_OUTPUT_INFO = t_otf_from_fm
EXCEPTIONS
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
CALL FUNCTION 'CONVERT_OTF'
EXPORTING
FORMAT = 'PDF'
IMPORTING
BIN_FILE = bin_pdfx
TABLES
OTF = lt_otf
LINES = lt_lines
EXCEPTIONS
ERR_MAX_LINEWIDTH = 1
ERR_FORMAT = 2
ERR_CONV_NOT_POSSIBLE = 3
ERR_BAD_OTF = 4
OTHERS = 5.
* Pass xstring value
ls_stream-value = bin_pdfx.
ls_stream-mime_type = 'application/pdf'.
* Copy data to reference
copy_data_to_ref(
exporting is_data = ls_stream
changing cr_data = er_stream ).
endmethod.
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.
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.