Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  ABAP RESTful Programming Model →  Behavior Implementations →  Handler Class 

Example - Handling of Messages

Other versions: 7.31 | 7.40 | 7.54


Example

For the sake of readability, the methods for handling the messages are implemented in the separate class zcl_messages. These methods are used to handle the messages in the methods of the handler class.

Messages that come from the legacy business logic must be assigned to the messages of the business object Travel. This is implemented in the method map_travel_message.

CLASS zcl_messages DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC.
  PUBLIC SECTION.
    TYPES:
      ts_travel_control_map TYPE STRUCTURE FOR UPDATE /DMO/I_Travel,
      tt_travel_reported    TYPE TABLE FOR REPORTED   /DMO/I_Travel,
      tt_travel_failed      TYPE TABLE FOR FAILED     /DMO/I_Travel.

    CLASS-METHODS:
      handle_messages
        IMPORTING
          iv_cid       TYPE abp_behv_cid   OPTIONAL
          iv_travel_id TYPE /dmo/travel_id OPTIONAL
          it_messages  TYPE /dmo/if_flight_legacy=>tt_message
        CHANGING
          failed       TYPE tt_travel_failed
          reported     TYPE tt_travel_reported,

      map_travel_message
        IMPORTING
          iv_cid           TYPE string OPTIONAL
          iv_travel_id     TYPE /dmo/travel_id OPTIONAL
          is_message       TYPE LINE OF
            /dmo/if_flight_legacy=>tt_message
        RETURNING VALUE(rs_report) TYPE LINE OF tt_travel_reported,

      map_travel_control
        IMPORTING
          is_travel         TYPE ts_travel_control_map
        RETURNING VALUE(rs_travelx) TYPE
           /dmo/if_flight_legacy=>ts_travel_inx.

  PROTECTED SECTION.
  PRIVATE SECTION.
    CLASS-METHODS:
      new_message
        IMPORTING
          id         TYPE symsgid
          number     TYPE symsgno
          severity   TYPE if_abap_behv_message=>t_severity
          v1         TYPE simple OPTIONAL
          v2         TYPE simple OPTIONAL
          v3         TYPE simple OPTIONAL
          v4         TYPE simple OPTIONAL
        RETURNING VALUE(obj) TYPE REF TO if_abap_behv_message.
ENDCLASS.
CLASS zcl_messages IMPLEMENTATION.

  METHOD handle_messages.
    LOOP AT it_messages INTO DATA(ls_message)
         WHERE msgty = 'E' OR msgty = 'A'.
      INSERT VALUE #(
             %cid         = iv_cid
             travel_id    = iv_travel_id )
         INTO TABLE failed.
      INSERT map_travel_message(
             iv_travel_id = iv_travel_id
             is_message   = ls_message )
         INTO TABLE reported.
    ENDLOOP.
  ENDMETHOD.

  METHOD map_travel_control.
    rs_travelx-travel_id     = is_travel-travel_id.
    rs_travelx-agency_id     = xsdbool(
      is_travel-%control-agency_id     = cl_abap_behv=>flag_changed ).
    rs_travelx-customer_id   = xsdbool(
      is_travel-%control-customer_id   = cl_abap_behv=>flag_changed ).
    rs_travelx-begin_date    = xsdbool(
      is_travel-%control-begin_date    = cl_abap_behv=>flag_changed ).
    rs_travelx-end_date      = xsdbool(
      is_travel-%control-end_date      = cl_abap_behv=>flag_changed ).
    rs_travelx-booking_fee   = xsdbool(
      is_travel-%control-booking_fee   = cl_abap_behv=>flag_changed ).
    rs_travelx-total_price   = xsdbool(
      is_travel-%control-total_price   = cl_abap_behv=>flag_changed ).
    rs_travelx-currency_code = xsdbool(
      is_travel-%control-currency_code = cl_abap_behv=>flag_changed ).
    rs_travelx-description   = xsdbool(
      is_travel-%control-description   = cl_abap_behv=>flag_changed ).
    rs_travelx-status        = xsdbool(
      is_travel-%control-status        = cl_abap_behv=>flag_changed ).
ENDMETHOD.

  METHOD map_travel_message.
    DATA(lo) = new_message(
      id       = is_message-msgid
      number   = is_message-msgno
      severity = if_abap_behv_message=>severity-error
      v1       = is_message-msgv1
      v2       = is_message-msgv2
      v3       = is_message-msgv3
      v4       = is_message-msgv4 ).
    rs_report-%cid      = iv_cid.
    rs_report-travel_id = iv_travel_id.
    rs_report-%msg      = lo.
  ENDMETHOD.

  METHOD new_message.
    obj = NEW lcl_abap_behv_msg(
      textid = VALUE #(
        msgid = id
        msgno = number
        attr1 = COND #( WHEN v1 IS NOT INITIAL
                        THEN 'IF_T100_DYN_MSG~MSGV1' )
        attr2 = COND #( WHEN v2 IS NOT INITIAL
                        THEN 'IF_T100_DYN_MSG~MSGV2' )
        attr3 = COND #( WHEN v3 IS NOT INITIAL
                        THEN 'IF_T100_DYN_MSG~MSGV3' )
        attr4 = COND #( WHEN v4 IS NOT INITIAL
                        THEN 'IF_T100_DYN_MSG~MSGV4' ))
      msgty = SWITCH #( severity
        WHEN if_abap_behv_message=>severity-error       THEN 'E'
        WHEN if_abap_behv_message=>severity-warning     THEN 'W'
        WHEN if_abap_behv_message=>severity-information THEN 'I'
        WHEN if_abap_behv_message=>severity-success     THEN 'S' )
      msgv1 = |{ v1 }|
      msgv2 = |{ v2 }|
      msgv3 = |{ v3 }|
      msgv4 = |{ v4 }| ).
    obj->m_severity = severity.
  ENDMETHOD.
ENDCLASS.