/*
Copyright (c) 2002, Matthias Bauer, LIVINGLOGIC AG, BAYREUTH
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are 
met:

    1. Redistributions of source code must retain the above copyright 
       notice, this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright 
       notice, this list of conditions and the following disclaimer in the 
       documentation and/or other materials provided with the distribution.
    3. The end-user documentation included with the redistribution, if
       any, must include the following acknowlegement:
       "This product includes software developed by 
        Matthias Bauer, LIVINGLOGIC AG, BAYREUTH 
        (http://www.livinglogic.de/)."
       Alternately, this acknowlegement may appear in the software itself,
       if and wherever such third-party acknowlegements normally appear.
    4. Neither the name of Matthias Bauer, nor the LIVINGLOGIC AG nor the 
       names of the other contributors may be used to endorse or promote 
       products derived from this software without specific prior written
       permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/

package es.ejie.frmk.presentacion.anomala;

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

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import es.ejie.frmk.presentacion.base.Q70WorkflowUtils;


/**
 * This action forwards to the action that caused a nextState violation
 * during this request (if any). If no nextState violation was encountered, 
 * e. g. because a prevState condition caused the initial workflow violation, 
 * the action forwards to the forward "noNextStateViolation" 
 *
 * @author M.Bauer
 * @version 1.0
 */
public class Q70ForwardNextStateViolationAction extends Action
{
	/**
	 * Key used to store a flag, if this action has already handled a 
	 * nextState violation during this request. 
	 */
	private static final String NEXT_STATE_VIOLATION_HANDLED = "es.ejie.frmk.presentacion.anomala.Q70ForwardNextStateViolationAction";
	
	/**
	 * Returns an <code>ActionForward</code> instance describing where and how
	 * control should be forwarded, or <code>null</code> if the response has
	 * already been completed.
	 *
	 * @param mapping The ActionMapping used to select this instance
	 * @param actionForm The ActionForm bean for this request
	 * @param request The HTTP request we are processing
	 * @param response The HTTP response we are creating
	 * @return ActionForward instance that describes where and how control should
	 * be forwarded to.
     * @exception Exception if the application business logic throws
     *  an exception
	 */
	public  ActionForward execute
		(ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response)
		throws Exception
	{
		ActionForward retVal;
		
		try {
			String nextStateViolationAction = Q70WorkflowUtils.getNextStateViolationAction(request);
			if ((null != nextStateViolationAction) && (null == request.getAttribute(NEXT_STATE_VIOLATION_HANDLED)))
			{
				retVal = new ActionForward(nextStateViolationAction);
				// remember that we have already forwarded to the action that caused the
				// nextState violation, so there is no chance to run into an endless loop
				request.setAttribute(NEXT_STATE_VIOLATION_HANDLED, Boolean.TRUE);
			}
			else
			{
				retVal = mapping.findForward("noNextStateViolation");
			}
			
			return retVal;
		} catch (Exception e) {
			throw new Q70NavegacionAnomalaException(this.getClass().toString(), "execute", "Error en el tratamiento de la navegación anómala detectada.", e);
		}
	}
}