Determine the arguments of an operation binding at runtime

On the OTN forum, Navaneetha Krishnan answered a question of how to access an Operation Binding at runtime and determine the arguments it expects. Background of the question is that if you call getParamsMap() on the Operation Binding, it does not contain the keys of the method arguments.

The example code shown below accesses an OperationBinding that is defined in the PageDef file for a method. It then dynamically parses the binding definition for configured method argument names.

For example, a method to relocate employees may be exposed on the ADF Business Component model and exposed in the Data Control panel for drag and drop UI binding in ADF. The method signature of the sample may be as shown below

relocateEmployee(Number departmentId, Number employeeId,
Boolean withSalaryRaise, Long salaryRaiseInPercent)

The easiest way for you to create a method like the one below, is to drag and drop the method from the DataControls panel to the page and have it rendered as a button or link. A binding dialog opens for you to define default values or reference objects that provide the argument values at runtime.

Double click the command item to create a managed bean method that contains generated code for the operation to invoke ("relocateEmployee" in the sample). The following managed bean method was built this way and then extended with the code posted by Navaneetha.

The code sample has a place holder line for where your application would look up the argument values to pass to the method. For example, if there is a HashMap available (e.g. exposed by a managed bean, or passed as an input parameter to a bounded task flow) then you could check if it contains values for the operation argument names you read from the OperationBinding.

import
java.util.Map;

import oracle.adf.model.BindingContext;
import oracle.adf.model.OperationParameter;
import oracle.adf.model.binding.DCInvokeMethod;
import oracle.adf.model.binding.DCInvokeMethodDef;
import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;
 //method called from a
command item in ADF Faces 
public String onRelocateEmployee() {
    //generated binding access code
    BindingContainer bindings = getBindings();
    //access the method binding in the  PageDef file
    OperationBinding operationBinding = 
                     bindings.getOperationBinding("relocateEmployee");
    //to pass arguments to an operation binding, a Map is used.
    //the Map is retrieved by a call to getParamsMap on the operation
    //binding
    Map operationParamsMap = operationBinding.getParamsMap();
    //get access to the operation definition
    DCInvokeMethod method = 
                  (DCInvokeMethod)operationBinding.getOperationInfo();
    if (method != null) 
    {
      DCInvokeMethodDef methodDef = method.getDef();
        if(methodDef != null) 
        {
           OperationParameter[] operationParameters = null;
           operationParameters  = methodDef.getParameters();
           if (operationParameters != null) {
             for (OperationParameter operationParameter : operationParameters) 
             {
               String argumentName = operationParameter.getName();
               Object argumentType = operationParameter.getTypeName();
               Object defaultValue = operationParameter.getValue();
               if (argumentName != null) {
                 Object value = <determine value for argumentNname>; 
                 operationParamsMap.put(argumentName, value != null ? 
                                        value : defaultValue); 
               }
             }
           }
        }
    }
    //the operation arguments are provided. Now it is time to
   //execute it
   Object result = operationBinding.execute();
   if (!operationBinding.getErrors().isEmpty()) 
   {
     //TODO log error
     //TODO handle error
     return null;
  }
  return null;
}

Comments:

Post a Comment:
Comments are closed for this entry.
About

The Oracle JDeveloper forum ranks in the Top 5 of the most active forums on the Oracle Technology Network (OTN).



The OTN Harvest blog is a summary of selected topics posted on the OTN Oracle JDeveloper forum.



It is an effort to turn knowledge exchange into an interesting read for developers who enjoy little nuggets of wisdom





Frank Nimphius

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today