Writing this blog for purpose of beginners in order to explain Deep structures handling in SAP OData.
Usually we encounter a business case where in we need to Fetch Parent child relationship data in single call or we may need to save the Header Item details to database.
OData framework provides an option to perform this operations using Deep Structures.
Agenda : Simple example to depict Fetch/Get scenario.
Procedure :
Let us consider case of Header to Item (1 to 1)
Header Table:
Usually we encounter a business case where in we need to Fetch Parent child relationship data in single call or we may need to save the Header Item details to database.
OData framework provides an option to perform this operations using Deep Structures.
Agenda : Simple example to depict Fetch/Get scenario.
Procedure :
Let us consider case of Header to Item (1 to 1)
Header Table:
Item Table :
Relation between header and item -> ID
Create a project in SEGW and import these 2 structures into the project
Create an association and navigation for Header and Item
Now for the purpose of Deep structures, we will create a SE11 structure similar to this navigation property.
Note: The Item structure name should be similar to Navigation property.
Note : The structure for this purpose can also be created in MPC_EXT public section, I have created in SE11 for demonstrating the other possibilities.
Now, we are set with declarations and we can start the coding part.
Go to DPC_EXT class after generating the services and redefine the /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_EXPANDED_ENTITYSET.
Expanded entityset will serve to expand entire header set to item, in case of single header and related item expansion we will have to redefine Expanded entity. we will discuss it in later part.
METHOD /iwbep/if_mgw_appl_srv_runtime~get_expanded_entityset.
DATA : it_out_tab TYPE STANDARD TABLE OF zdeep_s,
wa_out_tab LIKE LINE OF it_out_tab,
it_header TYPE STANDARD TABLE OF zheader,
wa_header TYPE zheader,
it_item TYPE STANDARD TABLE OF zitem,
wa_item TYPE zitem.
CASE iv_entity_set_name.
WHEN 'HeaderSet'.
SELECT * FROM zheader INTO TABLE it_header.
IF sy-subrc = 0.
SELECT * FROM zitem INTO TABLE it_item FOR ALL ENTRIES IN it_header WHERE id = it_header-id.
ENDIF.
LOOP AT it_header INTO wa_header.
MOVE-CORRESPONDING wa_header TO wa_out_tab.
LOOP AT it_item INTO wa_item WHERE id = wa_header-id.
APPEND wa_item TO wa_out_tab-headertoitemnav.
CLEAR : wa_item.
ENDLOOP.
APPEND wa_out_tab TO it_out_tab.
CLEAR : wa_header,wa_out_tab.
ENDLOOP.
copy_data_to_ref(
EXPORTING
is_data = it_out_tab
CHANGING
cr_data = er_entityset ).
ENDCASE.
ENDMETHOD.
I have written sample code to retrieve the data, this code is not optimized and for demo purpose.Change the code as per the standards if necessary.
We are now done with coding part, we can test the service by using the following URL.
URI : /sap/opu/odata/sap/ZDEEPSTRUCTURES_SRV/HeaderSet?$expand=HeaderToItemNav
Entries in Header Table
Entries in Item Table
Below is the output after executing the URI
{
"d" : {
"results" : [
{
"__metadata" : {
},
"Id" : "100",
"Name" : "MOUSE",
"Dept" : "DEPT1",
"HeaderToItemNav" : {
"results" : [
{
"__metadata" : {
},
"Id" : "100",
"Sdept" : "DEPT-1A",
"Stock" : "22"
},
{
"__metadata" : {
},
"Id" : "100",
"Sdept" : "DEPT-1B",
"Stock" : "44"
},
{
"__metadata" : {
},
"Id" : "100",
"Sdept" : "DEPT-1C",
"Stock" : "76"
}
]
}
},
{
"__metadata" : {
},
"Id" : "101",
"Name" : "KEYBOARD",
"Dept" : "DEPT2",
"HeaderToItemNav" : {
"results" : [
{
"__metadata" : {
},
"Id" : "101",
"Sdept" : "DEPT2-SM",
"Stock" : "115"
},
{
"__metadata" : {
},
"Id" : "101",
"Sdept" : "DEPT-2C",
"Stock" : "129"
},
{
"__metadata" : {
},
"Id" : "101",
"Sdept" : "DEPT-2D",
"Stock" : "251"
},
{
"__metadata" : {
},
"Id" : "101",
"Sdept" : "DEPT-2M",
"Stock" : "009"
}
]
}
}
]
}
}
We can do the similar kind of code for Expand entity for expanding single header and relative Item set. Difference would be to add the key in header set.
URI : /sap/opu/odata/sap/ZDEEPSTRUCTURES_SRV/HeaderSet(‘100’)?$expand=HeaderToItemNav
No comments:
Post a Comment