Thursday, October 31, 2013

Handling exceptions in the ADF taskflow

-         Through exception handling we can designate an Activity as exception handler
-          For the flow from the exception handling activity uses the standard exception handling activity
-          You can use router as exception handling activity and reaching the exception from here it will can route to various control flow cases based on exception.
-          A task flow can have only one exception handling activity
-          If no exception handling mechanism provided the exception propagated to the top level tf till the webcontainer
-          ADF Model also provide exception handling mechanism. An exception thrown by any type of binding is caught by the ADF Model which calls the reportException() stores the exception in the binding container

-          Later page renders error displays

Create a task flow below 


For the testMeth1 activity attch the method as 

#{pageFlowScope.handleException.exception}

View1.jspx

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
  <af:panelGroupLayout id="pgl1">
    <af:button text="button 1" id="b1" action="toMeth"/>
  </af:panelGroupLayout>
</jsp:root>
testMeth1
#{pageFlowScope.handleException.exception}

The managed bean handleException

package view.test;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import oracle.jbo.JboException;

public class TestException {
    public TestException() {
        super();
    }

    public void exception() {
        throw new JboException("test here");
    }

    public void exceptionMessage() {
        System.out.println("Here");
        try {
            System.out.println("Inside Handeler");
            FacesMessage message = new FacesMessage("This is custom Message for Jbo Exception-Exception Handeler");
            message.setSeverity(FacesMessage.SEVERITY_WARN);
            FacesContext fc = FacesContext.getCurrentInstance();
            fc.addMessage(null, message);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void badCall() {
          
       }

    public String badActionHere() {
System.out.println("Here...");
        return null;
    }
}

Create a page to test by drag and drop the page as region and run.Click on the button and you see the custom exception message.



Reenterng Bounded Task Flow

 task-flow-reentry  Option is used to handle cases with user re-enter to the task flow using back button.Task flow uses re-entry not allowed or reentry-outcome-dependent options for task-flow-re-entry.For reentry settings , redirect activity with in task flow should set true.



  • reentry-allowed - Reentry allowed on any view activity within bounded task flow 
  • reentry-not-allowed - User can go back to the screen but clicking on any button with in the view throws InvalidTaskFlowEntry exception 
  • reeenty-outcone-dependent - User can navigate to the a task flow using browser button solely on how user originally exited the tf.

 
reeentry-not-allowed 

Create a taskflow 
Reentry_view1.jspx
<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:f="http://java.sun.com/jsf/core"
          xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
  <!--<jsp:directive.page contentType="text/html;charset=UTF-8" />
-->
  <jsp:directive.page contentType="text/html;charset=UTF-8"/>
  <f:view>
    <af:document id="d1">
      <af:form id="f1">
        <af:panelGroupLayout id="pgl1">
          <af:button text="button 1" id="b1" action="toRet"/>
        </af:panelGroupLayout>
      </af:form>
    </af:document>
  </f:view>
</jsp:root>

Not that Redirect property=true for this view.
Create the main page in the adfc-config.xml 

Main.jspx
<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:f="http://java.sun.com/jsf/core"
          xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
    <jsp:directive.page contentType="text/html;charset=UTF-8"/>
    <f:view>
        <af:document title="main.jspx" id="d1">
            <af:form id="f1">
                <af:panelGroupLayout id="pgl1">
                    <af:button text="button 1" id="b1" action="toTF"/>
                </af:panelGroupLayout>
            </af:form>
        </af:document>
    </f:view>
</jsp:root>

Run the main.jspx . Navigate the UIs and try the browser back button
<Oct 6, 2013 6:08:42 PM IST> <Error> <oracle.adfinternal.view.faces.context.RichExceptionHandler> <BEA-000000> <ADF_FACES-30200:For more information, please see the server's error log for an entry beginning with: The UIViewRoot is null. Fatal exception during PhaseId: RESTORE_VIEW 1.
oracle.adf.controller.InvalidTaskFlowReentryException: ADFC-06016: Task flow reentry is not allowed: '/WEB-INF/tfReentry.xml#tfReentry'.
            at oracle.adfinternal.controller.state.ControllerState.reentryNotAllowed(ControllerState.java:1019)
            at oracle.adfinternal.controller.state.ControllerState.reenterTaskFlow(ControllerState.java:992)
            at oracle.adfinternal.controller.state.ControllerState.recoverState(ControllerState.java:925)
            at oracle.adfinternal.controller.state.ControllerState.synchronizeStatePart1(ControllerState.java:369)
            at oracle.adfinternal.controller.application.SyncNavigationStateListener.beforePhase(SyncNavigationStateListener.java:263)
            at oracle.adfinternal.controller.lifecycle.ADFLifecycleImpl$PagePhaseListenerWrapper.beforePhase(ADFLifecycleImpl.java:557)
            at oracle.adfinternal.controller.lifecycle.LifecycleImpl.internalDispatchBeforeEvent(LifecycleImpl.java:100)
            at oracle.adfinternal.controller.lifecycle.LifecycleImpl.dispatchBeforePagePhaseEvent(LifecycleImpl.java:147)
            at oracle.adfinternal.controller.faces.lifecycle.ADFPhaseListener$PhaseInvokerImpl.dispatchBeforePagePhaseEvent(ADFPhaseListener.java:290)
            at oracle.adfinternal.controller.faces.lifecycle.ADFPhaseListener.beforePhase(ADFPhaseListener.java:76)

reentry-outcome-dependent
Change the re-entry option for the tf return based on the task flow activity outcome the re-entry option changes.
Managed bean values upon taskf flow reentry
Value of managed bean reset to the value of the managed bean before the end user existed the btf.Value of btf before the re-entry is lost .To change this set the re-direct property of the tf.

Thursday, October 3, 2013

ADF TaskFlow Transaction Basics

Transaction is a collection of works either committed or rollback.Create a calling and called task flow to go through some of the transaction basics of task flow

Calling Task flow :


Called Task flow : 


From the behavior section of the called task flow you can see the following options


  •  No Controller transaction is the default option means the called task flow does not participate any transaction management.
  • Always begin New Transaction - Transaction is created when enter to the task flow and completes the task flow when exists the task flow 
  • Always Use Existing transaction - Called task flow participates the existing transaction 
  • User Existing transaction if possible - Participate the existing transaction if available or start a new transaction if not available 
  • restore-save-option


Use restore-save-option =true if you want to discard the changes the end user makes with in a called bounded task flow when the called bounded task flow exits.This option apply only if the bounded task flow is entered by joining the existing transaction and a save point created on entry



No save point on task flow entry if selected prevent the creation of ADF model save point on task flow entry if taskflow participate in 'Always Use existing transaction ' or 'User existing transaction if Possible' . In this case overhead associated with a save point is not created for the transaction.

End the transaction from task flow return activity by selecting the End transaction property to commit or rollback.

Shared Data control behavior and transaction

Share data controls with calling task flow have the options Shared and Isolated

Transaction setting
Shared Data Control scope
Isolated Data Control scope
No controller txn
DataControlFrame shared without open txn
New DataControlFrame is created without an open transaction
Always begin new txn
Begin new if one is not open
Always begin new one
Always Use existing txn
Throws exception if txn is already open
Invalid.The checkbox cannot be deselected
Use Existing txn if possible
Begin a new txn if one is not already open
Always begin a new txn

Sunday, September 29, 2013

ADF Page to render Unknown Number of Regions


Create two task flows

Task Flow 1:


dyregion_view1.jsff

<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
  <af:panelGroupLayout id="pgl1">
    <af:outputText value="Region 1" id="ot1"/>
  </af:panelGroupLayout>

</jsp:root>

Task Flow 2:
dynamicRegion_view2.jspx

<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
  <af:panelGroupLayout id="pgl1">
    <af:outputText value="Region 2" id="ot1"/>
  </af:panelGroupLayout>

</jsp:root>

Create the managed bean in the pageFlowScope. The code for the managed bean is as below
package view;

import java.util.ArrayList;
import java.util.List;

import oracle.adf.controller.TaskFlowId;
import oracle.adf.controller.binding.TaskFlowBindingAttributes;

public class MultiBean {
  
   private List<TaskFlowBindingAttributes> mattrs= new ArrayList<TaskFlowBindingAttributes>(5);
   public MultiBean() {
      
       TaskFlowBindingAttributes tfAttr= new TaskFlowBindingAttributes();
       tfAttr.setId("region1");
       tfAttr.setTaskFlowId(new TaskFlowId("/WEB-INF/tfDynamicRegion.xml","tfDynamicRegion"));
       mattrs.add(tfAttr);
      
       tfAttr= new TaskFlowBindingAttributes();
       tfAttr.setId("region2");
       tfAttr.setTaskFlowId(new TaskFlowId("/WEB-INF/tfDyanamicRegion1.xml","tfDyanamicRegion1"));
       mattrs.add(tfAttr);
   }
   public List<TaskFlowBindingAttributes> getTaskFlowList() {
       return mattrs;
   }
  

}

Create a test page and go to the pageDef of the page. Add Executables as below

Click on OK to go the screen below and enter the values as shown.

Create the test page
testMultiFlow.jspx
<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:f="http://java.sun.com/jsf/core"
          xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
    <jsp:directive.page contentType="text/html;charset=UTF-8"/>
    <f:view>
        <af:document title="testMultiFlow.jspx" id="d1">
            <af:form id="f1">
                <af:panelGroupLayout id="pgl1">
             
                    <af:panelTabbed id="pt1">
                    <af:forEach items="#{bindings.multiId.taskFlowBindingList}" var="regs">
                   
                        <af:showDetailItem text="#{regs.name}" id="sdi1">
                                <af:panelBox text="#{regs.name}" id="pb2">
                                    <f:facet name="toolbar">
                                     <af:region value="#{regs.regionModel}" id="r2"/>
                                    </f:facet>
                                </af:panelBox>
                            </af:showDetailItem>
                        </af:forEach>
                    </af:panelTabbed>
                </af:panelGroupLayout>
            </af:form>
        </af:document>
    </f:view>

</jsp:root>

Run and see the two region displayed in the showdetailItems of the Tabed layout.

ADF Dynamic Region

Create two bounded task flows

Task Flow1 :

dyregion_view1.jsff

<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
  <af:panelGroupLayout id="pgl1">
    <af:outputText value="Region 1" id="ot1"/>
  </af:panelGroupLayout>

</jsp:root>

Task Flow2 :
dynamicRegion_view2.jspx

<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
  <af:panelGroupLayout id="pgl1">
    <af:outputText value="Region 2" id="ot1"/>
  </af:panelGroupLayout>

</jsp:root>

Drag and drop the first task flow as Dynamic Region

Create Managed bean for Region Binding as below


Code for the TestBean.java
package view;

import oracle.adf.controller.TaskFlowId;

public class TestBean {
    private String taskFlowId = "/WEB-INF/tfDynamicRegion.xml#tfDynamicRegion";

    public TestBean() {
        System.out.println("taskFlowId=" + taskFlowId);
    }

    public String test() {
        taskFlowId = "/WEB-INF/tfDyanamicRegion1.xml#tfDyanamicRegion1";
        System.out.println("taskFlowId1 =" + taskFlowId);
        // Add event code here...
        return null;
    }

    public TaskFlowId getDynamicTaskFlowId() {
        return TaskFlowId.parse(taskFlowId);
    }

    public void setDynamicTaskFlowId(String taskFlowId) {
        this.taskFlowId = taskFlowId;
    }

    public String test1() {
        // Add event code here...
        taskFlowId = "/WEB-INF/tfDynamicRegion.xml#tfDynamicRegion";
        System.out.println("taskFlowId2=" + taskFlowId);
        return null;
    }

}

Create a testDynamicRegion.jspx as below 
<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:f="http://java.sun.com/jsf/core"
          xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
    <jsp:directive.page contentType="text/html;charset=UTF-8"/>
    <f:view>
        <af:document title="testDynamicRegion.jspx" id="d1">
            <af:form id="f1">
                <af:panelGroupLayout id="pgl1">
                    <af:link text="link 1" id="l1" action="#{pageFlowScope.TestBean.test1}"/>
                    <af:link text="link 2" id="l2" action="#{pageFlowScope.TestBean.test}"/>
                </af:panelGroupLayout>
                <af:region value="#{bindings.dynamicRegion1.regionModel}" id="r1"/>
            </af:form>
        </af:document>
    </f:view>

</jsp:root>

Run the pages and click on the pages to see the region dynamically changes.

Saturday, September 28, 2013

Navigating outside the region using Parent Action

Create a page Home.jspx as a view activity of adfc-config.xml
Create a bounded task flow using parent action activity
Parent action has the following properties
Parent Outcome - For parent action activity to navigate to the parent view activity
Root Outcome - For parent action activity to navigate to a the root page of the application
Activation_view1.jspx
<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
  <af:panelGroupLayout id="pgl1">
    <af:outputText value="Region jsff" id="ot1"/>
    <af:button text="button 1" id="b1" action="toParent"/>
  </af:panelGroupLayout>

</jsp:root>

Click on the button it will go to the goHome flow defined in adfc-config.

Sunday, September 22, 2013

Refreshing ADF region


  • We can configure region, refreshes to invoke the task flow 
  • ADF region can invoke the task flow if the region is in active state 

There are two type of refresh options
1) Default - Refreshes the region when the page loads and RefreshCondition=true.
2) ifNeeded - Refreshes the region if the value of the flow binding parameters changes.ifNeeded Option is set do not set the refresh condition

