Friday, May 16, 2014

Drag and Drop feature in ADF table-Committing Transactions

This exercise is again an extension to the previous exercise.

So far we have achieved a functionality to drag and drop multiple rows from one table to another however this works fine for one session.

That is as soon as we open the application page again all the changes we have done are reverted.

This happened as we were not committing the transaction and for that reason the rows in tables were not persisting.

In this exercise what changes we can make in our code so that after dragging and dropping the code from source table to target table

The rows at source table gets removed from the back end and the rows gets inserted at the target table in back end.

Go to your JSP page and go to binding tab



Click on plus icon and select action and say ok



Select the application module

Select commit operation and say ok



Validate the binding is created.



Now go back to your managed bean and add one line of code at the end of while loop

Dcbc.getOperationBinding("Commit").execute();

So the whole node now becomes this

package view;


import java.util.Iterator;

import oracle.adf.view.rich.component.rich.data.RichTable;
import oracle.adf.view.rich.datatransfer.DataFlavor;
import oracle.adf.view.rich.datatransfer.Transferable;
import oracle.adf.view.rich.dnd.DnDAction;
import oracle.adf.view.rich.event.DropEvent;

import java.util.List;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;

import oracle.jbo.Row;
import oracle.jbo.RowSetIterator;
import oracle.jbo.uicli.binding.JUCtrlHierNodeBinding;

import org.apache.myfaces.trinidad.model.RowKeySet;

public class DragDropRows {
public DragDropRows() {
}

// public void dropListener(DropEvent dropEvent) {
// // Add event code here...
// DCBindingContainer bc =
// (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
// DCIteratorBinding dcib =
// bc.findIteratorBinding("Emptable1View1Iterator");
// RowSetIterator iterator = dcib.getRowSetIterator();
//
//
// }

public DnDAction dropRows(DropEvent dropEvent) {
// Add event code here...

RichTable rt = (RichTable)dropEvent.getDragComponent();
Transferable t = dropEvent.getTransferable();
DataFlavor df =
DataFlavor.getDataFlavor(RowKeySet.class, "MoveRows");
RowKeySet rowKeySet = t.getData(df);
Iterator itr = rowKeySet.iterator();
RichTable dropTable = (RichTable)dropEvent.getDragComponent();

while (itr.hasNext()) {
List key = (List)itr.next();
rt.setRowKey(key);
JUCtrlHierNodeBinding rowBinding =
(JUCtrlHierNodeBinding)rt.getRowData();
Row row = (Row)rowBinding.getRow();

DCBindingContainer Dcbc =
(DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
DCIteratorBinding dc =
Dcbc.findIteratorBinding("Emptable2View1Iterator");
RowSetIterator iter = dc.getRowSetIterator();
Row newRow = iter.createRow();
newRow.setAttribute("Name", row.getAttribute("Name"));
newRow.setAttribute("Empid", row.getAttribute("Empid"));
newRow.setAttribute("Dept", row.getAttribute("Dept"));
iter.insertRowAtRangeIndex(iter.getRowCount(), newRow);
row.remove();
Dcbc.getOperationBinding("Commit").execute();
}
return DnDAction.MOVE;
}

}


Save the changes and deploy your project to server

Drag and drop two rows




now go to your sql worksheet and validate the data at backend

No comments: