Hi, I provide training in SAP UI5. Also feel free to checkout my new web page https://saptechblog.com
Webdynpro ABAP

Context change log

 

In many applications it is necessary to capture the changes made by the user in the screen. Imagine you have a number of UI elements in the screen and the user changes the value of only one field and click on some action. We may not exactly which UI element he has made the changes. We will read all contexts binded to the UI element and process the data. This may cause a negative impact on the performance as you process the huge amount of data even though the user has not done any actions on those data.

Imagine I have a table that displays the data in a editable mode and the user has the provision to update the data in the table and update it back on the database table on the click on submit button.

 

clip_image001

 

At this point of time we may not know which row he has exactly edited. We simply read all the records in the table and process it in the loop and also update the records which are not modified back in the database table.

Hence it is necessary to optimize the performance. Each context node has the provision to store the information about itself. We don’t have to process all the context nodes that are available in the controller; instead there is a Webdynpro framework which provides which particular context has been changed using change log function.

Within the controller all the information about the changes made to the context nodes of the controller is stored in a single internal table. At runtime we can access this table and based on the values in the table we can process the data.

It is like a recorder that records the user entries with relevant information’s.

Note: This only captures the changes made by the user in the screen. It does not capture the changes to context element made through programming.

Context Changes are recorded only for the original nodes in that controller and not for the mapped nodes.

In the case of OVS value helps and freely programmed value helps, the new value is not entered automatically into the table but must be entered actively using method ADD_CONTEXT_ATTRIBUTE_CHANGE of interface IF_WD_CONTEXT, provided an entry is desired.

By default the change log function in any controller will be disabled. We need to activate the change log function in order to use its functionality.

We activate it through the interface if_wd_context in any controller. This interface in contrast to if_wd_context_node not only refers to the root context but also to the context as the whole.

Methods of the interface if_wd_context and its uses:

clip_image002

 

The internal table that contains the user defined types is of type WDR_CONTEXT_CHANGE_LIST.

The component of internal table is as follows.

clip_image003

 

Example program given by SAP: DEMO_CONTEXT_CHANGES.

Now let us see an example on how to use the change log function.

Step 1: Create a table that displays the data in the editable mode.

Refer the below link for how to create a Table with input field.

http://www.teamabap.com/2014/05/table-with-input-field.html

Create a table as in the link that displays the flight detail as below.

Output:

 

clip_image004

Code in the method doinit:

 

clip_image005

 

Step 2: Create a context node with structure of the internal table WDR_CONTEXT_CHANGE_LIST that the change log function returns the output.

 

clip_image006

Enter the name for the node and provide the dictionary structure as the line type WDR_CONTEXT_CHANGE of the table type WDR_CONTEXT_CHANGE_LIST.

clip_image007

 

Click on add attribute from structure and choose all the fields and press enter.

 

 

clip_image008

 

 

 

 

Context node will be created as below.

 

clip_image009

 

Change the type of attribute as string for node, old_value and new_value.

 

Step 3: We will proceed from here. Go to the layout tab of the view controller and insert a button UI element.

 

clip_image010

 

Provide ID and Type for the UI element.

clip_image011

 

Change the Text property of the Button UI element and create an action for the button UI element.

 

clip_image012

 

 

Step 4: Insert a table UI element. Right click on the table and select create binding.

 

 

clip_image013

 

 

Choose the context node as changes and press enter.

 

clip_image014

 

Step 5: As I mentioned earlier the change log function by default will be deactivated in the controller. Let’s activate the change log function to capture the changes made by the user.

Go to the doinit method of the view controller and activate the change log.

We need the object reference to the interface if_wd_context to activate the change log. There is a method called get_context in the interface if_wd_context_node which returns the reference variable of type if_wd_context.

Using pattern call the method to get the interface of the context.

 

clip_image015

 

Coding will be generated as below.

 

clip_image016

Modify the code as below.

 

clip_image017

Using the object for context call the method enable change log of the interface to activate the change log function.

 

clip_image018

 

Coding will be generated as below.

 

clip_image019

Now we have activated the change log function.

Step 6: Now there is a method called get_change_log which will return the changes in the user entries in the form of the internal table specified. This method cannot be called inside a event handler method. Hence we will create a method and call this method inside it to get the change log.

Go to the methods tab of the view controller and create a method get_log.

 

clip_image020

 

 

Inside the method get the data reference for the context as we did before in the doinit method and call the method get_change_log of the interface to get the change log in the form of internal table.

 

clip_image021

Call the method get_change_log.

 

clip_image022

 

 

Coding will be generated as below.

clip_image023

Modify the code as below.

 

clip_image024

Now the change log has been captured in the internal table. Export this internal table contents so that these change log can be accessed inside an event handler method.

 

Code in the method GET_LOG:

data : lo_context type ref to if_wd_context.
call method wd_context->get_context
  receiving
    context
= lo_context.

call method lo_context->get_context_change_log
  receiving
    change_list
= it_changes.

.

 

Step 7: Go to the event handler method created for the button and get these changes and bind it on the context for the changes.

 

clip_image026

 

Get the changes in the event handler method as below.

Set the internal table content to the node changes.

 

clip_image027

 

Modify the generated code as below.

Code in the event handler method is as below.

 

 

data : it_changes type wdr_context_change_list.
it_changes
= wd_this->get_log( ).

data lo_nd_changes type ref to if_wd_context_node.
data lt_changes type wd_this->elements_changes.
data ls_changes type wd_this->element_changes.
data : wa_changes like line of it_changes.

field-symbols : <fs_value> type any.
field-symbols : <fs_value_new> type any.

lo_nd_changes
= wd_context->get_child_node( name = wd_this->wdctx_changes ).

loop at it_changes into wa_changes.
  ls_changes
-node_name = wa_changes-node_name.
  ls_changes
-sequence = wa_changes-sequence.
  ls_changes
-node_path = wa_changes-node_path.
  ls_changes
-change_kind = wa_changes-change_kind.
  ls_changes
-element_index = wa_changes-element_index.
  ls_changes
-attribute_name = wa_changes-attribute_name.

 
assign wa_changes-old_value->* to <fs_value>.
  ls_changes
-old_value = <fs_value>.

 
assign wa_changes-new_value->* to <fs_value_new>.
  ls_changes
-new_value = <fs_value_new>.

 
append ls_changes to lt_changes.
 
clear ls_changes.
endloop.

lo_nd_changes
->bind_table( new_items = lt_changes set_initial_elements = abap_true ).

Note: Do not pass any value to the attribute node as it is a reference variable it will cause dump as it of reference variable type.

I have converted the data reference variable old_value and new_value into string and binded to the table.

Save and activate the whole component and test the output.

Output:

 

 

clip_image029

 

Change some values in the table and click on get changes. I am changing the values of the first record.

 

 

clip_image030

 

About Arun

0 Comments:

Post a Comment

Powered by Blogger.