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
021 package org.granite.tide.seam;
022
023 import static org.jboss.seam.annotations.Install.FRAMEWORK;
024
025 import java.util.ArrayList;
026 import java.util.List;
027
028 import javax.faces.application.FacesMessage;
029
030 import org.granite.tide.TideMessage;
031 import org.granite.tide.TideStatusMessages;
032 import org.granite.util.Reflections;
033 import org.jboss.seam.ScopeType;
034 import org.jboss.seam.annotations.Install;
035 import org.jboss.seam.annotations.Name;
036 import org.jboss.seam.annotations.Scope;
037 import org.jboss.seam.annotations.intercept.BypassInterceptors;
038 import 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
048 public 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 }