Thursday, January 17, 2013

EJB DC - Deleting Multi-Selected Rows From Adf Table

Let us take a scenario where in users wants to delete multiple records in the ADF table. In EJB this can't be achieved in straight forward way, we have to manually get all the selected row keys and delete the rows data using EntityManager API.

You can download the sample workspace from here
[Runs with Oracle JDeveloper 11.1.2.3.0 + HR Schema]

You can also look at  how to achieve in this article link "Update multiple rows using EJB Data Control".

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 row.
* Iterating through the deptList and find the depratments object instance by primary key
* Delete the data using entity manager api
* @param deptList
*/
public void deleteMultipleDeptRows(List deptList) {
	if (deptList.size() > 0) {
		Iterator iter = deptList.iterator();
		while (iter.hasNext()) {
			HashMap map = (HashMap)iter.next();
			//Finding the departments object instance
			Departments departments = em.find(Departments.class, map.get("departmentId"));
			em.remove(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 "Delete Multiple Rows".

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


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


Open the IndexBean backing bean and add the below code.
public void deleteAction(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"));
		deptList.add(rowValues);
	}
	//Execute the deleteMultipleDeptRows method by passing deptList param
	OperationBinding oper = bindings.getOperationBinding("deleteMultipleDeptRows");
	oper.getParamsMap().put("deptList", deptList);
	oper.execute();
	deptIter.executeQuery();
	//Refresh the table
	AdfFacesContext.getCurrentInstance().addPartialTarget(this.getDeptTable());
}
Go to Bindings tab in index.jspx page and add deleteMultipleDeptRows method action.


Run the index.jspx page, select the multiple rows and clicking on "Delete Multiple Rows" button should delete the multiple records from the database.

1 comment:

  1. Hi Deepak,
    After creating data control for department table I could not understand what I would do next. when I downloaded your project you have created many java classes(beans) i could not understand why so? can you please elaborate it little bit, it is difficult to understand for newbie.

    Thanks

    ReplyDelete