Tuesday, May 24, 2011

Delete Rows using Managed Bean @ADF

In order to delete rows using managed bean that has component binding set for Table component, we can use a delete button with partial submit set to true and actionlistener property invoking the bean with following code:

public void onDelete(ActionEvent actionEvent) {
//access the RichTable from the JSF component binding
//created for the table component
RichTable _table = this.getTable1();
RowKeySet rks = _table.getSelectedRowKeys();
//iterate over the set of selected row keys and delete
//each of the found rows
Iterator selectedRowIterator = rks.iterator();
while (selectedRowIterator.hasNext()){
//get the first key
Object key = selectedRowIterator.next();
//make the row current
_table.setRowKey(key);
//the row is an instance of the ADF node binding class
JUCtrlHierNodeBinding rowWrapper = null;
rowWrapper = (JUCtrlHierNodeBinding) _table.getRowData();
//get the actual oracle.jbo.Row object
Row row = rowWrapper.getRow();

row.remove();
}
//partially update the ADF Faces Table
AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance();
adfFacesContext.addPartialTarget(_table);
}
However this is simple but not recommended always. Another way is to create delete method in View Object Impl taking list of keys as input and use managed bean returning that list as input parameter to that method. This can be done by droping the method in viewobjectimpl as action and then use the expression builder to refer the managed bean as input parameter.

View Object Impl code:

public void processEmployeeBulkDelete(List employees){
if (employees != null){
for (Object employee : employees) {
Key k = (Key)employee;
Row[] rowsFound = this.findByKey(k,1);
//if the key returns more than one row then the provided
//key obviously is not unique. Ignore in this sample
if (rowsFound != null && rowsFound.length == 1) {
//get the first entry in the array of rows found
Row employeeRow = rowsFound[0];
employeeRow.remove();
}
}
}
}

Managed Bean Code:


public List getSelectedAdfRowKeys() {
List<Key> retVal = new ArrayList<Key>();
//get the selected row keys from the table instance
for (Object rowKey : employeesTable.getSelectedRowKeys()) {
//make the row the current row
employeesTable.setRowKey(rowKey);
Object o = employeesTable.getRowData();
//table rows are represented by the JUCtrlHierNodeBinding
//binding class
JUCtrlHierNodeBinding rowData = (JUCtrlHierNodeBinding)o;
//add the row key to the list
retVal.add(rowData.getRow().getKey());
}
return retVal;
}
//JSF component binding of the table
public void setEmployeesTable(RichTable employeesTable) {
this.employeesTable = employeesTable;
}
public RichTable getEmployeesTable() {
return employeesTable;
}

Now drag the processEmployeeBulkDelete method entry from the Data Controls panel to the page and choose a command action component. Now we can refer getSelectedAdfRowKeys as the input component to the method.

No comments:

Post a Comment