Sunday, May 5, 2013

Programatic handle to ADF Query Component - Add View Criteria dynamically

we have already seen how to get handle to the query event : . with this , we move ahead and add a view criteria dynamically to the query event .

Sometimes we may want to validate the query input and based on it we want to add an another criteria. For example :  If the employe id is greater than 100 then add one more criteria which is not a part of ADF view criteria which was created in the adf vo. Lets see the snippet

public void onSearch(QueryEvent queryEvent) {

        String  EmployeeId = null;
        // Query Event is delivered when a query action is triggered
        QueryDescriptor qd = queryEvent.getDescriptor();
        // This line will represent group of criterion objects
        ConjunctionCriterion conCrit = qd.getConjunctionCriterion();
        //access the list of all search fields
        List<Criterion> criterionList = conCrit.getCriterionList();

        for (Criterion criterion : criterionList) {
            AttributeDescriptor attrDescriptor = ((AttributeCriterion)criterion).getAttribute();

            if (attrDescriptor.getName().equalsIgnoreCase("EmployeeId")) { // EmployeeId is one of the query items in the search pane
                EmployeeId  =  (String)((AttributeCriterion)criterion).getValues().get(0);
               //This is how we will access the query field
                System.out.println("EmployeeId :" + EmployeeId );


        DCBindingContainer bc =
        ViewCriteria vc = getViewCriteria(bc, qd);

    System.out.println("View Object"+ vc.getViewObject);

         if (EmployeeId > 100 )
             ViewObject vo  = vc.getViewObject();
              // since we are adding one more vc, we cant invoke querylistener and run it.
             ViewCriteria vc1 = vo.createViewCriteria();
             ViewCriteriaRow vcr = vc1.createViewCriteriaRow();
             vcr.setAttribute("salary", "109090"); // This is not part of VC created at VO

            //This will append the newly created ADF vc to VO along with the VC created in the query event

      //Execute the query Listener using EL. This will execute the query component .If u see the exp , this    was  initially applied to QueryListener.. Later we assigned QueryListener to our custom method.
       invokeMethodExpression("#{bindings.VOCriteriaQuery.processQuery}", queryEvent);


No comments:

Post a Comment