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