Hook Methods SAP ABAP Web Dynpro

These methods cannot be deleted and represent the interface between the Framework and our application. The Hook Methods are called in a specific sequence according to a phase model. After generation, these methods are empty, but can be filled with source codes in case we want to interfere in a certain step of this phase model.

shows the Hook methods to be found in different controllers.

shows the Hook methods to be found in different controllers.

As we can see, the Hook methods wdDoInit and wdDoExit are common to all the controllers. shows which Hook methods can be found in a view.

Hook methods in a view

Hook methods in a view

wdDoInit

This method can be considered the controller constructor. It is automatically called when the controller is initialized for the first time.We have many examples where we can use this method, for example:setting the initial values of a controller context, dynamically creation of a context node and attributes,filling context nodes.We can use this method instead of the supply function method (to populate with values a context node),only if we know that the context is filled only once and it’s not invalidated afterwards.

We create a WD component that uses the wdDoInit Hook method to populate with values a context node. The component structure and the view layout are presented.

WD component structure, view layout

WD component structure, view layout

In the context view, we create a context node named STUDENT, with the same structure as the data binding example –dictionary structure YSTR_PERSON,cardinality 1...1,singleton, without supply function.

Context structure

Context structure

Using the wdDoInit Hook method,we populate with values this node (Listing). When calling the application for the first time, the wdDoInit Hook method fills the node STUDENT with all the elements set here.

The wdDoInit Hook method

We can use the wdDoInit Hook Method to dynamically create the context nodeSTUDENT and to populate it with values.

Runtime

Runtime

The wdDoInit Hook method

To dynamically create the context node “STUDENT”, we have used the methodADD_NEW_CHILD_NODE of the IF_WD_CONTEXT_NODE_INFO interfacethat creates an Info Object and adds it as a Lower-Level Node for the CONTEXT root node. The method CREATE_NODEINFO_FROM_STRUCT of the CL_WD_DYNAMIC_TOOL class is obsolete and that’s why it is recommendable touse the if_wd_context_node_info->add_new_child_node.

ADD_NEW_CHILD_NODE have many import parameters of which,for the scope of our example, we have used:

  • NAME of STRING type – the name of the generated context node
  • IS_SINGLETON of ABAP_BOOL type,default value ABAP_FALSE – sets the property node SYNGLETON.In our example,the node STUDENT has to be singleton,and that’s why we have set this value ABAP_TRUE
  • IS_MULTIPLE of ABAP_BOOL type, default value ABAP_TRUE – sets the property node CARDINALITY.In our example,the node STUDENT has the cardinality 1...1,and that’s why we have set this value ABAP_FALSE
  • IS_MANDATORYf ABAP_BOOL type,default value ABAP_FALSE - sets the same property node CARDINALITY.We need the ABAP_TRUE value because the context node must have exactly one context element at runtime
  • STATIC_ELEMENT_TYPE of STRING type – we can use it to define the DICTIONARY_STRUCTURE property for our context node. Same as the case when this node is created at the design time, we can use the name of the development objects created in the ABAP Dictionary as Structure or Views

wdDoExit

This method can be considered the controller destructor. It is automatically called when exiting the controller, and can be used for executing closing statements.

In the ABAP Dictionary, we have defined a lock for the database table YPERSON. We can use wdDoExit Hook method to release a lock that has been set for this table to synchronize the access of several WD applications to the same data. To do this, we have to use the generated DEQUEUE_EYPERSON Function Module (Listing).

The wdDoExit Hook method

Here we have called the Function Module directly from our WD method, but we can create a method in a class used as model for theWDcomponent. If we don’t call the corresponding DEQUEUE_EYPERSON Function Module to release the database record that was locked with the EQUEUE_EYPERSON Function Module, this lock is generally removed at the end of the transaction.

wd Do Modify View

This method is mostly used in the dynamic programming, it is a method used for modifying the view before rendering.For the dynamically programming of the UI elements, we have many classes and methods that help us as application developer to dynamically modify a view. To show this, we create an example, aWDcomponent, where we use this Hook method to dynamically create an InputField, a Label, a TextView and a LinkToAction UIelement. The WD component structure and the context node are presented.

WD component structure and context node

WD component structure and context node

