Association - Navigation and Data Provider Expand in OData Service
Association - Navigation and Data Provider Expand in OData Service
o Framework expand
Implementing GET_EXPANDED_ENTITYSET
Implementing GET_EXPANDED_ENTITY
Closing Remarks
Introduction
In my earlier blog Let’s code CRUDQ and Function Import operations in OData service! we
understood the basic operation performed in OData service.
In this blog I will explain creation of simple SAP Gateway OData service having association and navigation
between entities. Also we will see how to implement it through code based approach and finally conclude with
implantation of GET_EXPANDED_ENTITYSET and GET_EXPANDED_ENTITY
invoked by $expand.
Note - Steps mentioned in this blog are performed in SAP Gateway System SP08 (Embedded Architecture)
Let’s see what is meant by association and navigation property.
Associations define the relationship between two or more Entity Types (for example, Employee
WorksFor Department). Instances of associations are grouped in Association Sets.
Navigation Properties are special properties on Entity Types which are bound to a specific
association and can be used to refer to associations of an entity.
Finally, all instance containers (Entity Sets and Association Sets) are grouped in an Entity
Container.
Also let's understand the difference between association/navigation and $expand. In short, it is as
below,
Give me
associated
(dependent)
Association/Navigation entity/entities http://services.odata.org/OData/OData.svc/Categories(1)/Products?$form
using
Navigation
property
Give me
associated
(dependent)
entity/entities
$expand + Principal http://services.odata.org/OData/OData.svc/Categories(1)?$expand=Produ
entity/entities
using
Navigation
property
you can also refer this nice blog Implementing Expand Entity/Entity Set by Srikanth Gajula
Scenario
We will read Sales order, items and product data from Enterprise Procurement Model (EPM).
This is the pictorial representation of sales order, items and product with their association.
We will use below BAPIs to get the Sales Order, Items and Product data in DPC_EXT class
methods.
BAPI_EPM_SO_GET_LIST
BAPI_EPM_SO_GET_DETAIL
BAPI_EPM_PRODUCT_GET_DETAIL
We will code for association/navigation and data provider expand scenario and will also
understand the framework expand.
Before that just look at difference between Data provider expand and framework expand
Reference - Expand in Framework and Data Provider - SAP NetWeaver Gateway - SAP Library
Procedure
Create entity SalesOrder by importing DDIC structure as shown below. Please note that Entity set will be
created by default if the check box “Create Default Entity Set” is checked.
Repeat the process for entities SalesOrderItem and Product. End result will be as displayed
below.
Now let’s create association, navigation etc. By using Create Association wizard, it is just 3 steps
as displayed below. This will create Navigation property, Association set etc.
Create association between entities SalesOrder and SalesOrderItem with navigation property as
OrderToItems.
On similar lines, create association between entities SalesOrderItem and Product with navigation
property as ItemToProduct.
Please note that we do not have referential constraints created between SalesOrderItem and
Product.
In this section, we will redefine methods in DPC_EXT class. Please note that code provided in
this blog is in simplest form. You may need to consider proper error handling and other best
practices while writing the code.
Association/Navigation
/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet
METHOD salesorderset_get_entityset.
DATA: lt_salesorder TYPE TABLE OF bapi_epm_so_header,
ls_salesorder LIKE LINE OF lt_salesorder,
ls_entity LIKE LINE OF et_entityset,
l_max_rows TYPE bapi_epm_max_rows.
l_max_rowsbapimaxrow = '10'.
CALL FUNCTION 'BAPI_EPM_SO_GET_LIST'
EXPORTING
max_rows = l_max_rows
TABLES
soheaderdata = lt_salesorder.
*Fill ET_ENTITYSET
LOOP AT lt_salesorder INTO ls_salesorder .
MOVECORRESPONDING ls_salesorder TO ls_entity.
APPEND ls_entity TO et_entityset.
ENDLOOP.
ENDMETHOD.
/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet('500000009')
METHOD salesorderset_get_entity.
DATA: ls_salesorder TYPE bapi_epm_so_header,
ls_key_tab TYPE /iwbep/s_mgw_name_value_pair,
lv_soid TYPE bapi_epm_so_id.
*Get the key property values
READ TABLE it_key_tab WITH KEY name = 'SoId' INTO ls_key_tab.
lv_soid = ls_key_tabvalue.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = lv_soid
IMPORTING
output = lv_soid.
CALL FUNCTION 'BAPI_EPM_SO_GET_DETAIL'
EXPORTING
so_id = lv_soid
IMPORTING
headerdata = ls_salesorder.
*Fill ER_ENTITY
MOVECORRESPONDING ls_salesorder TO er_entity.
ENDMETHOD.
Redefine method SALESORDERITEMSE_GET_ENTITYSET as below with URI
/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet('500000008')/OrderToItems
METHOD salesorderitemse_get_entityset.
DATA: ls_salesorder TYPE bapi_epm_so_header,
lt_itemdata TYPE TABLE OF bapi_epm_so_item,
ls_itemdata TYPE bapi_epm_so_item,
ls_entity LIKE LINE OF et_entityset.
DATA: ls_key_tab TYPE /iwbep/s_mgw_name_value_pair,
lv_soid TYPE bapi_epm_so_id.
*Get the key property values
READ TABLE it_key_tab WITH KEY name = 'SoId' INTO ls_key_tab.
lv_soid = ls_key_tabvalue.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = lv_soid
IMPORTING
output = lv_soid.
CALL FUNCTION 'BAPI_EPM_SO_GET_DETAIL'
EXPORTING
so_id = lv_soid
IMPORTING
headerdata = ls_salesorder
TABLES
itemdata = lt_itemdata.
LOOP AT lt_itemdata INTO ls_itemdata.
MOVECORRESPONDING ls_itemdata TO ls_entity .
APPEND ls_entity TO et_entityset.
ENDLOOP.
ENDMETHOD.
Notice that we used navigation property OrderToItems to get the associated entities.
Framework expand
/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet?$expand=OrderToItems
This calls method SALESORDERSET_GET_ENTITYSET and
SALESORDERITEMSE_GET_ENTITYSET in loop.
This will give you performance statistics for OData request. For more information, refer Some
new features in SAP NW Gateway 2.0 SP08
METHOD salesorderitemse_get_entity.
DATA: ls_salesorder TYPE bapi_epm_so_header.
DATA: ls_key_tab TYPE /iwbep/s_mgw_name_value_pair,
lv_soid TYPE bapi_epm_so_id,
lv_soitempos TYPE snwd_so_item_pos,
lt_itemdata TYPE TABLE OF bapi_epm_so_item,
ls_itemdata TYPE bapi_epm_so_item.
*Get the key property values
READ TABLE it_key_tab WITH KEY name = 'SoId' INTO ls_key_tab.
lv_soid = ls_key_tabvalue.
READ TABLE it_key_tab WITH KEY name = 'SoItemPos' INTO
ls_key_tab.
lv_soitempos = ls_key_tabvalue.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = lv_soid
IMPORTING
output = lv_soid.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = lv_soitempos
IMPORTING
output = lv_soitempos.
*Get data from BAPI
CALL FUNCTION 'BAPI_EPM_SO_GET_DETAIL'
EXPORTING
so_id = lv_soid
IMPORTING
headerdata = ls_salesorder
TABLES
itemdata = lt_itemdata.
READ TABLE lt_itemdata INTO ls_itemdata WITH KEY so_id =
lv_soid
so_item_pos
= lv_soitempos.
IF sysubrc = 0.
MOVECORRESPONDING ls_itemdata TO er_entity.
ENDIF.
ENDMETHOD.
To read the data for below URI, we need to implement logic as below in method
PRODUCTSET_GET_ENTITY
Execution URI -
/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet('500000001')/OrderToItems(SoId
='500000001',SoItemPos='0000000010')/ItemToProduct
METHOD productset_get_entity.
DATA: ls_salesorder TYPE bapi_epm_so_header.
DATA: ls_key_tab TYPE /iwbep/s_mgw_name_value_pair,
lv_soid TYPE bapi_epm_so_id,
lv_soitempos TYPE snwd_so_item_pos,
lt_itemdata TYPE TABLE OF bapi_epm_so_item,
ls_itemdata TYPE bapi_epm_so_item.
DATA: ls_navigation TYPE /iwbep/s_mgw_navigation_path,
lv_property TYPE string.
DATA: lv_product_id TYPE bapi_epm_product_id,
lv_product_header TYPE bapi_epm_product_header.
IF iv_source_name = iv_entity_name.
*Get the key property values
READ TABLE it_key_tab WITH KEY name = 'ProductId' INTO
ls_key_tab.
IF sysubrc = 0.
* MOVECORRESPONDING ls_itemdata to er_entity.
lv_product_id = ls_key_tabvalue.
CALL FUNCTION 'BAPI_EPM_PRODUCT_GET_DETAIL'
EXPORTING
product_id = lv_product_id
IMPORTING
headerdata = lv_product_header.
MOVECORRESPONDING lv_product_header TO er_entity.
ENDIF.
ELSE.
IF it_navigation_path IS NOT INITIAL.
READ TABLE it_navigation_path INTO ls_navigation INDEX 1.
IF sysubrc EQ 0.
CASE ls_navigationnav_prop.
WHEN 'OrderToItems'.
LOOP AT ls_navigationkey_tab INTO ls_key_tab.
CASE ls_key_tabname.
WHEN 'SoId'.
lv_soid = ls_key_tabvalue.
WHEN 'SoItemPos'.
lv_soitempos = ls_key_tabvalue.
WHEN OTHERS.
ENDCASE.
ENDLOOP.
ENDCASE.
ENDIF.
ENDIF.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = lv_soid
IMPORTING
output = lv_soid.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = lv_soitempos
IMPORTING
output = lv_soitempos.
*Get data from BAPI_EPM_SO_GET_DETAIL
CALL FUNCTION 'BAPI_EPM_SO_GET_DETAIL'
EXPORTING
so_id = lv_soid
IMPORTING
headerdata = ls_salesorder
TABLES
itemdata = lt_itemdata.
*Fill ER_ENTITY
READ TABLE lt_itemdata INTO ls_itemdata WITH KEY so_id =
lv_soid
so_item_pos = lv_soitempos.
IF sysubrc = 0.
lv_product_idproduct_id = ls_itemdataproduct_id.
CALL FUNCTION 'BAPI_EPM_PRODUCT_GET_DETAIL'
EXPORTING
product_id = lv_product_id
IMPORTING
headerdata = lv_product_header.
MOVECORRESPONDING lv_product_header TO er_entity.
ENDIF.
ENDIF.
ENDMETHOD.
Product details will not filled as in the navigation keys are empty because we do not have
referential constraint.
/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet('500000000')/
$links/OrderToItems
/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet('500000009')/
$links/OrderToItems/$count
In this section, we will see how to implement data provider expand by redefining
GET_EXPANDED_ENTITYSET and GET_EXPANDED_ENTITY.
Implementing GET_EXPANDED_ENTITYSET
You will not get any response as you need to implement the code yourself.
One of the important point while implementing logic is Data declaration! Based on level till
which you want to expand, you need to define your internal table having nested structure or table
type.
In below code, we want to expand Sales Order and its items. Hence the expand technical clause
will be ORDERTOITEMS.
METHOD /iwbep/if_mgw_appl_srv_runtime~get_expanded_entityset.
DATA: BEGIN OF t_expand_so.
INCLUDE TYPE
zcl_ztest_dp_expand_mpc_ext=>ts_salesorder.
DATA: ordertoitems TYPE
zcl_ztest_dp_expand_mpc_ext=>tt_salesorderitem,
END OF t_expand_so.
DATA: lt_expand_so LIKE TABLE OF t_expand_so,
ls_expand_so LIKE t_expand_so,
ls_item TYPE
zcl_ztest_dp_expand_mpc_ext=>ts_salesorderitem.
DATA: lt_salesorder TYPE TABLE OF bapi_epm_so_header,
ls_salesorder LIKE LINE OF lt_salesorder,
lt_itemdata TYPE TABLE OF bapi_epm_so_item,
ls_itemdata TYPE bapi_epm_so_item,
l_max_rows TYPE bapi_epm_max_rows.
CONSTANTS: lc_expand_tech_clause TYPE string VALUE
'ORDERTOITEMS'.
* Read Sales Order and Item data
l_max_rowsbapimaxrow = '10'.
CALL FUNCTION 'BAPI_EPM_SO_GET_LIST'
EXPORTING
max_rows = l_max_rows
TABLES
soheaderdata = lt_salesorder
soitemdata = lt_itemdata.
* Data processing logic
LOOP AT lt_salesorder INTO ls_salesorder.
MOVECORRESPONDING ls_salesorder TO ls_expand_so .
LOOP AT lt_itemdata INTO ls_itemdata WHERE so_id =
ls_salesorderso_id .
MOVECORRESPONDING ls_itemdata TO ls_item .
APPEND ls_item TO ls_expand_soordertoitems.
CLEAR: ls_item.
ENDLOOP.
APPEND ls_expand_so TO lt_expand_so.
CLEAR: ls_expand_so.
ENDLOOP.
* Fill EE_ENTITYSET
copy_data_to_ref(
EXPORTING
is_data = lt_expand_so
CHANGING
cr_data = er_entityset ).
* Insert Navigation property into ET_EXPANDED_TECH_CLAUSES
INSERT lc_expand_tech_clause INTO TABLE
et_expanded_tech_clauses.
ENDMETHOD.
Query with URI /sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet?
$expand=OrderToItems&sap-statistics=true to check the runtime statistics and compare the
values with before implementing data provider expand.
Now we will try to expand to one level more i.e. we will expand sales order, its items and
product of each item. In this case the expand technical clause will be
ORDERTOITEMS/ITEMTOPRODUCT.
METHOD /iwbep/if_mgw_appl_srv_runtime~get_expanded_entityset.
DATA: BEGIN OF t_orderitems.
INCLUDE TYPE
zcl_ztest_dp_expand_mpc_ext=>ts_salesorderitem.
DATA: itemtoproduct TYPE
zcl_ztest_dp_expand_mpc_ext=>ts_product,
END OF t_orderitems.
DATA: BEGIN OF t_expand_so.
INCLUDE TYPE
zcl_ztest_dp_expand_mpc_ext=>ts_salesorder.
DATA: ordertoitems LIKE TABLE OF t_orderitems,
END OF t_expand_so.
DATA: lt_expand_so LIKE TABLE OF t_expand_so,
ls_expand_so LIKE t_expand_so,
ls_item LIKE t_orderitems.
DATA: lt_salesorder TYPE TABLE OF bapi_epm_so_header,
ls_salesorder LIKE LINE OF lt_salesorder,
lt_itemdata TYPE TABLE OF bapi_epm_so_item,
ls_itemdata TYPE bapi_epm_so_item,
l_max_rows TYPE bapi_epm_max_rows.
DATA: lv_product_id TYPE bapi_epm_product_id,
ls_product_header TYPE bapi_epm_product_header.
CONSTANTS: lc_expand_tech_clause TYPE string VALUE
'ORDERTOITEMS/ITEMTOPRODUCT'.
* Read Sales Order and Item data
l_max_rowsbapimaxrow = '10'.
CALL FUNCTION 'BAPI_EPM_SO_GET_LIST'
EXPORTING
max_rows = l_max_rows
TABLES
soheaderdata = lt_salesorder
soitemdata = lt_itemdata.
* Data processing logic
LOOP AT lt_salesorder INTO ls_salesorder.
MOVECORRESPONDING ls_salesorder TO ls_expand_so .
LOOP AT lt_itemdata INTO ls_itemdata WHERE so_id =
ls_salesorderso_id .
MOVECORRESPONDING ls_itemdata TO ls_item .
lv_product_id = ls_itemdataproduct_id.
CALL FUNCTION 'BAPI_EPM_PRODUCT_GET_DETAIL'
EXPORTING
product_id = lv_product_id
IMPORTING
headerdata = ls_product_header.
MOVECORRESPONDING ls_product_header TO ls_item
itemtoproduct.
APPEND ls_item TO ls_expand_soordertoitems.
CLEAR: ls_item.
ENDLOOP.
APPEND ls_expand_so TO lt_expand_so.
CLEAR: ls_expand_so, lv_product_id.
ENDLOOP.
* Fill EE_ENTITYSET
copy_data_to_ref(
EXPORTING
is_data = lt_expand_so
CHANGING
cr_data = er_entityset ).
* Insert Navigation property into ET_EXPANDED_TECH_CLAUSES
INSERT lc_expand_tech_clause INTO TABLE
et_expanded_tech_clauses.
ENDMETHOD.
/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet('500000005')?
$expand=OrderToItems/ItemToProduct
METHOD /iwbep/if_mgw_appl_srv_runtime~get_expanded_entity.
DATA: BEGIN OF t_orderitems.
INCLUDE TYPE
zcl_ztest_dp_expand_mpc_ext=>ts_salesorderitem.
DATA: itemtoproduct TYPE
zcl_ztest_dp_expand_mpc_ext=>ts_product,
END OF t_orderitems.
DATA: BEGIN OF t_expand_so.
INCLUDE TYPE
zcl_ztest_dp_expand_mpc_ext=>ts_salesorder.
DATA: ordertoitems LIKE TABLE OF t_orderitems,
END OF t_expand_so.
DATA: ls_expand_so LIKE t_expand_so,
ls_item LIKE t_orderitems.
DATA: ls_salesorder TYPE bapi_epm_so_header,
lt_itemdata TYPE TABLE OF bapi_epm_so_item,
ls_itemdata TYPE bapi_epm_so_item.
DATA: ls_key_tab TYPE /iwbep/s_mgw_name_value_pair,
lv_soid TYPE bapi_epm_so_id.
DATA: lv_product_id TYPE bapi_epm_product_id,
ls_product_header TYPE bapi_epm_product_header.
CONSTANTS: lc_expand_tech_clause TYPE string VALUE
'ORDERTOITEMS/ITEMTOPRODUCT'.
*Get the key property values and Read Sales Order and Item data
READ TABLE it_key_tab WITH KEY name = 'SoId' INTO ls_key_tab.
lv_soid = ls_key_tabvalue.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = lv_soid
IMPORTING
output = lv_soid.
CALL FUNCTION 'BAPI_EPM_SO_GET_DETAIL'
EXPORTING
so_id = lv_soid
IMPORTING
headerdata = ls_salesorder
TABLES
itemdata = lt_itemdata.
* Data processing logic
MOVECORRESPONDING ls_salesorder TO ls_expand_so .
LOOP AT lt_itemdata INTO ls_itemdata WHERE so_id =
ls_salesorderso_id .
MOVECORRESPONDING ls_itemdata TO ls_item .
lv_product_id = ls_itemdataproduct_id.
CALL FUNCTION 'BAPI_EPM_PRODUCT_GET_DETAIL'
EXPORTING
product_id = lv_product_id
IMPORTING
headerdata = ls_product_header.
MOVECORRESPONDING ls_product_header TO ls_item
itemtoproduct.
APPEND ls_item TO ls_expand_soordertoitems.
ENDLOOP.
* Fill ER_ENTITY
copy_data_to_ref(
EXPORTING
is_data = ls_expand_so
CHANGING
cr_data = er_entity ).
* Insert Navigation property into ET_EXPANDED_TECH_CLAUSES
INSERT lc_expand_tech_clause INTO TABLE
et_expanded_tech_clauses.
ENDMETHOD.
Closing Remarks
Important points to be considered while coding for association/navigation and data provider
$expand,
Use of navigation path and navigation key. Entities can be directly accessed or via
navigation property. Code for both scenario using navigation path and navigation keys.
Data declaration of internal tables in case of data provider expand. Understand the
relations between entities. while declaring internal table, use navigation property name to
address dependent entity structure. It should be same. Check below data declaration.
DATA: BEGIN OF t_orderitems.
INCLUDE TYPE
zcl_ztest_fw_expand_mpc_ext=>ts_salesorderitem.
DATA: itemtoproduct TYPE
zcl_ztest_fw_expand_mpc_ext=>ts_product,
END OF t_orderitems.
DATA: BEGIN OF t_expand.
INCLUDE TYPE
zcl_ztest_fw_expand_mpc_ext=>ts_salesorder.
DATA: ordertoitems LIKE TABLE OF t_orderitems,
END OF t_expand.
DATA: lt_so LIKE TABLE OF t_expand,
ls_so LIKE t_expand,
ls_item LIKE t_orderitems.
Parent and its immediate child will be separated using "/" for e.g
$expand=OrderToItems/ItemToProduct if we would have 2nd sibling at order level for
e.g Partners of Sales order then we could have navigation property as OrderToPartners
with say its child as PartnerToAddress then we need to access it as
OrderToPartners/PartnerToAddress. To get the expanded result for both hierarchies, the
expand clause will look as
$expand=OrderToItems/ItemToProduct,OrderToPartners/PartnerToAddress (separated by
",")
Navigation property separated with "/" will be inserted into expanded technical clause.
2nd hierarchy will be appended in expanded technical clause
ls_expanded_clause_items = 'ORDERTOITEMS/ITEMTOPRODUCT'.
ls_expanded_clause_partners = 'ORDERTOPARTNERS/PARTNERTOADDRESS'.
APPEND ls_expanded_clause_items TO et_expanded_tech_clauses.
APPEND ls_expanded_clause_partners TO et_expanded_tech_clauses.
also refer this thread Error with $expand and navigation: Resource not found for the
segment 'NavAtp'
whether framework expand or data provider expand provides better result will depends
on your scenario. Please notice that with $expand, we are making single call to read the
parent and child data but at the same time ensure that the data to be retrieved is not too
large.
I hope you enjoyed reading this blog and now will be able to play with association/navigation
and data provider $expand!
Please feel free if you have any different thoughts to improve any section of this blog.