001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.platform.ui.web.seam;
018
019import static org.jboss.seam.ScopeType.APPLICATION;
020import static org.jboss.seam.annotations.Install.BUILT_IN;
021
022import org.jboss.seam.annotations.Install;
023import org.jboss.seam.annotations.Name;
024import org.jboss.seam.annotations.Observer;
025import org.jboss.seam.annotations.Scope;
026import org.jboss.seam.annotations.Startup;
027import org.jboss.seam.annotations.intercept.BypassInterceptors;
028import org.jboss.seam.faces.FacesMessages;
029import org.jboss.seam.international.StatusMessage.Severity;
030import org.jboss.seam.international.StatusMessages;
031import org.jboss.seam.transaction.FacesTransactionEvents;
032import org.jboss.seam.transaction.Transaction;
033
034/**
035 * Custom listener to "transaction failed" event to customize behaviour (show the message only if there are no other
036 * messages in the stack).
037 * <p>
038 * The default {@link FacesTransactionEvents} observer is disabled in components.xml.
039 *
040 * @see FacesTransactionEvents
041 * @since 6.0
042 */
043@Name("nuxeoFacesTransactionEvents")
044@Scope(APPLICATION)
045@Install(precedence = BUILT_IN, classDependencies = "javax.faces.context.FacesContext")
046@BypassInterceptors
047@Startup
048public class NuxeoFacesTransactionEvents {
049
050    protected boolean transactionFailedMessageEnabled = true;
051
052    @Observer(Transaction.TRANSACTION_FAILED)
053    public void addTransactionFailedMessage(int status) {
054        if (transactionFailedMessageEnabled) {
055            // NXP-12483 + VEND-13: only add the default message if none was
056            // already set
057            FacesMessages fm = FacesMessages.instance();
058            if (fm.getCurrentMessages().size() == 0 && fm.getLocalMessages().size() == 0) {
059                StatusMessages.instance().addFromResourceBundleOrDefault(getTransactionFailedMessageSeverity(),
060                        getTransactionFailedMessageKey(), getTransactionFailedMessage());
061            }
062        }
063    }
064
065    public String getTransactionFailedMessage() {
066        return "Transaction failed";
067    }
068
069    public Severity getTransactionFailedMessageSeverity() {
070        return Severity.WARN;
071    }
072
073    public String getTransactionFailedMessageKey() {
074        return "org.jboss.seam.TransactionFailed";
075    }
076
077    public boolean isTransactionFailedMessageEnabled() {
078        return transactionFailedMessageEnabled;
079    }
080
081    public void setTransactionFailedMessageEnabled(boolean enabled) {
082        transactionFailedMessageEnabled = enabled;
083    }
084
085}