For the scope of this example, we import in the MIMEs (Multipurpose Internet Mail Extensions) folder an image: next.JPG that we use for the linkToAction UI element. To import a file in this folder, we right-click on the WD component name and, from the contextual menu, we chooseCreate -> Mime Object -> Import.

The context node PERSON has the Cardinality 1. . .1, Singleton. Its attributes are NAME and GREETING, of string type. We can almost use the same example as for our first exercise. In this case, we create at design time only the UI element Group, the other UI elements being dynamically created by using the wdDoModify-View Hook method.The view layout is presented.

View layout

View layout

Listing shows the content of the wdDoModifyView Hook method By using the FIRST_TIME parameter of WDY_BOOLEAN type, we check if wdDoModifyView is called for the first time. In case this parameter is ABAP_TRUE (first time), we create a label UI Element:

To do this, we use the method new_label of the cl_wd_label class interface

The WdDoModifyView Hook method

The new_label method has many mandatory and optional parameters. We have used label_for, text and ID to set the main properties of the Label UI element. The text parameter helps us to set the label text. The Label_for parameter specifies the UI element for which this label is.

To embedding the UI elements into the layout, we need to specify the arrangement of these elements. For this, we have used the FlowLayout layout:

Class interface CL_WD_LABEL

Class interface CL_WD_LABEL

Parameters for the new_label method

Parameters for the new_label method

After creating the UI element and setting the layout, we have to attach them to the ROOTCONTAINERUIELEMENT or to another container.In our case, we have attached our new created label to the UI element Group with the GRP ID.

lr_container ?= view->get_element(‘GRP’).
lr_container->add_child(lr_label).

We have used the method add_child( ) that enabled us to do this.After this,we create an InputField UI element:

We use the new_input_field method from the cl_wd_input_field class interface.The bind_value parameter represents the value property for the UI element property.In this case, we dynamically create a data binding for this property to the context node PERSON, attribute NAME. Until now, we have seen how we can create a data binding at the design time. In this case, we can’t create a data binding at design time, because this UI element doesn’t exist at the design time; it is dynamically created at runtime.

To dynamically create a data binding of the property bind_value with the NAME attribute, we have to specify the absolute path of this attribute.lv_bind_name = ‘PERSON.NAME’.

In the next step, we dynamically create a LinkToAction UI element:

We use the method new_link_to_action of the cl_wd_link_to_action class interface.This UI element has an event associated. We have used the parameter on_action to define the action that the Framework triggers when the user interacts with this UI element.

For a linkToAction UI element, we can define a hypertext link or we can use an image instead a text.In our case, we have used the parameter image_source to specify the name of the image we want to use.Then, we specify the layout and we attach it to our UI element Group with the same GRP ID.

At the end, we dynamically create a TextView UI element by using the new_text_view method of the cl_wd_text_view class interface

The data binding to the context attribute GREETING is dynamically created.After creating the UI element, we specify the layout and we attach it to our UI element Group with the same GRP ID.

When the user presses the linkToAction UI element, the Framework triggers the event handler method onactionnext(Listing ).

Action Next

Action Next

The onactionnext event handler method

Runtime

Runtime

In case we want to set “required” the state property of the Input UI element, we need only an extra line code:

We use the state parameter to dynamically set the property with the same name of the InputField UI element.If we don’t use this optional parameter, the default value is set: CL_WD_INPUT_FIELD=>E_STATE-NORMAL.

parameter to dynamically set the property with the same name

wd Do Before Action

We can use this method to perform our own validation before an action is triggered.To exemplify this,we create a Web Dynpro component named Y_WDDOBEFOREACTION with a view named V_VIEW and a window W_DEFAULT.

In the context view, we create the well-known context node named STUDENT,with the same dictionary structure YSTR_PERSON, cardinality 1...1, singleton,without supply function. Our scope is to check if the user enters values in all the in put Fields UI elements bound to the attributes of the context node STUDENT. TheView layout is presented.

The View layout

The View layout

In the wd Do Before Action Hook method, we use the static method CHECK_MANDATORY_ATTR_ON_VIEW of the CL_WD_DYNAMIC_TOOL class tocheck if the user had entered values in all the mandatory fields (the state property of the inputField UI elements is set required).

The wdDoBeforeAction Hook method

When the user presses the Save button, the Web Dynpro Framework firstly checks,by using the wdDoBeforeAction Hook method, if all the required fields are filled.In case the user doesn’t enter values in all these fields, all the operations that had to take place after pressing the Save button are finished and the user is informed about this,through error messages and by highlighting the UI elements that generated these messages. In this way, we don’t have to manually check each mandatory field.

