Google+ Facebook Twitter MySpace SC

Friday, August 24, 2012

Context Nodes and Attributes at Design Time


Context Nodes and Attributes at Design Time


This chapter mainly focuses on the context nodes and attributes statically realized at design time. In this respect, we will explain not only the role and modality to create context nodes and attributes, but also the modality to access the values stored in the attributes of the nodes.
An attribute is a context entity that has certain properties and can be directly created in the root node CONTEXT, or as a child for another node. Usually, a node is used to group more attributes that belong together. In Fig.  we present a context example created in a view context.
A context node can have attributes or can hold other nodes, but a context attribute cannot have other attributes or nodes. We create a hierarchical arrangement that has, as the start point, the root node CONTEXT. This is automatically created when the controller is initialized and all the nodes and attributes we created are children of this root node.
Example of the structure of a View Context

The data hold in the context nodes and attributes are transient, they exist only for the lifetime of the controller. After this, all the data are lost if we don’t store them in storage media (e.g. database table).
Attributes
To create an attribute, we have to select the node where the attribute should be inserted (in this case, the root context node) and, with right-click, to open its context menu.
Creating an attribute
We have to enter the attribute properties – name and type are required, but the other settings are optional.
Defining the attribute properties

As data type for an attribute, we can use a data type as string, xstring, d,i or we can use the data type defined in the ABAP Dictionary. In our example,the attribute type YDATEOFBIRTH is a data element defined in the ABAP Dictionary. In the system, we find a list with all the data types we can use for an attribute. The coding presented in Listing shows how we can access, in a method, the value of the attribute created in the context node.
Searching for an attribute data type
Access of an attribute from context node
 DATA lv_dateofbirth type ydateofbirth.
 wd_context->get_attribute(EXPORTING name ='DATEOFBIRTH'
 IMPORTING value = lv_dateofbirth).
We have defined a local variable named lv_dateofbirth of YDATEOFBIRTH type, the same type as the attribute. We pass the value of the attribute DATEOFBIRTH in our local variable, by using the method get_attribute of the interface if_wd_context_node. The interface if_wd_context_node has many methods we can use to work with context nodes. We can see all the available methods of this interface by double-clicking on the get_attribute method. To set the value of an attribute, we can use the method set_attribute of the same interface.
As can be seen in Fig, the property Input Help Mode of the attribute is set AUTOMATIC. This means that it is used the search help assigned to the data type of the context attribute in the ABAP Dictionary. In principle, we have a data element of D type. This is the reason why we will have a CALENDAR as input help
Calendar input help
The read-only property set “no” means that the attribute is not write- protected. To put data on the screen and to read data from user, we connect the proper UI Elements properties with the attributes or nodes. The data are stored in the attributes, the UI Elements being the only object the user interacts with. For our example, we have an UI Element InputField where the user enters his date of birth. Web Dynpro Framework transports these data from the UI element to the attribute DATEOFBIRTH when the user presses the SAVE Button, after he enters the value. The attribute keeps the value for further processing. To be able to access the value of this attribute in our methods, we have to pass this value in a local variable (lv_dateofbirth). We can use this value, change it or pass the new value back in the context (Fig) This value is kept in the context for the lifetime of the controller. Then, these data are lost if we don’t store them.
Connection among attribute, ABAP Dictionary, method and UI Element
Nodes
We can directly create a node in the context root node or as child for other node. In Fig. we show how we can create a node.
Creating a node
For each node we create, we have to set some properties, as: cardinality, selection, etc. We can individually create the attributes for a node, or we can use some repository objects from the ABAP Dictionary. For example, when we work with tables, structures or views defined in the ABAP Dictionary, we can use them to define our node, and the attributes will be automatically generated.
Creating a Node that Uses an ABAP Dictionary Repository Object
We create a context node that uses the ABAP Dictionary structure YSTR_PERSON.
Node properties
The Button “Add Attribute from Structure” allows us to add all the structure components or only a part of them.
Selecting components of Structure
Because we need a proper attribute for all these structure components, we select all of them. As result, we have created a node, the attributes being automatically generated according to the structure components we have selected.
Context node STUDENT at design time
The properties of the node we have chosen are: cardinality 0. . .n, singleton – yes and Init. Lead Selection – yes. Web Dynpro Code Wizard,reading a context node or attribute.
The cardinality properties are very important, because they tell us how many elements a context node may have at runtime. We have four possible values:
  • 1. . .1 Exactly one context element is instantiated
  • 0. . .1 Maximum one context element is instantiated
  • 0. . .n Zero or more context elements are instantiated
  • 1. . .n One or more context elements are instantiated