Shown above are the activation options for a region

Conditional -Refreshes the region if true
Differed - This is relevant if the the applications uses Faceletes XHTML pages
Immediate - Refresh the region immediately.Region refresh when the page displays

There are 3 ways of refreshing the region

1) Neither refresh or RefreshCondition specified ,Region refreshes the parent page displays
2) ADF region get refreshes when the RefreshConditions EL returns true .Refresh condition is independent of the changing value of binding parameters
3) ifNeeded is set for the Refresh option.Any change of value of binding parameters the refresh take place.ifNeeded selected no RefreshCondition specified.

Lets go through an example demonstrating the refresh condition
Create a task flow
Create a managed bean in pageflowscope of the task flow.

package view;

import java.util.HashMap;
import java.util.Map;

import oracle.adf.view.rich.context.AdfFacesContext;

public class RegionMap {
    private boolean refresh;
    private String time;

    public void setTime(String time) {
        this.time = time;
    }

    public void setRefresh(boolean refresh) {
        this.refresh = refresh;
    }

    public boolean isRefresh() {

        String flag =(String) AdfFacesContext.getCurrentInstance().getPageFlowScope().get("varFlag");
     
        if ("true".equals(flag))
            return true;
        else
            return false;

    }

    public RegionMap() {
        super();
    }

