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:

  1. import oracle.adfinternal.view.faces.model.binding.FacesCtrlActionBinding;
  2. import javax.faces.application.FacesMessage;
  3. import javax.faces.context.FacesContext;
  4. import javax.faces.el.ValueBinding;
  5. import oracle.adf.model.binding.DCBindingContainer;
  6. import oracle.binding.BindingContainer;
  7. import oracle.jbo.Transaction;
  8. import oracle.jbo.domain.Number;
  9. import oracle.jbo.server.ApplicationModuleImpl;
  10. import oracle.jbo.uicli.binding.JUIteratorBinding;
  11.  
  12. public class ADFUtils {
  13.  
  14. private BindingContainer bindings; //this is mainly used to store bindings, but in fact its just an implementation of java.util.Map
  15. private DCBindingContainer dcBindings; //whilst this class gives much more control over the bindings
  16. private FacesContext ctx;
  17. private static Utils instance;
  18. private Transaction transaction;
  19.  
  20. public ADFUtils(BindingContainer bindings) {
  21. this.bindings = bindings;
  22. this.ctx = FacesContext.getCurrentInstance();
  23. ValueBinding vb = this.ctx.getApplication().createValueBinding("#{bindings}");
  24. this.dcBindings = (DCBindingContainer) vb.getValue(this.ctx);
  25. this.transaction = dcBindings.getDataControl().getApplicationModule().getTransaction();
  26. }
  27.  
  28. public Object resolveExpresssion(String expression) {
  29. Object obj = this.bindings.get(expression);
  30. return obj;
  31. }
  32.  
  33. public JUIteratorBinding resolveIterator(String iterName) {
  34. JUIteratorBinding iter = (JUIteratorBinding)this.getBindings().get(iterName);
  35. return iter;
  36. }
  37.  
  38. public FacesCtrlActionBinding resolveAction(String actionName) {
  39. FacesCtrlActionBinding action = (FacesCtrlActionBinding) this.getBindings().get(actionName);
  40. return action;
  41. }
  42.  
  43. public void resolveActionAndExecute(String actionName) {
  44. FacesCtrlActionBinding action = resolveAction(actionName);
  45. action.execute();
  46. }
  47.  
  48. public JUIteratorBinding executeActionAndGetIterator(String actionName) {
  49. FacesCtrlActionBinding action = resolveAction(actionName);
  50. action.execute();
  51. JUIteratorBinding iter = action.getIteratorBinding();
  52. return iter;
  53. }
  54.  
  55. /*
  56. * This affects only the application module the bindings object belongs to.
  57. */
  58. public void globalCommit() throws Exception {
  59. if (transaction.isDirty()) {
  60. transaction.commit();
  61. System.out.println("Changes have been committed");
  62. }
  63. else {
  64. System.err.println("No unmodified data to commit");
  65. }
  66. }
  67.  
  68. /*
  69. * This affects only the application module the bindings object belongs to.
  70. */
  71. public void globalRollback() {
  72. if (transaction.isDirty()) { //and transaction has modified data
  73. transaction.rollback();
  74. System.out.println("Changes have been rollbacked");
  75. }
  76. else {
  77. System.err.println("No unmodified data to rollback");
  78. }
  79. }
  80.  
  81. //MESSAGES
  82.  
  83. public boolean hasErrors() {
  84. FacesMessage.Severity maxSeverity = this.ctx.getMaximumSeverity();
  85. return (maxSeverity != null? maxSeverity.compareTo(FacesMessage.SEVERITY_ERROR) == 0 || maxSeverity.compareTo(FacesMessage.SEVERITY_FATAL) == 0 : false);
  86. }
  87.  
  88. public void inform(String messageText) {
  89. FacesMessage message = new FacesMessage(messageText);
  90. message.setSeverity(FacesMessage.SEVERITY_INFO);
  91. this.ctx.addMessage(null, message);
  92. }
  93.  
  94. public void setBindings(BindingContainer bindings) {
  95. this.bindings = bindings;
  96. }
  97.  
  98. public BindingContainer getBindings() {
  99. return bindings;
  100. }
  101.  
  102. public Transaction getTransaction() {
  103. return transaction;
  104. }
  105.  
  106. public DCBindingContainer getDcBindings() {
  107. return dcBindings;
  108. }
  109.  
  110. public ApplicationModuleImpl getAppModuleImpl(String dataControlName){
  111. javax.faces.context.FacesContext ctx = javax.faces.context.FacesContext.getCurrentInstance();
  112. javax.faces.el.ValueBinding bind = ctx.getApplication().createValueBinding("#{data}");
  113. oracle.adf.model.BindingContext bindingContext = (oracle.adf.model.BindingContext) bind.getValue(ctx); //resolve binding context
  114. oracle.adf.model.binding.DCDataControl dataControl = bindingContext.findDataControl(dataControlName);//find data control by name (defined in DataBindings.cpx) from BindingContext
  115. /*
  116. * finally get the View Object instance which the iterator is bound to (see the attribute Binds in the iterator definition in the pageDef)
  117. * then invoke the magic method executeEmptyRowSet on it
  118. */
  119. return (ApplicationModuleImpl) dataControl.getDataProvider();
  120. }
  121.  
  122. }

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