Showing posts with label validator. Show all posts
Showing posts with label validator. Show all posts

Sunday, 18 March 2018

ADF | Things not to do in a Component Validator in ADF

Hi,

This a very newbie post that will help new ADF Developers.

In ADF we use component validator to validate any value that Is being posted in the binding layer. This is a very effective way to avoid any garbage post in the Application.

During the development we face many challenges and to meet those challenges we do different types of things.  Sometimes we write codes at inappropriate places which in turn puts an adverse effect on the Applications performance. 

So here are few of the things that should not be done in a Validator of a Component in ADF.

  1. Never use the validator to validate the Mandatory field. This is because the validator doesn’t get fired in case of a null value in the component. So sometimes people are expecting a validation in case of null value and it doesn't get fired.
  2. Never use database function to do CRUD operation or any kind of CRUD Operation in the Application in a Validator because the Validator runs every time the data is being posted in the binding layer and the update will run many times which will have a negative impact on the database and Application performance.
  3. Never use processUpdates  inside a Validator ProcessUpdate updates the value of the component in the binding layer which will actually defeat the purpose of the validator.

Thanks.

Friday, 22 February 2013

ADF : Phonenumber validator

## Phonenumber validator


public void phoneNoValidator(FacesContext facesContext, UIComponent uIComponent, Object object) {
String msg2 = "";
if (object != null) {
    String phnNo = object.toString();
    int openB = 0;
    int closeB = 0;
    boolean closeFg = false;
    char[] xx = phnNo.toCharArray();
          for (char c : xx) {
               if (c == '(') {
                   openB = openB + 1;
               } else if (c == ')') {
                   closeB = closeB + 1;
               }

               if (closeB > openB) {
                   closeFg = true; //closed brackets will not be more than open brackets at any given                    time.
               }
           }
           //if openB=0 then no. of closing and opening brackets equal || opening bracket must always come before closing brackets
           //closing brackets must not come before first occurrence of openning bracket
           if (openB != closeB || closeFg == true || (phnNo.lastIndexOf("(") > phnNo.lastIndexOf(")")) ||
               (phnNo.indexOf(")") < phnNo.indexOf("("))) {
               msg2 = "Brackets not closed properly.";
               FacesMessage message2 = new FacesMessage(msg2);
               message2.setSeverity(FacesMessage.SEVERITY_ERROR);
               throw new ValidatorException(message2);
           }
           if (phnNo.contains("()")) {
               msg2 = "Empty Brackets are not allowed.";
               FacesMessage message2 = new FacesMessage(msg2);
               message2.setSeverity(FacesMessage.SEVERITY_ERROR);
               throw new ValidatorException(message2);
           }
           if (phnNo.contains("(.") || phnNo.contains("(-") || phnNo.contains("-)")) {
               msg2 = "Invalid Phone Number.Check content inside brackets.";
               FacesMessage message2 = new FacesMessage(msg2);
               message2.setSeverity(FacesMessage.SEVERITY_ERROR);
               throw new ValidatorException(message2);
           }

           openB = 0;
           closeB = 0;
           closeFg = false;
           //check for valid language name.Allowed- brackets,dots,hyphen

           String expression = "([0-9\\-\\+\\(\\)]+)";
           CharSequence inputStr = phnNo;
           Pattern pattern = Pattern.compile(expression);
           Matcher matcher = pattern.matcher(inputStr);
           String error = "Invalid Phone Number";
           System.out.println("Index of plus is--->" + phnNo.lastIndexOf("+"));
           System.out.println("Bracket index--->" + phnNo.charAt(0));

           if (matcher.matches()) {
               if (phnNo.contains("++") || phnNo.contains("--")) {
                   throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, error,"Can not contain two hyphen(--) or plus(++)"));
               } else if (phnNo.lastIndexOf("+") > 1) {
                   throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, error,"Plus sign should be in proper place"));
               } else if (phnNo.lastIndexOf("+") == 1 && phnNo.charAt(0) != '(') {
                   throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, error,"Plus sign should be in proper place"));
               }
               else if(phnNo.startsWith(" ") || phnNo.endsWith(" ")){
                   throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, error,"Space Not allowed at start and end"));
               }
               
           } else {
               throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, error,"Only numeric character,+,() and - allowed"));
           }

       }
   }

Thursday, 21 February 2013

ADF: Code to validate duplicate record in Java Bean.

Hi,

Here am posting the code for applying Duplicate validation in ADF Applications through Java Bean. This can be used to check duplicate records in the oracle ADF Application.


    public void empIdValidator(FacesContext facesContext, UIComponent uIComponent, Object object) {
        if (object != null) {
            // 'am' is the applcation module
            ViewObjectImpl vo = am.getVo();
            // To get current Row off the Vo
            Row cRow = vo.getCurrentRow();
            // Create a Iterator to Iterate Over the ViewObject Vo
            // There is a also a method called vo.getRowSetIterator() but it is advised to use vo.createRowSetIterator(null)
            Integer count = 0;
            RowSetIterator rsi = vo.createRowSetIterator(null);
            while (rsi.hasNext()) {
                Row rw = rsi.next();
                if (rw != cRow) {
                    if (rw.getAttribute("EmpId") != null) {
                        Integer empId = (Integer) rw.getAttribute("EmpId");
                        if (empId = (Integer) object) {
                            count = 1;
                        }
                    }
                }
            }
            // Closing the iterator is advised after the use
            rsi.closeRowSetIterator();
            if (count == 1) {
                FacesMessage message2 = new FacesMessage("Duplicate EmpId !");
                message2.setSeverity(FacesMessage.SEVERITY_ERROR);
                throw new ValidatorException(message2);
            }
        }
    }

If you have any Questions regarding the above code. Feel free to ask. :)

ADF: Show inline messege

## ADF: Show inline messege

public void showMessageButton(ActionEvent actionEvent) {
FacesMessage msg=new FacesMessage("This is a FacesMessage that+
"shows Fatal Error --JavaCup");
msg.setSeverity(FacesMessage.SEVERITY_FATAL);
FacesContext fctx=FacesContext.getCurrentInstance();
fctx.addMessage(null, msg);
}