Creating View Criteria having Bind Variables at run time

ADF BC let you to define View Criteria on the fly. In this post, I'm sharing some tips to create and populate criteria items with bind variables to implement the 'query-by-example' at run time.

The following code snippet illustrates the APIs to be used for creating View Criteria with Bind Variables.

String   attribName = "FirstName";
ViewObjectImpl voEmp = getEmployeesView1();


VariableValueManager vvm = voEmp.ensureVariableManager();

ViewCriteria vc = voEmp.createViewCriteria();
ViewCriteriaRow vcr = vc.createViewCriteriaRow();
ViewCriteriaItem vci = vcr.ensureCriteriaItem(attribName);
vci.setOperator(JboCompOper.OPER_LIKE);
vci.setRequired(ViewCriteriaItem.VCITEM_REQUIRED);

VariableImpl fstNameVar =
    (VariableImpl)vvm.addVariable("dynamicBindVarAttribute");
fstNameVar.setJavaType(String.class);
fstNameVar.setMandatory(true);
fstNameVar.setUpdateableFlag(Variable.UPDATEABLE);
fstNameVar.setVariableKind(Variable.VAR_KIND_VIEW_CRITERIA_PARAM);
fstNameVar.setProperty(AttributeHints.ATTRIBUTE_DISPLAY_HINT,
               AttributeHints.ATTRIBUTE_DISPLAY_HINT_HIDE);


vci.setValue(0, ":dynamicBindVarAttribute");
vci.setIsBindVarValue(0, true);
vvm.setVariableValue(fstNameVar, "A%");

vc.insertRow(vcr);
voEmp.applyViewCriteria(vc);
voEmp.executeQuery();


You can download the sample workspace from here.
[Runs with Oracle JDeveloper 11g R1 PS2 + HR Schema].

A glance at the implementation.

This example contains a task flow 'dynamicVC-sample-tf-definition' with a default method activity which is wired to AppModuleImpl::demoDynamicVCWithBindVar(String attribName). This method (demoDynamicVCWithBindVar) creates ViewCriteria on the fly, based on the input parameter passed from the callee. Please take a look at the source ( relevant part is copied above for your reference ).

Learn More ...

There are a lot more points like this. If  you are curious to learn the internals of the ADF Business Components and ADF Binding Layer,  the following book is for you - Oracle ADF Real World Developer’s Guide.
More details about this book can be found in this post- http://jobinesh.blogspot.in/2012/07/pre-order-your-copy-of-oracle-adf-real.html