forked from SAP-samples/abap-cheat-sheets
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
706 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CLASS zbp_demo_abap_rap_ro_m_as DEFINITION PUBLIC ABSTRACT FINAL FOR BEHAVIOR OF zdemo_abap_rap_ro_m_as. | ||
PUBLIC SECTION. | ||
CLASS-DATA num_raised_events TYPE i. | ||
ENDCLASS. | ||
|
||
CLASS zbp_demo_abap_rap_ro_m_as IMPLEMENTATION. | ||
ENDCLASS. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
CLASS lhc_zdemo_abap_rap_ro_m_as DEFINITION INHERITING FROM cl_abap_behavior_handler. | ||
PRIVATE SECTION. | ||
|
||
METHODS get_global_authorizations FOR GLOBAL AUTHORIZATION | ||
IMPORTING REQUEST requested_authorizations FOR root RESULT result. | ||
|
||
METHODS calc FOR MODIFY | ||
IMPORTING keys FOR ACTION root~calc. | ||
|
||
METHODS det_modify FOR DETERMINE ON MODIFY | ||
IMPORTING keys FOR root~det_modify. | ||
|
||
ENDCLASS. | ||
|
||
CLASS lhc_zdemo_abap_rap_ro_m_as IMPLEMENTATION. | ||
|
||
METHOD get_global_authorizations. | ||
ENDMETHOD. | ||
|
||
METHOD calc. | ||
READ ENTITY IN LOCAL MODE zdemo_abap_rap_ro_m_as | ||
FIELDS ( num1 num2 arithm_op crea_date_time lchg_date_time ) WITH CORRESPONDING #( keys ) | ||
RESULT DATA(lt_calc) | ||
FAILED DATA(f). | ||
|
||
DATA(timestamp) = cl_abap_tstmp=>utclong2tstmp( utclong_current( ) ). | ||
|
||
LOOP AT lt_calc ASSIGNING FIELD-SYMBOL(<calc>). | ||
TRY. | ||
<calc>-calc_result = SWITCH #( <calc>-arithm_op | ||
WHEN `+` THEN |{ CONV decfloat34( <calc>-num1 + <calc>-num2 ) STYLE = SIMPLE }| | ||
WHEN `-` THEN |{ CONV decfloat34( <calc>-num1 - <calc>-num2 ) STYLE = SIMPLE }| | ||
WHEN `*` THEN |{ CONV decfloat34( <calc>-num1 * <calc>-num2 ) STYLE = SIMPLE }| | ||
WHEN `/` THEN |{ CONV decfloat34( <calc>-num1 / <calc>-num2 ) STYLE = SIMPLE }| | ||
WHEN `P` THEN |{ CONV decfloat34( ipow( base = <calc>-num1 exp = <calc>-num2 ) ) STYLE = SIMPLE }| | ||
ELSE `Wrong operator` ). | ||
|
||
"Handling the fact that ABAP allows division by zero if the dividend itself is zero. | ||
IF <calc>-num1 = 0 AND <calc>-num2 = 0 AND <calc>-arithm_op = `/`. | ||
<calc>-calc_result = `Division by 0`. | ||
ENDIF. | ||
CATCH cx_sy_zerodivide. | ||
<calc>-calc_result = `Division by 0`. | ||
CATCH cx_sy_arithmetic_overflow. | ||
<calc>-calc_result = `Overflow error`. | ||
ENDTRY. | ||
ENDLOOP. | ||
|
||
MODIFY ENTITY IN LOCAL MODE zdemo_abap_rap_ro_m_as | ||
UPDATE FIELDS ( calc_result ) | ||
WITH CORRESPONDING #( lt_calc ). | ||
ENDMETHOD. | ||
|
||
METHOD det_modify. | ||
MODIFY ENTITY IN LOCAL MODE zdemo_abap_rap_ro_m_as | ||
EXECUTE calc | ||
FROM CORRESPONDING #( keys ). | ||
ENDMETHOD. | ||
|
||
ENDCLASS. | ||
|
||
CLASS lsc_zdemo_abap_rap_ro_m_as DEFINITION INHERITING FROM cl_abap_behavior_saver. | ||
PROTECTED SECTION. | ||
|
||
METHODS save_modified REDEFINITION. | ||
|
||
METHODS cleanup_finalize REDEFINITION. | ||
|
||
ENDCLASS. | ||
|
||
CLASS lsc_zdemo_abap_rap_ro_m_as IMPLEMENTATION. | ||
|
||
METHOD save_modified. | ||
IF create-root IS NOT INITIAL. | ||
RAISE ENTITY EVENT zdemo_abap_rap_ro_m_as~created | ||
FROM VALUE #( FOR <cr> IN create-root ( | ||
%key = VALUE #( id = <cr>-id ) ) ). | ||
|
||
zbp_demo_abap_rap_ro_m_as=>num_raised_events = zbp_demo_abap_rap_ro_m_as=>num_raised_events + lines( create-root ). | ||
ENDIF. | ||
|
||
IF update-root IS NOT INITIAL. | ||
"Demonstrating the BDEF derived type TYPE TABLE FOR EVENT | ||
DATA evt_tab_up TYPE TABLE FOR EVENT zdemo_abap_rap_ro_m_as~updated. | ||
|
||
evt_tab_up = VALUE #( FOR <up> IN update-root INDEX INTO updidx ( | ||
%key = VALUE #( id = <up>-id ) | ||
%param = VALUE #( col1 = 'Event raised' | ||
col2 = |UPDATED ({ updidx })| ) ) ). | ||
RAISE ENTITY EVENT zdemo_abap_rap_ro_m_as~updated FROM evt_tab_up. | ||
|
||
zbp_demo_abap_rap_ro_m_as=>num_raised_events = zbp_demo_abap_rap_ro_m_as=>num_raised_events + lines( update-root ). | ||
ENDIF. | ||
|
||
IF delete-root IS NOT INITIAL. | ||
RAISE ENTITY EVENT zdemo_abap_rap_ro_m_as~deleted | ||
FROM VALUE #( FOR <del> IN delete-root INDEX INTO delidx ( | ||
%key = VALUE #( id = <del>-id ) | ||
%param = VALUE #( col1 = 'Event raised' | ||
col2 = |DELETED ({ delidx })| ) ) ). | ||
|
||
zbp_demo_abap_rap_ro_m_as=>num_raised_events = zbp_demo_abap_rap_ro_m_as=>num_raised_events + lines( delete-root ). | ||
ENDIF. | ||
ENDMETHOD. | ||
|
||
METHOD cleanup_finalize. | ||
ENDMETHOD. | ||
|
||
ENDCLASS. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
********************************************************************** | ||
* Note: | ||
* | ||
* - This class is the RAP event handler class for zdemo_abap_rap_ro_m_as. | ||
* | ||
* - The RAP business events in this example are raised using RAISE | ||
* ENTITY EVENT statements in the save_modified saver method that | ||
* is implemented in the CCIMP include of the ABAP behavior pool | ||
* zbp_demo_abap_rap_ro_m_as. | ||
* | ||
********************************************************************** | ||
|
||
CLASS zcl_demo_abap_rap_evt_handler DEFINITION | ||
PUBLIC ABSTRACT FINAL | ||
FOR EVENTS OF zdemo_abap_rap_ro_m_as. | ||
|
||
PUBLIC SECTION. | ||
PROTECTED SECTION. | ||
PRIVATE SECTION. | ||
ENDCLASS. | ||
|
||
CLASS zcl_demo_abap_rap_evt_handler IMPLEMENTATION. | ||
ENDCLASS. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
CLASS lhe_event DEFINITION INHERITING FROM cl_abap_behavior_event_handler. | ||
|
||
PRIVATE SECTION. | ||
|
||
METHODS on_updated FOR ENTITY EVENT | ||
updated FOR root~updated. | ||
|
||
METHODS on_deleted FOR ENTITY EVENT | ||
deleted FOR root~deleted. | ||
|
||
METHODS on_created FOR ENTITY EVENT | ||
created FOR root~created. | ||
|
||
DATA evt_log TYPE TABLE OF zdemo_abap_draft WITH EMPTY KEY. | ||
ENDCLASS. | ||
|
||
CLASS lhe_event IMPLEMENTATION. | ||
|
||
"Note: | ||
"- For this example, database table entries are created for the individual | ||
" RAP BO instances that are imported into the event handler methods. | ||
"- The transactional phases are implicitly set when RAP business events are | ||
" consumed locally. This means that RAP event handler methods are started in | ||
" the modify phase when called. If database modifications are to be implemented | ||
" in RAP event handler methods, you must explicitly activate the save phase to | ||
" avoid causing errors detected by the controlled SAP LUW. | ||
|
||
METHOD on_created. | ||
cl_abap_tx=>save( ). | ||
LOOP AT created ASSIGNING FIELD-SYMBOL(<created>). | ||
TRY. | ||
APPEND VALUE #( id = cl_system_uuid=>create_uuid_x16_static( ) | ||
draftuuid = cl_system_uuid=>create_uuid_x16_static( ) | ||
calc_result = |Instance key: "{ <created>-id }" / Event CREATED raised| | ||
crea_date_time = cl_abap_tstmp=>utclong2tstmp( utclong_current( ) ) | ||
) TO evt_log. | ||
CATCH cx_uuid_error INTO DATA(err). | ||
ASSERT err IS INITIAL. | ||
ENDTRY. | ||
MODIFY zdemo_abap_draft FROM TABLE @evt_log. | ||
ENDLOOP. | ||
ENDMETHOD. | ||
|
||
METHOD on_updated. | ||
cl_abap_tx=>save( ). | ||
LOOP AT updated ASSIGNING FIELD-SYMBOL(<updated>). | ||
TRY. | ||
APPEND VALUE #( id = cl_system_uuid=>create_uuid_x16_static( ) | ||
draftuuid = cl_system_uuid=>create_uuid_x16_static( ) | ||
calc_result = |Instance key: "{ <updated>-id }" / %param: col1: "{ <updated>-%param-col1 }" col2: "{ <updated>-%param-col2 }"| | ||
crea_date_time = cl_abap_tstmp=>utclong2tstmp( utclong_current( ) ) | ||
) TO evt_log. | ||
CATCH cx_uuid_error INTO DATA(err). | ||
ASSERT err IS INITIAL. | ||
ENDTRY. | ||
MODIFY zdemo_abap_draft FROM TABLE @evt_log. | ||
ENDLOOP. | ||
ENDMETHOD. | ||
|
||
METHOD on_deleted. | ||
cl_abap_tx=>save( ). | ||
LOOP AT deleted ASSIGNING FIELD-SYMBOL(<deleted>). | ||
TRY. | ||
APPEND VALUE #( id = cl_system_uuid=>create_uuid_x16_static( ) | ||
draftuuid = cl_system_uuid=>create_uuid_x16_static( ) | ||
calc_result = |Instance key: "{ <deleted>-id }" / %param: col1: "{ <deleted>-%param-col1 }" col2: "{ <deleted>-%param-col2 }"| | ||
crea_date_time = cl_abap_tstmp=>utclong2tstmp( utclong_current( ) ) | ||
) TO evt_log. | ||
CATCH cx_uuid_error INTO DATA(err). | ||
ASSERT err IS INITIAL. | ||
ENDTRY. | ||
MODIFY zdemo_abap_draft FROM TABLE @evt_log. | ||
ENDLOOP. | ||
ENDMETHOD. | ||
ENDCLASS. |
Oops, something went wrong.