Greetings!
This post describes an example solution to the problem. However, I am not satisfied with this approach. I am looking for a more generic, less manual way to do this.
My use case:
I need to filter my view object using a list of attribute values. For example: I have a table of employees. I want to filter the table to only show people who's names are "Alexander", "Alexis" and "David".
My current solution:
public void filterEmployeeByFirstName() {
ViewObjectImpl vo = null;
List<String> employeeFirstNameList = new LinkedList<String>();
employeeFirstNameList.add("Alexander");
employeeFirstNameList.add("Alexis");
employeeFirstNameList.add("David");
String inClause = createSQLCSVString(employeeFirstNameList);
String attributeName = "FirstName";
if (employeeFirstNameList != null) {
vo = getEmployeeVO1();
System.out.println("Row size: " + vo.getAllRowsInRange().length);
ViewCriteria vc = vo.createViewCriteria();
ViewCriteriaRow vcr = vc.createViewCriteriaRow();
vcr.setAttribute(attributeName, "IN " + inClause);
vc.add(vcr);
vo.applyViewCriteria(vc);
vo.executeQuery();
}
System.out.println("Row size: " + vo.getAllRowsInRange().length);
}
/**
// Creates a string with CSV to use with SQL IN operand
// in our example, this method will return ('Alexander','Alexis','David')
*/
protected String createSQLCSVString(List<String> valueList) {
StringBuffer inClauseSB = new StringBuffer();
inClauseSB.append("(");
if (valueList != null && valueList.size() > 0) {
for (String stringValue : valueList) {
inClauseSB.append("'");
inClauseSB.append(stringValue);
inClauseSB.append("',");
}
inClauseSB.deleteCharAt(inClauseSB.length() - 1);
}
inClauseSB.append(")");
return inClauseSB.toString();
}
This is the approach described in the Developer's guide for ADF (11.1.1.6):
5.11.9.5 Searching for Rows Whose Attribute Value Matches a Value in a List
To search for all rows with a value in a column that matches any value in a list of
values that you specify, populate a corresponding view criteria row attribute with the
comma-separated list of values and use the IN operator. For example, to filter the list
of persons by IDs that match 204 and 206, set:
vcr.setAttribute("PersonId","IN (204,206)");
Note that there must be a space between the IN operator and the brace:
■ IN (204,206) is correct.
■ IN(204,206) throws a SQLSyntaxErrorException error.
As you can see, the documentation does not offer any approach that would be more generic than the one I use.
My question:
Is there a better, more "best-practice" way to do this? My solution does exactly what I need, but I am forced to converting the list to a CSV string manually.
I know that BindVariable can be of type Array, however I was unable to find anything fitting my scenario. I would imagine that this use case is quite a common one, and probably there are some better solutions out there.
If you know of a better approach, please share.
Kind regards,
Andrejs
Edited by: Andrejs Dasko on Jun 19, 2012 7:54 AM