I was trying to implement the $expand query option for an Odata Service in SEGW... I basically have UsersSet and ProductsSet... and the relation is m:n between users and products (Users can have multiple products, and products can belong to multiple users).
I want to create /UsersSet?$expand=ToProducts where all products are shown that belong to a user. For that, I have created an intermediary table "UsersProducts" with properties "userid" and "productid" to reflect the m:n association.
I basically created an "extended" deep entity ty_usersproducts in my MPC_EXT class, corresponding to user properties and an array of products (that belong to that user).
types: BEGIN OF TY_USERPRODUCTS ,
INCLUDE TYPE ZCL_ZALM_FS_APP_MPC_EXT=>ts_users,
toproducts TYPE STANDARD TABLE OF ZCL_ZALM_FS_APP_MPC_EXT=>ts_products WITH DEFAULT KEY,
END OF TY_USERPRODUCTS.
types: TT_USERPRODUCTS TYPE STANDARD TABLE OF TY_USERPRODUCTS.
I then extended the method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_EXPANDED_ENTITYSET.
in the DPC_EXT class:
method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_EXPANDED_ENTITYSET.
DATA : lt_deep_userProd TYPE TABLE OF zcl_zalm_fs_app_MPC_EXT=>ty_userproducts, "Deep Entity Type
ls_deep_userProd TYPE zcl_zalm_fs_app_MPC_EXT=>ty_userproducts, "Deep Entity Type
ls_product TYPE zcl_zalm_fs_app_MPC_EXT=>ts_products,
ls_reluprod TYPE zcl_zalm_fs_app_mpc_ext=>ts_reltableusersproducts,
lt_products TYPE TABLE OF zcl_zalm_fs_app_MPC_EXT=>ts_products.
DATA(lv_expand) = io_tech_request_context->get_expand( ).
DATA(lv_top) = io_tech_request_context->get_top( ).
DATA(lv_skip) = io_tech_request_context->get_skip( ).
DATA numTop TYPE int8.
numTop = lv_top.
DATA numSkip TYPE int8.
numSkip = lv_skip.
IF lv_top IS INITIAL.
numTop = 10.
ENDIF.
* Based on the entity set
IF iv_entity_set_name = 'UsersSet'.
SELECT * FROM zalm_fs_users
INTO TABLE @DATA(lt_users)
UP TO @numTop ROWS.
IF sy-subrc = 0.
SELECT * FROM zalm_fs_reluprod
INTO TABLE @DATA(lt_reluprod)
FOR ALL ENTRIES IN @lt_users
WHERE userid = @lt_users-id.
ENDIF.
IF lt_users IS NOT INITIAL AND lt_reluprod IS NOT INITIAL.
LOOP AT lt_users into DATA(ls_user).
ls_deep_userProd-include = CORRESPONDING #( ls_user ).
SELECT * FROM zalm_fs_reluprod
INTO TABLE @DATA(lt_reluprod2)
WHERE userid = @ls_user-id.
LOOP AT lt_reluprod2 into ls_reluprod.
SELECT SINGLE * FROM ZALM_FS_PRODUCTS
WHERE id = @ls_reluprod-productid
INTO @DATA(uprod).
APPEND uprod TO ls_deep_userProd-toproducts.
ENDLOOP.
APPEND ls_deep_userprod TO lt_deep_userprod.
CLEAR ls_deep_userprod-toproducts.
ENDIF.
* For converting the data in the deep structure to the output format
CALL METHOD me->/iwbep/if_mgw_conv_srv_runtime~copy_data_to_ref
EXPORTING
is_data = lt_deep_userprod
CHANGING
cr_data = er_entityset.
ENDIF.
endmethod.
The code seems to be correct in the debugger.. it returns the user properties and the products table in the deep entity... the only problem is.. in the SEGW Gateway service to test things out... the output does not appear there, so what is returned in the er_entityset
does not correspond to my deep structure that I put together above in the sql statement.. I wonder why?
Somehow in the debugger, not only get_expanded_entityset method is executed... after returning out of the method, some other methods like read_entityset are also executed.. maybe that is the problem?
Or second idea... I head that you had to name the properties in your deep entity model class in a specific way... eg the products collection would need to have the same name as the navigation (or was it association) name that you defined in SEGW? I'm not sure what exactly the problem is.. any ideas?