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    
015    package org.apache.tapestry5.alerts;
016    
017    import org.apache.tapestry5.ioc.internal.util.InternalUtils;
018    import org.apache.tapestry5.json.JSONObject;
019    
020    import java.io.Serializable;
021    import 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).
027     *
028     * @since 5.3
029     */
030    public class Alert implements Serializable
031    {
032        private static final AtomicLong idSource = new AtomicLong(System.currentTimeMillis());
033    
034        /**
035         * A unique id (unique within this JVM and execution), used to identify an alert (used primarily
036         * when individually dismissing an alert).
037         */
038        public final long id = idSource.getAndIncrement();
039    
040        public final Duration duration;
041    
042        public final Severity severity;
043    
044        public final String message;
045    
046        /**
047         * Alert with default duration of {@link Duration#SINGLE} and default severity
048         * of {@link Severity#INFO}.
049         */
050        public Alert(String message)
051        {
052            this(Severity.INFO, message);
053        }
054    
055        /**
056         * Alert with default duration of {@link Duration#SINGLE}.
057         */
058        public Alert(Severity severity, String message)
059        {
060            this(Duration.SINGLE, severity, message);
061        }
062    
063        public Alert(Duration duration, Severity severity, String message)
064        {
065            assert duration != null;
066            assert severity != null;
067            assert InternalUtils.isNonBlank(message);
068    
069            this.duration = duration;
070            this.severity = severity;
071            this.message = message;
072        }
073    
074        public String toString()
075        {
076            return String.format("Alert[%s %s %s]",
077                    duration.name(),
078                    severity.name(),
079                    message);
080        }
081    
082        public JSONObject toJSON()
083        {
084            JSONObject result = new JSONObject("message", message, "class", severity.cssClass);
085    
086            if (duration == Duration.TRANSIENT)
087            {
088                result.put("transient", true);
089            }
090    
091            if (duration.persistent)
092            {
093                result.put("id", id);
094            }
095    
096            return result;
097        }
098    }