• No results found

Check user input and send messages

In document NET311 Advanced ABAP Web Dynpro (Page 57-63)

The Controller Interface

Exercise 2: Check user input and send messages

Exercise Objectives

After completing this exercise, you will be able to:

• Define controller attributes and access them from other controllers.

• Send messages.

Business Example

In your Web Dynpro application you have to implement the validation of the user input. This may depend on the triggered action.

Template Component: NET311_INTR_S Solution Component: NET311_API_S

Task 1:

If you have finished the previous exercise, you can skip this task. Then, all changes can be implemented in the component you have created in your last exercise.

If you have not finished your last exercise, you can start with a copy of the template component. In this case, copy the template component and name the copy ZNET311_API_##. Assign this component to your package ZNET311_##.

Create an application having the same name as your component and assign the application to your package, too.

1. Copy the template component.

2. Create an application to access your component.

Task 2:

Create a component controller method that checks if the connection key entered by the user is valid.

To read a single connection for a given connection key, the static method READ_CONNECTIONS( ) of class CL_NET310_FLIGHTMODEL is to be used.

If the connection exists, the method should return the value ABAP_TRUE (= 'X'), otherwise the value ABAP_FALSE (= ' ').

2. Define a returning parameter (name: E_CONNECTION_EXISTS) of type string.

3. Call the method GET_USER_INPUT( ) to get the connection key entered by the user.

4. Call the static method READ_CONNECTIONS( ) of class

CL_NET310_FLIGHTMODEL to read the complete connection data set for the connection key entered by the user.

5. If the connection data set could be read by the static method, set the returning parameter to ABAP_TRUE, otherwise evaluate the parameter with ABAP_FALSE.

Task 3:

Define a controller attribute in the component controller to store the reference to the message manager. Make sure that this reference variable is visible to all other controllers of your component.

Get the reference to the message manager in the method WDDOINIT( ) of the component controller.

1. Define a controller attribute in the component controller to store the reference to the message manager. Make sure that this reference variable is visible to all other controllers of your component.

2. Get the reference to the message manager in the method WDDOINIT( ) of the component controller.

Task 4:

All texts displayed as messages have to be translatable. Thus, assign the assistance class CL_NET311_ASSIST to your component.

1. Assign the assistance class to your component.

Task 5:

Now edit the method WDDOBEFOREACTION( ) of the view controller. If the user has triggered the action DISP_FLIGHTS (assigned to the button), then the component controller method CHECK_CONNECTION( ) has to be called. If the connection is not valid, an error message hat to be send. Look for an appropriate message text in the assistance class.

1. Get the reference to the current action by calling the method GET_CURRENT_ACTION( ) of the view controller API.

Continued on next page

2. Check whether the reference to the current action is bound. Stop processing the method, if the reference to the action object is not bound.

Hint: In some situations a round trip can take place without having triggered an action (e.g. selecting a line in a table).

3. If the reference to the current action object is bound, check the value of the public attribute NAME. If this value equals the name of the action assigned to the button with the label Display Flights (DISP_FLIGHTS), call the component controller method CHECK_CONNECTION( ).

4. Check the returning parameter of method CHECK_CONNECTION( ). If the connection does not exist, read the text with id 001 from the text pool of the assistance class by calling its method GET_TEXT( ).

Send this text as an error message. Use the method REPORT_ER-ROR_MESSAGE( ) defined in the message manager interface.

5. Instead of using the controller attribute WD_COMP_CONTROLLER, what other possibility does exist to access the API of the component controller from a view controller?

Solution 2: Check user input and send messages

Task 1:

If you have finished the previous exercise, you can skip this task. Then, all changes can be implemented in the component you have created in your last exercise.

If you have not finished your last exercise, you can start with a copy of the template component. In this case, copy the template component and name the copy ZNET311_API_##. Assign this component to your package ZNET311_##.

Create an application having the same name as your component and assign the application to your package, too.

1. Copy the template component.

a) Display the template component in the object tree. Clicking on the component with the right mouse button will open the component's context menu. Choose Copy.... Enter the name of the component to be created. Press Continue.

