Dynamic Context Nodes from BEx Queries – Airline Report

Having a context node reflect the structure and content of a query data makes it easier to display it via webdynpro UI elements. InfoCube query calls are available via RRW3_GET_QUERY_VIEW_DATA function allowing advanced query control via run-time defined parameters. These parameters represent the query variables defined using BEx query designer.

RRW3_GET_QUERY_VIEW_DATA i_t_parameter is a table containing entries of type W3QUERY for defining the query variables. It contains two fields: name and value. To define a variable you need at least two table entries, one for defining the variable name, one for setting its value, something similar to:
VAR_NAME_xx = 'variable_name'.
VAR_VALUE_EXT_xx = 'variable_value'.

For all variable types (single values, interval, etc. ) please check the SAP help files.

RRW3_GET_QUERY_VIEW_DATA returns two tables: e_cell_data and e_axis_data.
e_cell_data table contains entries defining each query axis.  It has two fields:

  • axis: 0 for X axis, 1 for Y axis
  • set: olap tuple table most importantly containing tuple ordinal and name (caption field).

When a query dimension is hierarchical, in set tuple table we get multiple tuples with the same ordinal. To keep things simple we only consider the last defined tuple with a given ordinal and ignore the others. That tuple caption represents the full name of the context node attribute  to be created. In this way, we don’t miss any query data and we get to keep
the node structure as simple as possible.

e_axis_data table contains the actual values for each query cell. The most important fields are value and formatted_value. When displaying query data in tables, formatted_value is preferred because it contains all the formatting we need like currency symbol and numeric conventions. If we need query data to perform further calculations or display business graphics charts, we make use of value field.

Looping through the e_axis_data helps define the node attributes via if_wd_context_node_info, wdr_context_attribute_info objects.
ctx_node_attr_info-name = col_name. "COLxx
ctx_node_attr_info-type_name = 'STRING'.
ctx_node_attr_info-default_value = setx-caption.
ctx_node_info->add_attribute( ctx_node_attr_info ).

Node attributes name follow the “COLxx” naming convention, COL0, COL1 and so on. We can’t use the tuple caption, column full name from query data to define the node attribute because it can contain invalid characters like commas, spaces and so on. Instead we make use of attribute default_value to hold the full column name.

We now have the node structure and we need to populate it from e_cell_data table. Each entry contains one cell value and we can only loop it by rows and columns if we follow the rows/column structure from e_axis_data. For each row we create a new context element. We define its attribute by looping through each found column with a different tuple ordinal. By keeping counters for current cell position and current column position we can set the correct context attribute with the correct cell value. Before moving to the next available e_axis_data we bind the current context element to the context node.

"populate node with context elements, loop through each query report cell data
i_index = 0.
"loop through rows
LOOP AT gs_axis_y-set into sety.
current_tuple-tuple_ordinal = -1.
"create new node context element
ctx_element = ch_ctx_node->create_element( ).
"add 1st attribute COL0 as row name from bex query from sety tuple
col_name = 'COL0'.
"for row DATE types, captions are empty, need to use chavl, field for user defied values
if sety-caption = ''.
ctx_element->set_attribute( name = col_name value = sety-chavl_ext ).
ctx_element->set_attribute( name = col_name value = sety-caption ).
"loop through columns with different tuple ordinal = context element attributes
LOOP AT gs_axis_x-set into setx.
"only attempt to read cell data when we're not on the same tuple ordinal
if current_tuple-tuple_ordinal = setx-tuple_ordinal.
"update cell index
add 1 to i_index.
"read cell data
READ TABLE im_cell_data into current_cell INDEX i_index.
IF sy-subrc = 0.
n_index = setx-tuple_ordinal + 1.
concatenate 'COL' n_index into col_name.
"update context element attribute
if IM_FORMATTED_VALUES = abap_true.
ctx_element->set_attribute( name = col_name value = current_cell-formatted_value ).
ctx_element->set_attribute( name = col_name value = current_cell-value ).
"update current tuple
current_tuple = setx.
"add new context element
ch_ctx_node->bind_element( new_item = ctx_element set_initial_elements = abap_false ).

The end result is a node with the same structure and data as the query result data.

This post is part of Airline Report.

Leave a Reply

Your email address will not be published. Required fields are marked *