Monday, February 4, 2013

Let's Validate Email ID in ADF

1. Drag an inputText component. Label it as Email. Drop a validate Regular Expression component over it.

validateregexp messagedetailnomatch="Enter a Valid Email ID" pattern="[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}"

2. We will validate the above scenario by calling a PL/SQL function. This is quite interesting.

    public String validateEmail() {
        // Add event code here...
        BindingContainer bindings = getBindings();

        FacesContext facesContext = FacesContext.getCurrentInstance();
        UIViewRoot root = facesContext.getViewRoot();
        RichInputText inputText = (RichInputText)root.findComponent("it6");
        
        String myString = inputText.getValue().toString();
        System.out.println(myString.toString());
        OperationBinding operationBinding = bindings.getOperationBinding("validateEmailByPlsql");
        operationBinding.getParamsMap().put("email", myString);
        Object result = operationBinding.execute();
        System.out.println(result.toString());
        if (result.toString().equals("FAILURE")) {
            FacesContext fctx = FacesContext.getCurrentInstance();
            fctx.addMessage("it6", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Email is not Valid", null));
        } else {
            FacesContext fctx = FacesContext.getCurrentInstance();
            fctx.addMessage("it6", new FacesMessage(FacesMessage.SEVERITY_INFO, "Email Valid", null));
        }
        return null;
    }

    public String validateEmailByPlsql(String email) {
        return (String)callStoredFunction(VARCHAR2, "xx_check_email(?)", new Object[] { email });
    }

    protected Object callStoredFunction(int sqlReturnType, String stmt, Object[] bindVars) {
        CallableStatement st = null;
        try {
            // 1. Create a JDBC CallabledStatement
            st = getDBTransaction().createCallableStatement("begin ? := " + stmt + ";end;", 0);
            // 2. Register the first bind variable for the return value
            st.registerOutParameter(1, sqlReturnType);
            if (bindVars != null) {
                // 3. Loop over values for the bind variables passed in, if any
                for (int z = 0; z < bindVars.length; z++) {
                    // 4. Set the value of user-supplied bind vars in the stmt
                    st.setObject(z + 2, bindVars[z]);
                }
            }
            // 5. Set the value of user-supplied bind vars in the stmt
            st.executeUpdate();
            // 6. Return the value of the first bind variable
            System.out.println(st.getObject(1).toString());
            return st.getObject(1);
        } catch (SQLException e) {
            throw new JboException(e);
        } finally {
            if (st != null) {
                try {
                    // 7. Close the statement
                    st.close();
                } catch (SQLException e) {
                }
            }
        }
    }

Now define a function 'xx_check_email' in your database which will be taking one String parameter and validate whether the String is a proper email ID or not.

For more details, check the section Invoking Stored Procedure and Functions from Developer's Guide: http://docs.oracle.com/cd/E24382_01/web.1112/e16182/bcadvgen.htm#insertedID6

Now suppose we have an employee record and while creating it, we have to supply the emailID. Then we will press Commit button. In this case I am showing you 2 ways to validate the same.

1. Create a validation rule on email attribute. Set the rule type as Method, with a proper error message in Failure Handling tab. A method will be generated in EntityImpl class
    public boolean validateEmail(String email) {
        int atpos = email.indexOf('@');
        int dotpos = email.lastIndexOf('.');
        if (atpos == -1 || dotpos == -1) {
            return false;
        }
        return true;
    }
2. Domain: I am having some problem while validating the same with domains. I am getting this error message: 'Cannot convert testmailid of type class java.lang.String to class model.common.DomainEmail'.

3. We can validate in the setter method of email.
    public void setEmail(String value) {

        int atpos = value.indexOf('@');
        int dotpos = value.lastIndexOf('.');
        if (atpos == -1 || dotpos == -1) {
            throw new oracle.jbo.JboException("Invalid Email ID");
        }
        setAttributeInternal(EMAIL, value);
    }

No comments:

Post a Comment