A The FacesUtils Class

The FacesUtils class is used in the customization use cases shown in "Using Managed Beans". This class contains various helper methods for re-rendering components, evaluating EL expressions, and accessing attributes through binding.

Example A-1 provides the code snippet of the FacesUtils class with implementation of some of the methods:

Note:

If you add the code from Example A-1 in any ViewControllerProject source code, then some packages, such as the following, might not be found:
import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCControlBinding;
import oracle.binding.AttributeBinding;
import oracle.binding.ControlBinding;

You must add ADF Model Runtime to the class path to resolve the errors related to importing these packages. To add ADF Model Runtime to project class path:

  1. Right-click the project, and select Project Properties.

  2. On the left navigation bar, select Libraries and Classpath.

  3. Click Add Library.

  4. Under Extension, select ADF Model Runtime.

  5. Click OK.

  6. Click OK.

Example A-1 Sample FacesUtils Class

package oracle.iam.ui.sample.common.view.utils;
 
import java.io.IOException;
 
import java.util.Map;
import java.util.ResourceBundle;
 
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
import javax.el.ValueExpression;
 
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
 
import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCControlBinding;
import oracle.adf.view.rich.context.AdfFacesContext;
 
import oracle.binding.AttributeBinding;
import oracle.binding.ControlBinding;
 
import oracle.iam.ui.platform.utils.TaskFlowUtils;
 
import oracle.jbo.uicli.binding.JUCtrlActionBinding;
import oracle.jbo.uicli.binding.JUCtrlListBinding;
import oracle.jbo.uicli.binding.JUEventBinding;
 
 
public class FacesUtils {
 
    private FacesUtils() {
        // do not instantiate
        throw new AssertionError();
    }
 
    /*
     * Re-render the component.
     */
    public static void partialRender(UIComponent component) {
        if (component != null) {
            AdfFacesContext.getCurrentInstance().addPartialTarget(component);
        }
    }
 
    /*
     * Sets attribute value through attribute binding.
     */
    public static void setAttributeBindingValue(String attributeName,
                                                Object value) {
        AttributeBinding binding = getAttributeBinding(attributeName);
        if (binding != null) {
            binding.setInputValue(value);
        } else {
            throw new IllegalArgumentException("Binding " + attributeName +
                                               " does not exist.");
        }
    }
 
    /*
     * Gets attribute value using attribute binding.
     */
    public static <T> T getAttributeBindingValue(String attributeName,
                                                 Class<T> clazz) {
        AttributeBinding binding = getAttributeBinding(attributeName);
        if (binding != null) {
            return (T)binding.getInputValue();
        } else {
            throw new IllegalArgumentException("Binding " + attributeName +
                                               " does not exist.");
        }
    }
 
    /*
     * Gets attribute value using list binding.
     */
    public static <T> T getListBindingValue(String attributeName,
                                            Class<T> clazz) {
        ControlBinding ctrlBinding = getControlBinding(attributeName);
        if (ctrlBinding instanceof JUCtrlListBinding) {
            JUCtrlListBinding listBinding = (JUCtrlListBinding)ctrlBinding;
            return (T)listBinding.getAttributeValue();
        } else {
            throw new IllegalArgumentException("Binding " + attributeName +
                                               " is not list binding.");
        }
    }
 
    public static ControlBinding getControlBinding(String name) {
        ControlBinding crtlBinding = getBindings().getControlBinding(name);
        if (crtlBinding == null) {
            throw new IllegalArgumentException("Control Binding '" + name +
                                               "' not found");
        }
        return crtlBinding;
    }
 
    public static AttributeBinding getAttributeBinding(String name) {
        ControlBinding ctrlBinding = getControlBinding(name);
        AttributeBinding attributeBinding = null;
        if (ctrlBinding != null) {
            if (ctrlBinding instanceof AttributeBinding) {
                attributeBinding = (AttributeBinding)ctrlBinding;
            }
        }
        return attributeBinding;
    }
 
    public static DCBindingContainer getBindings() {
        FacesContext fc = FacesContext.getCurrentInstance();
        ExpressionFactory exprfactory =
            fc.getApplication().getExpressionFactory();
        ELContext elctx = fc.getELContext();
 
        ValueExpression valueExpression =
            exprfactory.createValueExpression(elctx, "#{bindings}",
                                              Object.class);
 
        DCBindingContainer dcbinding =
            (DCBindingContainer)valueExpression.getValue(elctx);
 
        return dcbinding;
    }
 
    /*
     * Evaluates EL expression and returns value.
     */
    public static <T> T getValueFromELExpression(String expression,
                                                 Class<T> clazz) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application app = facesContext.getApplication();
        ExpressionFactory elFactory = app.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        ValueExpression valueExp =
            elFactory.createValueExpression(elContext, expression, clazz);
        return (T)valueExp.getValue(elContext);
    }
 
    /*
     * Gets MethodExpression based on the EL expression. MethodExpression can then be used to invoke the method.
     */
    public static MethodExpression getMethodExpressionFromEL(String expression,
                                                             Class<?> returnType,
                                                             Class[] paramTypes) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application app = facesContext.getApplication();
        ExpressionFactory elFactory = app.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        MethodExpression methodExp =
            elFactory.createMethodExpression(elContext, expression, returnType,
                                             paramTypes);
        return methodExp;
    }
 
    public static ELContext getELContext() {
        return FacesContext.getCurrentInstance().getELContext();
    }
 
    /*
     * Shows FacesMessage. The message will not be bound to any component.
     */
    public static void showFacesMessage(FacesMessage fm) {
        FacesContext.getCurrentInstance().addMessage(null, fm);
    }
 
    /*
     * Launch bounded taskFlow based on provided parameters.
     */
    public static void launchTaskFlow(String id, String taskFlowId,
                                      String name, String icon,
                                      String description, String helpTopicId,
                                      boolean inDialog,
                                      Map<String, Object> params) {
        // create JSON payload for the contextual event
        String jsonPayLoad =
            TaskFlowUtils.createContextualEventPayLoad(id, taskFlowId,
                                                       name, icon, description,
                                                       helpTopicId, inDialog,
                                                       params);
        
        // create and enqueue contextual event
        DCBindingContainer bc =
            (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
        DCControlBinding ctrlBinding = bc.findCtrlBinding(TaskFlowUtils.RAISE_TASK_FLOW_LAUNCH_EVENT);        
        // support both bindings - using eventBinding as well as methodAction
        if (ctrlBinding instanceof JUEventBinding) {
            JUEventBinding eventProducer = (JUEventBinding) ctrlBinding;
            bc.getEventDispatcher().queueEvent(eventProducer, jsonPayLoad);
        } else if (ctrlBinding instanceof JUCtrlActionBinding) {
            JUCtrlActionBinding actionBinding = (JUCtrlActionBinding) ctrlBinding;
            bc.getEventDispatcher().queueEvent(actionBinding.getEventProducer(), jsonPayLoad);
        } else {
            throw new IllegalArgumentException("Incorrect binding for " + TaskFlowUtils.RAISE_TASK_FLOW_LAUNCH_EVENT);
        }
        bc.getEventDispatcher().processContextualEvents();
    }
 
    /*
     * Redirect to a provided url.
     */
    public static void redirect(String url) {
        try {
            FacesContext fctx = FacesContext.getCurrentInstance();
            fctx.getExternalContext().redirect(url);
            fctx.responseComplete();
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }
    
}