[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

[ABAP] OData: Keys einer Entität auswerten

* Groß-/Kleinschreibung beachten
* Variablennamen durch Komma getrennt, keine Leerzeichen
* Datumswerte mit VarDate=datetime'yyyy-mm-ddThh:mm:ss' konvertieren
* Sonderzeichen/Umlaute konvertieren

* Abfrage-URL
URL/SERVICENAME/ItemCollection(Matnr='000000000001',Date1=datetime'2013-09-04T00%3A00%3A00')?$format=xml

* Variante 1 (get_converted_keys)
METHOD xyz_get_entity.

  DATA: lv_carr TYPE /iwbep/s_mgw_name_value_pair-value.
  DATA: lv_conn TYPE /iwbep/s_mgw_name_value_pair-value.

  io_tech_request_context->get_converted_keys( IMPORTING es_key_values = er_entity ).

  TRY.
      lv_carr = er_entity-carrid.
      lv_conn = er_entity-connid.

      ...
    CATCH cx_root.
  ENDTRY.

  er_entity = ...
ENDMETHOD.

* Variante 2 (Navigation z.B.: Header -> Position, get_converted_source_keys)
METHOD xyz_get_entityset.

* Typen in MPC definert
  DATA: lv_pos TYPE zcl_abc_mpc=>ts_pos.
  DATA: lv_matnr TYPE /iwbep/s_mgw_name_value_pair-value.
  
  DATA(lv_source_entity_type_name) = io_tech_request_context->get_source_entity_type_name( ).
  
  CASE lv_source_entity_type_name.
* Konstanten in der MPC definiert
     WHEN zcl_abc_mpc=>gc_head.
* Key-Werte in Struktur des Typs zcl_abc_mpc=>ts_pos wandeln
       io_tech_request_context->get_converted_source_keys( IMPORTING es_key_values = lv_pos ).

       lv_matnr = |{ lv_pos-matnr WIDTH = 18 ALPHA = IN }|.
  ENDCASE.
  
  er_entity = ...
ENDMETHOD.

* Variante 3 (get_keys)
METHOD xyz_get_entity.

  DATA(it_keys) = io_tech_request_context->get_keys( ).

  DATA: lv_key_matnr TYPE /iwbep/s_mgw_tech_pair.
  DATA: lv_key_date1 TYPE /iwbep/s_mgw_tech_pair.

  TRY.
      lv_key_matnr = it_keys[ name = 'MATNR' ].
      lv_key_date1 = it_keys[ name = 'DATE1' ].

      DATA(lv_matnr) = lv_key_matnr-value.
      DATA(lv_date1) = lv_key_date1-value.

      ...
    CATCH cx_root.
  ENDTRY.

  er_entity = ...
ENDMETHOD.