Oracle ADF Code Corner: How-to declaratively create new table rows based on existing row content
How-to declaratively create new table rows based on existing row content
A frequent requirement posted on the Oracle JDeveloper
forum on OTN is to create new rows in a table based on a copy
of an existing row. Using the new CreateWithParams operation exposed
on the ADF Business Components ViewObject this task becomes fully
declarative in Oracle JDeveloper 11. This article provides instructions
on how to achieve the goal.
Written by Frank Nimphius,
Oracle Corporation
20-Nov-2008
Introduction
There are many ways to create a new row in an iterator and provision it with
the values of an existing row. Its just a matter of what your developer background
is and how much you like coding in Java. For those that have a 4GL background,
everything that is declarative end-to-end seems to be preferred. Lucky enough,
Oracle ADF and ADF Business Components greatly simplify web application development
in Java EE and this also includes the usecase mentioned above.
How-to
This how-to starts from an existing ADF Faces table, which is created by dragging
a ViewObject from the data control palette to the JSF page. The surrounding
af:panelCollection component
has a toolbar facet to which a a single toolbar button is added to initiate
the copy. The button ID is referenced in the table's "partialTrigger"
property so that clicking the button initiates a table refresh.
To assign the "Create with parameters" operation to the button, select
the "Create with parameters" operation under the ViewObject node and
drag it over the toolbar button
In the component rebind dialog, check the "keep current value" checkbox
of the "text" property and press Ok. This adds an
EL expression to the button that references the CreateWithParams operation binding
in he page's pagedef file.
To navigate to the page definition file, for further editing, select the toolbar
button and choose "Go to Page definition" from the context menu.
The pageDef file has an new entry "CreateWithParams"
under its bindings node to create a new row. However, it does
not yet know which attributes to provision with data when getting invoked. To
address this, select Insert inside CreateWithParams from the
context menu and then click the NamedData option.
Create a named data item for all attributes that you want to provision data
for. In this example, data should be provisioned for DepartmentName, ManagerId
and LocationId. The data should be copied from the current selected row in the
table. A nice trick in ADF is to create attribute bindings for the attributes
that you need to copy the value from. In the image below, you see attribute
bindings created for DepartmentName, LocationId and ManagerId. Because ADF synchronizes
the selected table row in the UI with the information held in the iterator,
the attribute bindings always contain the attribute values of the current selected
row, making them EL accessible.
The row attribute values can be copied from the attribute binding to the NDValue
property of the NamedData item using ExpressionLanguage. In
the example below, the NamedData item has a name of LocationId,
so the LocationId attribute of the new row gets provisioned, aNDType
of oracle.jbo.domain.Number,
which matches the attribute type of the underlying EntityObject and a NDValue
of #{bindings.LocationId.inputValue} that is referencing the current row's LocationId
attribute, exposed by the attribute binding.
At runtime, you can now select a table row and press the "Copy Row"
button. The button invokes the "CreateWithParams" operation and passes
the initial values for the DepartmentName, ManagerId and the LocationId as a
copy of the current selected row attributes.