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.CmsLog;
032import org.opencms.main.CmsRuntimeException;
033import org.opencms.ui.components.CmsRichTextArea;
034
035import java.util.GregorianCalendar;
036
037import org.apache.commons.logging.Log;
038
039/**
040 * A message to display when a user logs in to the system.<p>
041 *
042 * @since 6.0.0
043 */
044public class CmsLoginMessage {
045
046    /** Logger for this class. */
047    private static final Log LOG = CmsLog.getLog(CmsLoginMessage.class);
048
049    /** The default end time (if none has been set). This is December 31, 2039. */
050    public static final long DEFAULT_TIME_END = new GregorianCalendar(2039, 11, 31).getTimeInMillis();
051
052    /** The default start time (if none has been set). This is January 1, 2000. */
053    public static final long DEFAULT_TIME_START = new GregorianCalendar(2000, 0, 1).getTimeInMillis();
054
055    /** Indicates if the message is enabled or not. */
056    private boolean m_enabled;
057
058    /** Indicates if the configuration of this message is finalized (frozen). */
059    private boolean m_frozen;
060
061    /** Controls if logins are forbidden while this message is active. */
062    private boolean m_loginForbidden;
063
064    /** The message to display on a login. */
065    private String m_message;
066
067    /** The time when to finish displaying this message. */
068    private long m_timeEnd;
069
070    /** The time when to start displaying this message. */
071    private long m_timeStart;
072
073    /**
074     * Creates a new login message with all default values.<p>
075     */
076    public CmsLoginMessage() {
077
078        m_timeStart = DEFAULT_TIME_START;
079        m_timeEnd = DEFAULT_TIME_END;
080    }
081
082    /**
083     * Creates a new login message with the given parameters.<p>
084     *
085     * @param timeStart the time when to start displaying this message
086     * @param timeEnd the time when to finish displaying this message
087     * @param message the message to display
088     * @param loginForbidden controls if logins are forbidden while this message is active
089     */
090    public CmsLoginMessage(long timeStart, long timeEnd, String message, boolean loginForbidden) {
091
092        setTimeStart(timeStart);
093        setTimeEnd(timeEnd);
094        m_enabled = true;
095        setMessage(message);
096        m_loginForbidden = loginForbidden;
097    }
098
099    /**
100     * Creates a new login message with the given parameters.<p>
101     *
102     * @param message the message to display
103     * @param loginForbidden controls if logins are forbidden while this message is active
104     */
105    public CmsLoginMessage(String message, boolean loginForbidden) {
106
107        this(DEFAULT_TIME_START, DEFAULT_TIME_END, message, loginForbidden);
108    }
109
110    /**
111     * @see java.lang.Object#clone()
112     */
113    @Override
114    public Object clone() {
115
116        CmsLoginMessage result = new CmsLoginMessage();
117        result.m_timeStart = m_timeStart;
118        result.m_timeEnd = m_timeEnd;
119        result.m_loginForbidden = m_loginForbidden;
120        result.m_message = m_message;
121        result.m_enabled = m_enabled;
122
123        return result;
124    }
125
126    /**
127     * Returns the message.<p>
128     *
129     * @return the message
130     */
131    public String getMessage() {
132
133        return m_message;
134    }
135
136    /**
137     * Returns the time the message ends.<p>
138     *
139     * @return the time the message ends
140     */
141    public long getTimeEnd() {
142
143        return m_timeEnd;
144    }
145
146    /**
147     * Returns the time the message starts.<p>
148     *
149     * @return the time the message starts
150     */
151    public long getTimeStart() {
152
153        return m_timeStart;
154    }
155
156    /**
157     * Returns <code>true</code> if this message is currently active.<p>
158     *
159     * A message is active if it is enabled and
160     * the current time is after the message start time and before the message end time.<p>
161     *
162     * @return <code>true</code> if this message is currently active
163     */
164    public boolean isActive() {
165
166        if (!m_enabled) {
167            return false;
168        }
169        long currentTime = System.currentTimeMillis();
170        return ((currentTime > m_timeStart) && (currentTime < m_timeEnd));
171    }
172
173    /**
174     * Returns <code>true</code> if this login message is the enabled.<p>
175     *
176     * @return <code>true</code> if this login message is the enabled
177     */
178    public boolean isEnabled() {
179
180        return m_enabled;
181    }
182
183    /**
184     * Returns <code>true</code> if logins are currently forbidden according to the settings
185     * of this message.<p>
186     *
187     * This checks the time settings using <code>{@link #isActive()}</code> and
188     * <code>{@link #isEnabled()}</code> as well as the
189     * <code>{@link #isLoginForbidden()}</code> flag.<p>
190     *
191     * @return <code>true</code> if logins are currently forbidden according to the settings of this message
192     */
193    public boolean isLoginCurrentlyForbidden() {
194
195        return m_loginForbidden && isActive();
196    }
197
198    /**
199     * Returns <code>true</code> if logins are forbidden while this message is active.<p>
200     *
201     * @return <code>true</code> if logins are forbidden while this message is active
202     */
203    public boolean isLoginForbidden() {
204
205        return m_loginForbidden;
206    }
207
208    /**
209     * Sets the enabled status of this message.<p>
210     *
211     * @param enabled the enabled status to set
212     */
213    public void setEnabled(boolean enabled) {
214
215        checkFrozen();
216        m_enabled = enabled;
217    }
218
219    /**
220     * Sets the flag that controls if logins are forbidden while this message is active.<p>
221     *
222     * @param loginForbidden the flag to set
223     */
224    public void setLoginForbidden(boolean loginForbidden) {
225
226        checkFrozen();
227        m_loginForbidden = loginForbidden;
228    }
229
230    /**
231     * Sets the message to display.<p>
232     *
233     * @param message the message to set
234     */
235    public void setMessage(String message) {
236
237        checkFrozen();
238        if (isEnabled() && (message == null)) {
239            throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_LOGIN_MESSAGE_BAD_MESSAGE_0));
240        }
241        m_message = CmsRichTextArea.cleanHtml(message, true);
242    }
243
244    /**
245     * Sets the time when to finish displaying this message.<p>
246     *
247     * @param timeEnd the time to set
248     */
249    public void setTimeEnd(long timeEnd) {
250
251        checkFrozen();
252        if (timeEnd < 0) {
253            throw new CmsIllegalArgumentException(
254                Messages.get().container(Messages.ERR_LOGIN_MESSAGE_BAD_TIME_1, Long.valueOf(timeEnd)));
255        }
256        if (timeEnd == 0) {
257            timeEnd = DEFAULT_TIME_END;
258        }
259        if ((m_timeStart > 0) && (timeEnd <= m_timeStart)) {
260            throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_LOGIN_MESSAGE_BAD_END_TIME_0));
261        }
262        m_timeEnd = timeEnd;
263    }
264
265    /**
266     * Sets the time when to start displaying this message.<p>
267     *
268     * @param timeStart the time to set
269     */
270    public void setTimeStart(long timeStart) {
271
272        checkFrozen();
273        if (timeStart < 0) {
274            throw new CmsIllegalArgumentException(
275                Messages.get().container(Messages.ERR_LOGIN_MESSAGE_BAD_TIME_1, Long.valueOf(timeStart)));
276        }
277        if (timeStart == 0) {
278            timeStart = DEFAULT_TIME_START;
279        }
280        m_timeStart = timeStart;
281    }
282
283    /**
284     * Checks if this message is frozen.<p>
285     *
286     * @throws CmsRuntimeException in case the message is already frozen
287     */
288    protected void checkFrozen() throws CmsRuntimeException {
289
290        if (m_frozen) {
291            throw new CmsRuntimeException(Messages.get().container(Messages.ERR_LOGIN_MESSAGE_FROZEN_0));
292        }
293    }
294
295    /**
296     * Freezes the configuration of this login message object to prevent later changes.<p>
297     */
298    protected void setFrozen() {
299
300        m_frozen = true;
301    }
302}