The singleton property can be set YES or NO. When a node is singleton at the runtime, we have only one instance of this node. As usage example, for a nonsingleton node we can specify the context structure required for a Tree UI Element – sequential implementation.The Lead Selection Initialization property determines if the lead selection should be automatically set or manually programmed. In our case, this property is set “YES”, meaning that the lead selection is AUTOMATIC and the first element of this node it automatically selected. More details about lead selection – Table UI element.
For our node, we have defined the supply function named supply_student. Each node may have a supply function defined for it and automatically called by the Web Dynpro Framework. The scope of using a supply function is to populate a context node. In certain cases, we can use the Hook Method wdDoInit instead of a supply function. For more details, see the Hook Methods chapter.
The coding presented in Listing shows how we can use the supply function method to populate the context node STUDENT with three values. We have defined two variables: ls_student and lt_student. Ls_student is of type if_view_name=>element_student, where “view_name” represents the view name and “student” represents the node name. ”if_view_name” represents the programming interface for our view controller. By double-clicking on his name or clicking on the icon Display Controller Interface we can see the coding of this interface. Listing shows a coding part for the context node STUDENT.
Example of supply function Method
 METHOD supply_student .
 DATA ls_student TYPE if_view_name=>element_student.
 DATA lt_student LIKE TABLE OF ls_student.
 ls_student-firstname = 'Ionescu'.
 ls_student-lastname = 'Ana Maria'.
 ls_student-dateofbirth = '19700309'.
 APPEND ls_student TO lt_student.
 ls_student-firstname = 'Marinescu'.
 ls_student-lastname = 'Loredana'.
 ls_student-dateofbirth = '19800523'.
 APPEND ls_student TO lt_student.
 ls_student-firstname = 'Marton'.
 ls_student-lastname = 'Luminita'.
 ls_student-dateofbirth = '19831108'.
 APPEND ls_student TO lt_student.
 node->bind_table(
 new_items = lt_student).
 ENDMETHOD.
Example of view controller programming interface.
  constants:
  wdctx_Student type string value `STUDENT`.
  types:
  Element_Student type YSTR_PERSON,
  Elements_Student type
  standard table of Element_Student
  with default key.
  …..
As can be seen, Element_Student is of YSTR_PERSON type (our ABAP Dictionary structure).But, when we manually create context nodes without dictionary structure,in the view controller programming interface,a new structured type is created (Listing).
Example of view controller programming interface
  …..
  types:
  begin of Element_Faculty,
  FACULTY_NAME type String,
  SPECIALIZATION type String,
  end of Element_Faculty,
  ……
This is why we use the following form to append values: ls_student-firstname = ‘Ionescu’.
The way we append values in an ABAP structure is: structure_name-component_name = value
With the declaration “DATA lt_student LIKE TABLE OF ls_student”,we declare an internal table.We use APPEND statement to add each new line at the end of the last line of the internal table.
At the end,we use the bind_structure method to populate the node with values.Instead of the declaration:
DATA ls_student TYPE if_view_name=>element_student.
we can use:
DATA ls_student TYPE wd_this->element_student.
In this way, we don’t need the view name anymore,because we use the wd_this self-reference.
The runtime structure
The node STUDENT was set Singleton, it has only an instance at runtime and the cardinality was set 0. . .n, meaning that, at runtime, we can have from zero to n elements. Because the lead selection was set “YES”, it was selected the first element of the node. To read, set or append a context node or attribute, we can use the Web Dynpro Code Wizard.
The option “As table operation” can be used in combination with the Read, Set or Append options for a node that allows the usage of this combination. We can read, for example, the entire context node STUDENT in an internal table.
DATA lt_student TYPE wd_this->elements_student. lr_node->get_static_attributes_table( IMPORTING table = lt_student).
Web Dynpro Code Wizard, reading a context node or attribute
In this case, the wizard has generated a variable lt_student of type wd_this -> elements_student. In Listing we saw that, in Interface Controller, besides the definition of a variable, the Element_structure Framework has also defined a variable Elements_Student, with the form:
Elements_Student type standard table of Element_Student with default key.
This is the reason why the Wizard offers the possibility to read all the values from the STUDENT node in lt_student.
As we have mentioned in the last chapter, we can use a table type to populate with data a context node via a supply function. Our node STUDENT has its attributes from the YSTR_PERSON structure. The table type YTABLE_TYPE_STRUCTURE defined in the ABAP Dictionary has the same YSTR_PERSON structure, as line type. Listing shows how we can use a table type to populate with data a context node.
Example of supply function Method
 METHOD supply .
 DATA: lt_student TYPE ytable_type_structure .
 DATA: ls_student TYPE ystr_person.
 ls_student-firstname = 'Ionescu'.
 ls_student-lastname = 'Ana Maria'.
 ls_student-dateofbirth = '19700309'.
 APPEND ls_student TO lt_student.
 ls_student-firstname = 'Marinescu'.
 ls_student-lastname = 'Loredana'.
 ls_student-dateofbirth = '19800523'.
 APPEND ls_student TO lt_student.
 node->bind_table(new_items = lt_student).
 ENDMETHOD.
