Wednesday, July 24, 2013

ADF - Deleting Multi-Selected Rows from table


The user will select multiple records, and then from the menu options, click on Delete Employees for deletion. The user can also click on checkbox at the header level to select all the records in the table.

This is the Java code in the Bean:
package demo.view;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.view.rich.event.DialogEvent;
import oracle.adf.view.rich.event.PopupCanceledEvent;
import oracle.adf.view.rich.event.PopupFetchEvent;

import oracle.binding.OperationBinding;

import oracle.jbo.Row;
import oracle.jbo.RowSetIterator;


public class employeeOperations {
    public employeeOperations() {
    }

    public void deleteMultiple(ActionEvent actionEvent) {
        DCBindingContainer bindings = getBindings();
        DCIteratorBinding iteratorBinding = bindings.findIteratorBinding("EmployeeVO1Iterator");

        Row[] r = iteratorBinding.getViewObject().getFilteredRows("SelectBox", true);
        for (int i = 0; i < r.length; i++) {
            r[i].remove();
        }

        OperationBinding operationBinding = bindings.getOperationBinding("Commit");
        operationBinding.execute();
        if (!operationBinding.getErrors().isEmpty()) {
            addFacesMessage(FacesMessage.SEVERITY_ERROR, "Error in Deleting Records");

        } else {
            addFacesMessage(FacesMessage.SEVERITY_INFO, "Records Deleted Succesfully");
        }
    }

    public void dialogListener(DialogEvent dialogEvent) {
        if (dialogEvent.getOutcome().name().equals("ok")) {
            DCBindingContainer bindings = getBindings();
            OperationBinding method = bindings.getOperationBinding("Commit");
            method.execute();
        } else if (dialogEvent.getOutcome().name().equals("cancel")) {
            DCBindingContainer bindings = getBindings();
            OperationBinding method = bindings.getOperationBinding("Rollback");
            method.execute();
        }
        System.out.println("dialogListener Executed");
    }

    public static void addFacesMessage(FacesMessage.Severity severity, String message) {
        FacesMessage fm = new FacesMessage(severity, message, null);
        FacesContext.getCurrentInstance().addMessage(null, fm);
    }

    public DCBindingContainer getBindings() {
        return (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
    }

    public void popupCancelListener(PopupCanceledEvent popupCanceledEvent) {
        DCBindingContainer bindings = getBindings();
        OperationBinding method = bindings.getOperationBinding("Rollback");
        method.execute();
        System.out.println("popupCancelListener Executed");
    }

    public void popupFetchListener(PopupFetchEvent popupFetchEvent) {
        DCBindingContainer bindings = getBindings();
        OperationBinding method = bindings.getOperationBinding("CreateInsert");
        method.execute();
        System.out.println("popupFetchListener Executed");
    }

    public void selectAllRows(ValueChangeEvent valueChangeEvent) {
        DCBindingContainer bindings = getBindings();
        DCIteratorBinding it = bindings.findIteratorBinding("EmployeeVO1Iterator");
        RowSetIterator rit = it.getRowSetIterator();
        //rit.reset();
        if (valueChangeEvent.getNewValue() != null) {
            Boolean selectAll = Boolean.parseBoolean(valueChangeEvent.getNewValue().toString());
            if (rit.first() != null) {
                Row r = rit.first();
                r.setAttribute("SelectBox", selectAll);
            }
            while (rit.hasNext()) {
                Row r = rit.next();
                if (r != null) {
                    r.setAttribute("SelectBox", selectAll);
                }
            }
        }
    }
}

This is the code in the Main.jspx for the checkBox created on the header:
<f:facet name="header">
    <af:selectbooleancheckbox autosubmit="true" id="sbc2" simple="true" valuechangelistener="#{HRBean.selectAllRows}">
</af:selectbooleancheckbox></f:facet>

Download the sample application from File Cabinet: AddDeleteEmployees.rar

7 comments:

  1. I can not see file AddDeleteEmployees.rar to download, can you check again..thanks

    ReplyDelete
    Replies
    1. Yes, I checked. The file is already present.

      Delete
    2. there is no rar file here..??

      can you pls check it again

      Delete
    3. Download the file from the location below:
      https://sites.google.com/site/adfsonal/files/AddDeleteEmployees.rar?attredirects=0&d=1

      Delete
    4. Please send me the rar.file to revathi.kdss@gmail.com asap.. Thanks

      Delete
  2. Location is not accessible.. so it could be helpful if u drop the file to my mail..!@

    ReplyDelete
    Replies
    1. Got the file.., Thanks a lot it..:)

      Delete