    public Map getMyMap() {
        Map map = new HashMap();
        map.put("test1", "test1");
        map.put("test2", "test2");
        return map;
    }

    public String getTime() {
        long t =System.currentTimeMillis();
        System.out.println(t);
        return "" + t;
    }

    public boolean refreshRegion() {

        AdfFacesContext.getCurrentInstance().getPageFlowScope().put("curTime", System.currentTimeMillis());
        return true;
    }

    public String testMeth() {
        String flag=("true".equals((String) AdfFacesContext.getCurrentInstance().getPageFlowScope().get("varFlag")) ? "false" :
         "true");
        AdfFacesContext.getCurrentInstance().getPageFlowScope().put("varFlag", flag);
        System.out.println("Flag =" + flag);
        // Add event code here...
        return null;
    }
}

 Drag and drop the task flow to testRefreshRegion.jspx as a region and set the refresh condition as shown below

The code for testRefreshRegion.jspx is as below :
<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:f="http://java.sun.com/jsf/core"
          xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
    <jsp:directive.page contentType="text/html;charset=UTF-8"/>
    <f:view>
        <af:document title="testRefreshRegion.jspx" id="d1">
            <af:form id="f1">
                <af:region value="#{bindings.regionRefresh1.regionModel}" id="r1"/>
                <af:button text="button 1" id="b1" action="#{pageFlowScope.RegionMap.testMeth}"/>
            </af:form>
        </af:document>
    </f:view>

