001/*
002 * (C) Copyright 2013 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 *     Thomas Roger
016 */
017
018package org.nuxeo.ecm.automation.jsf.operations;
019
020import org.jboss.seam.contexts.Contexts;
021import org.jboss.seam.faces.FacesMessages;
022import org.jboss.seam.international.StatusMessage;
023import org.nuxeo.ecm.automation.OperationContext;
024import org.nuxeo.ecm.automation.core.Constants;
025import org.nuxeo.ecm.automation.core.annotations.Context;
026import org.nuxeo.ecm.automation.core.annotations.Operation;
027import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
028import org.nuxeo.ecm.automation.core.annotations.Param;
029
030/**
031 * Operation that displays a feedback message.
032 * <p>
033 * The {@code message} will be internationalized if available.
034 * <p>
035 * The severity of the message can be:
036 * <ul>
037 * <li>INFO</li>
038 * <li>WARN</li>
039 * <li>ERROR</li>
040 * </ul>
041 * The default one is INFO.
042 * <p>
043 * Message parameters, if any, are extracted form the {@link OperationContext} using the
044 * {@code AddMessage#MESSAGE_PARAMS_KEY} key.
045 * <p>
046 * Requires an active Seam context.
047 *
048 * @since 5.7
049 */
050@Operation(id = AddMessage.ID, category = Constants.CAT_UI, requires = Constants.SEAM_CONTEXT, label = "Add Message", description = "Add a feedback message to be displayed. The message will be internationalized. You can specify the severity of the message using INFO, WARN and ERROR, default is INFO. Messages parameters are extracted from the context using the 'AddMessage.messageParams'.", aliases = { "Seam.AddMessage" })
051public class AddMessage {
052
053    public static final String ID = "WebUI.AddMessage";
054
055    public static final String MESSAGE_PARAMS_KEY = "AddMessage.messageParams";
056
057    @Context
058    protected OperationContext ctx;
059
060    @Param(name = "message")
061    protected String message;
062
063    @Param(name = "severity")
064    protected String severityStr = StatusMessage.Severity.INFO.name();
065
066    @OperationMethod
067    public void run() {
068        StatusMessage.Severity severity = StatusMessage.Severity.valueOf(severityStr);
069        FacesMessages facesMessages = (FacesMessages) Contexts.getConversationContext().get(FacesMessages.class);
070        Object[] params = (Object[]) ctx.get(MESSAGE_PARAMS_KEY);
071        if (params == null) {
072            params = new Object[0];
073        }
074        facesMessages.addFromResourceBundle(severity, message, params);
075    }
076
077}