In this how-to guide, you will learn how to add filtering capabilities to an OData service with SAP NetWeaver Gateway that is built using code-based implementation. This how-to guide assumes that you have gone through the following how-to guide “How to Develop a Gateway Service using Code based Implementation” https://scn.sap.com/docs/DOC-43030 which describes the basic implementation steps of the service that we are now going to enhance.
The sample service leverages demo data from the Enterprise Procurement Model which is part of every SAP NetWeaver ABAP server as of 7.02.
After completing this lesson, you will be able to:
You want to build an application that shows a list of products that supports filtering for certain properties and that supports client-side paging.
You have implemented an OData service as described in the how-to guide “How to Develop a Gateway Service using Code based Implementation” https://scn.sap.com/docs/DOC-43030
In this how-to guide, you will implement filtering capabilities to the attributes ProductID, Supplier Name, Category and Price to the entity set Products. In addition you will add $skip and $top support to the entity set Products.
Hints:
Hints:
When checking the $metadata document you will find no entry such as sap:filterable = “true” for the properties you have maintained previously. However you will notice the setting sap:filterable = “false” being active for those properties that you have not maintained.
method PRODUCTS_GET_ENTITYSET.
DATA: ls_data LIKE LINE OF et_entityset,
lt_headerdata TYPE STANDARD TABLE OF bapi_epm_bp_header,
ls_headerdata TYPE bapi_epm_bp_header,
lv_maxrows TYPE bapi_epm_max_rows,
lv_top TYPE string,
lv_skip TYPE I,
lv_skiptoken TYPE string,
lt_return TYPE TABLE OF bapiret2,
lo_message_container TYPE REF TO /iwbep/if_message_container,
lt_filters TYPE /iwbep/t_mgw_select_option,
ls_filter TYPE /iwbep/s_mgw_select_option,
ls_so TYPE /iwbep/s_cod_select_option,
lt_product_id TYPE TABLE OF BAPI_EPM_PRODUCT_ID_RANGE,
ls_product_id TYPE BAPI_EPM_PRODUCT_ID_RANGE,
lt_supplier_name TYPE TABLE OF BAPI_EPM_SUPPLIER_NAME_RANGE,
ls_supplier_name TYPE BAPI_EPM_SUPPLIER_NAME_RANGE,
lt_category TYPE TABLE OF BAPI_EPM_PRODUCT_CATEG_RANGE,
ls_category TYPE BAPI_EPM_PRODUCT_CATEG_RANGE
.
DATA: lt_price TYPE RANGE OF BAPI_EPM_PRODUCT_HEADER-PRICE,
ls_price like LINE OF lt_price.
*-get filter
lt_filters = io_tech_request_context->get_filter( )->get_filter_select_options( ).
*-get filter for ProductID
READ TABLE lt_filters WITH TABLE KEY property = 'PRODUCT_ID' INTO ls_filter.
IF sy-subrc EQ 0.
LOOP AT ls_filter-select_options INTO ls_so.
MOVE-CORRESPONDING ls_so TO ls_product_id.
INSERT ls_product_id INTO TABLE lt_product_id.
ENDLOOP.
ENDIF.
*-get filter for category
READ TABLE lt_filters WITH TABLE KEY property = 'CATEGORY' INTO ls_filter.
IF sy-subrc EQ 0.
LOOP AT ls_filter-select_options INTO ls_so.
MOVE-CORRESPONDING ls_so TO ls_category.
INSERT ls_category INTO TABLE lt_category.
ENDLOOP.
ENDIF.
*-get filter for supplier name
READ TABLE lt_filters WITH TABLE KEY property = 'SUPPLIER_NAME' INTO ls_filter.
IF sy-subrc EQ 0.
LOOP AT ls_filter-select_options INTO ls_so.
MOVE-CORRESPONDING ls_so TO ls_supplier_name.
INSERT ls_supplier_name INTO TABLE lt_supplier_name.
ENDLOOP.
ENDIF.
CALL FUNCTION 'BAPI_EPM_PRODUCT_GET_LIST'
* EXPORTING
* MAX_ROWS = lv_maxrows
TABLES
HEADERDATA = et_entityset
SELPARAMPRODUCTID = lt_product_id
SELPARAMSUPPLIERNAMES = lt_supplier_name
SELPARAMCATEGORIES = lt_category
* RETURN =
.
*-get filter for Price
READ TABLE lt_filters WITH TABLE KEY property = 'PRICE' INTO ls_filter.
IF sy-subrc EQ 0.
LOOP AT ls_filter-select_options INTO ls_so.
MOVE-CORRESPONDING ls_so TO ls_price.
INSERT ls_price INTO TABLE lt_price.
ENDLOOP.
ENDIF.
* remove data returned by BAPI that does not match the
* additional filter criteria for price
DELETE et_entityset where price not IN lt_price.
ENDMETHOD.
<m:properties>
<d:ProductID>HT-1502</d:ProductID>
<d:Category>Workstation ensemble</d:Category>
<d:Name>Server Power Pro</d:Name>
<d:Description>Dual socket, quad-core processing server with 1644 MHz Front Side Bus with 100Gb connectivity</d:Description>
<d:SupplierID>100000025</d:SupplierID>
<d:SupplierName>Tessile Casa Di Roma</d:SupplierName>
<d:Price>25000.0000</d:Price>
<d:CurrencyCode>EUR</d:CurrencyCode>
</m:properties>
*- get number of records requested
lv_top = io_tech_request_context->get_top( ).
lv_skip = io_tech_request_context->get_skip( ).
*- value for maxrows must only be calculated if the request also contains a $top
IF lv_top IS NOT INITIAL.
lv_maxrows-bapimaxrow = lv_top + lv_skip.
ENDIF.
2. As in the previous tasks you may copy the code from the following code snippet:
method PRODUCTS_GET_ENTITYSET.
DATA: ls_data LIKE LINE OF et_entityset,
lt_headerdata TYPE STANDARD TABLE OF bapi_epm_bp_header,
ls_headerdata TYPE bapi_epm_bp_header,
lv_maxrows TYPE bapi_epm_max_rows,
lv_top TYPE string,
lv_skip TYPE I,
lv_skiptoken TYPE string,
lt_return TYPE TABLE OF bapiret2,
lo_message_container TYPE REF TO /iwbep/if_message_container,
lt_filters TYPE /iwbep/t_mgw_select_option,
ls_filter TYPE /iwbep/s_mgw_select_option,
ls_so TYPE /iwbep/s_cod_select_option,
lt_product_id TYPE TABLE OF BAPI_EPM_PRODUCT_ID_RANGE,
ls_product_id TYPE BAPI_EPM_PRODUCT_ID_RANGE,
lt_supplier_name TYPE TABLE OF BAPI_EPM_SUPPLIER_NAME_RANGE,
ls_supplier_name TYPE BAPI_EPM_SUPPLIER_NAME_RANGE,
lt_category TYPE TABLE OF BAPI_EPM_PRODUCT_CATEG_RANGE,
ls_category TYPE BAPI_EPM_PRODUCT_CATEG_RANGE
.
DATA: lt_price TYPE RANGE OF BAPI_EPM_PRODUCT_HEADER-PRICE,
ls_price like LINE OF lt_price.
*- get number of records requested
lv_top = io_tech_request_context->get_top( ).
*- get number of lines that should be skipped
lv_skip = io_tech_request_context->get_skip( ).
*- value for maxrows must only be calculated if the request also contains a $top
IF lv_top IS NOT INITIAL.
lv_maxrows-bapimaxrow = lv_top + lv_skip.
ENDIF.
*-get filter
lt_filters = io_tech_request_context->get_filter( )->get_filter_select_options( ).
*-get filter for ProductID
READ TABLE lt_filters WITH TABLE KEY property = 'PRODUCT_ID' INTO ls_filter.
IF sy-subrc EQ 0.
LOOP AT ls_filter-select_options INTO ls_so.
MOVE-CORRESPONDING ls_so TO ls_product_id.
INSERT ls_product_id INTO TABLE lt_product_id.
ENDLOOP.
ENDIF.
*-get filter for category
READ TABLE lt_filters WITH TABLE KEY property = 'CATEGORY' INTO ls_filter.
IF sy-subrc EQ 0.
LOOP AT ls_filter-select_options INTO ls_so.
MOVE-CORRESPONDING ls_so TO ls_category.
INSERT ls_category INTO TABLE lt_category.
ENDLOOP.
ENDIF.
*-get filter for supplier name
READ TABLE lt_filters WITH TABLE KEY property = 'SUPPLIER_NAME' INTO ls_filter.
IF sy-subrc EQ 0.
LOOP AT ls_filter-select_options INTO ls_so.
MOVE-CORRESPONDING ls_so TO ls_supplier_name.
INSERT ls_supplier_name INTO TABLE lt_supplier_name.
ENDLOOP.
ENDIF.
CALL FUNCTION 'BAPI_EPM_PRODUCT_GET_LIST'
EXPORTING
MAX_ROWS = lv_maxrows
TABLES
HEADERDATA = et_entityset
SELPARAMPRODUCTID = lt_product_id
SELPARAMSUPPLIERNAMES = lt_supplier_name
SELPARAMCATEGORIES = lt_category
* RETURN =
.
*-get filter for Price
READ TABLE lt_filters WITH TABLE KEY property = 'PRICE' INTO ls_filter.
IF sy-subrc EQ 0.
LOOP AT ls_filter-select_options INTO ls_so.
MOVE-CORRESPONDING ls_so TO ls_price.
INSERT ls_price INTO TABLE lt_price.
ENDLOOP.
ENDIF.
* remove data returned by BAPI that does not match the
* additional filter criteria for price
DELETE et_entityset where price not IN lt_price.
* skipping entries specified by $skip
IF lv_skip IS NOT INITIAL.
DELETE et_entityset TO lv_skip.
endif.
endmethod.
You are done !!!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
38 | |
19 | |
14 | |
12 | |
10 | |
10 | |
10 | |
8 | |
8 | |
8 |