001/*
002  GRANITE DATA SERVICES
003  Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005  This file is part of Granite Data Services.
006
007  Granite Data Services is free software; you can redistribute it and/or modify
008  it under the terms of the GNU Library General Public License as published by
009  the Free Software Foundation; either version 2 of the License, or (at your
010  option) any later version.
011
012  Granite Data Services is distributed in the hope that it will be useful, but
013  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015  for more details.
016
017  You should have received a copy of the GNU Library General Public License
018  along with this library; if not, see <http://www.gnu.org/licenses/>.
019*/
020
021package org.granite.tide.seam;
022
023import java.util.ArrayList;
024import java.util.List;
025
026import javax.faces.application.FacesMessage;
027
028import org.granite.config.flex.Destination;
029import org.granite.messaging.service.ExtendedServiceExceptionHandler;
030import org.granite.messaging.service.ServiceException;
031import org.granite.tide.TideMessage;
032import org.jboss.seam.faces.FacesMessages;
033
034import flex.messaging.messages.Message;
035
036
037/**
038 * @author Venkat DANDA
039 * @author Cameron INGRAM
040 *
041 * Update services-config.xml to use the seam service exception handler
042 * <factory id="tideSeamFactory" class="org.granite.tide.seam.SeamServiceFactory" >
043 *      <properties>
044 *              <service-exception-handler>org.granite.tide.seam.SeamServiceExceptionHandler</service-exception-handler>
045 *      </properties>
046 * </factory>
047 */
048public class SeamServiceExceptionHandler extends ExtendedServiceExceptionHandler {
049
050    private static final long serialVersionUID = -1L;
051
052    public SeamServiceExceptionHandler() {
053        super(true);
054    }
055
056    public SeamServiceExceptionHandler(boolean logException) {
057        super(logException);
058    }
059
060        @Override
061    protected ServiceException getServiceException(Message request, Destination destination, String method, Throwable t) {
062        ServiceException se = super.getServiceException(request, destination, method, t);
063        
064        // Add messages
065        // Prepare for the messages. First step is convert the tasks to Seam FacesMessages
066        FacesMessages.afterPhase();
067        // Second step is add the Seam FacesMessages to JSF FacesContext Messages
068        FacesMessages.instance().beforeRenderResponse();
069
070        List<FacesMessage> facesMessages = FacesMessages.instance().getCurrentMessages();
071        List<TideMessage> tideMessages = new ArrayList<TideMessage>(facesMessages.size());
072        for (FacesMessage fm : facesMessages) {
073            String severity = null;
074            if (fm.getSeverity() == FacesMessage.SEVERITY_INFO)
075                severity = TideMessage.INFO;
076            else if (fm.getSeverity() == FacesMessage.SEVERITY_WARN)
077                severity = TideMessage.WARNING;
078            else if (fm.getSeverity() == FacesMessage.SEVERITY_ERROR)
079                severity = TideMessage.ERROR;
080            else if (fm.getSeverity() == FacesMessage.SEVERITY_FATAL)
081                severity = TideMessage.FATAL;
082            
083            tideMessages.add(new TideMessage(severity, fm.getSummary(), fm.getDetail()));
084        }
085        se.getExtendedData().put("messages", tideMessages);
086        return se;
087    }
088}