Wednesday, July 31, 2013

Contextual Events - Basic Step by Step : ADF

So contextual events is a powerful event/handler framework which is provided by Oracle ADF. This framework facilitates inter region communication. Contextual Events are used in following type of communication.


  • Region to Page Communication.
  • Page to Region Communication.
  • Region to Region Communication.

We will be implementing Region to Region communication.

Use Case 
We will be creating two regions one page. One region displays an employee form with a button named "fire event". Second region just has an output component. So when first name of an employee is changed and fire button is pressed, the changed value of first name of an employee is shown in second region.This is how first region communicated with second region.

So lets get started


I have a simple ADF Web application in Jdev and have 2 BTF's.
  • without-param-employee-edit-btf ( We call it Task Flow -1)
  • employee-show-detail-btf (We call it Task Flow -2)
Task Flow -1

Task Flow -2


Task flow -1 has one page fragement which has a employee form with navigation buttons.

Task flow - 2 has a simple output value whose value is coming from pageFlowScope as defined in below screenshot.


Now there is a demo page named demo.jspx where both the BTF are dragged as region to create 2 regions on the single page.

When we run the demo page , at this time it shows like below.



As you can see there is a button fire event in the employee form in Task Flow -1.
So now we need to generate/fire an event on clicking of this button. This event will be having a payLoad which will be carrying the value (of Employee First Name) which we want to show in second region(Region of task flow -2). So lets create this event. Select "fire event" button and go down in property inspector and find contextual events tab. Create new one by clicking green button.
Name the event as FireActionEvent and Select Pass Custom Value and select the value which this event will carry as payload. For our use case it is First Name of the employee.


Now we need to create a handler for handling this event carrying payload. This handler needs to be a method which should be exposed in Data Controls. So one can define this method in AMImpl class and expose it as client interface or simple create a java standalone class and include handle method in it. So lets create a class HandleContextualEvent which has a method handleFirstNameEvent(String payLoad).


This method simple gets the reference of pageFlowScope and adds the payLoad value in it with key firstName.

Now to create Data Control out of this class, right click on the class in App Navigator and select Create DC as shown below.

Now we need to add this as method binding in page definition of fragment of task flow -2 like below.


Ok So now Publisher and Consumer both are there in the place. Now we need to configure and map these two.

This will be done in the parent page (demo.jspx) containing both the taskflows as regions. So open demo.jspx and go to bindings and then select contextual event tab. Click on Subscriber and click green button to add a new one.


Now Select the FireActionEvent from the pop up as shown below.



Select the Method handler binding in the Handler field of the Subscriber window as shown below.


Enter the parameter as shown below.


So that's it. You are all set for Contextual Events to work :)

Now run the demo page and here it is.


As shown above, Change the first name (added King to Steven) and click fire event button. Output is below.


You can see that value of first name from region 1 is being propagated to region 2.


 So we achieved communication between two regions using Contextual Events.

Attaching workspace for use. It uses jdev 11gR2.


Hope this helpful.
Happy Learning !!




16 comments:

  1. Hi Rohan,

    I followed all the steps given by you in above post. I hv debug the code Task Flow -1 is working fine but in Task Flow -2 the pageFlowScope.firstName is null.

    Could u plz tell me where is the issue, I am not getting any error log messages.

    Thanks
    nitesh

    ReplyDelete
  2. Hi Nitesh,

    Can you debug the handleFirstNameEvent method and check if payload is having value and its been getting set in pageflowscope?

    Thanks
    Rohan

    ReplyDelete
  3. Hi Rohan,

    Thanks for the reply i have done it .. I can able to get the value in handleFirstNameEvent method.

    ReplyDelete
  4. Hi Nitesh,

    Can you email me ur project (jws) . I will look into it.
    er.walia@gmail.com

    Thanks
    Rohan

    ReplyDelete
  5. Hi Rohan,
    Its working fine now. My mistake instead of creating method action (handleFirstNameEvent) in showEmpDetial.jff i hv created in editEmployee.jsff.

    kindly check the image 11 its wrong. But the given description is correct.
    Thanks
    Nitesh

    ReplyDelete
  6. Ok, good its working for you now.

    Thanks for pointing it out. Will fix it.

    Be in touch
    Thanks
    Rohan

    ReplyDelete
  7. Hi... Thanks a lot for the Blog.. I was able to replicate it with ease.. instead of a button Can i configure the fire event for change of first Name value ?

    ReplyDelete
  8. Hi Rohan, I have a scenario where in I have a BT linked with a fragment (frag1.jsff) and inside this fragment, I have included another page fragment (frag2.jsff) as region. In this case, would it be possible for the outer or parent jsff fragment - frag1.jsff to consume an event generated by a component inside the inner fragment - frag2.jsff ?

    ReplyDelete
  9. Mahesh, How can you include page fragement(frag2.jsff) as a region? you can use btf's as region only. Can you explain me again your use case?

    ReplyDelete
  10. Hi,

    I got a situation as follows:

    I got a three Task Flows created. ParentTaskFlow, ChildTaskFlow1, ChildTaskFlow2
    In the ParentTaskFlow Jsff page I got 2 buttons

    1. BUTToN1_ChildTaskFlow1
    2. BUTToN2_ChildTaskFlow2

    And a ROUTER to Both the child flows

    So when I click on button1 it should flow to ChildTaskFlow1 which is set as 'defult outcome' for router
    and it works fine.

    But when I click on button2 how do i route to ChildTaskFlow2 ?

    Please help me.

    REgards,
    Binu

    ReplyDelete
  11. Thanks Rohan
    Can u plz modify image no 11 as mentioned by nitesh.
    It would be helpful for freshers.

    ReplyDelete
  12. Hello Rohan,
    There is a place where we have used session scope to store one attribute value (due to some constraint we were not having alternate)
    But now we want to correct the code. Could you please suggest a way, how should we get rid off it ?
    Can we use a contextual event for it ?
    If yes then please let us know.

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. Your posts focus on important things.
    Rita

    ReplyDelete
  15. Can you let me know how to handle the contextual events mapping when there is no jspx i.e., when we are creating a jar of the application

    ReplyDelete