本文介绍SAP Gateway ODATA创建和测试DeepEntity的步骤和过程。
应用场景:需要ODATA更新抬头和多个行项目的应用场景
第一步:创建ZTHEAD表和ZTITEM表
第二步:参考数据表创建SEGW对象
第三步:创建Header和Items的Association(关联),名称为HeaderToItems
第四步:激活并生成ODATA RunTime对象。
第五步:在ZCL_ZDEEP_ENTITY_MPC_EXT 中创建DeepEntity的结构类型
代码如下:
class ZCL_ZDEEP_ENTITY_MPC_EXT definition
public
inheriting from ZCL_ZDEEP_ENTITY_MPC
create public .
public section.
types: BEGIN OF TS_DEEP_ENTITY,
id TYPE ZTHEAD-ID,
field1 TYPE ZTHEAD-FIELD1,
field2 TYPE ZTHEAD-FIELD2,
itemset TYPE STANDARD TABLE OF ts_items WITH DEFAULT KEY,
END OF TS_DEEP_ENTITY.
METHODS define
REDEFINITION .
第六步: 重定义ZCL_ZDEEP_ENTITY_MPC_EXT中的DEFINE 方法,参考DeepEntity类型重构Header EntityType
代码:
method DEFINE.
super->define( ).
DATA:
lo_annotation TYPE REF TO /iwbep/if_mgw_odata_annotation,
lo_entity_type TYPE REF TO /iwbep/if_mgw_odata_entity_typ,
lo_complex_type TYPE REF TO /iwbep/if_mgw_odata_cmplx_type,
lo_property TYPE REF TO /iwbep/if_mgw_odata_property,
lo_entity_set TYPE REF TO /iwbep/if_mgw_odata_entity_set.
lo_entity_type = model->get_entity_type( iv_entity_name = 'Header' ).
* DeepEntity类型绑定到Header EntityType
lo_entity_type->bind_structure( iv_structure_name = 'ZCL_ZDEEP_ENTITY_MPC_EXT=>TS_DEEP_ENTITY' ).
endmethod.
第七步:在ZCL_ZDEEP_ENTITY_DPC_EXT 中重定义/IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_DEEP_ENTITY
代码:
METHOD /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_DEEP_ENTITY.
DATA CUSTOME_CREATE_DEEP_ENTITY TYPE ZCL_ZDEEP_ENTITY_MPC_EXT=>TS_DEEP_ENTITY.
DATA: LR_DEEP_ENTITY TYPE ZCL_ZDEEP_ENTITY_MPC_EXT=>TS_DEEP_ENTITY.
DATA : LS_HEADER_DATA TYPE ZTHEAD,
LT_ITEMS_DATA TYPE STANDARD TABLE OF ZTITEM,
LS_ITEMS_DATA TYPE ZTITEM.
FIELD-SYMBOLS:
CASE IV_ENTITY_SET_NAME.
WHEN 'HeaderSet'.
* Transform data into the internal structure
IO_DATA_PROVIDER->READ_ENTRY_DATA(
IMPORTING ES_DATA = LR_DEEP_ENTITY ).
* **********Collect the header fields here
LS_HEADER_DATA-ID = LR_DEEP_ENTITY-ID.
LS_HEADER_DATA-FIELD1 = LR_DEEP_ENTITY-FIELD1.
LS_HEADER_DATA-FIELD2 = LR_DEEP_ENTITY-FIELD2.
* *******Collect items fields
LOOP AT LR_DEEP_ENTITY-ITEMSSET ASSIGNING
CLEAR LS_ITEMS_DATA.
LS_ITEMS_DATA-ID =
LS_ITEMS_DATA-IFIELD1 =
LS_ITEMS_DATA-IFIELD2 =
APPEND LS_ITEMS_DATA TO LT_ITEMS_DATA.
ENDLOOP.
MODIFY ZTHEAD FROM LS_HEADER_DATA.
MODIFY ZTITEM FROM LS_ITEMS_DATA.
IF SY-SUBRC = 0.
CUSTOME_CREATE_DEEP_ENTITY-ID = LS_HEADER_DATA-ID.
CUSTOME_CREATE_DEEP_ENTITY-FIELD2 = 'DeepEntity处理成功'.
ELSE.
ENDIF.
COPY_DATA_TO_REF(
EXPORTING
IS_DATA = CUSTOME_CREATE_DEEP_ENTITY
CHANGING
CR_DATA = ER_DEEP_ENTITY
).
ENDCASE.
ENDMETHOD.
第八步:测试DEEP ENTITY
测试数据:
{
"Id" : "123",
"Field1" : "2018001",
"ItemsSet" : [
{
"Id" : "123",
"Ifield1" : "2018001"
},
{
"Id" : "100",
"Ifield1" : "2018002"
}
]
}