Show TOC

3.2 Data Provider Class (DPC)Locate this document in the navigation structure

Use

Within the DEFINE method two entities, two collections, two function imports, one complex type, two associations and two navigation properties are defined. The data access to the defined model is described with the following lines.

Method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_ENTITY

Two entities are defined, and when they are requested the method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_ENTITY is called. With the import parameter IV_ENTITY_NAME the entities are differentiated as shown in the example below.

DATA  ls_flight             TYPE ty_s_flight.
  DATA  ls_carrier            TYPE scarr.
  DATA  lv_carrid             TYPE s_carr_id.
  DATA  lv_connid             TYPE s_conn_id.
  DATA  lv_fldate             TYPE s_date.

  FIELD-SYMBOLS  <ls_key>     TYPE /iwbep/s_mgw_name_value_pair.

  CASE iv_entity_name.
*-- get carrier entity
    WHEN gc_carrier_entity.
*---- transfer key value
      READ TABLE it_key_tab WITH KEY name = 'carrid'
        ASSIGNING <ls_key>.

      IF sy-subrc <> 0.
*------ raise business exception if key is not transferred
        RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
          EXPORTING
            textid  = /iwbep/cx_mgw_busi_exception=>business_error
            message = 'Key field ''carrid'' missing'.
      ENDIF.

      lv_carrid = <ls_key>-value.
*---- read carrier by ID
      SELECT SINGLE * FROM scarr INTO CORRESPONDING FIELDS OF ls_carrier
        WHERE carrid = lv_carrid.

      IF sy-subrc = 0.
        copy_data_to_ref(
          EXPORTING
            is_data = ls_carrier
          CHANGING
            cr_data = er_entity
        ).
      ENDIF.

*-- get flight entity and flight detail (complex type)
    WHEN gc_flight_entity.

*---- transfer all key values based on entity type
      LOOP AT it_key_tab ASSIGNING <ls_key>.
        CASE <ls_key>-name.
          WHEN 'carrid'.
            lv_carrid = <ls_key>-value.
          WHEN 'connid'.
            lv_connid = <ls_key>-value.
          WHEN 'fldate'.
            lv_fldate = <ls_key>-value.
        ENDCASE.
      ENDLOOP.

*---- raise business exception if not all key fields are entered
      IF lv_carrid IS INITIAL AND
         lv_connid IS INITIAL AND
         lv_fldate IS INITIAL.
        RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
          EXPORTING
            textid  = /iwbep/cx_mgw_busi_exception=>business_error
            message = 'Key fields missing'.
      ENDIF.

*---- read flight
      SELECT SINGLE * FROM sflight INTO CORRESPONDING FIELDS OF ls_flight
        WHERE carrid = lv_carrid
        AND   connid = lv_connid
        AND   fldate = lv_fldate.

      IF sy-subrc = 0.
*------ read flight details (complex type)
        SELECT SINGLE * FROM spfli INTO CORRESPONDING FIELDS OF ls_flight-flightdetails
          WHERE carrid = ls_flight-carrid
          AND   connid = ls_flight-connid.

        copy_data_to_ref(
          EXPORTING
            is_data = ls_flight
          CHANGING
            cr_data = er_entity
        ).
      ENDIF.

  ENDCASE.
 
Method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_ENTITYSET
  DATA  lt_flights              TYPE ty_t_flights.
  DATA  lt_carriers             TYPE STANDARD TABLE OF scarr.
  DATA  lv_carrid               TYPE s_carr_id.

  FIELD-SYMBOLS  <ls_flight>    TYPE ty_s_flight.
  FIELD-SYMBOLS  <ls_key>       TYPE /iwbep/s_mgw_name_value_pair.


  CASE iv_entity_name.
*-- Carrier Collection
    WHEN gc_carrier_entity.
      SELECT * FROM scarr INTO TABLE lt_carriers.

      copy_data_to_ref(
        EXPORTING
          is_data = lt_carriers
        CHANGING
          cr_data = er_entityset
      ).

*-- Flight Collection
    WHEN gc_flight_entity.

      IF iv_source_name = gc_carrier_entity.

        READ TABLE it_key_tab INDEX 1 ASSIGNING <ls_key>.

        lv_carrid = <ls_key>-value.

*------ Get all flights of a specific carrier
        SELECT * FROM sflight INTO TABLE lt_flights
          WHERE carrid = lv_carrid.

      ELSE.
*------ Get all flights for all carriers
        SELECT * FROM sflight INTO TABLE lt_flights.
      ENDIF.

