GetEntity和GetEntitys
- GetEntitys
创建Odata Project
- 导入结构
选择需要的字段
设定Key
勾选字段的creatable、updatable、sortable、nullable、filterable属性值。
再依上述步骤创建ZPOITEM结构和实体集
3. 创建ZPOHEADER和ZPOITEM的Association
两个实体集的关联字段,关联字段依情况填写,完了下一步点完成
-
生成服务对象
生成4个类,注册1个模型,1个服务
ZCL_ZTEST_ODATA1_MPC_EXT 数据处理增强类
接口逻辑主要处理类,在该类中重定义相应的方法来执行业务处理逻辑
ZCL_ZTEST_ODATA1_DPC_EXT 模型处理增强类
接口数据模型定义类,在该类中定义接口结构。 -
基于Odata对象生成相应服务
注册服务并指定系统别名
注意:别名不跨client,所以需要将别名放入定制请求中,通过scc1传输到开发系统的其他client。最后也需要传输到测试和生产系统。
注意:odata接口服务传输到测试或生产系统后需要手动激活服务。可以点击下图中的Internet通信框架节点按钮再点击激活按钮,或者在SCIF下激活服务。
点击定制进入到修改别名视图,点击修改按钮后,选中别名服务行,再点击标示图按钮,选则传输后会弹出选中请求输入框,指定需要放入的定制请求后,点击包含在请求中,最后点击保存按钮后则别名正式被包含在了请求中。
然后将它包含到请求中再点击保存
服务注册完成之后,点击SAP Gateway Client(T-Code:/IWFND/GW_CLIENT)按钮进入测试页面
-
实现GetEntitys方法(取多条数据)
重定义Get_EntitySet方法,鼠标右键点击方法
(注:GET_ENTITYSET后缀的表示取数方法取多条数据,GET_ENTITY后缀的表示取单条)
IV_ENTITY_NAME区分大小写,与下面的保持一致
都转成大写,保持一致
实施重定义的方法
DATA:lr_ebeln TYPE RANGE OF ekko-ebeln,
ls_ebeln LIKE LINE OF lr_ebeln,
lv_top TYPE i,
lv_skip TYPE i.
es_response_context-count = lines( et_entityset ).
lv_top = io_tech_request_context->get_top( ).
lv_skip = io_tech_request_context->get_skip( ).
CASE translate_entity_name( iv_entity_name ).
WHEN 'ZPOHEADER'.
"filter 操作符eq , ne, le, lt , ge, gt
IF it_filter_select_options[] IS NOT INITIAL.
READ TABLE it_filter_select_options INTO DATA(lw_filter) WITH KEY property = 'Ebeln'.
IF sy-subrc = 0.
LOOP AT lw_filter-select_options INTO DATA(lw_range).
MOVE-CORRESPONDING lw_range TO ls_ebeln.
APPEND ls_ebeln TO lr_ebeln.
ENDLOOP.
ENDIF.
ENDIF.
SELECT * FROM ekko
WHERE ebeln IN @lr_ebeln
ORDER BY ebeln
INTO CORRESPONDING FIELDS OF TABLE @et_entityset UP TO @lv_top ROWS OFFSET @lv_skip.
"sort
IF it_order[] IS NOT INITIAL.
READ TABLE it_order INTO DATA(lw_order) WITH KEY property = 'Ebeln'.
IF sy-subrc = 0.
CASE me->translate_entity_name( lw_order-order ).
WHEN 'DESC'.
SORT et_entityset BY ebeln DESCENDING.
WHEN 'ASC'.
SORT et_entityset BY ebeln ASCENDING.
WHEN OTHERS.
ENDCASE.
ENDIF.
ENDIF.
WHEN OTHERS.
ENDCASE.
Top,skip
Filter
Orderby
7. Get_entity
METHOD zpoheaderset_get_entity.
CASE me->translate_entity_name( iv_entity_name ).
WHEN 'ZPOHEADER'.
READ TABLE it_key_tab INTO DATA(lw_key_tab) WITH KEY name = 'Ebeln'.
IF sy-subrc = 0.
DATA(lv_ebeln) = |{ lw_key_tab-value ALPHA = IN WIDTH = 10 }|.
SELECT SINGLE * FROM ekko INTO CORRESPONDING FIELDS OF @er_entity WHERE ebeln = @lv_ebeln.
ENDIF.
ENDCASE.
ENDMETHOD.
Association和Navigation
Association 和 Navigation主要是用来关联两个entity,比如先前例子中的header和item,我们想在获取header数据的同时,还获取其item的数据。
只需要在ITEM的GET_ENTITYSET方法实现即可。
- 重定义ITEM的方法
DATA:lr_ebeln TYPE RANGE OF ekko-ebeln,
ls_ebeln LIKE LINE OF lr_ebeln,
lr_ebelp TYPE RANGE OF ekpo-ebelp,
ls_ebelp LIKE LINE OF lr_ebelp.
CASE translate_entity_name( iv_entity_name ).
WHEN 'ZPOITEM'.
IF it_filter_select_options[] IS NOT INITIAL.
READ TABLE it_filter_select_options INTO DATA(lw_filter) WITH KEY property = 'Ebeln'.
IF sy-subrc = 0.
LOOP AT lw_filter-select_options INTO DATA(lw_range).
MOVE-CORRESPONDING lw_range TO ls_ebeln.
APPEND ls_ebeln TO lr_ebeln.
ENDLOOP.
ENDIF.
READ TABLE it_filter_select_options INTO lw_filter WITH KEY property = 'Ebelp'.
IF sy-subrc = 0.
LOOP AT lw_filter-select_options INTO lw_range.
MOVE-CORRESPONDING lw_range TO ls_ebelp.
APPEND ls_ebeln TO lr_ebelp[].
ENDLOOP.
ENDIF.
ENDIF.
"Association和Navigation
READ TABLE it_key_tab INTO DATA(lw_key_tab) WITH KEY name = 'Ebeln'.
IF sy-subrc = 0.
DATA(lv_ebeln) = |{ lw_key_tab-value ALPHA = IN WIDTH = 10 }|.
ENDIF.
IF lv_ebeln IS NOT INITIAL.
SELECT * FROM ekpo INTO CORRESPONDING FIELDS OF TABLE @et_entityset UP TO 500 ROWS
WHERE ebeln = @lv_ebeln.
ELSE.
SELECT * FROM ekpo INTO CORRESPONDING FIELDS OF TABLE @et_entityset UP TO 500 ROWS
WHERE ebeln IN @lr_ebeln AND ebelp IN @lr_ebelp.
ENDIF.
IF it_order[] IS NOT INITIAL.
READ TABLE it_order INTO DATA(lw_order) WITH KEY property = 'Ebeln'.
READ TABLE it_order INTO lw_order WITH KEY property = 'Ebelp'.
IF sy-subrc = 0.
CASE me->translate_entity_name( lw_order-order ).
WHEN 'DESC'.
SORT et_entityset BY ebeln DESCENDING.
WHEN 'ASC'.
SORT et_entityset BY ebeln ASCENDING.
WHEN OTHERS.
ENDCASE.
ENDIF.
ENDIF.
WHEN OTHERS.
ENDCASE.
- Expand实现
需重定义/IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_EXPANDED_ENTITY方法
"GET po item
TYPES: ty_item TYPE TABLE OF zcl_ztest_po_mpc_ext=>ts_zpoitem WITH DEFAULT KEY.
TYPES: BEGIN OF ty_po_data.
INCLUDE TYPE zcl_ztest_po_mpc_ext=>ts_zpoheader.
TYPES: zpoitemset TYPE ty_item,
END OF ty_po_data.
DATA:ls_po_data TYPE ty_po_data,
ls_header TYPE zcl_ztest_po_mpc_ext=>ts_zpoheader,
lt_item TYPE ty_item.
TYPES: ty_header TYPE TABLE OF zcl_ztest_po_mpc_ext=>ts_zpoheader WITH DEFAULT KEY.
CASE translate_entity_name( io_tech_request_context->get_entity_set_name( ) ).
WHEN 'ZPOHEADERSET'.
READ TABLE it_key_tab INTO DATA(ls_key) WITH KEY name = 'Ebeln'.
IF sy-subrc = 0.
DATA(lv_ebeln) = |{ ls_key-value ALPHA = IN WIDTH = 10 }|.
SELECT SINGLE * FROM ekko INTO CORRESPONDING FIELDS OF @ls_header
WHERE ebeln = @lv_ebeln.
SELECT * FROM ekpo INTO CORRESPONDING FIELDS OF TABLE @lt_item
WHERE ebeln = @lv_ebeln.
MOVE-CORRESPONDING ls_header TO ls_po_data.
MOVE-CORRESPONDING lt_item[] TO ls_po_data-zpoitemset[].
copy_data_to_ref( EXPORTING is_data = ls_po_data
CHANGING cr_data = er_entity ).
ENDIF.
WHEN OTHERS.
ENDCASE.
Create方法实现
Create方法同上面一样,直接重定义对应的Create_Entity方法即可。
-
先定义结构,获取传来的参数
-
检查主键
METHOD check_ebeln.
IF is_header-ebeln IS INITIAL.
is_message = VALUE #( msgid = 'SY' msgno = '002' attr1 = '采购单为空' ).
ENDIF.
ENDMETHOD.
- 方法实施
io_data_provider->read_entry_data( IMPORTING es_data = gs_header ).
gs_message = check_ebeln( gs_header ).
IF gs_message IS INITIAL.
MOVE-CORRESPONDING gs_header TO er_entity."返回数据
ELSE.
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
textid = gs_message.
ENDIF.
主键为空,需报错
Update和Delete方法实现与Create一样
Create_Deep_Entity
重定义实现CREATE_DEEP_ENTITY方法,我们可以实现一起更新Navigation属性。比如前台创建PO 抬头和相应的行项目,传到后台可以通过此方法将抬头和行项目一起创建。
-
重定义/IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_DEEP_ENTITY方法
-
定义模型增强类(zcl_ztest_odata1_mpc_ext)中的类型
CLASS zcl_ztest_odata1_mpc_ext DEFINITION
PUBLIC
INHERITING FROM zcl_ztest_odata1_mpc
CREATE PUBLIC .
PUBLIC SECTION.
TYPES:
ty_t_po_data TYPE STANDARD TABLE OF zcl_ztest_odata1_mpc_ext=>ts_zpoitem WITH DEFAULT KEY .
TYPES:
BEGIN OF ty_po_data.
INCLUDE TYPE zcl_ztest_odata1_mpc_ext=>ts_zpoheader.
TYPES: toitem TYPE ty_t_po_data,
END OF ty_po_data .
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
- 实现方法Create_deep_entity
DATA:lv_compare_result TYPE /iwbep/if_mgw_odata_expand=>ty_e_compare_result.
DATA:ls_po TYPE zcl_ztest_po_mpc_ext=>ty_po_data,
ls_header TYPE ekko,
lt_item TYPE TABLE OF ekpo,
ls_item TYPE ekpo.
CONSTANTS:lc_toitem TYPE string VALUE 'ToItem'.
"判断传入数据是否包含ITEM数据
lv_compare_result = io_expand->compare_to_tech_names( lc_toitem ).
IF lv_compare_result EQ /iwbep/if_mgw_odata_expand=>gcs_compare_result-match_no.
io_data_provider->read_entry_data( IMPORTING es_data = ls_po ).
MOVE-CORRESPONDING ls_po TO ls_header.
MOVE-CORRESPONDING ls_po-toitem[] TO lt_item.
copy_data_to_ref( EXPORTING is_data = ls_po
CHANGING cr_data = er_deep_entity ).
ENDIF.