controllers and context programming

22
1 Controllers and Context Programming

Upload: kranthi-kumar

Post on 31-Oct-2014

67 views

Category:

Documents


4 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Controllers and context programming

1

Controllers and Context Programming

Page 2: Controllers and context programming

2

© SAP AG 2005, ABAP Web Dynpro

The Context At Runtime

Understand the controller methods that are available to you for application coding

The Context API

Contents:

Controller and Context Programming

Page 3: Controllers and context programming

3

© SAP AG 2005, ABAP Web Dynpro

After completing this lesson, you will be able to:

Understand how to access nodes and attributes in the context using the context API.

The Context API: Objectives

Page 4: Controllers and context programming

4

Code ImplementationEach Web Dynpro controller is a separate ABAP program. The definition of these programs is generated automatically when a new component or controller is declared. The source code, that implements these controllers is generated automatically. You are provided with various points within the controller code where you may add your own coding.Any attempt to entering coding outside the designated areas will result in that code being lost when the controller is regenerated.Every time the controller is activated the code is regenerated.

Standard Hook MethodsIn every controller, there are certain standard methods that are always present. At controller creation time, these methods are empty and can hold any coding the developer wishes to place within them.

The application developer is not permitted to deleted or rename any of these standard hook methods. If any attempt is made to do this, the changes will be lost when the coding is regenerated.

© SAP AG 2005, ABAP Web Dynpro

Common Controller Features: Standard Controller Hook Methods

Controller

Implementation

StandardHook

Methods

InstanceMethods

ContextRoot Node

RequiredControllers

ControllerInterface

Other WDControllers

CustomController

Business Logic

(Models)

Created by explicit declaration or coding Created by the Web Dynpro Framework (WDF)

ComponentUsage

Other WDComponents

Page 5: Controllers and context programming

5

WDDOINITStandard hook method for all Web Dynpro controllers. This method is only called once during the lifecycle of a controller. All your initialization code should go here since this method is called immediately after the controller has been instantiated.

WDDOEXITStandard hook method called at the end of a controller’s life cycle. All your cleanup code should go here.This method is called immediately before the controller’s lifecycle comes to an end.

© SAP AG 2005, ABAP Web Dynpro

Standard Hook Methods for all controllers

All controllers have these two standard hook methods.

The method will only be called during the controller’s lifecycle if they contain coding.

method WDDOINIT.endmethod.

method WDDOEXIT.endmethod.

Page 6: Controllers and context programming

6

Controller instance methodsThis information applies to both view and custom controllers.For all controllers, you can create an instance method by declaring the method name and its parameters in the “methods” tab of the controller editor window.

© SAP AG 2005, ABAP Web Dynpro

Common Controller Features: Controller Instance Methods

Controller

Implementation

StandardHook

Methods

InstanceMethods

ContextRoot Node

RequiredControllers

ControllerInterface

Other WDControllers

CustomController

Business Logic

(Models)

Created by explicit declaration or coding Created by the Web Dynpro Framework (WDF)

ComponentUsage

Other WDComponents

Page 7: Controllers and context programming

7

Attributes:

Attributes can be public or private.

Attributes can not be directly used inside the controller. The attributes can be accessed by using the controller attribute WD_THIS which provides a reference to the local controller interface.

Methods:

All methods are public and can be used by any other controllers. Prerequisite is that the controller which will call the method has defined a use relation to the used controller in his properties.

© SAP AG 2005, ABAP Web Dynpro

Controller Attributes and Utility methods

Attributes for the controller can be created (public or private)

Arbitrary methods can be created - …….

Page 8: Controllers and context programming

8

WD_THISwdThis is the Web Dynpro specific self reference and should be used in preference to the standard ABAP self reference of me. WD_THIS is a reference to the current controller’s interface IF<controller name> and represents all the functionality implemented in the generated class. This gives you access to standard Web Dynpro functionality such as logging, validation, and parameter mapping.

WD_CONTEXTWD_CONTEXT is the reference to the controller’s context root node, and thus to the entire context.

© SAP AG 2005, ABAP Web Dynpro

Standard Controller Attributes WD_CONTEXT and WD_THIS

WD_CONTEXT and WD_THISpresent in any WD controller (excepted interface and interface view controller).

WD_THIS - self reference of the local interface, type depends on the controller type.

WD_CONTEXT - reference to the context of associated controller.

