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