Working with Child Nodes
We create the context structure presented.
Example of child node
It has a context node FACULTY, cardinality 1. . .1, Singleton with child node ADDRESS, cardinality 1. . .1, Singleton and two attributes FACULTY_NAME type STRING and SPECIALIZATION type STRING.
We read data from the attributes of the node ADDRESS as a child node for FACULTY. Listing shows a possible method to read these attributes.
Reading the attributes values of the child node “ADDRESS”
 DATA: lr_node TYPE REF TO if_wd_context_node,
 lr_subnode TYPE REF TO if_wd_context_node.
 DATA: lv_street TYPE string,
 lv_number TYPE y_char,
 lv_city TYPE string.
 lr_node = wd_context->get_child_node('FACULTY').
 lr_subnode = lr_node->get_child_node('ADDRESS').
 lr_subnode->get_attribute EXPORTING name ='STREET'
 IMPORTING value = lv_street).
 lr_subnode->get_attribute(EXPORTING name ='NUMBER'
 IMPORTING value = lv_number).
 lr_subnode->get_attribute (EXPORTING name ='CITY'
 IMPORTING value = lv_city ).
With the first data definition, we create two variable lr_node and lr_subnode of if_wd_context_node type. With the second data definition, we create three variables with the same data type as the attributes we want to read. Data type Y_CHAR represents a data element defined in the ABAP Dictionary of CHAR type, length 6. Instead of the declaration:
lr_node = wd_context->get_child_node( ’FACULTY’).
we can use the form:
lr_node = wd_context->get_child_node( name = wd_this->wdctx_faculty).
In the first case we have to write with capital letters the node name, and in the second case we use the constant wdctx_faculty of string type, defined by the Framework in Interface Controller, with the form:
constants:
wdctx_faculty type string value ‘FACULTY’.
By using the get_child_node method of if_wd_context_node interface, we successively access the node FACULTY and the node ADDRESS, and by using the method get_attribute, we pass the values of the attributes in our local variable.Another possibility to read the values of the ADDRESS child node attributes is presented in Listing.
Reading the attributes values of the child node “ADDRESS”
 DATA: lr_node TYPE REF TO if_wd_context_node,
 lr_subnode TYPE REF TO if_wd_context_node,
 ls_subnode type wd_this->element_address.
 DATA: lv_street LIKE ls_subnode-street,
 lv_number LIKE ls_subnode-number,
 lv_city LIKE ls_subnode-city.
 lr_node = wd_context->get_child_node('FACULTY').
 lr_subnode = lr_node->get_child_node('ADDRESS').
 lr_subnode->get_static_attributes(IMPORTING
 static_attributes = ls_subnode).
 lv_street = ls_subnode-street.
 lv_number = ls_subnode-number.
 lv_city = ls_subnode-city.
In this case, we have used the get_static_attributes method that supplies a copy of all the static attributes for the ADDRESS child node. After this, we are able to access the attributes values through the local structure “ls_subnode”.Listing shows how we can populate with values the elements of the ADDRESS child node
Populating the attributes of the ADDRESS node
 DATA:lr_node TYPE REF TO if_wd_context_node,
 lr_subnode TYPE REF TO if_wd_context_node,
 ls_subnode TYPE if_view_name=>element_address.
 lr_node = wd_context->get_child_node('FACULTY').
 lr_subnode = lr_node->get_child_node('ADDRESS').
 ls_subnode-street = 'Pforzheimer'.
 ls_subnode-number = '106A'.
 ls_subnode-city = 'Pforzheim'.
 lr_subnode->set_static_attributes(ls_subnode). 

No comments:

Post a Comment