*---- Determine the flight details (complex type)
      LOOP AT lt_flights ASSIGNING <ls_flight>.
        SELECT SINGLE * FROM spfli INTO CORRESPONDING FIELDS OF <ls_flight>-flightdetails
          WHERE carrid = <ls_flight>-carrid
          AND   connid = <ls_flight>-connid.
      ENDLOOP.

      copy_data_to_ref(
        EXPORTING
          is_data = lt_flights
        CHANGING
          cr_data = er_entityset
      ).

  ENDCASE.
 
Method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~EXECUTE_ACTION

The two defined function imports find their counterpart in the DPC in method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~EXECUTE_ACTION. For each function import this method is called and the import parameter IV_ACTION_NAME holds the name of the action to be processed.

  DATA  lt_flights                      TYPE ty_t_flights.
  DATA  lt_flight_schedules             TYPE STANDARD TABLE OF spfli.
  DATA  ls_flight_schedule              TYPE spfli.

  DATA  lv_from_date                    TYPE dats.
  DATA  lv_to_date                      TYPE dats.
  DATA  lv_city_from                    TYPE s_from_cit.
  DATA  lv_city_to                      TYPE s_to_city.
  DATA  lv_carrid                       TYPE s_carr_id.
  DATA  lv_connid                       TYPE s_conn_id.

  DATA  ls_parameter                    TYPE /iwbep/s_mgw_name_value_pair.
  DATA  lv_parameter_error              TYPE abap_bool.

  FIELD-SYMBOLS <ls_flight>             TYPE ty_s_flight.


  CASE iv_action_name.
*-- Function Import 'GetFlightDetails'
    WHEN 'GetFlightDetails'.
*---- Get parameter to read from data base
      READ TABLE it_parameter WITH KEY name = 'airlineid' INTO ls_parameter.
      IF sy-subrc = 0.
        lv_carrid = ls_parameter-value.
      ELSE.
        lv_parameter_error = abap_true.
      ENDIF.

     READ TABLE it_parameter WITH KEY name = 'connectionid' INTO ls_parameter.
      IF sy-subrc = 0.
        lv_connid = ls_parameter-value.
      ELSE.
        lv_parameter_error = abap_true.
      ENDIF.

*---- If all parameters are transfered read from data base
      IF lv_parameter_error = abap_false.
        SELECT SINGLE * FROM spfli INTO ls_flight_schedule
          WHERE carrid = lv_carrid
            AND connid = lv_connid.


        copy_data_to_ref(
          EXPORTING
            is_data = ls_flight_schedule
          CHANGING
            cr_data = er_data
        ).

      ELSE.
        RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
          EXPORTING
            textid  = /iwbep/cx_mgw_busi_exception=>business_error
            message = 'Parameter(s) not found'.
      ENDIF.


*-- Function Import 'GetAvailableFlights'
    WHEN 'GetAvailableFlights'.
*---- Get parameter to read from data base
      READ TABLE it_parameter WITH KEY name = 'cityfrom' INTO ls_parameter.
      IF sy-subrc = 0.
        lv_city_from = ls_parameter-value.
      ELSE.
        lv_parameter_error = abap_true.
      ENDIF.

      READ TABLE it_parameter WITH KEY name = 'cityto' INTO ls_parameter.
      IF sy-subrc = 0.
        lv_city_to = ls_parameter-value.
      ELSE.
        lv_parameter_error = abap_true.
      ENDIF.

      READ TABLE it_parameter WITH KEY name = 'fromdate' INTO ls_parameter.
      IF sy-subrc = 0.
        lv_from_date = ls_parameter-value.
      ELSE.
        lv_parameter_error = abap_true.
      ENDIF.

      READ TABLE it_parameter WITH KEY name = 'todate' INTO ls_parameter.
      IF sy-subrc = 0.
        lv_to_date = ls_parameter-value.
      ELSE.
        lv_parameter_error = abap_true.
      ENDIF.

*---- If all parameters are transfered read from data base
      IF lv_parameter_error = abap_false.

        SELECT * FROM spfli AS a JOIN sflight AS b
          ON a~carrid = b~carrid AND
             a~connid = b~connid
          INTO CORRESPONDING FIELDS OF TABLE lt_flights
          WHERE a~cityfrom = lv_city_from
            AND a~cityto   = lv_city_to
            AND b~fldate BETWEEN lv_from_date AND lv_to_date.


