Tuesday, May 8, 2012

Update multiple rows using EJB Data Control

Let us take a scenario where in users wants to edit multiple records in the ADF table. So in ejb this can't be achieved in straight forward way, we have to manually get all the selected row keys and merge them to the database in programmatic way.

You can download the sample workspace from here
[Runs with Oracle JDeveloper 11.1.2.0.0 (11g R2) + HR Schema]

Implementation Steps

Create Fusion Web Application with entity based on Departments, then create a stateless session bean and add the below method. Expose the method to local/remote interface and generate data control.

Note:- Here in the below code "em" is a EntityManager.

/**
 * Here param deptList will be having the selected key row with modified data.
 * Iterating through the deptList and find the depratments object instance by primary key
 * Merge the data using merge api
 * @param deptList
 */ 
public void updateMultipleDeptRows(List deptList) {
 if (deptList.size() > 0) {
  Iterator iIter = deptList.iterator();
  while (iIter.hasNext()) {
   HashMap map = (HashMap)iIter.next();
   //Finding the departments object instance
   Departments departments = em.find(Departments.class, map.get("departmentId"));
   departments.setDepartmentName((String)map.get("departmentName"));
   departments.setLocationId((BigDecimal)map.get("locationId"));
   em.merge(departments);
  }
 }
}    

Create index.jspx page and drag and drop departmentsFindAll->Table as ADF Table. and create the backingBean as "IndexBean". Surround the table with panel collection, add the toolbar, drop button inside toolbar and name as "Save".

Bind the departments table to the backing bean as show below.


Create the ActionListener method called "updateMultipleRows" for save button.


Open the IndexBean backing bean and add the below code.

public void updateMultipleRows(ActionEvent actionEvent) {
 RowKeySet selectedDepts = getDeptTable().getSelectedRowKeys();
 Iterator selectedDeptIter = selectedDepts.iterator();
 DCBindingContainer bindings = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
 DCIteratorBinding deptIter = bindings.findIteratorBinding("departmentsFindAllIterator");
 RowSetIterator deptRSIter = deptIter.getRowSetIterator();
 List deptList = new ArrayList();
 while (selectedDeptIter.hasNext()) {
  Key key = (Key)((List)selectedDeptIter.next()).get(0);
  Row currentRow = deptRSIter.getRow(key);
  HashMap rowValues = new HashMap();
  rowValues.put("departmentId", currentRow.getAttribute("departmentId"));
  rowValues.put("departmentName", currentRow.getAttribute("departmentName"));
  rowValues.put("locationId", currentRow.getAttribute("locationId"));
  deptList.add(rowValues);
 }
 //Execute the updateMultipleDeptRows method by passing deptList param
 OperationBinding oper = bindings.getOperationBinding("updateMultipleDeptRows");
 oper.getParamsMap().put("deptList", deptList);
 oper.execute();
 //Refresh the table
 AdfFacesContext.getCurrentInstance().addPartialTarget(this.getDeptTable());
}    

Go to Bindings tab in index.jspx page and add updateMultipleDeptRows method action.


Run the index.jspx page, select the multiple rows and change the values respectively. Now click on save button should merge the multiple records to the database.

1 comment: