Wednesday, September 9, 2009

ADF 11g - Assign default value to attribute(from managed bean) while creating new record at runtime OR Execute with parameter ON PAGE LOAD

To assign default value to entity attribute at runtime:

At run time we want to assign value from a managed bean to an entity attribute while creating a new record

1) You need 3 files for this

1.1) ADFUtils.java

1.2) JSFUtiles.java

1.3) OnPageLoadBackingBeanBase.java

These can be downloaded from steve's undocumented list of application at (sample no. 58)



2) In backing bean (e.g NewShift.java) change it to extent OnPageLoadBackingBeanBase

public class NewShift extends OnPageLoadBackingBeanBase {


3) Now, goto NewShiftPageDef file, and in property section change the "Controller Class" property to the backing bean as in diag


(logic: whenever this page is called the backing bean will refer to the methods in parent class for on load functionality)
4) now you can modify beforePhase metod of OnPageLoadBackingBeanBase to set values of view objects using the JSFUtils class e.g
public void beforePhase(PagePhaseEvent event) { FacesPageLifecycleContext ctx = (FacesPageLifecycleContext)event.getLifecycleContext(); if (event.getPhaseId() == Lifecycle.PREPARE_MODEL_ID) { bc = ctx.getBindingContainer(); System.out.println("ON PAGE LOAD"); //JSFUtils.setExpressionValue("#{bindings.Locationid.inputValue}","20041");
JSFUtils.setExpressionValue("#{bindings.Locationid.inputValue}",UserDetailsPersist.pLocationId); onPageLoad(); bc = null; } }
thats it when your page is called the value from managed bean userdetailspersist is set into the location id attribute on your page
:-)

2) To execute query "execute with parameters" programmatically on page load
- Extend OnPageLoadBackingBeanBase in your page's backing bean
- override onPageLoad method as below.
- go to page definition and change controller class property to have value of this backing bean
(e.g Controller class= view.backing.StaffDetails)

public void onPageLoad(){


BindingContainer bindings = getBindings();

OperationBinding operationBinding = bindings.getOperationBinding("ExecuteWithParams");

Object result = operationBinding.execute();

JSFUtils.setExpressionValue("#{bindings.Location.inputValue}","1024");

if (!operationBinding.getErrors().isEmpty()) {

}

}