Generic GET_ENTITY

The generic GET_ENTITYSET method is a method that can be used in a gateway service to retrieve a list of entitys from the database. It is a generic method, so it can be used for any entity. The method is generated by the gateway service builder and can be found in the DPC_EXT class.

METHODS read_table_generic
    IMPORTING
        iv_table                TYPE string
        is_paging               TYPE /iwbep/s_mgw_paging
        it_order                TYPE /iwbep/t_mgw_sorting_order
        io_tech_request_context TYPE REF TO /iwbep/if_mgw_req_entityset
    EXPORTING
        es_response_context     TYPE /iwbep/if_mgw_appl_srv_runtime=>ty_s_mgw_response_context
        et_entityset            TYPE STANDARD TABLE
    RAISING
        /iwbep/cx_mgw_busi_exception
        /iwbep/cx_mgw_tech_exception.

METHOD read_table_generic.

    DATA: lv_property_selected TYPE string,
          lref_property        TYPE REF TO /iwbep/cl_mgw_odata_property,
          lt_order             TYPE /iwbep/t_mgw_sorting_order,
          lref_facade          TYPE REF TO /iwbep/if_mgw_dp_int_facade.

    lt_order = it_order.

    DATA(lv_where_clause) = io_tech_request_context->get_osql_where_clause( ).
    DATA(lt_select_table) = io_tech_request_context->get_select( ).

    IF lt_order IS INITIAL.
      lref_facade ?= /iwbep/if_mgw_conv_srv_runtime~get_dp_facade( ).
      DATA(lref_model) = lref_facade->get_model( ).
      DATA(lt_properties) = lref_model->get_entity_type( iv_entity_name = io_tech_request_context->get_entity_type_name( ) )->get_properties( ).
      LOOP AT lt_properties ASSIGNING FIELD-SYMBOL(<fs_property>).
        lref_property ?= <fs_property>-property.
        IF lref_property->/iwbep/if_mgw_odata_fw_prop~is_key( ) = abap_true.
          APPEND VALUE #( property = <fs_property>-technical_name order = /iwbep/cl_mgw_data_util=>gcs_sorting_order-ascending ) TO lt_order.
        ENDIF.
      ENDLOOP.
    ENDIF.

    LOOP AT lt_select_table INTO DATA(lv_select).
      IF lv_property_selected IS INITIAL.
        lv_property_selected = lv_select.
      ELSE.
        lv_property_selected = |{ lv_property_selected }, { lv_select }|.
      ENDIF.
    ENDLOOP.

    IF lv_property_selected IS INITIAL.
      lv_property_selected = '*'.
    ENDIF.

    DATA(lv_top) = io_tech_request_context->get_top( ).
    DATA(lv_skip) = io_tech_request_context->get_skip( ).
    DATA(lv_maxrows) = 0.

    IF lv_top > 0.
      lv_maxrows = lv_top + lv_skip.
    ENDIF.

    IF io_tech_request_context->has_inlinecount( ) = abap_true.
      SELECT COUNT(*) FROM (iv_table) WHERE (lv_where_clause).
      es_response_context-inlinecount = sy-dbcnt.
    ENDIF.

    IF is_paging IS NOT INITIAL.
      SELECT (lv_property_selected)
        FROM (iv_table)
        WHERE (lv_where_clause)
        INTO CORRESPONDING FIELDS OF TABLE @et_entityset
        UP TO @lv_maxrows ROWS.
    ELSE.
      SELECT (lv_property_selected)
       FROM (iv_table)
       WHERE (lv_where_clause)
       INTO CORRESPONDING FIELDS OF TABLE @et_entityset.
    ENDIF.

    /iwbep/cl_mgw_data_util=>orderby(
      EXPORTING
        it_order = lt_order
      CHANGING
        ct_data  = et_entityset
    ).

    IF NOT lv_skip IS INITIAL.
      DELETE et_entityset TO lv_skip.
    ENDIF.

    es_response_context-count = lines( et_entityset ).

ENDMETHOD.

Example of usage:

    read_table_generic(
       EXPORTING
         iv_table                = 'ZFLIGHT'
         is_paging               = is_paging
         it_order                = it_order
         io_tech_request_context = io_tech_request_context
       IMPORTING
         et_entityset        = et_entityset
         es_response_context = es_response_context ).