001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.platform.ui.web.seam;
020
021import static org.jboss.seam.ScopeType.APPLICATION;
022import static org.jboss.seam.annotations.Install.BUILT_IN;
023
024import org.jboss.seam.annotations.Install;
025import org.jboss.seam.annotations.Name;
026import org.jboss.seam.annotations.Observer;
027import org.jboss.seam.annotations.Scope;
028import org.jboss.seam.annotations.Startup;
029import org.jboss.seam.annotations.intercept.BypassInterceptors;
030import org.jboss.seam.faces.FacesMessages;
031import org.jboss.seam.international.StatusMessage.Severity;
032import org.jboss.seam.international.StatusMessages;
033import org.jboss.seam.transaction.FacesTransactionEvents;
034import org.jboss.seam.transaction.Transaction;
035
036/**
037 * Custom listener to "transaction failed" event to customize behaviour (show the message only if there are no other
038 * messages in the stack).
039 * <p>
040 * The default {@link FacesTransactionEvents} observer is disabled in components.xml.
041 *
042 * @see FacesTransactionEvents
043 * @since 6.0
044 */
045@Name("nuxeoFacesTransactionEvents")
046@Scope(APPLICATION)
047@Install(precedence = BUILT_IN, classDependencies = "javax.faces.context.FacesContext")
048@BypassInterceptors
049@Startup
050public class NuxeoFacesTransactionEvents {
051
052    protected boolean transactionFailedMessageEnabled = true;
053
054    @Observer(Transaction.TRANSACTION_FAILED)
055    public void addTransactionFailedMessage(int status) {
056        if (transactionFailedMessageEnabled) {
057            // NXP-12483 + VEND-13: only add the default message if none was
058            // already set
059            FacesMessages fm = FacesMessages.instance();
060            if (fm.getCurrentMessages().size() == 0 && fm.getLocalMessages().size() == 0) {
061                StatusMessages.instance().addFromResourceBundleOrDefault(getTransactionFailedMessageSeverity(),
062                        getTransactionFailedMessageKey(), getTransactionFailedMessage());
063            }
064        }
065    }
066
067    public String getTransactionFailedMessage() {
068        return "Transaction failed";
069    }
070
071    public Severity getTransactionFailedMessageSeverity() {
072        return Severity.WARN;
073    }
074
075    public String getTransactionFailedMessageKey() {
076        return "org.jboss.seam.TransactionFailed";
077    }
078
079    public boolean isTransactionFailedMessageEnabled() {
080        return transactionFailedMessageEnabled;
081    }
082
083    public void setTransactionFailedMessageEnabled(boolean enabled) {
084        transactionFailedMessageEnabled = enabled;
085    }
086
087}