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.
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.
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. |
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.
|
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.
|
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.
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.
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. |
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.
The following examples should be given for using calculated context attributes: