Friday, July 31, 2009

Programmatically publishing Contextual Events

ADF task flows provide modularity to the web applications. Bounded task flows are really useful while realizing business use cases, which may repeats across application(s). Please refer Fusion Developer's Guide if you want to know more about this topic. Bounded task flows are added to the page as regions. It is very common that one region may need to communicate with other regions. This is accomplished by Contextual Events. To know more about Contextual Events, please refer Creating Contextual Events.

There are scenarios where task flows may need to raise the Contextual Events based on complex business conditions. Declarative event handling mechanism might not be sufficient in this case. Now the question is how to raise 'Contextual Events' programmatically from backing bean? Let me try summarizing the steps below. Please refer the Fusion Developer's Guide for detailed explanation on creation of Contextual Event.

1. Create the Producer (Payload) Method
2. Bind the Producer Method with task flow, which raises the event
3. Create the Consumer (Handler) Method
4. Map the Contextual Event i.e.: tell the system to use above defined Consumer for a specific event Producer

Once the above infrastructure is in place job is pretty simple. The below code snippet will help you to publish Contextual Event programmatically from the backing (managed) beans. Typically the below code may goes to the action handler methods defined in the backing bean.

DCBindingContainer bc = (DCBindingContainer)BindingContext.getCurrent).getCurrentBindingsEntry();
JUCtrlActionBinding actionBnd =
(JUCtrlActionBinding)bc.getControlBinding("producerMethod");
((DCBindingContainer)bc).getEventDispatcher().queueEvent(actionBnd.getEventProducer(), parameter)
((DCBindingContainer)bc).getEventDispatcher().processContextualEvents();

You can download the sample workspace from here.

13 comments:

Edwin Biemond said...

Hi,

Is this also possible without making a dummy producer method in the pagedef and passing your own payload.

Frank Nimphius said...

Edwin,

you don't need a producer method. You can also create a producer that response to JSF events like ActionEvent or ValueChangeEvent.

The upcoming version of JDeveloper 11g has a PropertyInspector option for contextual events that makes this part simple and easy

Jobinesh said...

Thanks Frank for the comments.

Edwin,
As Frank pointed out, we really don't need producer method, even a simple 'event bidning' can form ContextualEvent.

Anwar said...
This comment has been removed by the author.
Anwar said...

Hi,
Can a table selection listener raise a contextual event ? If it is possible, can that method raise a contextual event programatically ?
thx
-Anwar

Jobinesh said...

Hi Anwar
Yes,You can define contextual event for a table selection change AKAS currency change (I assume you use 11gR1PS1 release) Select the table and define the event.

Anwar said...

Thanks Jobinesh..I am using 11gR1PS1. But when I select the table and click on the add event button, the dialog appears without having the evenType populated by default. I have seen that in your example and the example Frank has on ADF Code, the dialog is populated with event type. Is this something wrong with my installation or a bug ?
Thanks
Anwar

Jobinesh said...

Anwar,
I can see this working in my local copy(11gR1PS1). Not sure why this doesn't work for you. In worse case, you can try key in the eventType="Currency Change Event".
manually

Vinit said...

Hi,
Is there a way to extract the event from the pageDef of the view within a bounded taskflow? I observed this extracts the action bindings from the top level unbounded taskflow. Thanks.

Jobinesh said...

Vinit,
Can you detail the use case ? Are you interested to identify the event source? if yes, take a look at this post http://jobinesh.blogspot.com/2010/09/custom-event-dispatcher-for-contextual.html, May give you some idea on this part.

Hasim said...

Hey Jobinesh,

There is a bug associated with table selection contexual event.

there is a bug 10045872 filed for this. The bug is scheduled for fixing in PS4 (11.1.1.5) I'll work on a manual - though code centric - alternative for the time being and publish it on ADF Code Corner when done.

cong said...

Hi,

How can I do publish contextual events with an action on af:commandToolbarButton? I tried to follow your advice but I couldn't find "contextual events" under "Behavior" for commandToolbarButton.

Thanks,
Xue

Jobinesh said...

Xue,
Its missing currently. However you can either build the binding from the binding editor(click on binding tab) or as a work around select a commandButton, use the editor for creating contextual event and map the same to af:commandToolbarButton