SAP Odata実行命令(2)

前言

$ skiptokenは、アプリケーションに送信されるエントリ数を制限するために使用されます。 膨大な数のエントリが要求された場合、これはパフォーマンスの向上にも役立ちます。次のリンクがアプリケーションに戻って提供され、次の大量のデータを取得します。

構文

http://<server>:<port>/sap/opu/odata/sap/<service_name>/ProductsSet?$skiptoken=10

使い方法

  • サーバーでページサイズ、つまりDPC_EXTクラスのGET_ENTITYSETメソッドを定義する必要があります。
  • エンティティセットのエントリがページサイズより大きい場合は、エントリをページサイズで割ります。
  • skiptoken値と、次のエントリセットへの次のリンクに基づいて、要求されたエントリセットをアプリケーションに送信します。 詳細は下の画像をご覧ください。

skiptoken logic

Step.1


Go to ABAP Workbench

Step.2 

PRODUCTSSET_GET_ENTITYSET ソースコードは以下のようです。

DATA:      ls_order          TYPE /iwbep/s_mgw_sorting_order,
           lt_products       TYPE STANDARD TABLE OF bapi_epm_product_header,
           ls_products       TYPE bapi_epm_product_header,
           ls_entityset      TYPE zcl_zdemo_gw_srv_mpc=>ts_products,
           lt_entityset      TYPE zcl_zdemo_gw_srv_mpc=>tt_products,
           lv_max_rows       TYPE bapi_epm_max_rows,
           ls_filter         TYPE /iwbep/s_mgw_select_option,
           lr_product_id     TYPE TABLE OF  bapi_epm_product_id_range,
           ls_product_id     TYPE bapi_epm_product_id_range,
           ls_select_options TYPE /iwbep/s_cod_select_option.

* >> Check if $filter option is available
* Get the filter option for product ID
    READ TABLE it_filter_select_options
        INTO ls_filter
        WITH KEY property = 'ProductId'.
    IF sy-subrc = 0.
      LOOP AT ls_filter-select_options INTO ls_select_options.
        MOVE-CORRESPONDING ls_select_options TO ls_product_id.
        APPEND ls_product_id TO lr_product_id.
      ENDLOOP.
    ENDIF.

* Call the BAPI by providing the filter options
* if no filter, BAPI will get the entire list of products
    CALL FUNCTION 'BAPI_EPM_PRODUCT_GET_LIST'
      TABLES
        headerdata        = lt_products
        selparamproductid = lr_product_id.
    IF lt_products IS NOT INITIAL.
      LOOP AT lt_products INTO ls_products.
        MOVE-CORRESPONDING ls_products TO ls_entityset.
        APPEND ls_entityset TO et_entityset.
      ENDLOOP.
    ENDIF.

  上記のソースだとすべてデータを取得しますので、

$skiptoken オプションでページサイズを設定します。

Step.3

PRODUCTSSET_GET_ENTITYSETに以下のようにページサイズを設定します。

DATA:  ls_order          TYPE /iwbep/s_mgw_sorting_order,
           lt_products       TYPE STANDARD TABLE OF bapi_epm_product_header,
           ls_products       TYPE bapi_epm_product_header,
           ls_entityset      TYPE zcl_zdemo_gw_srv_mpc=>ts_products,
           lt_entityset      TYPE zcl_zdemo_gw_srv_mpc=>tt_products,
           lv_max_rows       TYPE bapi_epm_max_rows,
           ls_filter         TYPE /iwbep/s_mgw_select_option,
           lr_product_id     TYPE TABLE OF  bapi_epm_product_id_range,
           ls_product_id     TYPE bapi_epm_product_id_range,
           ls_select_options TYPE /iwbep/s_cod_select_option.

* >> Check if $filter option is available
* Get the filter option for roduct ID
    READ TABLE it_filter_select_options
        INTO ls_filter
        WITH KEY property = 'ProductId'.
    IF sy-subrc = 0.
      LOOP AT ls_filter-select_options INTO ls_select_options.
        MOVE-CORRESPONDING ls_select_options TO ls_product_id.
        APPEND ls_product_id TO lr_product_id.
      ENDLOOP.
    ENDIF.

* Call the BAPI by providing the filter options
* if no filter, BAPI will get the entire list of products
    CALL FUNCTION 'BAPI_EPM_PRODUCT_GET_LIST'
      TABLES
        headerdata        = lt_products
        selparamproductid = lr_product_id.
    IF lt_products IS NOT INITIAL.
      LOOP AT lt_products INTO ls_products.
        MOVE-CORRESPONDING ls_products TO ls_entityset.
        APPEND ls_entityset TO lt_entityset.
      ENDLOOP.
    ENDIF.

    DATA: lv_page_size   TYPE i VALUE 50,  " Define the Page Size/
          lv_index_start TYPE i,
          lv_skiptoken   TYPE string,
          lv_index_end   TYPE i,
          lv_table_size  TYPE i.


* Obtain the $skiptoken value if it exists in the URL
    lv_skiptoken = io_tech_request_context->get_skiptoken( ).

* Clear the skiptoken, if the result is empty
    DESCRIBE TABLE lt_entityset LINES lv_table_size.
    IF lv_table_size IS INITIAL.
      CLEAR es_response_context-skiptoken.
      EXIT.
    ENDIF.

* If the table size is less than the predefined page size,
* send all the data
    IF lv_table_size < lv_page_size.
      et_entityset = lt_entityset.

* If the table size is beyond the predefined page size,
*    cut the whole table into pieces with the page size
    ELSE.
      lv_index_start = lv_skiptoken.
      lv_index_end   = lv_skiptoken + lv_page_size.
    ENDIF.

    LOOP AT lt_entityset INTO ls_entityset.
      IF sy-tabix > lv_index_start AND
         sy-tabix <= lv_index_end.
        APPEND ls_entityset TO et_entityset.
      ENDIF.
    ENDLOOP.

*  Next Link
    es_response_context-skiptoken = lv_index_end + 1.
    CONDENSE es_response_context-skiptoken.

Step.4

※画像では$skiptoken=20書いてますが、正しくは$skiptoken=10です。  

原文地址:https://www.cnblogs.com/yjyongil/p/10614192.html