b) Adapt the description of the new component.

2. Create an application to access your component.

a) An application having the same name as the component can be created from the context menu of the component.

Task 2:

Create a component controller method that checks if the connection key entered by the user is valid.

To read a single connection for a given connection key, the static method READ_CONNECTIONS( ) of class CL_NET310_FLIGHTMODEL is to be used.

If the connection exists, the method should return the value ABAP_TRUE (= 'X'), otherwise the value ABAP_FALSE (= ' ').

1. Create a method (name: CHECK_CONNECTION( )) in the component controller. This method will contain the source code to validate the user input.

a) Edit the component controller of your component. Select the Methods tab. In the column Method enter the method's name and press Enter.

Double click on the method's name to edit its source code.

Continued on next page

2. Define a returning parameter (name: E_CONNECTION_EXISTS) of type string.

a) Parameters are defined by entering name, type, and associated type in the corresponding fields above the source code.

3. Call the method GET_USER_INPUT( ) to get the connection key entered by the user.

a) Source code see below.

4. Call the static method READ_CONNECTIONS( ) of class

CL_NET310_FLIGHTMODEL to read the complete connection data set for the connection key entered by the user.

a) Source code see below.

5. If the connection data set could be read by the static method, set the returning parameter to ABAP_TRUE, otherwise evaluate the parameter with ABAP_FALSE.

a) Source code see below.

Task 3:

Define a controller attribute in the component controller to store the reference to the message manager. Make sure that this reference variable is visible to all other controllers of your component.

Get the reference to the message manager in the method WDDOINIT( ) of the component controller.

1. Define a controller attribute in the component controller to store the reference to the message manager. Make sure that this reference variable is visible to all other controllers of your component.

a) In the component controller, select the tab Attributes. Define a reference variable (name: GR_MESS_MAN) of type

IF_WD_MESSAGE_MANAGER. Check the property PUBLIC.

2. Get the reference to the message manager in the method WDDOINIT( ) of the component controller.

a) Source code see below.

Task 4:

All texts displayed as messages have to be translatable. Thus, assign the assistance class CL_NET311_ASSIST to your component.

1. Assign the assistance class to your component.

a) Double click on your component. Enter the name of the assistance class in the corresponding field. Save.

Task 5:

Now edit the method WDDOBEFOREACTION( ) of the view controller. If the user has triggered the action DISP_FLIGHTS (assigned to the button), then the component controller method CHECK_CONNECTION( ) has to be called. If the connection is not valid, an error message hat to be send. Look for an appropriate message text in the assistance class.

1. Get the reference to the current action by calling the method GET_CURRENT_ACTION( ) of the view controller API.

a) Source code see below.

2. Check whether the reference to the current action is bound. Stop processing the method, if the reference to the action object is not bound.

Hint: In some situations a round trip can take place without having triggered an action (e.g. selecting a line in a table).

a) Source code see below.

3. If the reference to the current action object is bound, check the value of the public attribute NAME. If this value equals the name of the action assigned to the button with the label Display Flights (DISP_FLIGHTS), call the component controller method CHECK_CONNECTION( ).

a) Source code see below.

4. Check the returning parameter of method CHECK_CONNECTION( ). If the connection does not exist, read the text with id 001 from the text pool of the assistance class by calling its method GET_TEXT( ).

Send this text as an error message. Use the method REPORT_ER-ROR_MESSAGE( ) defined in the message manager interface.

a) Source code see below.

5. Instead of using the controller attribute WD_COMP_CONTROLLER, what other possibility does exist to access the API of the component controller from a view controller?

Continued on next page

Answer: The related code is commented in the source code of method

* get connection key from context

wd_this->get_user_input( IMPORTING es_connection = ls_connection ).

* try to read complete connection for connection key CALL METHOD cl_net310_flightmodel=>read_connections

DATA: lr_current_controller TYPE REF TO if_wd_controller.

lr_current_controller ?= wd_this->wd_get_api( ).

* get message manager

In document NET311 Advanced ABAP Web Dynpro (Page 57-63)