private Object getFormBean()

in ti/phase2/jars/core/src/java/org/apache/ti/processor/chain/pageflow/ChooseFormBean.java [44:137]


    private Object getFormBean(PageFlowAction action) {
        //
        // See if we're using a pageflow-scoped form (a member variable in the current pageflow).
        //
        Field formMemberField = getPageFlowScopedFormMember(action);
        
        //
        // First look to see whether the input form was overridden in the request.
        // This happens when a pageflow action forwards to another pageflow,
        // whose begin action expects a form.  In this case, the form is already
        // constructed, and shouldn't be instantiated anew or populated from request
        // parameters.
        //
        Object previousForm = InternalUtils.getForwardedFormBean( false );
        PageFlowActionContext actionContext = PageFlowActionContext.get();        
        
        if ( previousForm != null )
        {
            //
            // If there was a forwarded form, and if this action specifies a pageflow-scoped form member,
            // set the member with this form.
            //
            if ( formMemberField != null )
            {
                try
                {
                    FlowController fc = actionContext.getFlowController();
                    assert fc != null : "no FlowController in request " + actionContext.getRequestPath();
                    formMemberField.set( fc, previousForm );
                }
                catch ( IllegalAccessException e )
                {
                    _log.error( "Could not access page flow member " + formMemberField.getName()
                            + " as the form bean.", e );
                }
            }
            
            //
            // Return the forwarded form.
            //
            return previousForm;
        }
        
        //
        // First see if the previous action put a pageflow-scoped form in the request.  If so, remove it;
        // we don't want a normal request-scoped action to use this pageflow-scoped form.
        //
        String pageFlowScopedFormName = actionContext.getPageFlowScopedFormName();
        Map requestScope = actionContext.getWebContext().getRequestScope();
        if ( pageFlowScopedFormName != null )
        {
            requestScope.remove( pageFlowScopedFormName );
            actionContext.setPageFlowScopedFormName( null );
        }
        
        //
        // If this action specifies a pageflow-scoped form member variable, use it.
        //
        if ( formMemberField != null )
        {
            try
            {
                FlowController fc = actionContext.getFlowController();
                Object form = formMemberField.get( fc );
                
                if ( form == null ) // the pageflow hasn't filled the value yet
                {
                    form = createActionForm(action);
                    
                    // TODO: make an interface for reset(), and call reset.
                    // form.reset( mapping, request );
                    formMemberField.set( fc, form );
                }
                
                //
                // Store the form in the right place in the request, so Struts can see it.
                // But, mark a flag so we know to remove this on the next action request -- we don't
                // want this form used by another action unless that action uses the pageflow-scoped
                // form.
                //
                String formAttrName = action.getFormBeanAttribute();
                requestScope.put( formAttrName, form );
                actionContext.setPageFlowScopedFormName( formAttrName );
                return form;
            }
            catch ( IllegalAccessException e )
            {
                _log.error( "Could not access page flow member " + formMemberField.getName() + " as the form bean.", e );
            }
        }
        
        Object bean = createActionForm(action);
        return bean;
    }