Wednesday, September 28, 2011

Get value from task flow using Contextual Event


Often a page or a region within a page needs information from somewhere else on the page or from a different region. Among the options for passing information to and from regions, Contextual Events is the most powerful and the option that does not make regions refresh or require the referenced bounded task flow to restart.

Contextual events have two parts:
  • A publisher (or producer), such as a button that raises a named event, with or without a custom payload.
  • A handler (or consumer) that listens for a specifically named event or a wildcard event, to process that event.
Create a Contextual Event:

  • Create a bounded task flow with page fragment and create a view inside it. Create a view object and place it as a tree table or tree in page.
  • To create a contextual event just select the tree and go to its properties. There go under the option publish event and click on add button to create a contextual event. Select create new event and press ok, this will create a contextual event. Here event type will be currency change event.
Get Contextual Event:
  • Create a adf jar file of this application. Add this library to a new application where you will use the task flow as a region.
  • Create a page and put a input text on it, give its value from a bean class.
public class NewBean {
    private RichOutputText newtext;
    String MyValue=null;

    public NewBean() {
    }

    public void setNewtext(RichOutputText newtext) {
        this.newtext = newtext;
    }

    public RichOutputText getNewtext() {
        return newtext;
    }

    public void setMyValue(String MyValue) {
        this.MyValue = MyValue;
    }

    public String getMyValue() {
        return MyValue;
    }
}
  • Now create a event handler class which will act as a consumer of event generated by a producer. In it create a function which will get the value from producer event. This function will take a parameter of currency change event type which is produce by the tree node selection event.
public class EventHandler  {
    public EventHandler() {
        super();
    }
   
    public void handleEventStringPayload(DCBindingContainerCurrencyChangeEvent customPayLoad) {

        this.setNewValue(customPayLoad.getSource().toString());
        FacesContext fctx = FacesContext.getCurrentInstance();
        ELContext elctx = fctx.getELContext();
        ExpressionFactory exprFactory = fctx.getApplication().getExpressionFactory();
        ValueExpression ve = exprFactory.createValueExpression(elctx, "#{NewBean}", Object.class);
        NewBean pageBean = (NewBean)ve.getValue(elctx);
        pageBean.setMyValue(customPayLoad.getSource().toString());
        AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance();
        adfFacesContext.getCurrentInstance().addPartialTarget(pageBean.getNewtext());

    }
  • To make this function available at the binding level, create this java class' data control. To do this right click on this java class and go to create data control. Select all 3 option if you are using release 2 of jdeveloper.
  • Now go to page , drag and drop task flow from library and make it a region or dynamic region. By this event will also be available in this page. Go to binding section of page and add new method binding to it. Add method from data control to it. By this, method can be accessed from this page.
  • Again go to binding tab of page and open contextual event tab. In this tab go to subscriber tab and click on add button. This will pop up a window. Click on the search button it will show available events in this page. Select previously defined event in task flow's page.
  • select the publisher or keep it any. Now go to handler option and click search button it will show available functions in bindings. Select the function created for event handling. Go to last part of subscriber parameter. Give parameter name same given in event handler function. Its value will be ${payLoad}.
  • Now run application it will show selected tree value in output box which is outside the region.