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