Start of Content Area

Background documentation Calculated Context Attribute  Locate the document in its SAP Library structure

The values of context attributes are normally saved in node elements of a controller context at runtime, and with data binding are displayed in the UI using a generic formatting mechanism (for Dictionary-based types).

In many cases, saved context data should be transferred identically, instead it should be converted before the display. A typical example is displaying an address text based on name and gender. Using the two context attributes Name=’Maier’ and Gender=’male’, the standardized address text 'Dear Mr. Maier! should be displayed in the user interface. This text can be saved by defining an additional value attribute Address in the controller context. In multiple context nodes, each individual node element would then have the additional context attribute called Address, where the name would be saved redundantly as part of the address.

Calculated context attributes are a more elegant way of handling the example. These are not saved specially in the context, they are calculated (read) where necessary by the controller instead. Where necessary can mean that UI elements should display the content of a calculated attribute, or that the value is accessed by the program in the controller coding. The calculation method is called automatically by the Web Dynpro runtime environment. A calculated context attribute is calculated in an automatically generated access method using signature

<CalcAttrType> get<NodeName><CalcAttrName>(<NodeName>Element element).

For a calculated context attribute called FullNameCalc of type String in node UserData in the context of the FormView view, this results in access method

java.lang.String getUserDataFullNameCalc(

IPrivateFormView.IUserDataElement element).

The calculation method is called automatically where necessary by the Web Dynpro runtime environment. It passes a reference to the node element to which the calculated context attribute refers. If this method were defined in a multiple node with cardinality 0..n, for example, then the access method would be called for each node element that is available in the node at runtime. In this way, the full name of a customer could be displayed in a third table column, consisting of first and last names. The access method would be called for each row (each node element) in the table.

It is expected that the access method for calculating a calculated context attribute is based on the same node element’s other attributes.

This graphic is explained in the accompanying text

Within the generated access method for a context attribute, the context attribute to be calculated should not be accessed, as this results in the access method being called recursively. A recursion termination occurs only if you implement a recursion condition.

This graphic is explained in the accompanying text

According to the Web Dynpro naming convention, calculated context attributes should contain the ending Calc for Calculated in the name, such as SalutationCalc or TotalPriceCalc. This makes the application coding easier to read with respect to the components from the calculated context attributes.

 

This graphic is explained in the accompanying text

In the access method, only writable calculated context attributes usually access context attributes in the selected node element. A reference to this node element is passed by the Web Dynpro runtime environment using the element method parameter. The access method returns the result of the calculation as a value of the calculated context attribute.

 

Definition

The following properties are used to define calculated context attributes in the Web Dynpro tools:

·        calculated = true: Calculated context attribute. The default setting is false.

·        readOnly = true: Readable calculated context attribute. The value of the calculated context attribute is only displayed and cannot be changed by the user. Only one access method is generated to calculate the context attribute.

·        readOnly = false: Read- and writable calculated context attribute. The value of a calculated context attribute can be changed by the user. In addition to the access method, a void set<Name>(element, value) method is also generated in which the calculation of the calculated attribute can be reversed. Write access is also possible to the context attribute that is involved in calculating a calculated context attribute.

·        calculatedAttributeGetter: Name of the generated read method for the calculated context attribute (cannot be edited). The name is taken from the node name (except for top-level attributes) and the attribute name.

·        calculatedAttributeSetter: Name of the generated write method (cannot be edited).

Usually, calculated context attributes are only read, that is, the user cannot change their value. In most cases, calculated context attributes of type readOnly = true are sufficient, that is, those that have only read and not write access to other context attributes. Interface elements that are bound to readable calculated context attributes are displayed automatically in readable format only.

Example

This graphic is explained in the accompanying text

In the user interface (in the FormView view), an address should be displayed based on a person’s last name and gender. A calculated context attribute is suitable for solving this task.

  1. At design time, two context attributes Name and Gender are defined in the view controller context. The address text should be calculated at runtime using the definition of a calculated context attribute called TitleCalc. As the TitleCalc address text cannot be edited, its property readOnly is given the value true.
  2. At runtime the controller context only saves the two attributes Name and Gender. The calculated context attribute is not saved in the node element, but is calculated using method call.
  3. A new method for calculating the calculated context attribute TitleCalc is added to the controller class:
    String getTitleCalc( IPrivateFormView.IContextElement element)
    The method returns the address text as a concatenation of string values. It accesses the Name and Gender context attributes saved in the node element with the element reference.
  4. Before the rendering step, the Web Dynpro runtime environment performs type-based formatting of the context data for display in the user interface.
  5. The value of the calculated context attribute TitleCalc is displayed in the user interface in an interface element of type IWDTextView.

 

