/*
 * Copyright 2002-2004 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.web.flow.mvc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.flow.Flow;
import org.springframework.web.flow.FlowExecutionListener;
import org.springframework.web.flow.FlowLocator;
import org.springframework.web.flow.config.BeanFactoryFlowServiceLocator;
import org.springframework.web.flow.support.HttpFlowExecutionManager;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 * Web controller for the Spring MVC framework that handles requests using a web
 * flow.
 * 
 * @author Erwin Vervaet
 * @author Keith Donald
 */
public class FlowController extends AbstractController implements InitializingBean {

	private Flow flow;

	private Collection flowExecutionListeners;

	private HttpFlowExecutionManager manager;

	/**
	 * Set the top level fow started by this controller. This is optional. When
	 * not specified, the controller will try to obtain a flow id from the
	 * incoming request.
	 */
	public void setFlow(Flow flow) {
		this.flow = flow;
	}

	/**
	 * Set the flow execution listener that should be notified of flow execution
	 * lifecycle events.
	 */
	public void setFlowExecutionListener(FlowExecutionListener listener) {
		this.flowExecutionListeners = new ArrayList(1);
		this.flowExecutionListeners.add(listener);
	}

	/**
	 * Set the flow execution listeners that should be notified of flow
	 * execution lifecycle events.
	 */
	public void setFlowExecutionListeners(FlowExecutionListener[] listeners) {
		this.flowExecutionListeners = Arrays.asList(listeners);
	}

	public void afterPropertiesSet() throws Exception {
		//web flows need a session!
		setRequireSession(true);
		
		FlowLocator flowLocator = new BeanFactoryFlowServiceLocator(getApplicationContext());
		this.manager = new HttpFlowExecutionManager(this.flow, flowLocator, flowExecutionListeners);
	}

	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//delegate to the flow execution manager to process the request
		return manager.handleRequest(request, response);
	}
}
@@ -1397,20 +1397,986 @@
  * flow.
-%0A * 
+ Requests are managed using an %7B@link HttpFlowExecutionManager%7D. Consult%0A * the JavaDoc of that class for more information on how requests are processed.%0A * %0A * %3Cp%3E%0A * This controller requires sessions to keep track of flow state. So it will force%0A * the %22requireSession%22 attribute defined by the AbstractController to true.%0A *%0A * %3Cp%3E%0A * %3Cb%3EExposed configuration properties%3C/b%3E%3Cbr%3E%0A * %3Ctable border=%221%22%3E%0A *  %3Ctr%3E%0A *      %3Ctd%3E%3Cb%3Ename%3C/b%3E%3C/td%3E%0A *      %3Ctd%3E%3Cb%3Edefault%3C/b%3E%3C/td%3E%0A *      %3Ctd%3E%3Cb%3Edescription%3C/b%3E%3C/td%3E%0A *  %3C/tr%3E%0A *  %3Ctr%3E%0A *      %3Ctd%3Eflow%3C/td%3E%0A *      %3Ctd%3E%3Ci%3Enull%3C/i%3E%3C/td%3E%0A *      %3Ctd%3ESet the top level fow started by this controller. This is optional.%3C/td%3E%0A *  %3C/tr%3E%0A *  %3Ctr%3E%0A *      %3Ctd%3EflowExecutionListener(s)%3C/td%3E%0A *      %3Ctd%3E%3Ci%3Enull%3C/i%3E%3C/td%3E%0A *      %3Ctd%3ESet the flow execution listener(s) that should be notified of flow%0A *      execution lifecycle events.%3C/td%3E%0A *  %3C/tr%3E%0A * %3C/table%3E%0A * %0A * @see org.springframework.web.flow.support.HttpFlowExecutionManager
 %0A * @aut
@@ -2697,107 +2697,8 @@
 nal.
- When%0A%09 * not specified, the controller will try to obtain a flow id from the%0A%09 * incoming request.
 %0A%09 *
@@ -3406,93 +3406,42 @@
 %09%0A%09%09
-FlowLocator flowLocator = new BeanFactoryFlowServiceLocator(getApplicationContext());
+//setup our flow execution manager
 %0A%09%09t
@@ -3454,20 +3454,22 @@
 nager = 
-new 
+create
 HttpFlow
@@ -3489,54 +3489,8 @@
 ger(
-this.flow, flowLocator, flowExecutionListeners
 );%0A%09
@@ -3738,9 +3738,380 @@
 se);%0A%09%7D%0A
+%09%0A%09/**%0A%09 * Create a new HTTP flow execution manager. Subclasses can override%0A%09 * this to return a specialed manager.%0A%09 */%0A%09protected HttpFlowExecutionManager createHttpFlowExecutionManager() %7B%0A%09%09FlowLocator flowLocator = new BeanFactoryFlowServiceLocator(getApplicationContext());%0A%09%09return new HttpFlowExecutionManager(this.flow, flowLocator, flowExecutionListeners);%0A%09%7D%0A
 %7D

JavaDoc.