*------ read details and add to flight details (complex type)
        SELECT * FROM spfli INTO TABLE lt_flight_schedules
          WHERE cityfrom = lv_city_from
            AND cityto   = lv_city_to.

        LOOP AT lt_flights ASSIGNING <ls_flight>.
          READ TABLE lt_flight_schedules INTO <_flight>-flightdetails
            WITH KEY  carrid = <ls_flight>-carrid
                      connid = <ls_flight>-connid.
        ENDLOOP.


        copy_data_to_ref(
          EXPORTING
            is_data = lt_flights
          CHANGING
            cr_data = er_data
        ).

      ELSE.
        RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
          EXPORTING
            textid  = /iwbep/cx_mgw_busi_exception=>business_error
            message = 'Parameter(s) not found'.
      ENDIF.
  ENDCASE.
 
Query Options
  • $skip

    The maximum number of items returned in the result set for each page.

  • $top

    The number of rows to skip in the result set before beginning to return results.

  • $filter

  • $orderby

    Specifies the sort order of the result set.

  • $select

    Specifies the fields returned in the result set.

  • $skiptoken

    An opaque value that must be passed back to the server in order to continue getting results for the query.

    Use of the $skiptoken query parameter can be implemented, for example within /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_ENTITYSET. The example uses the $skiptoken to return ten carriers per round trip.

    *---------------------------------------------------------------------
    * skiptoken
    *---------------------------------------------------------------------
          DESCRIBE TABLE lt_carriers LINES lv_entity_count.
    *---- check technical request context for skiptoken to get the current position
          lv_skiptoken = io_tech_request_context->get_skiptoken( ).
    
          IF lv_skiptoken IS NOT INITIAL.
            lv_start_index = lv_skiptoken.
          ELSE.
            lv_start_index = 1.
          ENDIF.
    
    *---- number of entities to be displayed (10)
          lv_end_index = lv_start_index + 10.
    *---- set skiptoken for next round trip
          IF lv_end_index <= lv_entity_count.
            es_response_context-skiptoken = lv_end_index.
          ENDIF.
          CONDENSE es_response_context-skiptoken.
    
    *---- remove at the end
          IF lv_end_index <= lv_entity_count.
            DELETE lt_carriers
              FROM lv_end_index
              TO   lv_entity_count.
          ENDIF.
    
    *---- remove at the beginning
          IF lv_start_index > 1.
            lv_start_index_d = lv_start_index - 1.
            DELETE lt_carriers
              FROM 1
              TO   lv_start_index_d.
          ENDIF.
     
  • $count

    Returns the count of a collection of entities

  • $inlinecount

    $inlinecount is only supported for flexible query services. It is not supported for fixed query services.

Request Object

The request object is introduced to rename properties, entities, and entity sets. To use this functionality the coding in the DEFINE method of the MPC has to be adjusted as well as the corresponding method of the DPC.

Example: Entity

MPC

  lo_entity_type = model->create_entity_type(
    iv_entity_type_name = 'Flight'
    iv_def_entity_set   = abap_false
 ).
  lo_entity_type->set_name( 'FlightEntity' ).
 

Example: Entity

DPC

To handle the renaming on the DPC the following lines of code have to be entered at the beginning of method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_ENTITY.

  DATA: lv_entity_name TYPE /iwbep/mgw_tech_name.

  lv_entity_name = io_tech_request_context->get_entity_type_name( ).
 

For further processing use LV_ENTITY_NAME instead of the importing parameter IV_ENTITY_NAME.

Example: Entity Set

MPC

  lo_flight_entity_set = lo_flight_entity_type->create_entity_set( 'FlightCollection' ).
  lo_flight_entity_set->/iwbep/if_mgw_odata_item~set_name( 'FlightHugoCollection' ).
 

Example: Entity Set

DPC

To handle the renaming on the DPC the following lines of code have to be entered at the beginning of method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_ENTITYSET.

  DATA: lv_entity_set_name TYPE /iwbep/mgw_tech_name.

  lv_entity_set_name = io_tech_request_context->get_entity_type_name( ).
 

For further processing use LV_ENTITY_SET_NAME instead of the importing parameter IV_ENTITY_NAME.

Example: Property

MPC

The standard coding to create a property looks like this.

  lo_property = lo_flight_object->create_property( 'CURRENCY' ).
 

Example: Property

MPC

With the request object functionality it could look like this.

  lo_property = lo_flight_object->create_property(
    iv_property_name = 'conn_identifier'
    iv_abap_fieldname = 'CONNID'
  ).

or

  lo_property = lo_flight_object->create_property( 'connid' ). " corresponding ABAP field name is 'CONNID'

or

  lo_property = lo_flight_object->create_property( 'CONNID' ).
  lo_property->set_name( 'conn_identifier' ).
 

The first example shows that the property name can be changed on create and does not have to be identically to the ABAP field name. The second example shows that the property name can be set on create. This means that it must be possible to determine the corresponding ABAP field name. In this case the ABAP field name is CONNID. The third example shows that the property name can be set separately after the create.