ADF BC: Fun with Bind Parameters – Part One – Complex Order By clauses

1

Bind parameters (:paramName) are typically used in the Where clause of SQL queries underlying ADF BC ViewObjects. However, there is no rule that they can only occur in the where-clause. Any part of a query can be made a little dynamic through the use of bind-parameters. In this article an example of how we use a bind-parameters in an order by clause inside a ViewObject, and how we can leverage this bind-parameter from our (JHeadstart generated) ADF Faces user interface to choose from various wildy varying order by clauses.

In this article, I will demonstrate how we can order a list of employees (yes, table EMP again) by various Order By clauses, by selecting the appropriate option from the Dropdown list.

 

....
 

Steps:

1. Add an Order By clause to the ViewObject, using a bindparameter:

case <br />when :orderBy = 1  <br />then (select count(*) from emp sub where sub.mgr = emp.empno)  <br />when :orderBy=2 <br />then extract(month from hiredate) <br />when :orderBy=3 <br />then SalaryRankInDept <br />when :orderBy=4 <br />then (select count(*) from emp colleagues where colleagues.job = job) <br />end<br />

Here I have specified four different Order By cases: 

  • order by the number of subordinates an employee manages
  • order by the month in which an employee is hired
  • order by the position an employee has in the ranking by salary in his own department
  • order by the popularity of the job an employee has (the number of colleagues in the same job)

 

2. Use the bind parameter in some non-intrusive way in the Where clause (it seems that we can only use the VO.setWhereClauseParams() method for bindparameters that actually appear in the WHERE clause:

nvl(:orderBy,0) &lt;&gt; -1<br />

3. Define a bind-parameter for the ViewObject of type Number

This completes the ViewObject. Now we turn our attention to the ViewController project. (I am assuming a project that has already been JHeadstart enabled using the JHeadstart wizard).

4. Create a Group of type Table, based on the EmpView1 ViewObject usage. 

5. Create a domain OrderEmployeesBy with all the order by options we want to support

6. Create a Non-Bound Item (not bound to a Model Attribute) of display type Dropdownlist, associated with the new OrderEmployeesBy domain.

7. Configure a Managed Bean that will hold the value of the Order By item – note: we already referred to that bean in the Value property of the OrderBy item!

The class is very simple:

package nl.amis.hrm.view;<br /><br />public class OrderByBean {<br /><br />    private Integer orderBy= new Integer(1);<br />    public OrderByBean() {<br />    }<br /><br />    public void setOrderBy(Integer orderBy) {<br />        this.orderBy = orderBy;<br />    }<br /><br />    public Integer getOrderBy() {<br />        return orderBy;<br />    }<br />}<br /><br />

8. Have one of the other attributes depend on OrderBy (in order have OrderBy have its autoSubmit attribute set to true)

<img vspace="0" hspace="0" border="0" align="bottom" src="../wp-content/images/bindOrderby_dependson.jpg" />

 

9. After generating the application, change the table element in the EmployeesTable.jspx page: set its partialTriggers attribute to include the OrderBy item; this ensures that whenever the dropdownlist Order By? changes, the table is refreshed.

 

When we run the application, we will see a table with the first 10 employees. By choosing a different value in the Order By dropdownlist, we will cause the table to refresh, with the employees listed in the order we selected in the dropdownlist.

In this case, we see the best paid employees from each department listed first, followed by the runners-up from each department, followed by those employees who come third etc.

Resources

Download JDeveloper 10.1.3.1 Application with the projects described in this article: AdvancedOrderByBasedOnBinds.zip

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

1 Comment

  1. Hi
    Pretty old blog but good and useful to me. Could you please explain how the managed bean is updating the view object bind variables? I am using 11g now and couldn’t understand the use of managed bean in this blog.
    Thanks
    Tarun