Writable Calculated Context Attributes

If the readOnly property of a calculated context attribute has the value false, a mutator method with signature

void set<NodeNameAttributeName>(<NodeName>element, <CalcAttrClass>value)is also generated in the controller class. This method is only called by the Web Dynpro runtime environment if the value of the writable calculated context attribute was changed in the user interface before an action was triggered. It is assumed that the method changes context attributes of the same node element (element). Only changing values of other context attributes leads to the user interface being adjusted.

In practice, most calculated context attributes are of type readOnly = true for the following reasons:

·        The mutator method has to parse and also validate the user input. For example, both the name and the gender would have to be determined from the address text ‘Dear Mr. Maier!’. The effort required to implement the mutator method correctly can easily outweigh the benefits that result from using a writable calculated context attribute.

·        If the calculation process of a calculated context attribute cannot be reversed, the mutator method cannot be implemented uniquely.

¡        As an example, imagine a small form for calculating the total price of two products, Number1 and Number2. The result should be displayed and editable using a calculated context attribute.  Although the access method is easy to implement (return element.getNumber1() + element.getNumber2()), implementing the mutator method is ambiguous. If the user enters the number 10 in the editable result field, then several number pairs of numbers Number1 and Number2 (10+0, 5+5, 2+3, and so on) could result in the same total of 10.  

This graphic is explained in the accompanying text

Within the mutator method for a calculated context attribute, the context attribute to be calculated should not be overwritten when method I<NodeName>.set<CalcAttrName>(value) is called, as this results in a recursive call of the mutator method. There would never be a recursion termination if a recursion condition was not implemented.

 

This graphic is explained in the accompanying text

For writable calculated context attributes, a mutator method is also created in the controller class in which the calculation can be reversed. This means that the context attributes on which the calculation is based have to be set in such a way that another call of the access method would also return the value value. In some circumstances, this condition cannot be fulfilled, however.

 

Exceptions and Calculated Context Attributes

The access and mutator methods of calculated context attributes should not throw exceptions nor execute actions that can lead to exceptions being thrown by the Web Dynpro runtime environment itself. If exceptions are thrown in the methods that are generated for calculated context attributes, the underlying Web Dynpro application is automatically ended by the Web Dynpro runtime environment.

If it is necessary to display a writable calculated context attribute in the interface, an additional context node for saving error messages should be defined with corresponding attributes. The mutator method should then save the results of the input validation in this context attribute.

Use of Calculated Context Attributes

The following examples should be given for using calculated context attributes:

  1. Price calculation: The total price of a shopping cart is displayed using the binding to a readable calculated context attribute. The generated access method returns the total of the articles contained in the shipping cart.
  2. Text concatenation: Text concatenations that values of context attributes are based on, can be implemented using calculated context attributes.
  3. Application-specific validations: You can use the setter and getter methods that are generated for writable context attributes to format as well as parse and validate data application-specific. You can check the user input in the mutator method and save it in a separate, non-calculated context attribute. An error message is displayed if this is not successful. If you want to display the input value in the case of errors as well, then first you have to save the incorrect value in an additional usual context attribute, from which the access method of the calculated attribute is then supplied.
  4. Immediate display of the display text for the EVS list of values: After a key/display text pair has been selected for extended input help, the Web Dynpro runtime environment saves the selected key value in the corresponding context attribute. This takes place within a server round trip, during which no application coding is processed by default. How can the display text belonging to the selected key now be displayed immediately? The solution to the problem is the definition of an additional calculated context attribute DisplayTextCalc in the same context node, to which a TextView interface element is bound. The access method is called in the same server round trip in which the selected key is stored in the context.  This takes place because the value of a context attribute (key) in the same node element changed.

  

  

 

End of Content Area