!--a11y-->
Dynamically Building the Appropriate
Context
The input fields defined in the class SomeBackEnd must be mapped to a context attribute for each user input.
For dynamic creation of the context belonging to the input fields, the standard method wdDoInit() of the (View) Controller is suitable since this method is executed as soon as the controller is instantiated. Another advantage of the method wdDoInit() is that it is executed only once during the life cycle of the view.
The metadata information of the controller context is represented at runtime in the objects of the types IWDNodeInfo and IWDAttributeInfo. Using the program code wdContext.getNodeInfo(), you can access – for example – the metadata information of the root node available in each context.
So that value attributes and value nodes can be dynamically created in the controller context, the interface IWDNodeInfo provides the following methods:
· addAttribute( java.lang.String name, java.lang.String dataType)
· addAttribute( java.lang.String name,
· com.sap.dictionary.runtime.IDataType dataType)
· addChild( java.lang.String name,
java.lang.Class elementClass,
boolean singleton,
boolean mandatory,
boolean multiple,
boolean mandatorySelection,
boolean multipleSelection,
boolean initializeLeadSelection,
java.lang.String dataType,
IWDNodeCollectionSupplier supplier,
IWDNodeCollectionDisposer disposer)
After dynamic
enhancement of the context structure, the added context elements can be
accessed using the generic methods IWDNode.getChildNode(String,int) IWDNodeElement.getAttributeValue(String) und
IWDNodeElement.setAttributeValue(String,Object).
...
1. To dynamically add a new value node at the uppermost level in the context of the DynamicView view, enter the following program code in the method wdDoInit():
wdDoInit() |
public void wdDoInit() { //@@begin wdDoInit()
//Dynamically create a context node IWDNodeInfo node = wdContext.getNodeInfo().addChild( "DynamicNode", null, true, true, false, false, false, true, null, null, null);
//@@end } |

You could declare this context node at design time.
2. To dynamically add the value attributes belonging to the form input fields to the DynamicNode node just created, add the following program code to the method wdDoInit():
wdDoInit() |
public void wdDoInit() { //@@begin wdDoInit()
//Dynamically create a context node IWDNodeInfo node = wdContext.getNodeInfo().addChild( "DynamicNode", null, true, true, false, false, false, true, null, null, null);
// “Create“ backend connection to SomeBackEnd.java SomeBackEnd theBackend = new SomeBackEnd();
// loop through all fields of the Object[] fields containing the // FieldDescriptors for (int index=0; index < theBackend.getNumberFields(); index++) { // ... and simply create the required context attribute SomeBackEnd.FieldDescriptor fld = theBackend.getField(index); node.addAttribute(fld.getName(),fld.getType()); } //@@end } |

The class SomeBackEnd.java is in the project template under TutWD_DynamicàsrcàpackagesàcomàsapàtutàwdàDynamic and contains the example data for the form input fields.
You have dynamically set up the context for the form.
