ADF 11g : Change a Viewobjects’ Query on the Fly

Luc Bors 1
0 0
Read Time:3 Minute, 33 Second

One of the requirements at my current project is to have one ADF table display data from different database tables. That is, depending on criteria entered by the user, the query behind the view object needs to change. All in all 12 different database tables are involved in this story. This requirement is based on functionally in the original (oracle forms) application. This forms application used the set_block_property built-in:

set_block_property('<blockName>, query_data_source_name, <datasource>);

I was able to reproduce this behavior in an ADF application. In this post I explain how I did this

Database and ADF Business Components.
Luckily these tables all have the same attributes so it is relatively easy to achieve this. I created two database views. One based on the employees table, and a second one based on the locations table. Mark that I defined identical column aliases for both views.

create view EMP_ABSTRACT_VW as
select
EMPLOYEE_ID IDENTIFIER,
FIRST_NAME DESCRIPTION
from employees
create view LOC_ABSTRACT_VW as
select
location_id IDENTIFIER,
city DESCRIPTION
from LOCATIONS

Next I created an ADF Business Components viewObject based on one of the database views. I called this viewObject ‘dataprovider’.

select
IDENTIFIER objId
,DESCRIPTION objDesc
from emp_abstract_vw

ADF Faces Page.

To display the data on a page I just created a simple page and dropped the collection from the datacontrol pallet onto the page as an ADF Table component. When running the page, you see data from the emp_abstract_vw as expected.

Preparing the Query Change Method.

In order to change the query I have to create a method on the application module. This method gets a handle to the viewObject, sets some properties, changes the query and executes it. Ok, not that fast. In the codefragment below you see one line where I use setFullSqlMode. Lets explain what FULLSQL_MODE_AUGMENTATION does.

First I apply the new programmatic query by calling setQuery() and execute the query. If I need to call setWhereClause or if a user changes criteria the query, the ADF Business Components framework augments the whereClause on the programmatic query. If you don’t use FULLSQL_MODE_AUGMENTATION any changes by setWhereClause or change of query criteria are applied to the original query (that is the query that was defined design time).

static String baseQuery = "select IDENTIFIER objId, DESCRIPTION objDesc from ";

public void changeBaseTable(String baseTable){

String query = baseQuery + baseTable;
ViewObjectImpl voi = getdataProvider();
voi.setFullSqlMode(voi.FULLSQL_MODE_AUGMENTATION);
voi.setQuery(query);
voi.executeQuery();
}

Next step is to publish this method so I can use it on my page. I drop the method from the datacontrol, resulting in a methodAction in my bindingcontainer.

<methodAction id="changeBaseTable" RequiresUpdateModel="true"
Action="invokeMethod" MethodName="changeBaseTable"
IsViewObjectMethod="false" DataControl="AppModuleDataControl"
InstanceName="AppModuleDataControl.dataProvider">
<NamedData NDName="baseTable" NDType="java.lang.String"/>
</methodAction>

Final step is the implementation of a selectOneChoice with a value change listener to change the basetable for the query.
The selected value corresponds to the name of one of the database views, and will be directly applied to the viewObjects’ query.

<af:selectOneChoice label="Basetable...&gt;&gt;&gt; " id="soc1"
value="#{pageFlowScope.SwitchIterator.baseTable}"
autoSubmit="true"
valueChangeListener="#{pageFlowScope.SwitchIterator.switchBaseTable}">
<af:selectItem label="Employees" value="EMP_ABSTRACT_VW"
id="si1"/>
<af:selectItem label="Locations" value="LOC_ABSTRACT_VW"
id="si2"/>
</af:selectOneChoice>

The valueChangeListener executes the methodBinding, thus changing the query.

public void switchBaseTable(ValueChangeEvent valueChangeEvent) {
setSource(valueChangeEvent.getNewValue().toString());
}

public void setSource(String baseTable) {
// Add event code here...

DCBindingContainer bc = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
OperationBinding oper = bc.getOperationBinding("changeBaseTable");
oper.getParamsMap().put("baseTable", baseTable );
oper.execute();
}

Now when you start the application you will see the table displaying all employees.
This is the default query behind the viewobject.

By changing the value of the listbox, you will also change the viewobjects’ query, resulting in locations data being displayed in the table.

A sample workspace can be downloaded here.

About Post Author

Luc Bors

Luc Bors is Expertise Lead ADF and technical specialist/architect at AMIS, Nieuwegein (The Netherlands). He developed several Workshops and training on ADF and also is an ADF and JHeadstart instructor. Luc is a member of the ADF Methodology group and publishes articles on ADF in oracle technology related magazines, on the AMIS technology blog, (http://technology.amis.nl/blog).
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

One thought on “ADF 11g : Change a Viewobjects’ Query on the Fly

Comments are closed.

Next Post

Masterclass for Java Developers - Make that Database Work for You! 17th December 2010

Really getting the most out of your database. That would be nice, given wat databases can do (and what you may have paid for them). Few serious Java Applications are completely devoid of interaction of some kind with a database. However, most ORM and  Java Persistency frameworks do their utmost to […]
%d bloggers like this: