001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/db/CmsLoginMessage.java,v $
003: * Date : $Date: 2008-02-27 12:05:42 $
004: * Version: $Revision: 1.6 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.db;
033:
034: import org.opencms.main.CmsIllegalArgumentException;
035: import org.opencms.main.CmsRuntimeException;
036: import org.opencms.util.CmsStringUtil;
037:
038: import java.util.GregorianCalendar;
039:
040: /**
041: * A message to display when a user logs in to the system.<p>
042: *
043: * @author Alexander Kandzior
044: *
045: * @version $Revision: 1.6 $
046: *
047: * @since 6.0.0
048: */
049: public class CmsLoginMessage {
050:
051: /** The default end time (if none has been set). This is December 31, 2039. */
052: public static final long DEFAULT_TIME_END = new GregorianCalendar(
053: 2039, 11, 31).getTimeInMillis();
054:
055: /** The default start time (if none has been set). This is January 1, 2000. */
056: public static final long DEFAULT_TIME_START = new GregorianCalendar(
057: 2000, 0, 1).getTimeInMillis();
058:
059: /** Indicates if the message is enabled or not. */
060: private boolean m_enabled;
061:
062: /** Indicates if the configuration of this message is finalized (frozen). */
063: private boolean m_frozen;
064:
065: /** Controls if logins are forbidden while this message is active. */
066: private boolean m_loginForbidden;
067:
068: /** The message to display on a login. */
069: private String m_message;
070:
071: /** The time when to finish displaying this message. */
072: private long m_timeEnd;
073:
074: /** The time when to start displaying this message. */
075: private long m_timeStart;
076:
077: /**
078: * Creates a new login message with all default values.<p>
079: */
080: public CmsLoginMessage() {
081:
082: m_timeStart = DEFAULT_TIME_START;
083: m_timeEnd = DEFAULT_TIME_END;
084: }
085:
086: /**
087: * Creates a new login message with the given parameters.<p>
088: *
089: * @param timeStart the time when to start displaying this message
090: * @param timeEnd the time when to finish displaying this message
091: * @param message the message to display
092: * @param loginForbidden controls if logins are forbidden while this message is active
093: */
094: public CmsLoginMessage(long timeStart, long timeEnd,
095: String message, boolean loginForbidden) {
096:
097: setTimeStart(timeStart);
098: setTimeEnd(timeEnd);
099: m_enabled = true;
100: setMessage(message);
101: m_loginForbidden = loginForbidden;
102: }
103:
104: /**
105: * Creates a new login message with the given parameters.<p>
106: *
107: * @param message the message to display
108: * @param loginForbidden controls if logins are forbidden while this message is active
109: */
110: public CmsLoginMessage(String message, boolean loginForbidden) {
111:
112: this (DEFAULT_TIME_START, DEFAULT_TIME_END, message,
113: loginForbidden);
114: }
115:
116: /**
117: * @see java.lang.Object#clone()
118: */
119: public Object clone() {
120:
121: CmsLoginMessage result = new CmsLoginMessage();
122: result.m_timeStart = m_timeStart;
123: result.m_timeEnd = m_timeEnd;
124: result.m_loginForbidden = m_loginForbidden;
125: result.m_message = m_message;
126: result.m_enabled = m_enabled;
127:
128: return result;
129: }
130:
131: /**
132: * Returns the message.<p>
133: *
134: * @return the message
135: */
136: public String getMessage() {
137:
138: return m_message;
139: }
140:
141: /**
142: * Returns the time the message ends.<p>
143: *
144: * @return the time the message ends
145: */
146: public long getTimeEnd() {
147:
148: return m_timeEnd;
149: }
150:
151: /**
152: * Returns the time the message starts.<p>
153: *
154: * @return the time the message starts
155: */
156: public long getTimeStart() {
157:
158: return m_timeStart;
159: }
160:
161: /**
162: * Returns <code>true</code> if this message is currently active.<p>
163: *
164: * A message is active if it is enabled and
165: * the current time is after the message start time and before the message end time.<p>
166: *
167: * @return <code>true</code> if this message is currently active
168: */
169: public boolean isActive() {
170:
171: if (!m_enabled) {
172: return false;
173: }
174: long currentTime = System.currentTimeMillis();
175: return ((currentTime > m_timeStart) && (currentTime < m_timeEnd));
176: }
177:
178: /**
179: * Returns <code>true</code> if this login message is the enabled.<p>
180: *
181: * @return <code>true</code> if this login message is the enabled
182: */
183: public boolean isEnabled() {
184:
185: return m_enabled;
186: }
187:
188: /**
189: * Returns <code>true</code> if logins are currently forbidden according to the settings
190: * of this message.<p>
191: *
192: * This checks the time settings using <code>{@link #isActive()}</code> and
193: * <code>{@link #isEnabled()}</code> as well as the
194: * <code>{@link #isLoginForbidden()}</code> flag.<p>
195: *
196: * @return <code>true</code> if logins are currently forbidden according to the settings of this message
197: */
198: public boolean isLoginCurrentlyForbidden() {
199:
200: return m_loginForbidden && isActive();
201: }
202:
203: /**
204: * Returns <code>true</code> if logins are forbidden while this message is active.<p>
205: *
206: * @return <code>true</code> if logins are forbidden while this message is active
207: */
208: public boolean isLoginForbidden() {
209:
210: return m_loginForbidden;
211: }
212:
213: /**
214: * Sets the enabled status of this message.<p>
215: *
216: * @param enabled the enabled status to set
217: */
218: public void setEnabled(boolean enabled) {
219:
220: checkFrozen();
221: m_enabled = enabled;
222: }
223:
224: /**
225: * Sets the flag that controls if logins are forbidden while this message is active.<p>
226: *
227: * @param loginForbidden the flag to set
228: */
229: public void setLoginForbidden(boolean loginForbidden) {
230:
231: checkFrozen();
232: m_loginForbidden = loginForbidden;
233: }
234:
235: /**
236: * Sets the message to display.<p>
237: *
238: * @param message the message to set
239: */
240: public void setMessage(String message) {
241:
242: checkFrozen();
243: if (isEnabled()
244: && CmsStringUtil.isEmptyOrWhitespaceOnly(message)) {
245: throw new CmsIllegalArgumentException(
246: Messages.get().container(
247: Messages.ERR_LOGIN_MESSAGE_BAD_MESSAGE_0));
248: }
249: m_message = message;
250: }
251:
252: /**
253: * Sets the time when to finish displaying this message.<p>
254: *
255: * @param timeEnd the time to set
256: */
257: public void setTimeEnd(long timeEnd) {
258:
259: checkFrozen();
260: if (timeEnd < 0) {
261: throw new CmsIllegalArgumentException(Messages.get()
262: .container(Messages.ERR_LOGIN_MESSAGE_BAD_TIME_1,
263: new Long(timeEnd)));
264: }
265: if (timeEnd == 0) {
266: timeEnd = DEFAULT_TIME_END;
267: }
268: if ((m_timeStart > 0) && (timeEnd <= m_timeStart)) {
269: throw new CmsIllegalArgumentException(Messages.get()
270: .container(
271: Messages.ERR_LOGIN_MESSAGE_BAD_END_TIME_0));
272: }
273: m_timeEnd = timeEnd;
274: }
275:
276: /**
277: * Sets the time when to start displaying this message.<p>
278: *
279: * @param timeStart the time to set
280: */
281: public void setTimeStart(long timeStart) {
282:
283: checkFrozen();
284: if (timeStart < 0) {
285: throw new CmsIllegalArgumentException(Messages.get()
286: .container(Messages.ERR_LOGIN_MESSAGE_BAD_TIME_1,
287: new Long(timeStart)));
288: }
289: if (timeStart == 0) {
290: timeStart = DEFAULT_TIME_START;
291: }
292: m_timeStart = timeStart;
293: }
294:
295: /**
296: * Checks if this message is frozen.<p>
297: *
298: * @throws CmsRuntimeException in case the message is already frozen
299: */
300: protected void checkFrozen() throws CmsRuntimeException {
301:
302: if (m_frozen) {
303: throw new CmsRuntimeException(Messages.get().container(
304: Messages.ERR_LOGIN_MESSAGE_FROZEN_0));
305: }
306: }
307:
308: /**
309: * Freezes the configuration of this login message object to prevent later changes.<p>
310: */
311: protected void setFrozen() {
312:
313: m_frozen = true;
314: }
315: }
|