Monday 11 November 2013

WHEN-NEW-FORM-INSTANCE in ADF

A frequently asked question for ADF developers is: how can I execute a method on page load?
It would be the equivalent of the WHEN-NEW-FORM-INSTANCE of Form Builder.
On forums and blogs there are many solutions but I have adopted an alternative.
I needed a method that triggers on load of each fragment of a taskflow (with all the items already designed).
So, in ADF I wanted to implements two moments:

  1. The first called WHEN-NEW-TASKFLOW-INSTANCE fires when the first fragment of the taskflow loads: fires once.
  2. The second called WHEN-NEW-FRAGMENT-INSTANCE fires on load of each fragment of the taskflow: fires every time the fragment is invoked (therefore depends of the navigation).

An easy way to do this is using the rendered property of the outer UIComponent cotainer.
In each my fragments I use a panelStretchLayout like container (I think many of you do the same thing).
The rendered property is a method in my backingBean like:

<af:panelStretchLayout id="psl1" rendered="#{backingBeanScope.MyBean.myRendered}">
[...]

We can use the "get" method of "myRendered" property for execute a method.
This is not over.
We know that the method of rendered is executed multiple times during the loading of the page (we are not certain of having control).
To perform our methods only when we need we can do:

    public String getMyRendered(){
        final String TASK_FLOW_INSTANCE_FIRED = "TASK_FLOW_INSTANCE_FIRED";
        final String FRAGMENT_INSTANCE_FIRED = "FRAGMENT_INSTANCE_FIRED";
        AdfFacesContext afc = AdfFacesContext.getCurrentInstance();
        if(afc == null){
            return "true";
        }
        String tf = (String)afc.getPageFlowScope().get(TASK_FLOW_INSTANCE_FIRED);
        if(tf == null){
            <<CALL WHEN-NEW-TASKFLOW-INSTANCE METHOD>>
            afc.getPageFlowScope().put(TASK_FLOW_INSTANCE_FIRED, "1");
        }
        String frag = (String)afc.getViewScope().get(FRAGMENT_INSTANCE_FIRED);
        if(frag == null){
            <<CALL WHEN-NEW-FRAGMENT-INSTANCE METHOD>>
            afc.getViewScope().put(FRAGMENT_INSTANCE_FIRED, "1");
        }
        return "true";
    }

The method returns always "true" but uses the View and the PageFlow scopes for control the executions of  our triggers: when-new-taskflow-instance and when-new-fragment-instance.
As previously mentioned in these moments we have all of the items loaded and we can do operations on them.

No comments:

Post a Comment