Pages - Menu

Sunday, September 15, 2013

Back Bean Validation Methods || Oracle ADF

Hi
today, i write a small article to show how to create General Validation Methods to be used in ADF Applications - JSF Interface.
there are 2 types of Validations, in Attribute & Entity Level or general Validation based on business rules.
from technical point of view, in ADF Applications JSF/Interface, we use Faces Messages to control validations with 4 Categories of Errors until info Messages (FATAL,ERROR,WARN,INFO) and finally you add the Faces Message to Null for general Error Message or Component ID for Attribute Validation Message.
  • Examples
    public void ValidateItem(UIComponent MyComponent, FacesContext MyContext, String Header, String Footer,
                             int Level) { // Force Validation
        FacesMessage MyMessage = new FacesMessage();
        if (Level == 1) {
            MyMessage = new FacesMessage(MyMessage.SEVERITY_FATAL, Header, Footer);
        } else if (Level == 2) {
            MyMessage = new FacesMessage(MyMessage.SEVERITY_ERROR, Header, Footer);
        } else if (Level == 3) {
            MyMessage = new FacesMessage(MyMessage.SEVERITY_WARN, Header, Footer);
        } else if (Level == 4) {
            MyMessage = new FacesMessage(MyMessage.SEVERITY_INFO, Header, Footer);
        }

        if (MyComponent instanceof RichInputText) {
            ((RichInputText)MyComponent).setValid(false);
        } else if (MyComponent instanceof RichSelectOneChoice) {
            ((RichSelectOneChoice)MyComponent).setValid(false);
        }

        MyContext.addMessage(MyComponent.getClientId(MyContext), MyMessage);
    }
    public void Validate(FacesContext MyContext, String Header, String Footer,
                             int Level) { // Force Validation
        FacesMessage MyMessage = new FacesMessage();
        if (Level == 1) {
            MyMessage = new FacesMessage(MyMessage.SEVERITY_FATAL, Header, Footer);
        } else if (Level == 2) {
            MyMessage = new FacesMessage(MyMessage.SEVERITY_ERROR, Header, Footer);
        } else if (Level == 3) {
            MyMessage = new FacesMessage(MyMessage.SEVERITY_WARN, Header, Footer);
        } else if (Level == 4) {
            MyMessage = new FacesMessage(MyMessage.SEVERITY_INFO, Header, Footer);
        }

        MyContext.addMessage(null, MyMessage);

    }
Now , in your Back Beans, you can call Validations as below
       cwc.Validate(FacesContext.getCurrentInstance(),
                         “Error Message Header”,
                         “Error Message Footer”, 2);
or
       cwc.ValidateItem(uIComponent, FacesContext.getCurrentInstance(),
                                 “Error Message Header”,
                                 “Error Message Footer”,
                                 2);

No comments:

Post a Comment