    001// Copyright 2011 The Apache Software Foundation
    002//
    003// Licensed under the Apache License, Version 2.0 (the "License");
    004// you may not use this file except in compliance with the License.
    005// You may obtain a copy of the License at
    006//
    007//     http://www.apache.org/licenses/LICENSE-2.0
    008//
    009// Unless required by applicable law or agreed to in writing, software
    010// distributed under the License is distributed on an "AS IS" BASIS,
    011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012// See the License for the specific language governing permissions and
    013// limitations under the License.
    014
    015package org.apache.tapestry5.alerts;
    016
    017import org.apache.tapestry5.ioc.internal.util.InternalUtils;
    018import org.apache.tapestry5.json.JSONObject;
    019
    020import java.io.Serializable;
    021import java.util.concurrent.atomic.AtomicLong;
    022
    023/**
    024 * An Alert that may be presented to the user. The Alert has a message, but also includes
    025 * a severity (that controls how it is presented to the user), and a duration (that controls how long
    026 * it is presented to the user). If the <code>markup</code> field is <code>true</code>,
    027 * the message is treated as HTML and used without any escaping.
    028 *
    029 * @since 5.3
    030 */
    031public class Alert implements Serializable
    032{
    033    private static final AtomicLong idSource = new AtomicLong(System.currentTimeMillis());
    034
    035    /**
    036     * A unique id (unique within this JVM and execution), used to identify an alert (used primarily
    037     * when individually dismissing an alert).
    038     */
    039    public final long id = idSource.getAndIncrement();
    040
    041    public final Duration duration;
    042
    043    public final Severity severity;
    044
    045    public final String message;
    046
    047    /**
    048     * Defines whether the message will be treated as HTML or not.
    049     * @since 5.4
    050     */
    051    public final boolean markup;
    052
    053    /**
    054     * Alert with default duration of {@link Duration#SINGLE} and default severity
    055     * of {@link Severity#INFO}.
    056     */
    057    public Alert(String message)
    058    {
    059        this(Severity.INFO, message);
    060    }
    061
    062    /**
    063     * Alert with default duration of {@link Duration#SINGLE}.
    064     */
    065    public Alert(Severity severity, String message)
    066    {
    067        this(Duration.SINGLE, severity, message, false);
    068    }
    069
    070    public Alert(Duration duration, Severity severity, String message)
    071    {
    072        this(duration, severity, message, false);
    073    }
    074
    075    public Alert(Duration duration, Severity severity, String message, boolean markup)
    076    {
    077        assert duration != null;
    078        assert severity != null;
    079        assert InternalUtils.isNonBlank(message);
    080
    081        this.duration = duration;
    082        this.severity = severity;
    083        this.message = message;
    084        this.markup = markup;
    085    }
    086
    087    public String toString()
    088    {
    089        return String.format("Alert[%s %s %s %s]",
    090                duration.name(),
    091                severity.name(),
    092                message,
    093                markup);
    094    }
    095
    096    public JSONObject toJSON()
    097    {
    098        JSONObject result = new JSONObject("message", message,
    099                "severity", severity.name().toLowerCase(), "markup", markup );
    100
    101        if (duration == Duration.TRANSIENT)
    102        {
    103            result.put("transient", true);
    104        }
    105
    106        if (duration.persistent)
    107        {
    108            result.put("id", id);
    109        }
    110
    111        return result;
    112    }
    113}