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 static org.jboss.seam.annotations.Install.FRAMEWORK;
024
025import java.util.ArrayList;
026import java.util.List;
027
028import javax.faces.application.FacesMessage;
029
030import org.granite.tide.TideMessage;
031import org.granite.tide.TideStatusMessages;
032import org.granite.util.Reflections;
033import org.jboss.seam.ScopeType;
034import org.jboss.seam.annotations.Install;
035import org.jboss.seam.annotations.Name;
036import org.jboss.seam.annotations.Scope;
037import org.jboss.seam.annotations.intercept.BypassInterceptors;
038import org.jboss.seam.faces.FacesMessages;
039
040
041/**
042 * @author William DRAI
043 */
044@Scope(ScopeType.SESSION)
045@Name("org.granite.tide.seam.serviceContext")
046@Install(precedence=FRAMEWORK)
047@BypassInterceptors
048public class SeamServiceContext extends AbstractSeamServiceContext {
049    
050    private static final long serialVersionUID = 1L;
051
052
053    @Override
054    protected void initTideMessages() {
055        FacesMessages.instance();   // Forces initialization of TideMessages component
056    }
057    
058    @Override
059    protected void clearTideMessages() {
060        if (FacesMessages.instance() instanceof TideMessages)
061            ((TideMessages)FacesMessages.instance()).clearTideMessages();
062        else {
063                try {
064                        Reflections.invoke(FacesMessages.instance().getClass().getMethod("clear"), FacesMessages.instance());
065                }
066                catch (Exception e) {
067                        log.error("Could not clear list of TideMessages", e);
068                }
069        }
070    }
071    
072    /**
073     * Retrieve current messages
074     * 
075     * @return list of messages
076     */
077    @Override
078    protected TideStatusMessages getTideMessages() {
079        // Prepare for the messages. First step is convert the tasks to Seam FacesMessages
080                FacesMessages.afterPhase();
081
082        // Second step is add the Seam FacesMessages to JSF FacesContext Messages
083        FacesMessages.instance().beforeRenderResponse();
084        
085        List<FacesMessage> facesMessages = FacesMessages.instance().getCurrentMessages();
086        List<TideMessage> tideMessages = new ArrayList<TideMessage>(facesMessages.size());
087        for (FacesMessage fm : facesMessages) {
088            String severity = null;
089            if (fm.getSeverity() == FacesMessage.SEVERITY_INFO)
090                severity = TideMessage.INFO;
091            else if (fm.getSeverity() == FacesMessage.SEVERITY_WARN)
092                severity = TideMessage.WARNING;
093            else if (fm.getSeverity() == FacesMessage.SEVERITY_ERROR)
094                severity = TideMessage.ERROR;
095            else if (fm.getSeverity() == FacesMessage.SEVERITY_FATAL)
096                severity = TideMessage.FATAL;
097            
098            tideMessages.add(new TideMessage(severity, fm.getSummary(), fm.getDetail()));
099        }
100        return new TideStatusMessages(tideMessages);
101    }
102}