Monday, 8 February 2016

Using f:attribute to pass parameter in the actionEvent in Oracle ADF

Explanation: The f:attribute tag(JSF tag supported in ADF Faces) allows us to pass the attribute value to a component, or a parameter to a component via actionListener. 

In other words I can say that f:attribute is used to pass some additional attribute value to the associated component. Sometimes using f:attribute simplifies the complex piece of code. The f:attribute tag has very simple structure. It has two properties:
  • Name: Name of the tag attribute
  • Value: Value or an EL reference of value
Here in this post I will demonstrate how to use f:attribute tag with ADF Faces. For demonstration purpose I will be using the Departments table of HR Schema in the Oracle Database XE, and requirement is to delete departments with attribute DepartmentId  greater than 100. If the attribute DepartmentId  is less than 100, then it should throw an error message.

Solution: For solution to the above requirement follow the steps as shown below:

Step 1: Create an Oracle ADF Fusion Web Application named as f_attributeDemo.

Step 2: Create an ADF Business Component from Table for the Departments table. The ADF Business Components will consist of Entity Object, View Object, and Application Module.

Step 3: Create a demo.jspx page.

Step 4: Expand Data Controls à ApModuleDataControl àSelect DepartmentsVO1, and drag and drop on the demo.jspx as an ADF Table. Select Row Selection and Single Row and Check Read-Only Table as shown below:

Click OK


Step 5: Open demo.jspx. Now we will add one more column to this table as the end. This column will have a Delete link to delete record. To do this drag af:column from the component palette and drop it as a last column in the Countries table.

Now drag and drop af:link inside the newly created column and name the link as Delete.
Create and actionListener for the af:link as actionListener="#{pageFlowScope.MyBean.deleteDepartmentAction}"

Step 6: Drag f:attribute from the component palette and drop it inside the af:link. It will ask for Name and Value as shown below: Here we will give name="DepartmentId" value="#{row.DepartmentId}"


Thus the newly added column will look like as shown below:


Now add the action binding on the demo.jspx page for Delete and Execute as shown below:


Step 7: Open MyBean.java and write the code as shown below:
package com.susanto;

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

import oracle.adf.model.BindingContext;

import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;

public class MyBean {
       public MyBean() {
       }

       // Get BindingContainer of current view port
       public BindingContainer getBindingsCont() {
             return BindingContext.getCurrent().getCurrentBindingsEntry();
       }

       // Generic Method to execute operation Binding
       public OperationBinding executeOperation(String operation) {
             OperationBinding createParam = getBindingsCont().getOperationBinding(operation);
             return createParam;
       }

       // Method to delete Department row on condition basis
       public void deleteDepartmentAction(ActionEvent actionEvent) {
             // Here DepartmentId is the name of the f:attribute tag in demo.jspx
             Object obj = actionEvent.getComponent().getAttributes().get("DepartmentId");
             if (obj != null && ((Integer) obj) > 100) {
                    // If department id is greater than 100 then call delete operation
                    // on it
                    executeOperation("Delete").execute();
                    executeOperation("Execute").execute();
             } else {
                    FacesMessage errMsg = new FacesMessage("Department Id for selected Department is not greater than 100");
                    errMsg.setSeverity(FacesMessage.SEVERITY_ERROR);
                    FacesContext.getCurrentInstance().addMessage(null, errMsg);
             }
       }
}

Step 8: Save all and run the application.Thus the ran application is shown below:


Step 9: When we click on the Delete link of DepartmentId 10 we will get an error message in a popup displaying “Department Id for selected Department is not greater than 100“, as shown below:


Step 10: When we click on the Delete link of DepartmentId 240 the Department get deleted as shown below:


Hence, the solution to our requirement.

If you like the post, please comment, share and like me on Facebook


Thanks & Regards,
Susanto Paul