Showing posts with label Programmatic. Show all posts
Showing posts with label Programmatic. Show all posts

Wednesday 27 August 2014

How to Get Application module object programmatically


You can use any of the below methods to get the application module programmatically.
>> private EmployeeAMImpl getAm() {

        FacesContext fctx = FacesContext.getCurrentInstance();
        DCBindingContainer dc =
            (DCBindingContainer)fctx.getApplication().getExpressionFactory().createValueExpression(fctx.getELContext(),
                                                                                                   DCBindingContainer.class).getValue(fctx.getELContext());
        return (WorkbenchAMImpl)dc.findDataControl("EmployeeAMDataControl").getApplicationModule();
    }
    

>> private EmployeeAMImpl getAm() {
        
        FacesContext fc = FacesContext.getCurrentInstance();
        Application app = fc.getApplication();
        ExpressionFactory elFactory = app.getExpressionFactory();
        ELContext elContext = fc.getELContext();
        ValueExpression valueExp =
            elFactory.createValueExpression(elContext, "#{data.EmployeeAMDataControl.dataProvider}",
                                            Object.class);
        return (WorkbenchAMImpl)valueExp.getValue(elContext);
    }
 

Programmatic validation UI side

1.Class implements Validator{
2.    @Override
    public void validate(FacesContext facesContext, UIComponent uIComponent,Object newValue) throws ValidatorException {
       String val = String.valueOf(newValue);     
        if (val != null) {
            if (!getMethodResult(val)) {
                throw new ValidatorException(new FacesMessage("Name " + val + " already exists."));
            }
        }
    }

    public boolean getMethodResult(String name) {
        ViewObjectImpl vo = this.getAm().getVo1();
        RowQualifier rowQualifier = new RowQualifier(vo);
                rowQualifier.setWhereClause("Name='"+name+"'");
                Row[] rowsList = vo.getFilteredRows(rowQualifier);
                if(rowsList.length>0){
                        return false;
                    }
                return true;
    }

3. in jsff validator="#{backingBeanScope.myManagedBean.validate}"