Tuesday, October 12, 2010

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

11 comments:

Anonymous said...

Hi Jobinesh
Sometimes i use EJB & JPA in Fusion applications, what is your opinion on that ?

Jobinesh said...

Answer depends on what you are trying to implement(your use case + time + resources you have + lot of other factors...). In a nutshell - Use the technology if that works for you :) . btw, on a related note, if you have used ADF BC for building part of business service, then personally I don't prefer mixing ADF BC with JPA, as these two are trying to solve similar issues. Stick on one stack, which may help you during different stages of dev. cycle.

Anonymous said...

Alexander
Question on
vci.setRequired(ViewCriteriaItem.VCITEM_REQUIRED);
its do FIELD=:dynamicBindVarAttribute

If need
(Upper(FIELD) like Upper(:dynamicBindVarAttribute) or (FIELD is Null) ); ?

Jobinesh said...

Override ViewObjectImpl::getCriteriaItemClause(ViewCriteriaItem vci) and generate the query you want.Please take a look at this post http://jobinesh.blogspot.com/2010/09/using-bind-variable-for-sql-statements.html

VK said...

Hi Jobinesh,
I need few clarifications on this topic. If we bind variables in Viewcriteria and if we drag this into jsp page those bind varibles are coming as input fields. Suppose if we dont want to show some bindvariables as input fields in front end but need to use in viewcriteria as bindvariables can we get the option to do that. If so please post the comment ASAP.

Jobinesh said...

Set the Display hint=Hide using the Control hints tab for the bind variable

Ankur Pradhan said...

Hi Jobinesh,
Working on Jdev Version 11.1.1.6.0.

I have a 2 selectOneChoice (language and status) box outside Table, based on 2 Table attributes. I want to control filtering of table based on selectOneChoice input.

I am not able to filter the table data based on those input, Currently, I am able to get the value selected in selectOneChoice via valueChangeEvent, and all the column data value for which filtering need to be done,
but not getting right approach how to filter table based on choice.

Code snippet....

Managed Bean

public void filterByLanguage(ValueChangeEvent valueChangeEvent) {
// Add event code here...
valueChangeEvent.getComponent().processUpdates(FacesContext.getCurrentInstance());
BindingContainer bindings =
BindingContext.getCurrent().getCurrentBindingsEntry();
AttributeBinding attributeBinding =
(AttributeBinding)bindings.getControlBinding("LanguageName1");
if (attributeBinding != null) {
System.out.println("@@@@@@@@@@@"+attributeBinding.getInputValue());
}
if(valueChangeEvent != null) {
Object newVal = valueChangeEvent.getNewValue().toString();
Object oldVal = valueChangeEvent.getOldValue();
int i=0;
DCIteratorBinding binding = ADFUtils.findIterator("SprPatchDetailsVO1Iterator");
Row[] rows = binding.getAllRowsInRange();
for (Row row : rows)
{
String language = (String)row.getAttribute("Language");
i++;
System.out.println("Language in Row :"+ i + ":" + language);
}
jspx code:





Pls suggest.

Anonymous said...

Hi Jobinesh
i have one requirement that is same as it is i followed in tutorial after that i can maintaining the select option box in that selected based on the option the "go" button action is perform two things that is the selected option is have to match table then open table otherwise it goes to open the form


i need code for this requirment

Anonymous said...

Hi Jobinesh
i have one requirement that is same as it is i followed in tutorial after that i can maintaining the select option box in that selected based on the option the "go" button action is perform two things that is the selected option is have to match table then open table otherwise it goes to open the form


i need code for this requirment

Anonymous said...

Hi Jobinesh
i have one requirement that is same as it is i followed in tutorial after that i can maintaining the select option box in that selected based on the option the "go" button action is perform two things that is the selected option is have to match table then open table otherwise it goes to open the form


i need code for this requirment

bantal silikon said...

this is good post...

i like this...

please can you visit here..

http://bantalsilikon01.blogspot.com/
http://bantalsilikon01.blogdetik.com/
http://bantalkekabumurah.blogspot.com/
http://bumbupecel1.blogspot.com/
http://bantalgood.blogspot.com/
http://bantalsilikonmalang1.blogspot.com/
http://bantalsilikonmurah.blogspot.com/

http://marinirseo.blogspot.com/
http://marinirseo.blogspot.com/
http://marinirseo.blogspot.com/


tengs very much...