4 Ekim 2012 Perşembe

ADF Task Flow'da PageScope variable'ı View Object Query Classına atamak


You can make a JDeveloper model View Object (VO) use a data query that has a custom variable accessor in Java.  This is useful for cases where you want your where clause to select based on the current user name from the ADF Security context.
Go to the “Query” tab of your view object:

Add a new Bind Variable and make sure to specify “Expression” and give the value the Groovy expression “adf.object.viewObject.yourPropertyName”.  The “adf.object.viewObject” portion is essentially like saying “this object” and then it will call getYourPropertyName() to fetch the value:

Back in your query expression, refer to this new bind variable by its name but prepend “:” to indicate that it is a bind variable as shown above.
Select the “Java” tab of your view object and then click the pencil icon to edit the settings:

Select the “Generate View Object Class” and “Include bind variable accessors” options:

This will generate a new “.java” entry under your view object in the JDeveloper application navigator:

Finally, open that Java file and change the implementation of your get accessor as you wish.  In my case, I am simply returning the user name property from the ADF security context.  I also changed my set accessor to do nothing:

Example:
When you press the button on the screen it will count and display how many times you have pressed the button and counter will be different for each browser window or tab
//Method on the button
 public void doCounting(ActionEvent actionEvent) {
    
     Number value = (Number)getPageFlowScopeValue("myCounter");
     if (value.intValue() >= 0) {
         setPageFlowScopeValue("myCounter", value.intValue() + 1);
     }
      
     setManagedBeanValue("pageFlowScope.pFlowBean.counter",getPageFlowScopeValue("myCounter"));
 
 }
 //Method to set the value of page flow scope created on runtime
 public void setPageFlowScopeValue(String name, Number value) {
     ADFContext adfCtx = ADFContext.getCurrent();
     Map pageFlowScope = adfCtx.getPageFlowScope();
     pageFlowScope.put(name, value);
 }
 
//method to get the value of page flow scope created on runtime
 public Object getPageFlowScopeValue(String name) {
     ADFContext adfCtx = ADFContext.getCurrent();
     Map pageFlowScope = adfCtx.getPageFlowScope();
     Object val = pageFlowScope.get(name);
  
     if (val == null)
         return 0;
     else
         return val;
 }
 
//Methods used to get and set the values in a Managed bean
 public Object getManagedBeanValue(String beanName) {
     StringBuffer buff = new StringBuffer("#{");
     buff.append(beanName);
     buff.append("}");
     return resolveExpression(buff.toString());
 }
 
 public Object resolveExpression(String expression) {
     FacesContext facesContext = FacesContext.getCurrentInstance();
     Application app = facesContext.getApplication();
     ExpressionFactory elFactory = app.getExpressionFactory();
     ELContext elContext = facesContext.getELContext();
     ValueExpression valueExp = elFactory.createValueExpression(elContext, expression, Object.class);
     return valueExp.getValue(elContext);
 }
 
 
 public void setManagedBeanValue(String beanName, Object newValue) {
     StringBuffer buff = new StringBuffer("#{");
     buff.append(beanName);
     buff.append("}");
     setExpressionValue(buff.toString(), newValue);
 }
 
 public static void setExpressionValue(String expression, Object newValue) {
     FacesContext facesContext = FacesContext.getCurrentInstance();
     Application app = facesContext.getApplication();
     ExpressionFactory elFactory = app.getExpressionFactory();
     ELContext elContext = facesContext.getELContext();
     ValueExpression valueExp = elFactory.createValueExpression(elContext, expression, Object.class);
   
     Class bindClass = valueExp.getType(elContext);
     if (bindClass.isPrimitive() || bindClass.isInstance(newValue)) {
         valueExp.setValue(elContext, newValue);
     }
 }

Hiç yorum yok:

Yorum Gönder