/*
 * AutoTestListener.java		Date created: 14.10.2008
 * Last modified by: $Author$
 * $Revision$	$Date$
 */

package org.ajax4jsf.autotest.bean;

import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
import javax.servlet.http.HttpSession;

/**
 * Listener for nested ajax listeners for automatic testing
 * @author Andrey Markavtsov
 *
 */
public class AutoTestListener implements ActionListener {

	/* (non-Javadoc)
	 * @see javax.faces.event.ActionListener#processAction(javax.faces.event.ActionEvent)
	 */
	public void processAction(ActionEvent event)
			throws AbortProcessingException {
		AutoTestBean bean = getAutoTestBean();
		if (bean != null) {
			bean.setStatus(bean.getStatus() + AutoTestBean.NESTED_ACTION_LISTENER_STATUS);
		}

	}
	
	private AutoTestBean getAutoTestBean() {
		FacesContext context = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) context.getExternalContext()
				.getSession(false);
		if (session != null) {
			return (AutoTestBean) session.getAttribute("autoTestBean");
		}
		return null;
	}

}
