Tuesday, May 8, 2012

ADF: Programmatic View Criteria


To define view criteria programmatically:
  • VO must contain the attributes which are to be added in where clause.
  • The following method is added in AMImpl java class & method is exposed via client interface.
import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaItem;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.domain.Number;
...
...

public void filterEmployees(){
   ViewObjectImpl empVOImpl = getEmployeesView1();            
   ViewCriteria vc = empVOImpl.createViewCriteria();
   ViewCriteriaRow vcr = vc.createViewCriteriaRow();            
    
   //criteria for employee id
   ViewCriteriaItem vci1 = vcr.ensureCriteriaItem("JobId");
   vci1.setValue("SH_CLERK");

   //criteria for showing employees whose salary are more than 10000
   ViewCriteriaItem vci2 = vcr.ensureCriteriaItem("Salary");
   vci2.setOperator(">");
   vci2.setValue(new Number(2500));

   //criteria for department
   int[] deptIds = {50,100};
   ViewCriteriaItem vci3 = vcr.ensureCriteriaItem("DepartmentId");
   vci3.setOperator("IN");
   int i = 0;
   for(int deptId: deptIds){
     vci3.setValue(i++, new Number(deptId));
   }

   vc.addElement(vcr);
   empVOImpl.applyViewCriteria(vc);
   System.out.println("Query: " + empVOImpl.getQuery());
   empVOImpl.executeQuery();
}

Above implementation is showing three conditions:
  • Equal (JobId = "SH_CLERK")
  • Greater than (Salary > 2500)
  • IN (DepartmentId in (50,100))

ADF: Programmatically adding partial trigger/target


To refresh component programmatically:
  • Bind the component in bean. e.g.
<af:outputtext binding="#{pageFlowScope.BackingBean.outputText}" id="ot1" 
               value="outputText1">
</af:outputtext>

  • Wherever required use following API inside bean:
AdfFacesContext.getCurrentInstance().addPartialTarget(getOutputText());

Monday, May 7, 2012

ADF: Access attribute binding

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;

