This article describes how to merge multiple selected records in Session Bean using ADF drag & drop feature. Below described is simple use case that shows how exactly this can be achieved. Here we will have table & user input field.Table shows EMP records & user input field accepts Salary.When we drag & drop multiple records on user input field,the selected records get updated with the new Salary provided. Steps: Let us suppose that we have created Java EE Web Application with Entities from Emp table.Then create EJB Session Bean & generate Data control for the same. Write a simple code in sessionEJBBean & expose this method to local interface : public void updateEmprecords(List empList, Object sal) { Emp emp = null; for (int i = 0; i < empList.size(); i++) { emp = em.find(Emp.class, empList.get(i)); emp.setSal((BigDecimal)sal); } em.merge(emp); } Now let us create updateEmpRecords.jspx page in viewController project & Drop empFindAll object as ADF Table
Define custom SelectionListener method for the table : public void selectionListener(SelectionEvent selectionEvent) { // This method gets the Empno of the selected record & stores in the list object UIXTable table = (UIXTable)selectionEvent.getComponent(); FacesCtrlHierNodeBinding fcr =(FacesCtrlHierNodeBinding)table.getSelectedRowData(); Number empNo = (Number)fcr.getAttribute("empno") ; this.getSelectedRowsList().add(empNo); }Set table's selectedRowKeys to #{bindings.empFindAll.collectionModel.selectedRow}"Drop inputText on the same jspx page that accepts Salary .Now we would like to drag records from the above table & drop that on the inputtext field.This feature can be achieved by inserting dragSource operation inside the table & dropTraget operation inside the inputText:<af:dragSource discriminant="tab"/> //Insert this inside the table<af:inputText label="Enter Salary" id="it13" autoSubmit="true" binding="# {test.deptValue}"> <af:dropTarget dropListener="#{test.handleTableDrop}"> <af:dataFlavor flavorClass="org.apache.myfaces.trinidad.model.RowKeySet" discriminant="tab"/> </af:dropTarget> <af:convertNumber/> </af:inputText> In the above code when the user drags & drops multiple records on inputText,the dropListener method gets called.Goto the respective page definition file & create updateEmprecords method action& execute action
dropListener method code: public DnDAction handleTableDrop(DropEvent dropEvent) { //Below code gets the updateEmprecords method,passes parameters & executes method DataFlavor<RowKeySet> df = DataFlavor.getDataFlavor(RowKeySet.class); RowKeySet droppedKeySet = dropEvent.getTransferable().getData(df); if (droppedKeySet != null && droppedKeySet.size() > 0) { DCBindingContainer bindings =
(DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry(); OperationBinding updateEmp; updateEmp= bindings.getOperationBinding("updateEmprecords"); updateEmp.getParamsMap().put("sal", this.getDeptValue().getAttributes().get("value")); updateEmp.getParamsMap().put("empList", this.getSelectedRowsList()); updateEmp.execute();
//Below code performs execute operation to refresh the updated records OperationBinding executeBinding; executeBinding= bindings.getOperationBinding("Execute"); executeBinding.execute(); AdfFacesContext.getCurrentInstance().addPartialTarget(dropEvent.getDragComponent()); this.getSelectedRowsList().clear(); } return DnDAction.NONE; }Run updateEmpRecords.jspx page & enter any Salary say '5000'.Select multiple records in table & drop these selected records on the inputText Salary.
Note that all the selected records salary value gets updated to 5000.Technorati Tags: ADF Drag and drop,EJB Session bean,ADF table,inputText,DropEvent