Wednesday, December 28, 2011

Oracle ADF - Storing temporary values in PageFlowScope created at Run time

Greetings,

Some time you need to store temporary values on the page like a global variable. Oracle ADF provides various bean scopes like pageflow scope, session scope etc etc we can definitely use them

But can you create pageflow scope at run-time? the answer is YES


Lets explore a simple example

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



Technical detail:

  • We have an input text component and its value using a page flow scope bean attribute called 'counter'
  • We got a button on screen which is calling an action listener method doCounting()

How Example works:

  • When we pressed the button on screen it creates a pageflow scope bean on runtime called 'myCounter' and increment this value on every button pressed.
  • The value of myCounter then assigned to pageFlowscope attribute in a managed bean called 'counter' which is visible on screen as output text

Following is the code on button with other helping methods

    //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);
        }
    }


Download the sample code

Note: There are many ways to do the same task one technique is demonstrated by Andrejus click here

Have a nice day,
Zeeshan Baig

Tuesday, December 13, 2011

ADF task flow webinar details - Repost


Hi,

I appreciate for all your amazing support for this session even i had to rescheduled it due to emergency.

This Thursday the show must go on.....

Here are the details i will cover much more than the description

December 15, 2011 3:00 PM - 4:00 PM EST
ADF Task Flows for Beginners
Zeeshan Baig


ADF Task flow is most important and essential feature of ADF development which makes ADF unique in the battle of frameworks. This session will help you to understand different components and techniques to build task flows as well as building static and dynamic regions on JSF pages.

During this session we will dive into the following:

1. Understanding different components to build task flows.
2. Building task flows templates.
3. Building static and dynamic regions.
4. Basic techniques to build reusable task flows.


Registration Web Link: https://www3.gotomeeting.com/register/133729022

Thursday, December 1, 2011

Today's webinar is rescheduled to December 15, 2011

Hi,

Due to some emergency we have rescheduled the webinar which was going to held today at 2 PM CST to December 15, 2011 to same time.

More info will post soon.

Zeeshan Baig