Friday, March 1, 2013

Delete Multiple records in Oracle ADF Table

In this post i am going to show how to delete multiple records in Oracle ADF table. You can select multiple records and then delete them in one transaction instead of doing one by one.

1) Create a new fusion application and create Employees entity object, then its view object.

2) Create an application module and add your view object in it. you can perform all above step in just one wizard as well.

3) Double click on EmpoyeeVo and go to Java tab as shown in the following snap
4) Click on Edit icon in front of Java classes and then on pop up select "Generate view object class " and "Generate view row class" and click Ok.
5) Now go to your AppModule and double click on it. then go to Java tab and click on edit icon from of Java classes. and from pop up choose " Generate application module class" and click ok
6) Now go to your EmployeeViewImpl class as shown in the following snap.

7) Past the following code in EmployeeViewImpl class


"     public void deleteEmps(List <Key> empKeys )
    {
        if(empKeys!= null)
            {
            for(Key k: empKeys)
            {
                Row[] row = this.findByKey(k, 1);
                if(row!= null && row.length==1)
                {
                    EmployeeVORowImpl r = (EmployeeVORowImpl)row[0];
                    r.remove();
                }
                }
            }
    }"

this is the complete code of EmployeeViewImpl

"package model;

import java.util.List;

import oracle.jbo.Key;
import oracle.jbo.Row;
import oracle.jbo.server.ViewObjectImpl;
// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Fri Mar 01 16:39:55 PKT 2013
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
public class EmployeeVoImpl extends ViewObjectImpl {
    /**
     * This is the default constructor (do not remove).
     */
    public EmployeeVoImpl() {
    }
    public void deleteEmps(List <Key> empKeys )
    {
        if(empKeys!= null)
            {
            for(Key k: empKeys)
            {
                Row[] row = this.findByKey(k, 1);
                if(row!= null && row.length==1)
                {
                    EmployeeVoRowImpl r = (EmployeeVoRowImpl)row[0]; //EmployeeVoRowImpl class that //we have create earlier
                    r.remove();
                }
                }
            }
    }
}
"
8) Now go to your view layer and double click on adfc-config.xml for creating and Unbounded task flow.
9) Drag and drop a view component from component palette.
  10) Double click on it and choose defaults from popup and then drag and drop EmployeeView1 data control on page  as a ADF table and make sure you have selected multiple rows from popup as shown in the following snaps.

11) Now from structure window select af:table and then go to advanced properties section and from binding field select down arrow and choose edit.

12) then click new to create new bean give it name delEmps and class name as well other choose default and click ok. Now in front of property click new to create new property in java bean
13) go to your newly created bean and copy past following code in it.

    public List TableKey()
    {
        List<Key> list= new ArrayList<Key>();
        for(Object empKey:empsTable.getSelectedRowKeys())
        empsTable.setRowKey(empKey);
        JUCtrlHierNodeBinding data = (JUCtrlHierNodeBinding)empsTable.getRowData();
        list.add(data.getRow().getKey());
        return list;
        }
following is the my complete code of backing bean


package view;

import java.util.ArrayList;
import java.util.List;

import oracle.adf.view.rich.component.rich.data.RichTable;

import oracle.jbo.Key;
import oracle.jbo.uicli.binding.JUCtrlHierNodeBinding;

public class delEmps {
    private RichTable empsTable;

    public delEmps() {
    }

    public void setEmpsTable(RichTable empsTable) {
        this.empsTable = empsTable;
    }

    public RichTable getEmpsTable() {
        return empsTable;
    }
    public List TableKey()
    {
        List<Key> list= new ArrayList<Key>();
        for(Object empKey:empsTable.getSelectedRowKeys())
        empsTable.setRowKey(empKey);
        JUCtrlHierNodeBinding data = (JUCtrlHierNodeBinding)empsTable.getRowData();
        list.add(data.getRow().getKey());
        return list;
        }
}




14) Now Drag and drop PanelGroup Layout on before af:Table. Look at the below snap
15) Now go to your view object, double click to open it and from java tab, click edit icon in front of client interface and from popup move deleteEmps to right as shown below. It will now appear in your data control.

16) Now go to your jsf page again and then go to data control and drag and drop deleteEmps on PanelGroupLayout and choose ADF button. see below snap.
17) In value field choose expression builder select the method of your bean as shown below
18) then click ok and then go to your jsf page again and after this from data control operation section choose commit function and then drop it on panelGroupLayout as adf button.

19) Now one last thing go to binding from jsf page and form binding section  choose deleteEmps and then click on source tab. the code will be like this

<methodAction id="deleteEmps" RequiresUpdateModel="true" Action="invokeMethod" MethodName="deleteEmps"
                  IsViewObjectMethod="true" DataControl="AppModuleDataControl"
                  InstanceName="data.AppModuleDataControl.EmployeeVo1" IterBinding="EmployeeVo1Iterator">
      <NamedData NDName="empKeys" NDValue="#{delEmps.empsTable}" NDType="java.util.List&lt;oracle.jbo.Key>"/>
    </methodAction>

change it to


<methodAction id="deleteEmps" RequiresUpdateModel="true" Action="invokeMethod" MethodName="deleteEmps"
                  IsViewObjectMethod="true" DataControl="AppModuleDataControl"
                  InstanceName="data.AppModuleDataControl.EmployeeVo1" IterBinding="EmployeeVo1Iterator">
      <NamedData NDName="empKeys" NDValue="#{delEmps.empsTable}" NDType="java.util.List"/>
    </methodAction>

I have just removed &lt;oracle.jbo.Key from about code. save all the code and go to jsf page and run it and test it.

0 comments:

Post a Comment