Page 9: Controllers and context programming

9

© SAP AG 2005, ABAP Web Dynpro

Standard Controller Attribute WD_COMP_CONTROLLER

WD_COMP_CONTROLLERpresent in any WD controller.

reference to the component controller with access to all public methods and attributes.

Attribute will automatically assigned to all view controllers when a view is created.

For all other controller the WD_COMP_CONTROLLER attribute will be assigned, when the properties of the controller the componentcontroller is used.

Page 10: Controllers and context programming

10

wdDoBeforeNavigation()This standard hook method is found only in the component controller.Whenever an outbound plug is fired from a view controller, a navigation event is raised. It is perfectly possible for each view in a view set to fire an outbound plug; therefore, all navigation events are placed into a queue and are only processed once all the views in the current view assembly have been processed. The wdDoBeforeNavigation() method is called just before the Web DynproFramework processes the events in the navigation queue. This allows you to implement your own coding to do such things a navigation event prioritisation etc.

wdDoPostProcessing()This standard hook method is found only in the component controller.In complex Web Dynpro applications, it is possible that the data from multiple components must be validated before the next step in the business process can be taken. This method has been implemented in order for cross component validation to take place.

© SAP AG 2005, ABAP Web Dynpro

Standard Hook Methods – Component Controller

Note: Only a component controller has these hook methods.

WDDOBEFORENAVIGATIONIt is executed before the navigation stack is processed

WDDOPOSTPROCESSINGData from multiple components can be validated before the next step is execute

Page 11: Controllers and context programming

11

To access a context node element, the following steps must be performed:1.Use the get_Child_Node() method to get a reference of If_Wd_Context_Node node

instance. This method requires the name of the node and optional the index of the element in the parent node the desired node instance belongs to. In this case, the parent node is the context root which only ever has one element, therefore the index parameter is 1.

© SAP AG 2005, ABAP Web Dynpro

Access to Attribute of Node Element I

Flights..nCARRID

CONNID2CARRID

CONNID1CARRID

CONNID

Context Root

0Default Element

data: Node_Flights type ref to If_Wd_Context_Node.

* navigate from <CONTEXT> to <FLIGHTS> via lead selectionNode_Flights = wd_Context->get_Child_Node( Name = `FLIGHTS` ).

* @TODO handle not set lead selectionif ( Node_Flights is initial ). endif.

Note: Node and attribute names must be used in upper case

Page 12: Controllers and context programming

12

To access a context node element, the following steps must be performed:1.Use the get_Child_Node() method to get a reference of If_Wd_Context_Node node

instance. This method requires the name of the node and optional the index of the element in the parent node the desired node instance belongs to. In this case, the parent node is the context root which only ever has one element, therefore the index parameter is 1.

2.Calling method get_Element() to get the element with the lead selection. This returns a reference of If_Wd_Context_Element element instance.

© SAP AG 2005, ABAP Web Dynpro

Access to Attribute of Node Element II

Flights..nCARRID

CONNID2CARRID

CONNID1CARRID

CONNID

Context Root

0Default Element

data: Node_Flights type ref to If_Wd_Context_Node,Elem_Flights type ref to If_Wd_Context_Element.

* navigate from <CONTEXT> to <FLIGHTS> via lead selectionNode_Flights = wd_Context->get_Child_Node( Name = `FLIGHTS` ).

* get element via lead selectionElem_Flights = Node_Flights->get_Element( ).

* @TODO handle not set lead selectionif ( Elem_Flights is initial ). endif.

Page 13: Controllers and context programming

13

To access a context node element, the following steps must be performed:1.Use the get_Child_Node() method to get a reference of If_Wd_Context_Node node

instance. This method requires the name of the node and optional the index of the element in the parent node the desired node instance belongs to. In this case, the parent node is the context root which only ever has one element, therefore the index parameter is 1.

2.Calling method get_Element() to get the element with the lead selection. This returns a reference of If_Wd_Context_Element element instance.

3.Calling method get_Attribute() to get the value of the attribute.

© SAP AG 2005, ABAP Web Dynpro

Access to Attribute of Node Element III

Flights..nCARRID

CONNID2CARRID

CONNID1CARRID

CONNID

Context Root

0Default Element

data: Node_Flights type ref to If_Wd_Context_Node,Elem_Flights type ref to If_Wd_Context_Element,Stru_Flights type If_Componentcontroller=>Element_Flights,Item_CARRID like Stru_Flights-CARRID.

* navigate from <CONTEXT> to <FLIGHTS> via lead selectionNode_Flights = wd_Context->get_Child_Node( Name = `FLIGHTS` ).

* get element via lead selectionElem_Flights = Node_Flights->get_Element( ).

* get single attributeElem_Flights->get_Attribute(exportingName = `CARRID`

importingValue = Item_Carrid ).

Page 14: Controllers and context programming

14

With the method get_Static_Attributes all static attributes of a context node can be retrieved as a structure.

The method get_Static_Attributes will provide the values of the attributes by using a move-corresponding. So the target structure can be different to the node structure.

© SAP AG 2005, ABAP Web Dynpro

Access to all Static Attributes of a Node Element

Flights..nCARRID

CONNID2CARRID

CONNID1CARRID

CONNID

Context Root

0Default Element

data: Node_Flights type ref to If_Wd_Context_Node,Elem_Flights type ref to If_Wd_Context_Element,Stru_Flights type If_Componentcontroller=>Element_Flights.

* navigate from <CONTEXT> to <FLIGHTS> via lead selectionNode_Flights = wd_Context->get_Child_Node( Name = `FLIGHTS` ).

* get element via lead selectionElem_Flights = Node_Flights->get_Element( ).

* get all declared attributesElem_Flights->get_Static_Attributes(importingStatic_Attributes = Stru_Flights ).

Page 15: Controllers and context programming

15

With the method get_Static_Attributes_Table the attributes of all elements can be retrieved as a internal table.

© SAP AG 2005, ABAP Web Dynpro

Access to all Elements of a Node

Flights..nCARRID

CONNID2CARRID

CONNID1CARRID

CONNID

Context Root

0Default Element

data: Node_Flights type ref to If_Wd_Context_Node,Elem_Flights type ref to If_Wd_Context_Element,lt_Flights type If_Main_View=>Elements_Flights .

* navigate from <CONTEXT> to <FLIGHTS> via lead selectionNode_Flights = wd_Context->get_Child_Node( Name = `FLIGHTS` ).

* @TODO handle not set lead selectionif ( Node_Flights is initial ).endif.

* get all node elementNode_Flights->GET_STATIC_ATTRIBUTES_TABLE( importing table = lt_Flights ).

Itab

Page 16: Controllers and context programming

16

Addition of elements to a nodeThe process of adding an element to a node requires the following steps:

1.Access the relevant context node. Use the get_Child_Node() method to get a reference of If_Wd_Context_Node node instance.

© SAP AG 2005, ABAP Web Dynpro

Binding of an Element to a Node I

FLIGHTSContext Root

0Default Element

data:Node_Flights type ref to If_Wd_Context_Node.

* navigate from <CONTEXT> to <FLIGHTS> via lead selectionnode_flights = wd_context->get_child_node( Name = 'FLIGHTS' ).

Page 17: Controllers and context programming

17

Addition of elements to a nodeThe process of adding an element to a node requires the following steps:

1.Access the relevant context node. Use the get_Child_Node() method to get a reference of If_Wd_Context_Node node instance.

2.Create a new elements with the method create_element().

© SAP AG 2005, ABAP Web Dynpro

Binding of an Element to a Node II

FLIGHTSContext Root

0Default Element

data:Node_Flights type ref to If_Wd_Context_Node,First_flight_Elem type ref to If_Wd_Context_Element.

* navigate from <CONTEXT> to <FLIGHTS> via lead selectionnode_flights = wd_context->get_child_node( Name = 'FLIGHTS' ).

* create new element for node FLIGHTSFirst_flight_Elem = Node_Flights->create_element( ).

Page 18: Controllers and context programming

18

Addition of elements to a nodeThe process of adding an element to a node requires the following steps:

1.Access the relevant context node. Use the get_Child_Node() method to get a reference of If_Wd_Context_Node node instance.

2.Create a new elements with the method create_element().

3.Set the corresponding attributes to the element.

© SAP AG 2005, ABAP Web Dynpro

Binding of an Element to a Node III

FLIGHTSContext Root

0Default Element

CARRID

CONNID

data:Node_Flights type ref to If_Wd_Context_Node,First_flight_Elem type ref to If_Wd_Context_Element.

