KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > db > CmsLoginMessage


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/db/CmsLoginMessage.java,v $
3  * Date : $Date: 2005/06/23 11:11:24 $
4  * Version: $Revision: 1.4 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.db;
33
34 import org.opencms.main.CmsIllegalArgumentException;
35 import org.opencms.main.CmsRuntimeException;
36 import org.opencms.util.CmsStringUtil;
37
38 import java.util.GregorianCalendar JavaDoc;
39
40 /**
41  * A message to display when a user logs in to the system.<p>
42  *
43  * @author Alexander Kandzior
44  *
45  * @version $Revision: 1.4 $
46  *
47  * @since 6.0.0
48  */

49 public class CmsLoginMessage {
50
51     /** The default end time (if none has been set). This is December 31, 2039. */
52     public static final long DEFAULT_TIME_END = new GregorianCalendar JavaDoc(2039, 11, 31).getTimeInMillis();
53
54     /** The default start time (if none has been set). This is January 1, 2000. */
55     public static final long DEFAULT_TIME_START = new GregorianCalendar JavaDoc(2000, 0, 1).getTimeInMillis();
56
57     /** Indicates if the message is enabled or not. */
58     private boolean m_enabled;
59
60     /** Indicates if the configuration of this message is finalized (frozen). */
61     private boolean m_frozen;
62
63     /** Controls if logins are forbidden while this message is active. */
64     private boolean m_loginForbidden;
65
66     /** The message to display on a login. */
67     private String JavaDoc m_message;
68
69     /** The time when to finish displaying this message. */
70     private long m_timeEnd;
71
72     /** The time when to start displaying this message. */
73     private long m_timeStart;
74
75     /**
76      * Creates a new login message with all default values.<p>
77      */

78     public CmsLoginMessage() {
79
80         m_timeStart = DEFAULT_TIME_START;
81         m_timeEnd = DEFAULT_TIME_END;
82     }
83
84     /**
85      * Creates a new login message with the given parameters.<p>
86      *
87      * @param timeStart the time when to start displaying this message
88      * @param timeEnd the time when to finish displaying this message
89      * @param message the message to display
90      * @param loginForbidden controls if logins are forbidden while this message is active
91      */

92     public CmsLoginMessage(long timeStart, long timeEnd, String JavaDoc message, boolean loginForbidden) {
93
94         setTimeStart(timeStart);
95         setTimeEnd(timeEnd);
96         m_enabled = true;
97         setMessage(message);
98         m_loginForbidden = loginForbidden;
99     }
100
101     /**
102      * Creates a new login message with the given parameters.<p>
103      *
104      * @param message the message to display
105      * @param loginForbidden controls if logins are forbidden while this message is active
106      */

107     public CmsLoginMessage(String JavaDoc message, boolean loginForbidden) {
108
109         this(DEFAULT_TIME_START, DEFAULT_TIME_END, message, loginForbidden);
110     }
111
112     /**
113      * @see java.lang.Object#clone()
114      */

115     public Object JavaDoc clone() {
116
117         CmsLoginMessage result = new CmsLoginMessage();
118         result.m_timeStart = m_timeStart;
119         result.m_timeEnd = m_timeEnd;
120         result.m_loginForbidden = m_loginForbidden;
121         result.m_message = m_message;
122         result.m_enabled = m_enabled;
123
124         return result;
125     }
126
127     /**
128      * Returns the message.<p>
129      *
130      * @return the message
131      */

132     public String JavaDoc getMessage() {
133
134         return m_message;
135     }
136
137     /**
138      * Returns the time the message ends.<p>
139      *
140      * @return the time the message ends
141      */

142     public long getTimeEnd() {
143
144         return m_timeEnd;
145     }
146
147     /**
148      * Returns the time the message starts.<p>
149      *
150      * @return the time the message starts
151      */

152     public long getTimeStart() {
153
154         return m_timeStart;
155     }
156
157     /**
158      * Returns <code>true</code> if this message is currently active.<p>
159      *
160      * A message is active if it is enabled and
161      * the current time is after the message start time and before the message end time.<p>
162      *
163      * @return <code>true</code> if this message is currently active
164      */

165     public boolean isActive() {
166
167         if (!m_enabled) {
168             return false;
169         }
170         long currentTime = System.currentTimeMillis();
171         return ((currentTime > m_timeStart) && (currentTime < m_timeEnd));
172     }
173
174     /**
175      * Returns <code>true</code> if this login message is the enabled.<p>
176      *
177      * @return <code>true</code> if this login message is the enabled
178      */

179     public boolean isEnabled() {
180
181         return m_enabled;
182     }
183
184     /**
185      * Returns <code>true</code> if logins are currently forbidden according to the settings
186      * of this message.<p>
187      *
188      * This checks the time settings using <code>{@link #isActive()}</code> and
189      * <code>{@link #isEnabled()}</code> as well as the
190      * <code>{@link #isLoginForbidden()}</code> flag.<p>
191      *
192      * @return <code>true</code> if logins are currently forbidden according to the settings of this message
193      */

194     public boolean isLoginCurrentlyForbidden() {
195
196         return m_loginForbidden && isActive();
197     }
198
199     /**
200      * Returns <code>true</code> if logins are forbidden while this message is active.<p>
201      *
202      * @return <code>true</code> if logins are forbidden while this message is active
203      */

204     public boolean isLoginForbidden() {
205
206         return m_loginForbidden;
207     }
208
209     /**
210      * Sets the enabled status of this message.<p>
211      *
212      * @param enabled the enabled status to set
213      */

214     public void setEnabled(boolean enabled) {
215
216         checkFrozen();
217         m_enabled = enabled;
218     }
219
220     /**
221      * Sets the flag that controls if logins are forbidden while this message is active.<p>
222      *
223      * @param loginForbidden the flag to set
224      */

225     public void setLoginForbidden(boolean loginForbidden) {
226
227         checkFrozen();
228         m_loginForbidden = loginForbidden;
229     }
230
231     /**
232      * Sets the message to display.<p>
233      *
234      * @param message the message to set
235      */

236     public void setMessage(String JavaDoc message) {
237
238         checkFrozen();
239         if (isEnabled() && CmsStringUtil.isEmptyOrWhitespaceOnly(message)) {
240             throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_LOGIN_MESSAGE_BAD_MESSAGE_0));
241         }
242         m_message = message;
243     }
244
245     /**
246      * Sets the time when to finish displaying this message.<p>
247      *
248      * @param timeEnd the time to set
249      */