</jsp:root>

We need to add the managed bean to adfc-config.xml to visible to this page.Run the page. Click on the button based on the pageFlowScope variable = true the region will get refreshed.

ifNeeded and Binding variable change 

 Change the refresh property to ifNeeded as below .


Code for the testRefreshRegion.jspx is as below 

<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:f="http://java.sun.com/jsf/core"
          xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
    <jsp:directive.page contentType="text/html;charset=UTF-8"/>
    <f:view>
        <af:document title="testRefreshRegion.jspx" id="d1">
            <af:form id="f1">
                <af:region value="#{bindings.regionRefresh1.regionModel}" id="r1"/>
                <af:button text="button 1" id="b1" action="#{pageFlowScope.RegionMap.testMeth}"/>
                <af:button text="button 2" id="b2" action="#{pageFlowScope.RegionMap.testChange}"/>
            </af:form>
        </af:document>
    </f:view>
Added button 2 here with action method as #{pageFlowScope.RegionMap.testChange}

Add testChange() method to the managed bean.
    public String testChange() {
        AdfFacesContext.getCurrentInstance().getPageFlowScope().put("changeVar", getTime());
       
        // Add event code here...
        return null;
    }


Go to the page Def file of the testRefreshRegionPage.jspx and add parameters there .

<?xml version="1.0" encoding="UTF-8" ?>
<pageDefinition xmlns="http://xmlns.oracle.com/adfm/uimodel" version="12.1.2.66.68" id="testRefreshRegionPageDef"
                Package="view.pageDefs">
  <parameters/>
  <executables>
    <variableIterator id="variables"/>
    <taskFlow id="regionRefresh1" taskFlowId="/WEB-INF/regionRefresh.xml#regionRefresh" activation="deferred"
              xmlns="http://xmlns.oracle.com/adf/controller/binding" Refresh="ifNeeded">
      <parameters>
        <parameter id="parameter" value="#{pageFlowScope.changeVar}"/>
      </parameters>
    </taskFlow>
  </executables>
  <bindings/>
</pageDefinition>

Run the page and click on the 2nd button to see the region refreshes .