Announcement Announcement Module
Collapse
No announcement yet.
Inspecting transitions available from current state Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Inspecting transitions available from current state

    I am trying to generate a set of buttons on a jsp view, one button per available transition. To do this I need to examine the currentState of the flow when in a view-state.

    I would find it most attractive to be able to put the code inside my webflow somewhere. I thought maybe to put it in the doPostExecute of my Action implementation, but it turns out that this is called *before* the transition to the view-state is made.

    Does anyone have a suggestion about where to put the inspection of the flow to pull out available transitions, or a better way to think about this problem?

    Here is some code that works in a JSP script, if anyone cares. You can see why I want to extract this into a safe place, because it relies on the toString implementation of TransitionCriteria...

    Code:
        // return a list of Strings representing the available transitions
        // (events) from the current state.
        private ArrayList getEvents(PageContext pageContext) {
            FlowContext flow = (FlowContext) pageContext.findAttribute(FlowExecutionManager.FLOW_EXECUTION_ATTRIBUTE);
            ArrayList ret = new ArrayList();
            if (flow==null) return ret;
            State state = flow.getCurrentState();
            if (!state.isTransitionable()) return ret; // no transitions available
            TransitionCriteria[] criteria = ((TransitionableState)state).getTransitionCriterias();
            for &#40;int i=0; i<criteria.length; i++&#41; &#123;
                String crit = criteria&#91;i&#93;.toString&#40;&#41;;
                String event = getEvent&#40;crit&#41;;
                if &#40;event!=null&#41; ret.add&#40;event&#41;;
            &#125;
            return ret;
        &#125;
    
        // Strip off the enclosing single quotes if they are there &#40;if they
        // are not we either have a non-simple event, or webflow has changed
        // the behaviour of TransitionableState&#41;
        private String getEvent&#40;String crit&#41; &#123;
            if &#40;crit==null || "".equals&#40;crit&#41;&#41; return null;
            if &#40;!crit.startsWith&#40;"'"&#41;&#41; return null;
            return crit.substring&#40;1,crit.length&#40;&#41;-1&#41;;
        &#125;
    [/code]

  • #2
    Anser my own question....

    My favourite solution so far is to use a FlowExecutionListener to set up the buttons. Cool.

    Code:
    public class EventsListener extends FlowExecutionListenerAdapter &#123;
    
        public void stateEntered&#40;RequestContext context, State oldState, State newState&#41; &#123;
            context.getRequestScope&#40;&#41;.setAttribute&#40;"events",getEvents&#40;newState&#41;&#41;;
        &#125;
    
        // return a list of Strings representing the available transitions
        // &#40;events&#41; from the current state.
        private ArrayList getEvents&#40;State state&#41; &#123;
            ...
        &#125;
    and in the servlet config:

    Code:
    <bean id="flowExecutionManager" class="org.springframework.web.flow.execution.servlet.ServletFlowExecutionManager">
        <property name="listeners">
            <list>
                <bean class="org.springframework.samples.sellitem.EventsListener"/>
                ....
            </list>
         </property>
    ...
    </bean>

    Comment

    Working...
    X