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 = {
053        "WebUI.AddMessage" })
054public class AddMessage {
055
056    public static final String ID = "Seam.AddMessage";
057
058    public static final String MESSAGE_PARAMS_KEY = "AddMessage.messageParams";
059
060    @Context
061    protected OperationContext ctx;
062
063    @Param(name = "message")
064    protected String message;
065
066    @Param(name = "severity")
067    protected String severityStr = StatusMessage.Severity.INFO.name();
068
069    @OperationMethod
070    public void run() {
071        StatusMessage.Severity severity = StatusMessage.Severity.valueOf(severityStr);
072        FacesMessages facesMessages = (FacesMessages) Contexts.getConversationContext().get(FacesMessages.class);
073        Object[] params = (Object[]) ctx.get(MESSAGE_PARAMS_KEY);
074        if (params == null) {
075            params = new Object[0];
076        }
077        facesMessages.addFromResourceBundle(severity, message, params);
078    }
079
080}