In the previous exercise, you've defined side effects for the Travel BO (see Exercise 9).
In this exercise, you will implement the function getDaysToFlight
that you've defined in Exercise 3. This function will be used to determine all the day to flight related virtual elements defined in the booking BO projection view (see Exercise 4).
Reminder: Do not forget to replace the suffix placeholder
###
with your choosen or assigned assigned suffix in the exercise steps below.
Click to expand!
A function in RAP is a custom read-operation that is part of the business logic.
Functions perform calculations or reads on business objects without causing any side effects. Functions don't issue any locks on database tables and you can't modify or persist any data computed in a function implementation.
Further reading: Functions
Implement the only one function that was defined in Exercise 3.6:
getDaysToFlight
.
🔵 Click to expand!
Implement the the Function
getDaysToFlight
for the booking entity in the behavior implementation classZRAP110_BP_BookingTP_###
. This function can be used to determine the values of the virtual elements of the Booking BO projection view:BookingStatusIndicator
,InitialDaysToFlight
,RemainingDaysToFlight
, andDaysToFlightIndicator
.This function can, for example, be used at runtime to calculate the virtual elements via direct EML calls.
🟣 Click to expand!
-
Open the behavior implementation class of the Travel entity
ZRAP110_BP_BOOKINGTP_###
and navigate to the methodgetDaysToFlight
of the local handler classlhc_booking
-
Implement the function method
getDaysToFlight
.For that, replace the empty method implementation of
getDaysToFlight
with the source code provided below and replace all occurences of the placeholder###
with your assigned suffix using Ctrl+F.************************************************************************** * Instance-bound function for calculating virtual elements via EML calls ************************************************************************** METHOD getDaysToFlight. DATA: c_booking_entity TYPE ZRAP110_C_BookingTP_###, bookings_result TYPE TABLE FOR FUNCTION RESULT zrap110_r_traveltp_###\\booking~getdaystoflight, booking_result LIKE LINE OF bookings_result. "read relevant data READ ENTITIES OF ZRAP110_R_TravelTP_### IN LOCAL MODE ENTITY booking FIELDS ( TravelID BookingStatus BookingID FlightDate BookingDate ) * ALL FIELDS WITH CORRESPONDING #( keys ) RESULT DATA(bookings). LOOP AT bookings ASSIGNING FIELD-SYMBOL(<booking>). c_booking_entity = CORRESPONDING #( <booking> ). "set relevant transfered data booking_result = CORRESPONDING #( <booking> ). "calculate virtual elements booking_result-%param = CORRESPONDING #( zrap110_calc_book_elem_###=>calculate_days_to_flight( c_booking_entity ) MAPPING booking_status_indicator = BookingStatusIndicator days_to_flight_indicator = DaysToFlightIndicator initial_days_to_flight = InitialDaysToFlight remaining_days_to_flight = RemainingDaysToFlight ). "append APPEND booking_result TO bookings_result. ENDLOOP. result = bookings_result. ENDMETHOD.
Implement a READ FUNCTION in an ABAP class to test the implemented function.
🔵 Click to expand!
-
Open the class
ZRAP110_EML_PLAYGROUND_###
and uncomment following coding:"execute function getDaysToFlight READ ENTITIES OF ZRAP110_R_TravelTP_### ENTITY Booking EXECUTE getDaysToFlight FROM VALUE #( FOR link IN travels_to_bookings ( %tky = link-target-%tky ) ) RESULT DATA(days_to_flight). "output result structure LOOP AT days_to_flight ASSIGNING FIELD-SYMBOL(<days_to_flight>). out->write( | TravelID = { <days_to_flight>-%tky-TravelID } | ). out->write( | BookingID = { <days_to_flight>-%tky-BookingID } | ). out->write( | RemainingDaysToFlight = { <days_to_flight>-%param-remaining_days_to_flight } | ). out->write( | InitialDaysToFlight = { <days_to_flight>-%param-initial_days_to_flight } | ). out->write( | --------------- | ). ENDLOOP.
-
Start your Travel App and copy a Travel ID from a former created entry of your choice.
-
Go back to your class
ZRAP110_EML_PLAYGROUND_###
and paste your Travel ID on the corresponding space. -
Right-click your class, select Run As > ABAP Application (Console) (or press F9).
-
Check your result.
Now that you've...
- implemented a function with a return structure as output parameter, and
- determined the values of virtual elements via an EML call using a function,
you can continue with the next exercise – Exercise 11: Enhance the BO Behavior with Business Events