View Javadoc
1   /**
2    * This file Copyright (c) 2012-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.api.message;
35  
36  import info.magnolia.context.MgnlContext;
37  
38  import java.util.HashMap;
39  import java.util.Map;
40  import java.util.Objects;
41  import java.util.Set;
42  
43  /**
44   * Models a message. Except for timestamp all fields are optional.
45   */
46  public class Message implements Cloneable {
47  
48      public static final String MESSAGE_VIEW = "messageView";
49  
50      private String id;
51      private final long timestamp;
52      private MessageType type;
53      private String subject;
54      private String message;
55      private String sender;
56      private boolean cleared;
57      /**
58       * View associated with this message.
59       */
60      private String view;
61  
62      private Map<String, Object> properties = new HashMap<String, Object>();
63  
64      public Message() {
65          this(System.currentTimeMillis());
66          if (MgnlContext.hasInstance()) {
67              setSender(MgnlContext.getInstance().getUser().getName());
68          }
69      }
70  
71      public Message(long timestampInMillis) {
72          this.timestamp = timestampInMillis;
73          setCleared(false);
74      }
75  
76      public Message(final MessageType type, final String subject, final String message) {
77          this();
78          setSubject(subject);
79          setMessage(message);
80          setType(type);
81      }
82  
83      public long getTimestamp() {
84          return timestamp;
85      }
86  
87      public String getMessage() {
88          return message;
89      }
90  
91      public void setMessage(String message) {
92          this.message = message;
93      }
94  
95      public String getSubject() {
96          return subject;
97      }
98  
99      public void setSubject(String subject) {
100         this.subject = subject;
101     }
102 
103     public MessageType getType() {
104         return type;
105     }
106 
107     public void setType(MessageType type) {
108         this.type = type;
109     }
110 
111     public void setId(String id) {
112         this.id = id;
113     }
114 
115     public String getId() {
116         return id;
117     }
118 
119     public boolean isCleared() {
120         return cleared;
121     }
122 
123     public void setCleared(boolean cleared) {
124         this.cleared = cleared;
125     }
126 
127     public String getSender() {
128         return sender;
129     }
130 
131     public void setSender(String sender) {
132         this.sender = sender;
133     }
134 
135     public String getView() {
136         return view;
137     }
138 
139     public void setView(String view) {
140         this.view = view;
141     }
142 
143     @Override
144     public Message clone() throws CloneNotSupportedException {
145         return (Message) super.clone();
146     }
147 
148     public void addProperty(final String name, final Object value) {
149         properties.put(name, value);
150     }
151 
152     public Object getProperty(final String name) {
153         return properties.get(name);
154     }
155 
156     public boolean hasProperty(final String name) {
157         return properties.containsKey(name);
158     }
159 
160     public Set<String> getPropertNames() {
161         return properties.keySet();
162     }
163 
164     @Override
165     public boolean equals(Object o) {
166         if (this == o) return true;
167         if (o == null || getClass() != o.getClass()) return false;
168 
169         Message/../../../../info/magnolia/ui/api/message/Message.html#Message">Message message = (Message) o;
170 
171         return Objects.equals(message.getId(), getId());
172     }
173 
174     @Override
175     public int hashCode() {
176         return Objects.hashCode(getId());
177     }
178 }