* navigate from <CONTEXT> to <FLIGHTS> via lead selectionnode_flights = wd_context->get_child_node( Name = 'FLIGHTS' ).

* create new element for node FLIGHTSFirst_flight_Elem = Node_Flights->create_element( ).

* set attributesFirst_flight_Elem->set_attribute( name = 'CARRID' value = 'LH' ).First_flight_Elem->set_attribute( name = 'CONNID' value = '400' ).

Page 19: Controllers and context programming

19

Addition of element as a structure to a nodeThe process of adding an element as a structure to a node requires the following steps:

1.Access the relevant context node. Use the get_Child_Node() method to get a reference of If_Wd_Context_Node node instance.

2.Fill the required structure elements.

3.Set the corresponding attributes to the element.

4.Bind the element to the node. The parameter SET_INITIAL_ELEMENTS allows to delete all existing elements of a node and bind only the actual element to the node. In this case the parameter must be set to abap_true (default value). If the element should be added to the node as an additional element then the parameter must be set to abap_false.

© SAP AG 2005, ABAP Web Dynpro

FLIGHTSContext Root

0Default Element

CARRID

CONNID

1

Binding of an Element to a Node IV

data:Node_Flights type ref to If_Wd_Context_Node,First_flight_Elem type ref to If_Wd_Context_Element.

* navigate from <CONTEXT> to <FLIGHTS> via lead selectionnode_flights = wd_context->get_child_node( Name = 'FLIGHTS' ).

* create new element for node FLIGHTSFirst_flight_Elem = Node_Flights->create_element( ).

* set attributesFirst_flight_Elem->set_attribute( name = 'CARRID' value = 'LH' ).First_flight_Elem->set_attribute( name = 'CONNID' value = '400' ).

* bind first element to nodeNode_Flights->bind_element( new_item = First_flight_ElemSET_INITIAL_ELEMENTS = abap_false ).

Page 20: Controllers and context programming

20

Addition of element as a structure to a nodeThe process of adding an element as a structure to a node requires the following steps:

1.Access the relevant context node. Use the get_Child_Node() method to get a reference of If_Wd_Context_Node node instance.

2.Fill the required structure elements.

3.Set the corresponding attributes to the element.

4.Bind the structure to the node. The parameter SET_INITIAL_ELEMENTS allows to delete all existing elements of a node and bind only the actual element to the node. In this case the parameter must be set to abap_true (default value). If the structure should be added to the node as an additional element then the parameter must be set to abap_false.

© SAP AG 2005, ABAP Web Dynpro

Static Attributes and Binding of a Element to a Node

data:Node_Flights type ref to If_Wd_Context_Node,Stru_flights type If_Componentcontroller=>Element_flights.

* navigate from <CONTEXT> to <FLIGHTS> via lead selectionnode_flights = wd_context->get_child_node( Name = 'FLIGHTS' ).

* set values to node->attributesStru_flights-carrid = 'AA'.Stru_flights-connid = '017'.

* bind new element to nodeNode_flights->bind_structure( new_item = Stru_flightsSET_INITIAL_ELEMENTS = abap_false ).

Page 21: Controllers and context programming

21

Addition of multiple elements as a internal table to a nodeThe process of multiple elements as a internal table to a node requires the following steps:

1.Define an internal table of the node type.

2.Define a structure of the table type.

3.Append values to the table.

4.Bind the internal table to the node.

© SAP AG 2005, ABAP Web Dynpro

FLIGHTSContext Root

0Default Element

Binding of a Table to a Node

data:Node_Flights type ref to If_Wd_Context_Node,lt_flights type If_Componentcontroller=>Elements_flights,Stru_flights like line of lt_flights.

* append values to local tableStru_flights-carrid = 'LH'.Stru_flights-connid = '400'.append Stru_flights to lt_flights.

Stru_flights-carrid = 'AA'.Stru_flights-connid = '017'.append Stru_flights to lt_flights.

* get node referenceNode_Flights = wd_context->get_child_node( 'FLIGHTS' ).

* bind local tableNode_Flights->bind_table( lt_flights ).

CARRID

CONNID

1

ItabCARRID

Page 22: Controllers and context programming

22

© SAP AG 2005, ABAP Web Dynpro

You should now be able to:

Understand how to access nodes and attributes in the context using the context API

Controller and Context Programming: Summary