250     public void setTimeEnd(long timeEnd) {
251
252         checkFrozen();
253         if (timeEnd < 0) {
254             throw new CmsIllegalArgumentException(Messages.get().container(
255                 Messages.ERR_LOGIN_MESSAGE_BAD_TIME_1,
256                 new Long JavaDoc(timeEnd)));
257         }
258         if (timeEnd == 0) {
259             timeEnd = DEFAULT_TIME_END;
260         }
261         if ((m_timeStart > 0) && (timeEnd <= m_timeStart)) {
262             throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_LOGIN_MESSAGE_BAD_END_TIME_0));
263         }
264         m_timeEnd = timeEnd;
265     }
266
267     /**
268      * Sets the time when to start displaying this message.<p>
269      *
270      * @param timeStart the time to set
271      */

272     public void setTimeStart(long timeStart) {
273
274         checkFrozen();
275         if (timeStart < 0) {
276             throw new CmsIllegalArgumentException(Messages.get().container(
277                 Messages.ERR_LOGIN_MESSAGE_BAD_TIME_1,
278                 new Long JavaDoc(timeStart)));
279         }
280         if (timeStart == 0) {
281             timeStart = DEFAULT_TIME_START;
282         }
283         m_timeStart = timeStart;
284     }
285
286     /**
287      * Checks if this message is frozen.<p>
288      *
289      * @throws CmsRuntimeException in case the message is already frozen
290      */

291     protected void checkFrozen() throws CmsRuntimeException {
292
293         if (m_frozen) {
294             throw new CmsRuntimeException(Messages.get().container(Messages.ERR_LOGIN_MESSAGE_FROZEN_0));
295         }
296     }
297
298     /**
299      * Freezes the configuration of this login message object to prevent later changes.<p>
300      */

301     protected void setFrozen() {
302
303         m_frozen = true;
304     }
305 }
Popular Tags