ADF 11g : Query Component with 'dynamic' view criteria

In my current project use a lot of re usable taskflows. In one particular situation I needed exactly the same taskflow to be re-used with one tiny small difference: The displayed query component needed to have different fields compared to page in the base taskflow. Now there are lots of possible solutions (two query components and a switcher, or two query components using the rendered property). I choose a different solution using ternary operator and EL in the page definition
Use case
I need to display a different query component when the taskflow is started in a specific mode. For instance I can startup the taskflow a being a HR manager, or I can startup the taskflow as being an employee. In the first case I need to be able to search on items like salary and commission and hiredate. In the second case I need to be able to search on firstname. lastname and more stuff like that, and I can only see employees in my own department.

Setup:
Lets start with the creation of default business components for the Employee table. Next step is the creation of view criteria. I need two view criteria. The first one is called ManagerVC, the second on EmployeeVC.

ADF 11g : Query Component with 'dynamic' view criteria criteria1

With this in place I create a simple taskflow with a pagefragment. The taskflow has an inputParameter called ‘startupMode’. The pagefragment contains a table with a query component. This is easy to create by dragging the Employees collection from the datacontrol onto the pagefragment and pick “query panel with table”.

ADF 11g : Query Component with 'dynamic' view criteria dropAs

Make sure to have single row selection enabled.

ADF 11g : Query Component with 'dynamic' view criteria singleRow

I also create a testpage in order to run my taskflow. The testpage is a rather simple page containing two buttons, and a bean to hold the value of the button that is pushed.

 <af:toolbar id="t1">
<af:commandToolbarButton text="Start as Manager" partialSubmit="true" id="ctb1">
<af:setPropertyListener from="mgr" to="#{TesterBean.buttonPushed}" type="action"/>
</af:commandToolbarButton>
<af:commandToolbarButton text="Start as Employee" id="ctb2"partialSubmit="true">
<af:setPropertyListener from="emp" to="#{TesterBean.buttonPushed}" type="action"/>
</af:commandToolbarButton>
</af:toolbar>

I drop my taskflow on the page as a region. I link the inputparameter of the taskflow to the buttonPushed property. Don’t forget to set the refresh option to ‘ifNeeded’, so the region will refresh whenever the input paramaters change.

ADF 11g : Query Component with 'dynamic' view criteria dropAsRegion1

When I run the page you will see the querycomponent as expected.

ADF 11g : Query Component with 'dynamic' view criteria manager

The trick:
Now take a look what happened when the query component was created. The page definition states contains an executable binding for the search region.

 <searchRegion Criteria="CriteriaForManager"
Customizer="oracle.jbo.uicli.binding.JUSearchBindingCustomizer"
Binds="EmployeesView1Iterator" id="CriteriaForManagerQuery"/>

This is exactly the place where I will use an EL expression (yes you can do that) to dynamically set the viewcriteria to be used by the query component. Lets change the “criteria” attribute. Whenever the taskflow is started by a manager I want the CriteriaForManager viewcriteria to be used by the querycomponent, and in other cases the CriteriaForEmployee.

 <searchRegion Criteria="#{pageFlowScope.startupMode eq 'mgr' ? 'CriteriaForManager' : 'CriteriaForEmployee'}"
Customizer="oracle.jbo.uicli.binding.JUSearchBindingCustomizer"
Binds="EmployeesView1Iterator" id="CriteriaForManagerQuery"/>

Now this is really all I changed. No changes to the page whatsoever. When I run the page after this minor change, depending on the startupmode, a different querycomponent is rendered. Or actually, the querycomponent stays the same, only the viewcriteria used are different.

ADF 11g : Query Component with 'dynamic' view criteria all

Resources:
The workspace can be downloaded here.