alex_khryst

Travel BD example

Aug 4th, 2021
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ABAP 26.45 KB | None | 0 0
  1. CLASS lhc_Travel DEFINITION INHERITING FROM cl_abap_behavior_handler.
  2.   PRIVATE SECTION.
  3.     CONSTANTS:
  4.       BEGIN OF travel_status,
  5.         open     TYPE c LENGTH 1  VALUE 'O', " Open
  6.         accepted TYPE c LENGTH 1  VALUE 'A', " Accepted
  7.         canceled TYPE c LENGTH 1  VALUE 'X', " Cancelled
  8.       END OF travel_status.
  9.  
  10.     METHODS get_instance_features FOR INSTANCE FEATURES
  11.       IMPORTING keys REQUEST requested_features FOR Travel RESULT result.
  12.  
  13.     METHODS acceptTravel FOR MODIFY
  14.       IMPORTING keys FOR ACTION Travel~acceptTravel RESULT result.
  15.  
  16.     METHODS recalcTotalPrice FOR MODIFY
  17.       IMPORTING keys FOR ACTION Travel~recalcTotalPrice.
  18.  
  19.     METHODS rejectTravel FOR MODIFY
  20.       IMPORTING keys FOR ACTION Travel~rejectTravel RESULT result.
  21.  
  22.     METHODS calculateTotalPrice FOR DETERMINE ON MODIFY
  23.       IMPORTING keys FOR Travel~calculateTotalPrice.
  24.  
  25.     METHODS setInitialStatus FOR DETERMINE ON MODIFY
  26.       IMPORTING keys FOR Travel~setInitialStatus.
  27.  
  28.     METHODS calculateTravelID FOR DETERMINE ON SAVE
  29.       IMPORTING keys FOR Travel~calculateTravelID.
  30.  
  31.     METHODS validateAgency FOR VALIDATE ON SAVE
  32.       IMPORTING keys FOR Travel~validateAgency.
  33.  
  34.     METHODS validateCustomer FOR VALIDATE ON SAVE
  35.       IMPORTING keys FOR Travel~validateCustomer.
  36.  
  37.     METHODS validateDates FOR VALIDATE ON SAVE
  38.       IMPORTING keys FOR Travel~validateDates.
  39.  
  40.     METHODS get_authorizations FOR AUTHORIZATION
  41.       IMPORTING keys REQUEST requested_authorization FOR travel RESULT result.
  42.  
  43.  
  44.  
  45.     METHODS is_update_granted IMPORTING has_before_image      TYPE abap_bool
  46.                                         overall_status        TYPE /dmo/overall_status
  47.                               RETURNING VALUE(update_granted) TYPE abap_bool.
  48.  
  49.     METHODS is_delete_granted IMPORTING has_before_image      TYPE abap_bool
  50.                                         overall_status        TYPE /dmo/overall_status
  51.                               RETURNING VALUE(delete_granted) TYPE abap_bool.
  52.  
  53.     METHODS is_create_granted RETURNING VALUE(create_granted) TYPE abap_bool.
  54.  
  55. ENDCLASS.
  56.  
  57. CLASS lhc_Travel IMPLEMENTATION.
  58.  
  59.  
  60.   METHOD get_instance_features.
  61.     " used for the instance specific dynamic feature control, spedifyies if the related feature
  62.     " is enabled for the instance
  63.     " Read the travel status of the existing travels
  64.     READ ENTITIES OF zi_rap_travel_test IN LOCAL MODE
  65.       ENTITY Travel
  66.         FIELDS ( TravelStatus ) WITH CORRESPONDING #( keys )
  67.       RESULT DATA(travels)
  68.       FAILED failed.
  69.  
  70.     result =
  71.       VALUE #(
  72.       " FOR is for mapping and fast iteration through list or table I guess...
  73.         FOR travel IN travels
  74.         " with LET keyword you can define some help variables in local scope
  75.         " another interesting keyword COND is for getting some result (data type or exception) by specific conditions
  76.           LET is_accepted =   COND #( WHEN travel-TravelStatus = travel_status-accepted
  77.                                       THEN if_abap_behv=>fc-o-disabled
  78.                                       ELSE if_abap_behv=>fc-o-enabled  )
  79.               is_rejected =   COND #( WHEN travel-TravelStatus = travel_status-canceled
  80.                                       THEN if_abap_behv=>fc-o-disabled
  81.                                       ELSE if_abap_behv=>fc-o-enabled )
  82.           IN
  83.             ( %tky                 = travel-%tky
  84.               %action-acceptTravel = is_accepted
  85.               %action-rejectTravel = is_rejected
  86.              ) ).
  87.   ENDMETHOD.
  88.  
  89.   METHOD acceptTravel.
  90.     " Set the new overall status. in local mode allows us to manipulate fields with no log?
  91.     MODIFY ENTITIES OF zi_rap_travel_test IN LOCAL MODE
  92.       ENTITY Travel
  93.          UPDATE
  94.            FIELDS ( TravelStatus )
  95.            WITH VALUE #( FOR key IN keys
  96.                            ( %tky         = key-%tky
  97.                              TravelStatus = travel_status-accepted ) )
  98.       FAILED failed
  99.       REPORTED reported.
  100.  
  101.     " Fill the response table
  102.     " previously we wrote that this action should return $self (an instance of the accepted travel). so lets read instance from table
  103.     READ ENTITIES OF zi_rap_travel_test IN LOCAL MODE
  104.       ENTITY Travel
  105.         ALL FIELDS WITH CORRESPONDING #( keys )
  106.       RESULT DATA(travels).
  107.  
  108.     result = VALUE #( FOR travel IN travels
  109.                         ( %tky   = travel-%tky
  110.                           %param = travel ) ).
  111. **                          interesting... %tky stands for transactional  key. contains exact keys rather it draft mode or not
  112.  
  113.  
  114.   ENDMETHOD.
  115.  
  116.   METHOD recalcTotalPrice.
  117.     " internal action for recalculating total price
  118.  
  119.     TYPES: BEGIN OF ty_amount_per_currencycode,
  120.              amount        TYPE /dmo/total_price,
  121.              currency_code TYPE /dmo/currency_code,
  122.            END OF ty_amount_per_currencycode.
  123.  
  124.     DATA: amount_per_currencycode TYPE STANDARD TABLE OF ty_amount_per_currencycode.
  125.  
  126.     " Read all relevant travel instances.
  127.     READ ENTITIES OF zi_rap_travel_test IN LOCAL MODE
  128.           ENTITY Travel
  129.           " getting all needed travel instances by fields fee
  130.              FIELDS ( BookingFee CurrencyCode )
  131.              WITH CORRESPONDING #( keys )
  132.           RESULT DATA(travels).
  133.     " cleaning some initial instances
  134.     DELETE travels WHERE CurrencyCode IS INITIAL.
  135.     " looping over travel instances with field-symbol
  136.     " remember that field-symbol is like a pointer. which doesn't need memory and just
  137.     " points to any object using its memory address
  138.     LOOP AT travels ASSIGNING FIELD-SYMBOL(<travel>).
  139.       " Set the start for the calculation by adding the booking fee.
  140.       amount_per_currencycode = VALUE #( ( amount        = <travel>-BookingFee
  141.                                            currency_code = <travel>-CurrencyCode ) ).
  142.       " Read all associated bookings and add them to the total price.
  143.       READ ENTITIES OF ZI_RAP_Travel_test IN LOCAL MODE
  144.          ENTITY Travel BY \_Booking
  145.             FIELDS ( FlightPrice CurrencyCode )
  146.           WITH VALUE #( ( %tky = <travel>-%tky ) )
  147.           RESULT DATA(bookings).
  148.       LOOP AT bookings INTO DATA(booking) WHERE CurrencyCode IS NOT INITIAL.
  149.         COLLECT VALUE ty_amount_per_currencycode( amount        = booking-FlightPrice
  150.                                                   currency_code = booking-CurrencyCode ) INTO amount_per_currencycode.
  151.       ENDLOOP.
  152.  
  153.       CLEAR <travel>-TotalPrice.
  154.       LOOP AT amount_per_currencycode INTO DATA(single_amount_per_currencycode).
  155.         " If needed do a Currency Conversion
  156.         IF single_amount_per_currencycode-currency_code = <travel>-CurrencyCode.
  157.           <travel>-TotalPrice += single_amount_per_currencycode-amount.
  158.         ELSE.
  159.           /dmo/cl_flight_amdp=>convert_currency(
  160.              EXPORTING
  161.                iv_amount                   =  single_amount_per_currencycode-amount
  162.                iv_currency_code_source     =  single_amount_per_currencycode-currency_code
  163.                iv_currency_code_target     =  <travel>-CurrencyCode
  164.                iv_exchange_rate_date       =  cl_abap_context_info=>get_system_date( )
  165.              IMPORTING
  166.                ev_amount                   = DATA(total_booking_price_per_curr)
  167.             ).
  168.           <travel>-TotalPrice += total_booking_price_per_curr.
  169.         ENDIF.
  170.       ENDLOOP.
  171.     ENDLOOP.
  172.  
  173.     " write back the modified total_price of travels
  174.     MODIFY ENTITIES OF ZI_RAP_Travel_test IN LOCAL MODE
  175.       ENTITY travel
  176.         UPDATE FIELDS ( TotalPrice )
  177.         WITH CORRESPONDING #( travels ).
  178.   ENDMETHOD.
  179.  
  180.   METHOD rejectTravel.
  181.     " Set the new overall status
  182.     MODIFY ENTITIES OF zi_rap_travel_test IN LOCAL MODE
  183.       ENTITY Travel
  184.          UPDATE
  185.            FIELDS ( TravelStatus )
  186.            WITH VALUE #( FOR key IN keys
  187.                            ( %tky         = key-%tky
  188.                              TravelStatus = travel_status-canceled ) )
  189.       FAILED failed
  190.       REPORTED reported.
  191.  
  192.     " Fill the response table
  193.     READ ENTITIES OF zi_rap_travel_test IN LOCAL MODE
  194.       ENTITY Travel
  195.         ALL FIELDS WITH CORRESPONDING #( keys )
  196.       RESULT DATA(travels).
  197.  
  198.     result = VALUE #( FOR travel IN travels
  199.                         ( %tky   = travel-%tky
  200.                           %param = travel ) ).
  201.   ENDMETHOD.
  202.  
  203.   METHOD calculateTotalPrice.
  204.     " triggered every time when fields BookingFee and CurrencyCode are changed
  205.     MODIFY ENTITIES OF zi_rap_travel_test IN LOCAL MODE
  206.       ENTITY travel
  207.       " there we using internal implementation which calculates the total price
  208.       " rcalctTotalPrice looping over all bookings, collecting all prices and adding to Booking fee
  209.       " also provided currency conversion
  210.         EXECUTE recalcTotalPrice
  211.         FROM CORRESPONDING #( keys )
  212.       REPORTED DATA(execute_reported).
  213.  
  214.     reported = CORRESPONDING #( DEEP execute_reported ).
  215.   ENDMETHOD.
  216.  
  217.   METHOD setInitialStatus.
  218.     " sets initial status when new travel instance is created
  219.     " Read relevant travel instance data
  220.     READ ENTITIES OF zi_rap_travel_test IN LOCAL MODE
  221.       ENTITY Travel
  222.         FIELDS ( TravelStatus ) WITH CORRESPONDING #( keys )
  223.       RESULT DATA(travels).
  224.  
  225.     " Remove all travel instance data with defined status
  226.     DELETE travels WHERE TravelStatus IS NOT INITIAL.
  227.     CHECK travels IS NOT INITIAL.
  228.  
  229.     " Set default travel status
  230.     MODIFY ENTITIES OF zi_rap_travel_test IN LOCAL MODE
  231.     ENTITY Travel
  232.       UPDATE
  233.         FIELDS ( TravelStatus )
  234.         WITH VALUE #( FOR travel IN travels
  235.                       ( %tky         = travel-%tky
  236.                         TravelStatus = travel_status-open ) )
  237.     REPORTED DATA(update_reported).
  238.  
  239.     reported = CORRESPONDING #( DEEP update_reported ).
  240.   ENDMETHOD.
  241.  
  242.   METHOD calculateTravelID.
  243.     " determination on save to calculate id
  244.     " Please note that this is just an example for calculating a field during _onSave_.
  245.     " This approach does NOT ensure for gap free or unique travel IDs! It just helps to provide a readable ID.
  246.     " The key of this business object is a UUID, calculated by the framework.
  247.  
  248.     " check if TravelID is already filled
  249.     READ ENTITIES OF zi_rap_travel_test IN LOCAL MODE
  250.       ENTITY Travel
  251.         FIELDS ( TravelID ) WITH CORRESPONDING #( keys )
  252.       RESULT DATA(travels).
  253.  
  254.     " remove lines where TravelID is already filled.
  255.     DELETE travels WHERE TravelID IS NOT INITIAL.
  256.  
  257.     " anything left ?
  258.     CHECK travels IS NOT INITIAL.
  259.  
  260.     " Select max travel ID
  261.     SELECT SINGLE
  262.         FROM  zrap_travel_test
  263.         FIELDS MAX( travel_id ) AS travelID
  264.         INTO @DATA(max_travelid).
  265.  
  266.     " Set the travel ID
  267.     MODIFY ENTITIES OF zi_rap_travel_test IN LOCAL MODE
  268.     ENTITY Travel
  269.       UPDATE
  270.       " interesting syntax
  271.         FROM VALUE #( FOR travel IN travels INDEX INTO i (
  272.           %tky              = travel-%tky
  273.           TravelID          = max_travelid + i
  274.           %control-TravelID = if_abap_behv=>mk-on ) )
  275.     REPORTED DATA(update_reported).
  276.  
  277.     reported = CORRESPONDING #( DEEP update_reported ).
  278.   ENDMETHOD.
  279.  
  280.   METHOD validateAgency.
  281. *  executed on save and checks provided agencyID
  282.     " Read relevant travel instance data
  283.     " firstly, try to get an instances with provided key
  284.     READ ENTITIES OF zi_rap_travel_test IN LOCAL MODE
  285.       ENTITY Travel
  286.         FIELDS ( AgencyID ) WITH CORRESPONDING #( keys )
  287.       RESULT DATA(travels).
  288.  
  289.     " creating agencies into internal sorted table
  290.     DATA agencies TYPE SORTED TABLE OF /dmo/agency WITH UNIQUE KEY agency_id.
  291.  
  292.     " Optimization of DB select: extract distinct non-initial agency IDs
  293.     " filling internal table with agencies without same rows
  294.     agencies = CORRESPONDING #( travels DISCARDING DUPLICATES MAPPING agency_id = AgencyID EXCEPT * ).
  295.     " deleting initial rows
  296.     DELETE agencies WHERE agency_id IS INITIAL.
  297.  
  298.     " filling another table with simple select with every entry field id from sorted table
  299. *    IF agencies IS NOT INITIAL.
  300. *      " Check if agency ID exist
  301. *      SELECT FROM /dmo/agency FIELDS agency_id
  302. *        FOR ALL ENTRIES IN @agencies
  303. *        WHERE agency_id = @agencies-agency_id
  304. *        INTO TABLE @DATA(agencies_db).
  305. *    ENDIF.
  306.  
  307.     " Raise msg for non existing and initial agencyID
  308.     LOOP AT travels INTO DATA(travel).
  309.       " Clear state messages that might exist
  310.       APPEND VALUE #(  %tky               = travel-%tky
  311.                        %state_area        = 'VALIDATE_AGENCY' )
  312.         TO reported-travel.
  313.     ENDLOOP.
  314.  
  315.     DATA filter_conditions  TYPE if_rap_query_filter=>tt_name_range_pairs .
  316.     DATA ranges_table TYPE if_rap_query_filter=>tt_range_option .
  317.     DATA business_data TYPE TABLE OF zz_travel_agency_es5ea52376b86.
  318.  
  319.     IF  agencies IS NOT INITIAL.
  320.  
  321.       ranges_table = VALUE #( FOR agency IN agencies (  sign = 'I' option = 'EQ' low = agency-agency_id ) ).
  322.       filter_conditions = VALUE #( ( name = 'AGENCYID'  range = ranges_table ) ).
  323.  
  324.  
  325.  
  326.       TRY.
  327.           "skip and top must not be used
  328.           "but an appropriate filter will be provided
  329.           NEW zcl_ce_rap_agency_test( )->get_agencies(
  330.             EXPORTING
  331.               filter_cond    = filter_conditions
  332.               is_data_requested  = abap_true
  333.               is_count_requested = abap_false
  334.             IMPORTING
  335.               business_data  = business_data
  336.             ) .
  337.  
  338.         CATCH /iwbep/cx_cp_remote
  339.               /iwbep/cx_gateway
  340.               cx_web_http_client_error
  341.               cx_http_dest_provider_error
  342.  
  343.        INTO DATA(exception).
  344.  
  345.           DATA(exception_message) = cl_message_helper=>get_latest_t100_exception( exception )->if_message~get_text( ) .
  346.  
  347.           LOOP AT travels INTO travel.
  348.             APPEND VALUE #( %tky = travel-%tky ) TO failed-travel.
  349.  
  350.             APPEND VALUE #( %tky        = travel-%tky
  351.                             %state_area = 'VALIDATE_AGENCY'
  352.                             %msg        =  new_message_with_text( severity = if_abap_behv_message=>severity-error text = exception_message )
  353.                             %element-AgencyID = if_abap_behv=>mk-on )
  354.               TO reported-travel.
  355.           ENDLOOP.
  356.  
  357.           RETURN.
  358.  
  359.       ENDTRY.
  360.  
  361.     ENDIF.
  362.  
  363.  
  364.  
  365.     " Raise msg for non existing and initial agencyID
  366. **    LOOP AT travels INTO DATA(travel).
  367.     LOOP AT travels INTO travel.
  368.  
  369.  
  370. **      " Clear state messages that might exist
  371. **      APPEND VALUE #(  %tky               = travel-%tky
  372. **                       %state_area        = 'VALIDATE_AGENCY' )
  373. **        TO reported-travel.
  374.  
  375.  
  376.  
  377. **      IF travel-AgencyID IS INITIAL OR NOT line_exists( agencies_db[ agency_id = travel-AgencyID ] ).
  378.       IF travel-AgencyID IS INITIAL OR NOT line_exists( business_data[ agencyid = travel-AgencyID ] ).
  379.  
  380.         APPEND VALUE #( %tky = travel-%tky ) TO failed-travel.
  381.  
  382.         APPEND VALUE #( %tky        = travel-%tky
  383.                         %state_area = 'VALIDATE_AGENCY'
  384.                         %msg        = NEW zcm_rap_test(
  385.                                           severity = if_abap_behv_message=>severity-error
  386.                                           textid   = zcm_rap_test=>agency_unknown
  387.                                           agencyid = travel-AgencyID )
  388.                         %element-AgencyID = if_abap_behv=>mk-on )
  389.           TO reported-travel.
  390.       ENDIF.
  391.     ENDLOOP.
  392.   ENDMETHOD.
  393.  
  394.   METHOD validateCustomer.
  395.     " Read relevant travel instance data
  396.     READ ENTITIES OF zi_rap_travel_test IN LOCAL MODE
  397.       ENTITY Travel
  398.         FIELDS ( CustomerID ) WITH CORRESPONDING #( keys )
  399.       RESULT DATA(travels).
  400.  
  401.     DATA customers TYPE SORTED TABLE OF /dmo/customer WITH UNIQUE KEY customer_id.
  402.  
  403.     " Optimization of DB select: extract distinct non-initial customer IDs
  404.     customers = CORRESPONDING #( travels DISCARDING DUPLICATES MAPPING customer_id = CustomerID EXCEPT * ).
  405.     DELETE customers WHERE customer_id IS INITIAL.
  406.     IF customers IS NOT INITIAL.
  407.       " Check if customer ID exist. getting specific fields for the table costumers
  408.       SELECT FROM /dmo/customer FIELDS customer_id
  409.         FOR ALL ENTRIES IN @customers
  410.         WHERE customer_id = @customers-customer_id
  411.         INTO TABLE @DATA(customers_db).
  412.     ENDIF.
  413.  
  414.     " Raise msg for non existing and initial customerID
  415.     LOOP AT travels INTO DATA(travel).
  416.       " Clear state messages that might exist
  417.       APPEND VALUE #(  %tky        = travel-%tky
  418.                        %state_area = 'VALIDATE_CUSTOMER' )
  419.         TO reported-travel.
  420.  
  421.       IF travel-CustomerID IS INITIAL OR NOT line_exists( customers_db[ customer_id = travel-CustomerID ] ).
  422.         APPEND VALUE #(  %tky = travel-%tky ) TO failed-travel.
  423.  
  424.         APPEND VALUE #(  %tky        = travel-%tky
  425.                          %state_area = 'VALIDATE_CUSTOMER'
  426.                          %msg        = NEW zcm_rap_test(
  427.                                            severity   = if_abap_behv_message=>severity-error
  428.                                            textid     = zcm_rap_test=>customer_unknown
  429.                                            customerid = travel-CustomerID )
  430.                          %element-CustomerID = if_abap_behv=>mk-on )
  431.           TO reported-travel.
  432.       ENDIF.
  433.     ENDLOOP.
  434.   ENDMETHOD.
  435.  
  436.   METHOD validateDates.
  437.     " executed when entry trying to be saved by user and checks travel dates
  438.     " Read relevant travel instance data
  439.     READ ENTITIES OF zi_rap_travel_test IN LOCAL MODE
  440.       ENTITY Travel
  441.         FIELDS ( TravelID BeginDate EndDate ) WITH CORRESPONDING #( keys )
  442.       RESULT DATA(travels).
  443.  
  444.     LOOP AT travels INTO DATA(travel).
  445.       " Clear state messages that might exist
  446.       APPEND VALUE #(  %tky        = travel-%tky
  447.                        %state_area = 'VALIDATE_DATES' )
  448.         TO reported-travel.
  449.       " validating travel end date saving to not be earlier than begin date
  450.       IF travel-EndDate < travel-BeginDate.
  451.         APPEND VALUE #( %tky = travel-%tky ) TO failed-travel.
  452.         APPEND VALUE #( %tky               = travel-%tky
  453.                         %state_area        = 'VALIDATE_DATES'
  454.                         %msg               = NEW zcm_rap_test(
  455.                                                  severity  = if_abap_behv_message=>severity-error
  456.                                                  textid    = zcm_rap_test=>date_interval
  457.                                                  begindate = travel-BeginDate
  458.                                                  enddate   = travel-EndDate
  459.                                                  travelid  = travel-TravelID )
  460.                         %element-BeginDate = if_abap_behv=>mk-on
  461.                         %element-EndDate   = if_abap_behv=>mk-on ) TO reported-travel.
  462.         " validating travel start date to be more than system date
  463.       ELSEIF travel-BeginDate < cl_abap_context_info=>get_system_date( ).
  464.         APPEND VALUE #( %tky               = travel-%tky ) TO failed-travel.
  465.         APPEND VALUE #( %tky               = travel-%tky
  466.                         %state_area        = 'VALIDATE_DATES'
  467.                         %msg               = NEW zcm_rap_test(
  468.                                                  severity  = if_abap_behv_message=>severity-error
  469.                                                  textid    = zcm_rap_test=>begin_date_before_system_date
  470.                                                  begindate = travel-BeginDate )
  471.                         %element-BeginDate = if_abap_behv=>mk-on ) TO reported-travel.
  472.       ENDIF.
  473.     ENDLOOP.
  474.   ENDMETHOD.
  475.  
  476.   METHOD get_authorizations.
  477.     " some authority checks
  478.     " controls authorization by reading before image
  479.     DATA: has_before_image    TYPE abap_bool,
  480.           is_update_requested TYPE abap_bool,
  481.           is_delete_requested TYPE abap_bool,
  482.           update_granted      TYPE abap_bool,
  483.           delete_granted      TYPE abap_bool.
  484.  
  485.     DATA: failed_travel LIKE LINE OF failed-travel.
  486.  
  487.     " Read the existing travels
  488.     READ ENTITIES OF zi_rap_travel_test IN LOCAL MODE
  489.       ENTITY Travel
  490.         FIELDS ( TravelStatus ) WITH CORRESPONDING #( keys )
  491.       RESULT DATA(travels)
  492.       FAILED failed.
  493.  
  494.     CHECK travels IS NOT INITIAL.
  495.  
  496. * *   In this example the authorization is defined based on the Activity + Travel Status
  497. * *   For the Travel Status we need the before-image from the database. We perform this for active (is_draft=00) as well as for drafts (is_draft=01) as we can't distinguish between edit or new drafts
  498.     SELECT FROM zrap_travel_test
  499.       FIELDS travel_uuid, overall_status
  500.       FOR ALL ENTRIES IN @travels
  501.       WHERE travel_uuid EQ @travels-TravelUUID
  502.       ORDER BY PRIMARY KEY
  503.       INTO TABLE @DATA(travels_before_image).
  504.  
  505.     is_update_requested = COND #( WHEN requested_authorization-%update              = if_abap_behv=>mk-on OR
  506.                                        requested_authorization-%action-acceptTravel = if_abap_behv=>mk-on OR
  507.                                        requested_authorization-%action-rejectTravel = if_abap_behv=>mk-on OR
  508.                                        requested_authorization-%action-Prepare      = if_abap_behv=>mk-on OR
  509.                                        requested_authorization-%action-Edit         = if_abap_behv=>mk-on OR
  510.                                        requested_authorization-%assoc-_Booking      = if_abap_behv=>mk-on
  511.                                   THEN abap_true ELSE abap_false ).
  512.  
  513.     is_delete_requested = COND #( WHEN requested_authorization-%delete = if_abap_behv=>mk-on
  514.                                     THEN abap_true ELSE abap_false ).
  515.  
  516.     LOOP AT travels INTO DATA(travel).
  517.       update_granted = delete_granted = abap_false.
  518.  
  519.       READ TABLE travels_before_image INTO DATA(travel_before_image)
  520.            WITH KEY travel_uuid = travel-TravelUUID BINARY SEARCH.
  521.       has_before_image = COND #( WHEN sy-subrc = 0 THEN abap_true ELSE abap_false ).
  522.  
  523.       IF is_update_requested = abap_true.
  524.         " Edit of an existing record -> check update authorization
  525.         IF has_before_image = abap_true.
  526.           update_granted = is_update_granted( has_before_image = has_before_image  overall_status = travel_before_image-overall_status ).
  527.           IF update_granted = abap_false.
  528.             APPEND VALUE #( %tky        = travel-%tky
  529.                             %msg        = NEW zcm_rap_test( severity = if_abap_behv_message=>severity-error
  530.                                                             textid   = zcm_rap_test=>unauthorized )
  531.                           ) TO reported-travel.
  532.           ENDIF.
  533.           " Creation of a new record -> check create authorization
  534.         ELSE.
  535.           update_granted = is_create_granted( ).
  536.           IF update_granted = abap_false.
  537.             APPEND VALUE #( %tky        = travel-%tky
  538.                             %msg        = NEW zcm_rap_test( severity = if_abap_behv_message=>severity-error
  539.                                                             textid   = zcm_rap_test=>unauthorized )
  540.                           ) TO reported-travel.
  541.           ENDIF.
  542.         ENDIF.
  543.       ENDIF.
  544.  
  545.       IF is_delete_requested = abap_true.
  546.         delete_granted = is_delete_granted( has_before_image = has_before_image  overall_status = travel_before_image-overall_status ).
  547.         IF delete_granted = abap_false.
  548.           APPEND VALUE #( %tky        = travel-%tky
  549.                           %msg        = NEW zcm_rap_test( severity = if_abap_behv_message=>severity-error
  550.                                                           textid   = zcm_rap_test=>unauthorized )
  551.                         ) TO reported-travel.
  552.         ENDIF.
  553.       ENDIF.
  554.  
  555.       APPEND VALUE #( %tky = travel-%tky
  556.  
  557.                       %update              = COND #( WHEN update_granted = abap_true THEN if_abap_behv=>auth-allowed ELSE if_abap_behv=>auth-unauthorized )
  558.                       %action-acceptTravel = COND #( WHEN update_granted = abap_true THEN if_abap_behv=>auth-allowed ELSE if_abap_behv=>auth-unauthorized )
  559.                       %action-rejectTravel = COND #( WHEN update_granted = abap_true THEN if_abap_behv=>auth-allowed ELSE if_abap_behv=>auth-unauthorized )
  560.                       %action-Prepare      = COND #( WHEN update_granted = abap_true THEN if_abap_behv=>auth-allowed ELSE if_abap_behv=>auth-unauthorized )
  561.                       %action-Edit         = COND #( WHEN update_granted = abap_true THEN if_abap_behv=>auth-allowed ELSE if_abap_behv=>auth-unauthorized )
  562.                       %assoc-_Booking      = COND #( WHEN update_granted = abap_true THEN if_abap_behv=>auth-allowed ELSE if_abap_behv=>auth-unauthorized )
  563.  
  564.                       %delete              = COND #( WHEN delete_granted = abap_true THEN if_abap_behv=>auth-allowed ELSE if_abap_behv=>auth-unauthorized )
  565.                     )
  566.         TO result.
  567.     ENDLOOP.
  568.  
  569.   ENDMETHOD.
  570.  
  571.   METHOD is_create_granted.
  572.     " helper method which checks if the create operation is allowed, binded to the activity 01
  573.     " making authority check with created previously authority object
  574.     AUTHORITY-CHECK OBJECT 'ZOSTATTEST'
  575.   ID 'ZOSTATTEST' DUMMY
  576.   ID 'ACTVT' FIELD '01'.
  577.     create_granted = COND #( WHEN sy-subrc = 0 THEN abap_true ELSE abap_false ).
  578.     " Simulate full access - for testing purposes only! Needs to be removed for a productive implementation.
  579.     create_granted = abap_true.
  580.   ENDMETHOD.
  581.  
  582.   METHOD is_delete_granted.
  583.     " checks if delete activity granted.
  584.     IF has_before_image = abap_true.
  585.       AUTHORITY-CHECK OBJECT 'ZOSTATtest'
  586.         ID 'ZOSTATtest' FIELD overall_status
  587.         ID 'ACTVT' FIELD '06'.
  588.     ELSE.
  589.       AUTHORITY-CHECK OBJECT 'ZOSTATtest'
  590.         ID 'ZOSTATtest' DUMMY
  591.         ID 'ACTVT' FIELD '06'.
  592.     ENDIF.
  593.     delete_granted = COND #( WHEN sy-subrc = 0 THEN abap_true ELSE abap_false ).
  594.  
  595.     " Simulate full access - for testing purposes only! Needs to be removed for a productive implementation.
  596.     delete_granted = abap_true.
  597.   ENDMETHOD.
  598.  
  599.   METHOD is_update_granted.
  600.     " checks if update activity is granted. corresponds to activity 021
  601.     IF has_before_image = abap_true.
  602.       AUTHORITY-CHECK OBJECT 'ZOSTATtest'
  603.         ID 'ZOSTATtest' FIELD overall_status
  604.         ID 'ACTVT' FIELD '02'.
  605.     ELSE.
  606.       AUTHORITY-CHECK OBJECT 'ZOSTATtest'
  607.         ID 'ZOSTATtest' DUMMY
  608.         ID 'ACTVT' FIELD '02'.
  609.     ENDIF.
  610.     update_granted = COND #( WHEN sy-subrc = 0 THEN abap_true ELSE abap_false ).
  611.  
  612.     " Simulate full access - for testing purposes only! Needs to be removed for a productive implementation.
  613.     update_granted = abap_true.
  614.   ENDMETHOD.
  615.  
  616. ENDCLASS.
Add Comment
Please, Sign In to add comment