Please disable your adblock and script blockers to view this page

Search this blog

Tuesday 17 January 2017

ADF Basics: Duplicate Record Validation Using Custom Java Method


Hello All

Recently I have posted about duplicate record validation using model level business rules (Unique Key Validation) but sometimes we need to check some more conditions or we have some other logic that should be checked before validating the input then in that case we can write custom java method in model (Application Module Impl class) and call it in bean validator using binding layer




So In this post I am showing how to validate duplicate record using custom java method, Here I am using Departments table of HR Schema to create business components (EO,VO)


Open Application Module , create Application Module Impl class and method to check duplicate department name



    /**Custom Java Method to validate department name**/
    public String deptNameValidator(String deptNm) {
        ViewObject vo = this.getDepartments1();
        //Create RowSetIterator of ViewObject
        RowSetIterator rsi = vo.createRowSetIterator(null);
        //Get current row of viewObject
        Row cRow = vo.getCurrentRow();
        int count = 0;
        String departmentName = "";
        //Iterate over viewObject to check duplicate record
        while (rsi.hasNext()) {
            Row r = rsi.next();
            //Check all rows other than current row
            if (!r.equals(cRow) && r.getAttribute("DepartmentName") != null) {

                departmentName = r.getAttribute("DepartmentName").toString();
                if (deptNm.equalsIgnoreCase(departmentName)) {
                    count = count + 1;
                }
            }
        }
        rsi.closeRowSetIterator();
        if (count > 0) {
            return "Y";
        } else {
            return "N";
        }
    }

and created a bean validator for Department Name attribute

<af:inputText value="#{bindings.DepartmentName.inputValue}"
                                          label="#{bindings.DepartmentName.hints.label}"
                                          required="#{bindings.DepartmentName.hints.mandatory}"
                                          columns="#{bindings.DepartmentName.hints.displayWidth}"
                                          maximumLength="#{bindings.DepartmentName.hints.precision}"
                                          shortDesc="#{bindings.DepartmentName.hints.tooltip}" id="it2"
                                          contentStyle="width:150px;" autoSubmit="true"
                                          validator="#{viewScope.DuplicateRecordBean.departmentNameValidator}">
                                <f:validator binding="#{bindings.DepartmentName.validator}"/>
                            </af:inputText>

and code for bean validator in managed bean is

    /**Method to get BindingContainer of current view port
     * @return
     */
    public BindingContainer getBindingsCont() {
        return BindingContext.getCurrent().getCurrentBindingsEntry();
    }


    /**
     * Generic Method to execute operation
     * */
    public OperationBinding executeOperation(String operation) {
        OperationBinding createParam = getBindingsCont().getOperationBinding(operation);
        return createParam;

    }

    /**Validator to check duplicate department name.
     * @param facesContext
     * @param uIComponent
     * @param object
     */
    public void departmentNameValidator(FacesContext facesContext, UIComponent uIComponent, Object object) {
        if (object != null) {
            String currency = object.toString();
            OperationBinding ob = executeOperation("deptNameValidator");
            ob.getParamsMap().put("deptNm", currency);
            ob.execute();

            if (ob.getResult() != null) {
                String flag = ob.getResult().toString();

                if ("Y".equalsIgnoreCase(flag)) {
                    throw new ValidatorException(new FacesMessage("Duplicate Record",
                                                                  "This Department Name exists in Database"));
                }
            }
        }
    }

Now run and check application -

Sample ADF Application (Jdeveloper 12.1.3)-Download
Cheers :) Happy Learning

1 comment :

  1. Thanks for sharing your experience on java and good coding about it

    Very good blog.

    ReplyDelete