import oracle.binding.AttributeBinding;
...
...
    DCBindingContainer bindings = 
        ((DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry());
    AttributeBinding attrBinding = 
        (AttributeBinding) bindings.getControlBinding("AttributeNameFromBindings");

    //get input value
    attrBinding.getInputValue();
    //get label
    attrBinding.getLabel();
Similiarly, we can cast bindings.getControlBinding("AttributeNameFromBindings") to:
  • JUCtrlListBinding: for getting value from list binding.

ADF: Access/Execute method binding


Below is the way to access/execute method binding from managed bean:
import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;

import oracle.binding.OperationBinding;
...
...
    DCBindingContainer bindings = 
        ((DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry());
    OperationBinding operationBinding = 
        bindings.getOperationBinding("MethodNameFromBindings");
    
    //passing value into method arguments
    operationBinding.getParamsMap().put("ArgumentName",value);
    operationBinding.execute();
    
    //getting return value from method
    operationBinding.getResult();

Friday, May 4, 2012

ADF: Programmatic Redirection


Below is the way to programmatic navigate to other view:
    public void redirectToSelf(String viewId) {
        FacesContext fctx = FacesContext.getCurrentInstance();
        ExternalContext ectx = fctx.getExternalContext();
        ControllerContext controllerCtx = null;
        controllerCtx = ControllerContext.getInstance();
        String activityURL = controllerCtx.getGlobalViewActivityURL(viewId);
        try {
            ectx.redirect(activityURL);
        } catch (IOException e) {
            //Can't redirect
            e.printStackTrace();
        }
    }
Note: For executing "controllerCtx.getGlobalViewActivityURL", the view must be present in adfc-config.xml .

ADF: Programmatic Logging out


Below are the ways to programmatically logging out the user:
  • First Approach
FacesContext fc = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession)fc.getExternalContext().getSession(false);
session.invalidate();

try {
   fc.getExternalContext().redirect("faces/welcome");
} catch (IOException e) {
   e.printStackTrace();
}
  • Second Approach
ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext();
StringBuilder logoutURL = new StringBuilder(ectx.getRequestContextPath());

logoutURL.append("/adfAuthentication?logout=true&end_url=");
logoutURL.append("/faces/welcome");

try {
   ectx.redirect(logoutURL.toString());
} catch (IOException e) {
   adfLogger.warning("Error logging off");
   e.printStackTrace();
}

ADF: Programmatic Navigation


Below is the API that can be used to programmatically navigate to next/new activity of taskflow:
FacesContext facesContext = FacesContext.getCurrentInstance();
NavigationHandler navHandler = facesContext.getApplication().getNavigationHandler();
navHandler.handleNavigation(facesContext, null, "controlFlowCaseName");
API javadoc:
Package: javax.faces.application.NavigationHandler

public abstract void handleNavigation(FacesContext context, String fromAction, String outcome)

Perform navigation processing based on the state information in the specified FacesContext,
plus the outcome string returned by an executed application action.

Parameters:
context - The FacesContext for the current request
fromAction - The action binding expression that was evaluated to retrieve the specified outcome,
or null if the outcome was acquired by some other means
outcome - The logical outcome returned by a previous invoked application action (which may be null)
Throws: NullPointerException - if context is null

Thursday, May 3, 2012

ADF: Tree using managed bean


For creating ADF tree component using managed bean:
  • Create a object representing tree item & this will have references to its children.
public class TreeNode {
    public TreeNode() {
        super();
    }    
    public TreeNode(int id, String name){
        this.id = id;
        this.name = name;
    }
    
    private int id;
    private String name;
    private List<TreeNode> childNodes;

    ...with getters & setters...
}
  • Create and Populate objects representing tree item with data, i did that in constructor of managed bean. Managed bean can be registered in either adfc-config.xml or faces-config.xml.
    public TreeBackingBean() {
        rootNode = new TreeNode(1, "World");
        TreeNode node2 = new TreeNode(2, "USA");
        TreeNode node3 = new TreeNode(3, "India");
        TreeNode node4 = new TreeNode(4, "Los Angeles");
        TreeNode node5 = new TreeNode(5, "Delhi");
        TreeNode node6 = new TreeNode(6, "Mumbai");
        
        List<TreeNode> l1 = new ArrayList<TreeNode>();
        l1.add(node2);
        l1.add(node3);
        rootNode.setChildNodes(l1);
        
        List<TreeNode> l2 = new ArrayList<TreeNode>();
        l2.add(node4);
        node2.setChildNodes(l2);
        
        List<TreeNode> l3 = new ArrayList<TreeNode>();
        l3.add(node5);
        l3.add(node6);
        node3.setChildNodes(l3);
    }
  • Use following API to create TreeModel:
org.apache.myfaces.trinidad.model.TreeModel treeModel = 
new org.apache.myfaces.trinidad.model.ChildPropertyTreeModel(objectOfNodeTreeItem, "childNodes");
Note: The second argument is String matching the property of objectOfTreeItem representing references to its children. If the name is mismatched, you will end up with "javax.el.PropertyNotFoundException" error.

  • Bind the value of <af:tree> from page to above "treeModel" instance. Final result be like:
The sample application can be downloaded from https://rapidshare.com/files/4090558697/TreeFromBeanExample.zip

Tuesday, May 1, 2012

ADF: Ghost Labels

To have Ghost Labels on ADF pages:
    <af:document id="d1">
      <af:messages id="m1"/>
      <f:facet name="metaContainer">
        <af:resource type="javascript" source="/jquery-1.7.min.js"/>
        <af:resource type="javascript" source="/label-in-field.js"/>
        <af:resource type="css">
            .text-label {
                color: #cdcdcd;
                font-weight: bold; 
            }
        </af:resource>
      </f:facet>
      <af:form id="f1">
        <af:panelStretchLayout id="psl1">
          <f:facet name="center">
            <af:panelGroupLayout layout="scroll" id="pgl1" halign="center">
              <af:inputText label="Username" id="it1" simple="true"
                            required="true"/>
              <af:inputText label="Password" id="it2" secret="true"
                            simple="true" autoTab="true"/>
              <af:selectOneChoice value="#{bindings.DepartmentId.inputValue}"
                                  id="soc1">
                <f:selectItems value="#{bindings.DepartmentId.items}" id="si1"/>
              </af:selectOneChoice>
            </af:panelGroupLayout>
            <!-- id="af_one_column_stretched"   -->
          </f:facet>
        </af:panelStretchLayout>
      </af:form>
      <af:clientListener type="load" method="onPageLoad"/>
    </af:document>
Final Result will be like:
Note: http://lspatil25.blogspot.in/2012/05/adf-make-findcomponentbyabsoluteid-js.html

ADF: Make findComponentByAbsoluteId JS work


JS: AdfPage.PAGE.findComponentByAbsoluteId(componentId); 
To make it work:
  • There must be some validation/behaviour property or client attribute present in that component (as in figure, maximum length is present).
  • Or the component itself should be client component.
<af:inputText label="Label 1" id="it1">
   <af:clientAttribute name="defaultFocusField" value="Y"/>
</af:inputText>