[ABAP] OData: Navigation/Assoziation zwischen Entitätsmengen

Transaktion SEGW

  • Neues Projekt: ZFLIGHT_ASSOC
  • Entitätstyp:
    • Carrier aus DDIC Struktur „SCARR“
      • Carrid (Key)
      • Carrname
      • Url
  • Flights aus DDIC Struktur „SPFLI“
    • Carrid (Key)
    • Connid (Key)
    • Countryfr
    • Cityfrom
    • Airpfrom
  • Entitätsmenge:
    • CarrierSet
    • FlightsSet
  • Serviceklassen generieren
  • Service registrieren

Serviceimplementierung: GetEntity(Read)

* URL/SERVICENAME/CarrierSet('AA')
METHOD carrierset_get_entity.

  io_tech_request_context->get_converted_keys( IMPORTING es_key_values = er_entity ).
  
  IF NOT er_entity-carrid IS INITIAL.
    SELECT SINGLE * FROM scarr INTO CORRESPONDING FIELDS OF @er_entity WHERE carrid = @er_entity-carrid.
  ENDIF.

ENDMETHOD.
    
* URL/SERVICENAME/FlightsSet(Carrid='AA',Connid='64')
METHOD flightsset_get_entity.

  io_tech_request_context->get_converted_keys( IMPORTING es_key_values = er_entity ).

  IF NOT er_entity-carrid IS INITIAL AND NOT er_entity-connid IS INITIAL..
    SELECT SINGLE * FROM spfli INTO CORRESPONDING FIELDS OF @er_entity
      WHERE carrid = @er_entity-carrid
        AND connid = @er_entity-connid.
  
    IF sy-subrc <> 0.
      CLEAR: er_entity.
    ENDIF.
  ENDIF.
  
ENDMETHOD.

Serviceimplementierung: GetEntitySet(Query)

* URL/SERVICENAME/CarrierSet
METHOD carrierset_get_entityset.

  SELECT * FROM scarr INTO CORRESPONDING FIELDS OF TABLE @et_entityset.

ENDMETHOD.
    
* URL/SERVICENAME/FlightsSet
METHOD flightsset_get_entityset.

  SELECT * FROM spfli INTO CORRESPONDING FIELDS OF TABLE @et_entityset.

ENDMETHOD.

Assoziation anlegen

  • Rechtsklick auf „Assoziationen“ -> Anlegen
  • Name: CarrierToFlights
  • Principal-Entität: Name „Carrier“
  • Untergeordnete Entität: Name „Flights“
  • Kardinalität: Carrier 1 : Flights n
  • Navigationsproperty anlegen: ToFlights
  • Bezug: Carrid – Carrid

Code anpassen

* URL/SERVICENAME/CarrierSet('AA')/ToFlights?$format=json
METHOD flightsset_get_entityset.

  * Typen und Konstanten sind in der MPC definiert
  DATA: lv_flights TYPE zcl_zflight_assoc2_mpc=>ts_flights.

  DATA(lv_source_entity_type_name) = io_tech_request_context->get_source_entity_type_name( ).

  CASE lv_source_entity_type_name.
  * Typen und Konstanten sind in der MPC definiert
    WHEN zcl_zflight_assoc2_mpc=>gc_carrier.
      io_tech_request_context->get_converted_source_keys( IMPORTING es_key_values = lv_flights ).
  ENDCASE.

  IF NOT lv_flights IS INITIAL.
    SELECT * FROM spfli INTO CORRESPONDING FIELDS OF TABLE @et_entityset WHERE carrid = @lv_flights-carrid.
  ELSE.
    SELECT * FROM spfli INTO CORRESPONDING FIELDS OF TABLE @et_entityset.
  ENDIF.

ENDMETHOD.

Weiterführende Infos: Link