!--a11y-->
Initializing the Controller Context of a
View 
Initializing the declared controller context of the view is the crux of this example application, in a technical programming sense.
In this section, you will learn how to fill single value nodes with associated node elements at runtime using generated context interface methods, to display these using data binding in the two tables CustomerTable and OrderTable. The master table CustomerTable will then look like this in the Web browser:

In the above graphic, the lead selection of the first node element of the value node Customer is indicated by the fact that the first row in the table is highlighted.

The graphic shows which context data structure must be available for filling the Customer table using data binding.
As you can see, several address data records (node element of type Address) must be instantiated at the same time, for you to be able to display them in all rows of the Customer table (and thus separately for each customer). This is why the value node Address is declared as a non-singleton node.
The value node Customer is filled step-by-step:
· Instantiating node elements: Repeated instantiation of a node element of the type Customer.
· Defining value attributes: Assigning values for the value attributes declared in the node element of the type Customer – in this case for the Name attribute.
· Inserting node elements: Inserting the node elements of the type Customer in the value node Customer. “Inserting” means adding the elements to the list of node elements that have been aggregated from the value node Customer.
A similar procedure is then carried out with each node element of the type Address. Each of these elements is inserted in the appropriated Address value node, which has been instantiated separately for each node element.

Note that each individual node element of the type Customer has its own Address value node instance only because the value node Address was declared as a non-singleton node at design time. Otherwise, you would only be able to access directly the one instance of Address associated with the current lead selection of the Customer value node, in the controller context, by calling the method wdContext.nodeAddress().
...
...
1. Now implement the wdDoInit()method of the Work view controller. When doing so, make sure you add the import statements and the declare the instance variable someBOL.
Implementation of the wdDoInit()method in the Work view controller |
... //@@begin imports import java.util.*; import com.sap.tc.webdynpro.tutorials.masterdetail.bol.SomeBOL; import com.sap.tc.webdynpro.tutorials.masterdetail.wdp.IPrivateWork; //@@end ... /** Hook method called to initialize controller. */ public void wdDoInit() { //@@begin wdDoInit() SomeBOL.Customer customer; SomeBOL.Address address; IPrivateWork.ICustomerElement newCustomerNodeElement; IPrivateWork.IAddressElement newAddressNodeElement;
someBOL.initialize(); Collection customers = someBOL.getCustomers();
// ======== Populate context =================== for (Iterator iter = customers.iterator(); iter.hasNext();){ customer = (SomeBOL.Customer)iter.next(); address = customer.getAddress(); // Instantiate new value node element of type Customer and define attributes newCustomerNodeElement = wdContext.createCustomerElement(); newCustomerNodeElement.setName(customer.getName()); // The creation of a new inner node element instance requires // the existance of a parent node element already bound to the // parent node. So call method bind() (or alternatively addElement()) before. wdContext.nodeCustomer().addElement(newCustomerNodeElement); // Instantiate new value node element of type Address and define attributes newAddressNodeElement = wdContext.createAddressElement(); newAddressNodeElement.setCity(address.getCity()); newAddressNodeElement.setCountry(address.getCountry()); newAddressNodeElement.setHouseNo(address.getHouseNo()); newAddressNodeElement.setPostalCode(address.getPostalCode()); newAddressNodeElement.setStreet(address.getStreet()); // the dependent context value node Address can only be referenced here for // each context value node element of type customer separately because // we defined it to be a non-singleton node. newCustomerNodeElement.nodeAddress().bind(newAddressNodeElement); } //@@end } ... /* * The following coding section can be used for any Java coding that has * not to be visible to other controllers/views or that contains constructs * currently not directly supported by Web Dynpro (such as inner classes or * member variables, and so on). </p> * * Note: The content of this segment is not managed/controlled in any way * either by the Web Dynpro design time nor the Web Dynpro runtime. */ //@@begin others public SomeBOL someBOL = new SomeBOL(); //@@end ... |

Note that, if you call the method bind(newCustomerNodeElement) instead of addElement(newCustomerNodeElement), only the last bound node element of the type Customer is contained in the list of node elements. That is, wdContext.nodeCustomer().bind(newCustomerNodeElement) overwrites the list of node elements of the type Customer.
Once the Master/Detail Viewer has been generated again and deployed in the Web browser again, it looks like this:

The (upper) Customer table is filled correctly by means of the data binding (already declared) between the UI elements associated with the table and the context elements. Conversely, the Order table remains empty. How can we display the order records, for the selected customer, in the Detail table (that is, the Orders for Customers table)?
To
answer this question, refer to the section Implementation of a
Supply Function!