Runtime

Runtime

wd Do On Context Menu

We can use this method to provide hierarchical context menus in web Dynpro Views.In our first example, we have already seen the default context menu displayed by the Web Dynpro Framework when the user presses right-click on an UI element.

We can create a context menu at design time,by using the CONTEXT_MENUS root,or dynamically, by coding the wd Do On Context Menu Hook method.To exemplify how we can use this method to dynamically create context menus, we use the example from the wdDoBeforeAction Hook method. In this case,we offer to the user the possibility to personalize the field he wants to be mandatory. He will be able to choose between his first name as mandatory field and his last name as mandatory field.

When he chooses the first name as mandatory,we will mark the corresponding field with a red star, and when he chooses the last name, we will mark the corresponding field with a red star. In the default mode, the first name is set as mandatory.

To create this example, we have to perform some modifications into the last application.In this case,we have to set dynamic the state properties of the two inputField UI elements,corresponding to first name and last name. To be able to do this, we have to create, in the context view,two extra context attributes of WDUI_STATE type,to bind these attributes with the corresponding properties.In this way, at design time, the two inputField UI elements are not marked as mandatory,anyway. To set in the default mode the first name as mandatory,we use the wdDoInit Hook method we have just learnt about.

Context structure and data binding

Context structure and data binding

Setting the initial values for our node DYNAMIC

To offer to the end user the capability to dynamically choose what value he wants to be mandatory from the two optional values, we have to code the wd Do On-ContextMenu Hook method.

With the first data statement, we create a local reference to the CL_WD_MENU class and two local references to the CL_WD_MENU_ACTION_ITEM, required to dynamically create our menu,and two menu options of menuActionItem type. We need the two menu options to offer to the user the possibility to choose between options:set first name as mandatory or set last name as mandatory.

With the second data statement, we create two local variables required to read from the context attributes the status properties corresponding to the two input-Fields UI elements(firstname, lastname)and a local reference required to access our context node.Firstly,we create a context menu by using the static method new_menu of the cl_wd_menu class.

By using the static methodnew_menu_action_item of the cl_wd_menu_action_item class,we create the two menu options we need.Fromthe parameters of this class,we have used:

  • id: To set the ID of the view element
  • on_action: To associate the action that will be triggered when the user presses the respective option
  • text: The text that will be displayed for the respective menu option

The wd Do On Context Menu example

  • enabled: To set if this option is active or inactive. In the default mode,this parameter is set ABAP_TRUE,respective enabled

The local variables used to pass the values of the two attributes (firstname and lastname) are of the same type as the attributes: WDUI_STATE.By doubleclicking on the TYPE,the forward navigation is open and we can see a data element with a Domain as an elementary type.This domain has two fixed values:00 for Normal item and 01 for required.

Before we effectively create the two menu options, we have to check which one of the two state proprieties is set required and which one is set normal.In case the firstname is required and lastname is normal,we create the two menu options with “Set last name as mandatory” active and “Set first name as mandatory” inactive.In case the firstname is normal and the lastname is required,we create the two menu options with “Set last name as mandatory” inactive and “Set first name as mandatory”active.

Then, we add the two created menu options of menuActionItem type to our context menu and display the respective menu by using the wdDoOnContextMenu Hook method, returning the parameter named menu.we have already seen that, for our InputFields UI elements, we have set the properties:

  • context Menu Behaviour: Provides, not inherits from the container
  • context Menu Id: Set with the IDs of our context menu options

Runtime

Runtime

All we have to do now is to create the respective actions: FN_MANDATORY and LN_MANDATORY,actions that the Framework triggers when the user presses our menu action items. The onactionln_mandatory event handler method is presented.

Event handler method triggered for the menu option with LAST_NAME ID

In case the user chooses the last name as mandatory, we have to set required the property state of the corresponding UI element and normal the property state of the UI element corresponding to the first name.

The onactionfn_mandatory event handler method is presented in Listing In case the user chooses the first name as mandatory, we have to set required the property state of the corresponding UI element and to set normal the property state of the UI element corresponding to the last name.

Event handler method triggered for the menu option with FIRST_NAME ID

Runtime

Runtime


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SAP ABAP Web Dynpro Topics