Friday 23 January 2015

How to filter rows in VO

There are different ways to filter the data/rows in a VO.

1. Using Where clause:
  You can use setWhereCluse() method from VO to set filter constraint before calling executeQuery() method to filter data based on the input condition. This means we are appending the where clause while querying the data to the DB. This will take string as in input.
Ex: ViewObjectImpl empVO = am.getEmployeeVO();
   //filtering data using  setWhereClause on employee_id
   empVO.setWhereClause("employee_id=" + empId);
   empVO.executeQuery();
 


Note: Here employee_id is the column name from the db table.

 You can have multiple constraints in where clause just like in sql.
 emp.setWhereClause("employee_id=" + empId + "employee_name like " + empName);

2. Using getFilteredRows(): getFilteredRows() is from VO, this method is used to filter rows based on one column only and returns Row[]. getFilteredRows() returns the Row[] by filtering the rows from VO cache based on the column attribute passed.
Ex: ViewObjectImpl empVO = am.getEmployeeVO();
    //filtering data using  setWhereClause on employee_id
    empVO.executeQuery();
    Row[] rows = empV.getFilteredRows("EmployeeId", empId);


Note: EmployeeId is the attribute name from VO.


3. Using Row Qualifier: Using RowQualifier you can filter the data based on multiple columns from VO cache.
Ex: ViewObjectImpl empVO = am.getEmployeeVO();
    //Create RowQualifier from VO 
    RowQualifier rowQualifier = new RowQualifier(empVO);
    //set where clause to row qualifier 
    rowQualifier.setWhereClause("EmployeeId="+empId+" AND EmployeeName="+empName);
    //get filtered rows using row qualifier 
    Row[] filteredRows = emp.getFilteredRows(rowQualifier);  

Note: EmployeeId & EmployeeName are the attribute names from VO.

Monday 19 January 2015

Select All rows in ADF tabel

I got a requirement to provide 'Select All' option for ADF table. Tried a lot in different ways and finally got the solution with the following approach.

Code in UI:
 
<af:commandToolbarButton partialSubmit="true" text="Select All" id="cb1"
actionListener="#{myBean.selectAllListener}">
</af:commandToolbarButton> 

Code in the bean for selection listener is like this:
public void selectAllListener() {
  RowKeySet rkset = new RowKeySetImpl();
  CollectionModel model = (CollectionModel)myTable.getValue();
  int rowcount = model.getRowCount();
 
  for (int i = 0; i < rowcount; i++) {
     model.setRowIndex(i);
     Object key = model.getRowKey();
     rkset.add(key);
  }
 
  myTable.setSelectedRowKeys(rkset);
}

When we click on 'Select All' button, it will select all the rows from the model, adds to the selected row keys of the table and also displays the selected row count in the 'Rows selected' section in the status bar.

If the selection listener has makeCurrent entry, then the above approach may fail. When you select one row and then go for 'Select All', this wont work  to overcome this issue you can have selection listener code in the bean as follows:

public void selectAllListener() {
  RowKeySet rkset = myTable.getSelectedRowKeys();
  CollectionModel model = (CollectionModel)myTable.getValue();
  int rowcount = model.getRowCount();
 
  for (int i = 0; i < rowcount; i++) {
     model.setRowIndex(i);
     Object key = model.getRowKey();
     rkset.add(key);
  }
}

This means that, you are going to add remaining rows on top of selected rows.