Using ADFUtils/JSFUtils
In this recipe, we will talk about how to incorporate and use the ADFUtils
and JSFUtils
utility classes in your ADF application. These are utility classes used at the ViewController level that encapsulate a number of lower level ADF and JSF calls into higher level methods. Integrating these classes in your ADF application early in the development process, and subsequently using them, will be of great help to you as a developer and contribute to the overall project's clarity and consistency. The ADFUtils
and JSFUtils
utility classes, at the time of writing, are not part of any official JDeveloper release. You will have to locate them, configure them, and expand them as needed in your project.
Getting ready
We will be adding the ADFUtils
and JSFUtils
classes to the SharedComponents
ViewController project that we developed in the Breaking up the application in multiple workspaces recipe in this chapter.
How to do it…
1. To get the latest version of these classes, download and extract the latest version of the Fusion Order Demo application in your PC. This sample application can be found currently in the Fusion Order Demo (FOD) - Sample ADF Application page at the following address: http://www.oracle.com/technetwork/developer-tools/jdev/index-095536.html.
2. The latest version of the Fusion Order Demo application is 11.1.2.1 R2 at the time of this writing and is bundled in a zipped file. So go ahead download and extract the Fusion Order Demo application in your PC.
3. You should be able to locate the
ADFUtils
andJSFUtils
classes in the location where you have extracted the Fusion Order Demo application. If multiple versions of the same class are found, compare them and use the ones that are most up-to-date. For this recipe, we have included in the source code theADFUtils
andJSFUtils
found in theSupplierModule\ViewController\src\oracle\fodemo\supplier\view\utils
directory.4. Copy these classes to a specific location in your shared ViewController components project. For this recipe, we have copied them into the
SharedComponents\SharedViewController\src\com\packt\jdeveloper\cookbook\shared\view\util
directory.5. Once copied, open both files with JDeveloper and change their
package
to reflect their new location, in this case tocom.packt.jdeveloper.cookbook.shared.view.util
.
How it works…
The public interfaces of both ADFUtils
and JSFUtils
define static
methods, so you can call them directly without any class instantiations. The following are some of the methods that are commonly used.
Locating an iterator binding
To locate an iterator in the bindings, use the ADFUtils.findIterator()
method. The method accepts the bound iterator's identifier and returns an oracle.adf.model.binding.DCIteratorBinding
. The following is an example:
DCIteratorBinding it = ADFUtils.findIterator("IteratorID");
Locating an operation binding
To locate an operation in the bindings, use the ADFUtils.findOperation()
method. This method accepts the bound operation's identifier and returns an oracle.binding.OperationBinding
.
OperationBinding oper = ADFUtils.findOperation("OperationID");
Locating an attribute binding
Use ADFUtils.findControlBinding()
to retrieve an attribute from the bindings. This method accepts the bound attribute's identifier and returns an oracle.binding.AttributeBinding
.
AttributeBinding attrib = ADFUtils.findControlBinding("AttributeId");
Getting and setting an attribute binding value
To get or set a bound attribute's value, use the ADFUtils.getBoundAttributeValue()
and ADFUtils.setBoundAttributeValue()
methods respectively. Both of these methods accept the identifier of the attribute binding as an argument. The getBoundAttributeValue()
method returns the bound attribute's data value as a java.lang.Object
. The setBoundAttributeValue()
method accepts a java.lang.Object
and uses it to set the bound attribute's value.
// get some bound attribute data String someData = (String)ADFUtils.getBoundAttributeValue("AttributeId"); // set some bound attribute data ADFUtils.setBoundAttributeValue("AttributeId", someData);
Getting the binding container
You can get the oracle.adf.model.binding.DCBindingContainer
binding container by calling the ADFUtils.getDCBindingContainer()
method.
DCBindingContainer bindings = ADFUtils.getDCBindingContainer();
Adding Faces messages
Use the JSFUtils.addFacesInformationMessage()
and JSFUtils.addFacesErrorMessage()
methods to display Faces information and error messages respectively. These methods accept the message to display as a String
argument.
JSFUtils.addFacesInformationMessage("Information message"); JSFUtils.addFacesErrorMessage ("Error message");
Finding a component in the root view
To locate a UI component in the root view based on the component's identifier, use the JSFUtils.findComponentInRoot()
method. This method returns a javax.faces.component.UIComponent
matching the specified component identifier.
UIComponent component = JSFUtils.findComponentInRoot("ComponentID");
Getting and setting managed bean values
Use the JSFUtils.getManagedBeanValue()
and JSFUtils.setManagedBeanValue()
methods to get and set a managed bean value respectively. These methods both accept the managed bean name. The JSFUtils.getManagedBeanValue()
method returns the managed bean value as a java.lang.Object
. The JSFUtils.setManagedBeanValue()
method accepts a java.lang.Object
and uses it to set the managed bean value.
Object filePath = JSFUtils.getManagedBeanValue ("bindings.FilePath.inputValue"); JSFUtils.setManagedBeanValue("bindings.FilePath.inputValue", null);