In this exercise, you extend your local class with attributes for the departure airport and the destination airport, and read the values for these attributes from a database table.
Task 1: Copy Template
Copy the template class. Alternatively, copy your solution of the previous exercise.
Steps
Copy the class /LRN/CL_S4D400_CLS_CONSTRUCTOR to a class in your own package (suggested name: ZCL_##_SELECT, where ## stands for your group number).
Open the source code of the global class /LRN/CL_S4D400_CLS_CONSTRUCTOR.
Link the Project Explorer view with the editor.
In the Project Explorer view, right-click class /LRN/CL_S4D400_CLS_CONSTRUCTOR to open the context menu.
From the context menu, choose Duplicate ....
Enter the name of your package in the Package field. In the Name field, enter the name ZCL_##_SELECT, where ## stands for your group number.
Adjust the description and choose Next.
Confirm the transport request and choose Finish.
Task 2: Declare Additional Attributes
Extend the local class lcl_connection with the private instance attributes airport_from_idand airport_to_id. Add some output for the new attributes to the implementation of the method get_output.
Note
The new attributes are not filled yet. In order to fill them we will add a SELECT statement to the constructor in one of the next tasks of this exercise.Steps
Switch to the local class lcl_connection.
In the global class, choose Local Types.
Add the following private attributes to the class definition:
Attributes
Attribute Name | Scope | Data Type |
---|
airport_from_id | instance | /DMO/AIRPORT_FROM_ID |
airport_to_id | instance | /DMO/AIRPORT_TO_ID |
Adjust the code as follows:
12345678910
PRIVATE SECTION.
DATA carrier_id TYPE /dmo/carrier_id.
DATA connection_id TYPE /dmo/connection_id.
DATA airport_from_id TYPE /dmo/airport_from_id.
DATA airport_to_id TYPE /dmo/airport_to_id.
ENDCLASS.
Extend the implementation of the method get_output. Append more string templates to the returning parameter r_output. Embed the new attributes as expressions into the string templates.
Navigate to the implementation of the method get_output.
Adjust the code as follows:
1234567
APPEND |--------------------------------| TO r_output.
APPEND |Carrier: { carrier_id }| TO r_output.
APPEND |Connection: { connection_id }| TO r_output.
APPEND |Departure: { airport_from_id }| TO r_output.
APPEND |Destination: { airport_to_id }| TO r_output.
Activate the class. Execute it and analyze the console output.
Press Ctrl + F3 to activate the class.
Press F9 to run the class.
Task 3: Analyze the Database Table
Analyze the definition of the database table /DMO/CONNECTION.
Steps
Open the development object that contains the definition of the database table /DMO/CONNECTION.
From the eclipse toolbar, choose Open ABAP Development Object or press Ctrl + Shift + A.
In the input field, enter /dmo/con as a search string.
In the list of matching items, click on /DMO/CONNECTION (Database Table) and choose OK.
Open the Tooltip Description for the database table /DMO/CONNECTION.
Click on /dmo/connection after the keyword define table and press F2 to show the tooltip description.
Task 4: Read Data from the Database
In the method constructor of the local class lcl_connection, implement a SELECT statement that reads values for the new attributes from the database table /DMO/CONNECTION.
Steps
Return to the local class lcl_connection in your global class.
In the editor view of Eclipse, open tab ZCL_##_SELECT.
In the global class, choose Local Types.
Navigate to the implementation of the method constructor.
Search for the code line METHOD constructor..
After the ENDIF. statement, add a SELECT statement that reads a single record from database table /DMO/CONNECTION.
Adjust the code as follows:
123456789
IF i_carrier_id IS INITIAL OR i_connection_id IS INITIAL.
RAISE EXCEPTION TYPE cx_abap_invalid_value.
ENDIF.
SELECT SINGLE
FROM /dmo/connection
Implement the FIELDS clause. Read the table fields airport_from_id and airport_to_id.
Hint
Use auto-completion (Ctrl + Space) to enter the field names.After FROM /DMO/CONNECTION enter FIELDS.
After a blank, press Ctrl + Space and choose airport_from_id.
After a comma and a blank press Ctrl + Space again and choose airport_to_id.
The complete FIELDS clause should look like this:
123
FIELDS DepartureAirport, DestinationAirport
Implement the WHERE condition. Restrict all key fields of the database table (except for the client field) with the values of importing parameters i_carrier_id and i_connection_id. Do not forget to escape the parameters with prefix @.
Hint
Use auto-completion (Ctrl + Space) to enter the element names and parameter names.Add the following code after the FIELDS clause:
1234
WHERE carrier_id = @i_carrier_id
AND connection_id = @i_connection_id
Implement the INTO clause. Store the SELECT result in the attributes airport_from_id, and airport_to_id. Do not forget to escape the attributes with prefix @.
Hint
Use auto-completion (Ctrl + Space) to enter the attribute names.Adjust the code as follows:
123456
WHERE carrier_id = @i_carrier_id
AND connection_id = @i_connection_id
INTO ( @airport_from_id, @airport_to_id ).
The complete SELECT statement should look like this:
12345678
SELECT SINGLE
FROM /dmo/connection
FIELDS airport_from_id, airport_to_id
WHERE carrier_id = @i_carrier_id
AND connection_id = @i_connection_id
INTO ( @airport_from_id, @airport_to_id ).
Implement error handling after the SELECT statement. Check the content of the system field sy-subrc. If it does not equal zero, raise exception CX_ABAP_INVALID_VALUE.
Add the following code after the SELECT statement:
12345
IF sy-subrc <> 0.
RAISE EXCEPTION TYPE cx_abap_invalid_value.
ENDIF.
Activate the class. Execute it and analyze the console output. Check that the output for the new attributes displays data.
Press Ctrl + F3 to activate the class.
Press F9 to run the class.