Thursday, January 24, 2013

Display Employees Based on Department ID

User will enter a department ID in the input text component, and then press Tab key. Employees corresponding to that department ID will be displayed. The slider will move representing the number of employees in that department.


Properties of the input text component:
(Appearence) ShowRequired: True
AutoSubmit: True
ValueChangeListener: #{test.displayTable}
(Behaviour) Required: True


This is the method defined in the managed bean:


private RichInputNumberSlider slider;
 
public void displayTable(ValueChangeEvent valueChangeEvent) {
 
    Integer getValue = null;
 
    try {
        //Validating integer. If it is a string, else it will go into exception
        getValue = Integer.valueOf(valueChangeEvent.getNewValue().toString());
 
        BindingContext bctx = BindingContext.getCurrent();
        BindingContainer bindings = bctx.getCurrentBindingsEntry();
        OperationBinding method = (OperationBinding)bindings.get("displayEmployeeTable");
        method.getParamsMap().put("deptId", getValue);
 
        Object retValue = method.execute();
        slider.setValue(retValue);
        //setSlider(retValue);
        AdfFacesContextImpl.getCurrentInstance().addPartialTarget(slider);
    } catch (Exception e) {
        //Display message on the screen. '"it1' is the id of the inputtext component
        FacesContext fctx = FacesContext.getCurrentInstance();
        fctx.addMessage("it1", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Enter a Valid Department ID", null));
    }
}


Now imagine there is no input text component, and we want to show it as an LOV. In that case we can use the code below to fetch the value from LOV:

    public void displayNumber(ValueChangeEvent valueChangeEvent) {
        BindingContext bctx = BindingContext.getCurrent();
        BindingContainer bindings = bctx.getCurrentBindingsEntry();

        /*The DepartmentId is a choice list and its default binding gives you the position of the selected value in the list, not the value. So create an attribute
value named DepartmentIdValue. This could have been done rather than using JUCtrlListBinding.
        AttributeBinding attr = (AttributeBinding)bindings.getControlBinding("DepartmentIdValue");
        Number selectedValue = (Integer)attr.getInputValue();
        System.out.println("selectedValue: " + selectedValue);*/
        
        //The class below does the exact job what we want
        JUCtrlListBinding list = (JUCtrlListBinding) bindings.get("DepartmentId");  
        Number selectedValue = (Number) list.getAttributeValue();
        System.out.println("selectedValue: " + selectedValue);

        OperationBinding method = (OperationBinding)bindings.get("displayNumberEmployees");
        ...
        ...
    }

Here we are defining a view criteria programatically in AM

    public Number displayEmployeeTable(Integer deptId) {
        ViewObjectImpl vo = getEmployees1();
        ViewCriteria vc = vo.createViewCriteria();
        ViewCriteriaRow vcr1 = vc.createViewCriteriaRow();
        vcr1.setAttribute("DepartmentId", +deptId);
        vc.add(vcr1);
        vo.applyViewCriteria(vc);
        System.out.println("Query: " + vo.getQuery());
        vo.executeQuery();
        return vo.getEstimatedRowCount();
    }
TIP: In case we have to disable the view criteria that we have created programatically, then use the below code:

vo.applyViewCriteria(null);
vo.executeQuery();
In case we had thought of appending a where clause at runtime instead of view criteria, then

    public Number displayEmployeeTable(Integer deptId) {
        ViewObjectImpl vo = getEmployees1();
        vo.setWhereClause("EmployeesEO.DEPARTMENT_ID = :bindVarDeptId");
        vo.defineNamedWhereClauseParam("bindVarDeptId", null, null);
        vo.setNamedWhereClauseParam("bindVarDeptId", deptId);
        vo.executeQuery();
        return vo.getEstimatedRowCount();
    }
Download the sample application from File Cabinet: DispEmployeesBasedDeptId.rar

No comments:

Post a Comment