This is a follow up to the original post about acquiring a bindings object in backing beans in ADF. Some have had the question, how to use the bindings for standard operations in the backing beans. Hence this post!

The answer is – easy. As soon as you’ve got the bindings (oracle.binding.BindingContainer object) instance, you can do a lot of interesting stuff with it.

Always keep in mind, that it represents the page definition, meaning that it “contains” whatever the pageDef does.

Create a class like the following one:

import oracle.adfinternal.view.faces.model.binding.FacesCtrlActionBinding;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.binding.BindingContainer;
import oracle.jbo.Transaction;
import oracle.jbo.domain.Number;
import oracle.jbo.server.ApplicationModuleImpl;
import oracle.jbo.uicli.binding.JUIteratorBinding;

public class ADFUtils {

private BindingContainer bindings; //this is mainly used to store bindings, but in fact its just an implementation of java.util.Map
private DCBindingContainer dcBindings; //whilst this class gives much more control over the bindings
private FacesContext ctx;
private static Utils instance;
private Transaction transaction;

public ADFUtils(BindingContainer bindings) {
this.bindings = bindings;
this.ctx = FacesContext.getCurrentInstance();
ValueBinding vb = this.ctx.getApplication().createValueBinding("#{bindings}");
this.dcBindings = (DCBindingContainer) vb.getValue(this.ctx);
this.transaction = dcBindings.getDataControl().getApplicationModule().getTransaction();
}

public Object resolveExpresssion(String expression) {
Object obj = this.bindings.get(expression);
return obj;
}

public JUIteratorBinding resolveIterator(String iterName) {
JUIteratorBinding iter = (JUIteratorBinding)this.getBindings().get(iterName);
return iter;
}

public FacesCtrlActionBinding resolveAction(String actionName) {
FacesCtrlActionBinding action = (FacesCtrlActionBinding) this.getBindings().get(actionName);
return action;
}

public void resolveActionAndExecute(String actionName) {
FacesCtrlActionBinding action = resolveAction(actionName);
action.execute();
}

public JUIteratorBinding executeActionAndGetIterator(String actionName) {
FacesCtrlActionBinding action = resolveAction(actionName);
action.execute();
JUIteratorBinding iter = action.getIteratorBinding();
return iter;
}

/*
* This affects only the application module the bindings object belongs to.
*/
public void globalCommit() throws Exception {
if (transaction.isDirty()) {
transaction.commit();
System.out.println("Changes have been committed");
}
else {
System.err.println("No unmodified data to commit");
}
}

/*
* This affects only the application module the bindings object belongs to.
*/
public void globalRollback() {
if (transaction.isDirty()) { //and transaction has modified data
transaction.rollback();
System.out.println("Changes have been rollbacked");
}
else {
System.err.println("No unmodified data to rollback");
}
}

//MESSAGES

public boolean hasErrors() {
FacesMessage.Severity maxSeverity = this.ctx.getMaximumSeverity();
return (maxSeverity != null? maxSeverity.compareTo(FacesMessage.SEVERITY_ERROR) == 0 || maxSeverity.compareTo(FacesMessage.SEVERITY_FATAL) == 0 : false);
}

public void inform(String messageText) {
FacesMessage message = new FacesMessage(messageText);
message.setSeverity(FacesMessage.SEVERITY_INFO);
this.ctx.addMessage(null, message);
}

public void setBindings(BindingContainer bindings) {
this.bindings = bindings;
}

public BindingContainer getBindings() {
return bindings;
}

public Transaction getTransaction() {
return transaction;
}

public DCBindingContainer getDcBindings() {
return dcBindings;
}

public ApplicationModuleImpl getAppModuleImpl(String dataControlName){
javax.faces.context.FacesContext ctx = javax.faces.context.FacesContext.getCurrentInstance();
javax.faces.el.ValueBinding bind = ctx.getApplication().createValueBinding("#{data}");
oracle.adf.model.BindingContext bindingContext = (oracle.adf.model.BindingContext) bind.getValue(ctx); //resolve binding context
oracle.adf.model.binding.DCDataControl dataControl = bindingContext.findDataControl(dataControlName);//find data control by name (defined in DataBindings.cpx) from BindingContext
/*
* finally get the View Object instance which the iterator is bound to (see the attribute Binds in the iterator definition in the pageDef)
* then invoke the magic method executeEmptyRowSet on it
*/
return (ApplicationModuleImpl) dataControl.getDataProvider();
}

}

So now all you have to do is instantiate the ADFUtil class and invoke its methods!