package br.com.dbsoft.ui.exception;

import java.util.Iterator;
import java.util.Map;

import javax.enterprise.context.NonexistentConversationException;
import javax.faces.FacesException;
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.application.ViewExpiredException;
import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerWrapper;
import javax.faces.context.FacesContext;
import javax.faces.event.ExceptionQueuedEvent;
import javax.faces.event.ExceptionQueuedEventContext;
import javax.faces.view.facelets.TagAttributeException;

import org.apache.log4j.Logger;

import br.com.dbsoft.ui.core.DBSFaces;


public class DBSFacesExceptionHandler  extends ExceptionHandlerWrapper {
	
	private Logger wLogger = Logger.getLogger(this.getClass());

	private final ExceptionHandler wWrapped;

	public DBSFacesExceptionHandler(ExceptionHandler wrapped) {
		wWrapped = wrapped;
	}

	@Override
	public ExceptionHandler getWrapped() {
	    return wWrapped;
	}

	@Override
	public void handle() throws FacesException {
		final Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator();         

	    while (i.hasNext()) {             
			ExceptionQueuedEvent xEvent = i.next();             
			ExceptionQueuedEventContext xContext = (ExceptionQueuedEventContext) xEvent.getSource();               
			// get the exception from context             
			Throwable xThrowable = xContext.getException();
			Boolean xRemove = true;
			final FacesContext xFC = FacesContext.getCurrentInstance();   
			
			final Map<String, Object> xRequestMap = xFC.getExternalContext().getRequestMap();            
			final ConfigurableNavigationHandler xNav = (ConfigurableNavigationHandler) xFC.getApplication().getNavigationHandler();               
			try {                   
				//Não encontrou a conversação (CoversationScoped)
				if (xThrowable instanceof NonexistentConversationException){
					//Se não finalizada a resposta
					if (!xFC.getRenderResponse()){ 
						//Atualiza integralmente a página atual
						xNav.performNavigation(DBSFaces.getCurrentViewRefresh());
//					}else{
//						System.out.println("asdcascd");
					}
//					xFC.renderResponse();       
				//View Expirada
				}else if (xThrowable instanceof TagAttributeException){
					//Direciona para a pasta raiz do sistema(normalmente o sistema irá redirecionar para a páginal index.xhtml)
//					xNav.performNavigation(xFC.getExternalContext().getRequestContextPath());
//					xFC.renderResponse();       
//					xNav.performNavigation("/");
//					xNav.handleNavigation(xFC, null, "/index.xhtml?faces-redirect=true");
					xNav.handleNavigation(xFC, null, DBSFaces.getCurrentViewRefresh());
				}else if (xThrowable instanceof ViewExpiredException){
						//Direciona para a pasta raiz do sistema(normalmente o sistema irá redirecionar para a páginal index.xhtml)
//						xNav.performNavigation(xFC.getExternalContext().getRequestContextPath());
//						xFC.renderResponse();       
						xNav.performNavigation("/");
				}else if (xThrowable instanceof javax.el.PropertyNotFoundException){
					wLogger.error("Erro no encode do componente - Propriedade não encontrada", xThrowable); 
				}else if (xThrowable instanceof javax.faces.FacesException){ 
					if (xThrowable.getCause() instanceof com.sun.faces.context.FacesFileNotFoundException){
						wLogger.error("Página não encontrada. " + xThrowable.getMessage()); 
//						FacesMessage xM = new FacesMessage("aaaaa");
//						xFC.addMessage(clientId, message);
					}
					xRemove = false;
				}else{
					wLogger.error("Severe Exception Occured", xThrowable);
					xRequestMap.put("exceptionMessage", xThrowable.getMessage());                 
					xNav.performNavigation(xFC.getExternalContext().getRequestContextPath());
					xFC.renderResponse();                   
					// remove the comment below if you want to report the error in a jsf error message                 
//					JsfUtil.addErrorMessage(t.getMessage());     
				}
			} finally {     
				if (xRemove){
				    i.remove();             
				}
			}         
	   }         
	    //parent hanle         
	    getWrapped().handle(); 

	}

}

@@ -1167,17 +1167,16 @@
        %0A
-%0A
 %09    whi
@@ -1888,24 +1888,25 @@
 ped)%0A%09%09%09%09if 
+(
 (xThrowable 
@@ -1941,32 +1941,104 @@
 sationException)
+ %7C%7C (xThrowable.getCause() instanceof NonexistentConversationException))
 %7B%0A%09%09%09%09%09//Se n%C3%A3o 
@@ -2058,16 +2058,17 @@
 resposta
+ 
 %0A%09%09%09%09%09if
@@ -2211,62 +2211,8 @@
 ));%0A
-//%09%09%09%09%09%7Delse%7B%0A//%09%09%09%09%09%09System.out.println(%22asdcascd%22);%0A
 %09%09%09%09
@@ -2751,32 +2751,33 @@
 );%0A%09%09%09%09%7Delse if 
+(
 (xThrowable inst
@@ -2796,32 +2796,92 @@
 xpiredException)
+ %7C%7C (xThrowable.getCause() instanceof ViewExpiredException))
 %7B%0A%09%09%09%09%09%09//Direci

Teste também do getCause