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, 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.ui.apps.sessions;
029
030import org.opencms.configuration.CmsVariablesConfiguration;
031import org.opencms.db.CmsLoginMessage;
032import org.opencms.main.CmsLog;
033import org.opencms.main.OpenCms;
034import org.opencms.ui.A_CmsUI;
035import org.opencms.ui.CmsVaadinUtils;
036import org.opencms.ui.apps.Messages;
037import org.opencms.ui.components.CmsBasicDialog;
038import org.opencms.ui.components.CmsDateField;
039import org.opencms.ui.components.CmsRichTextArea;
040
041import java.util.Date;
042
043import org.apache.commons.logging.Log;
044
045import com.vaadin.data.Binder;
046import com.vaadin.ui.Button;
047import com.vaadin.ui.Button.ClickEvent;
048import com.vaadin.ui.Button.ClickListener;
049import com.vaadin.ui.CheckBox;
050import com.vaadin.ui.TabSheet;
051import com.vaadin.ui.Window;
052
053/**
054 * Class for the Edit Login View.<p>
055 */
056public class CmsEditLoginView extends CmsBasicDialog {
057
058    /** The logger for this class. */
059    static Log LOG = CmsLog.getLog(CmsEditLoginView.class.getName());
060
061    /**vaadin serial ok.*/
062    private static final long serialVersionUID = -1053691437033852491L;
063
064    /**vaadin component.*/
065    private Button m_cancel;
066
067    /**vaadin component.*/
068    private Button m_deleteBefore;
069
070    /**vaadin component.*/
071    private Button m_deleteAfter;
072
073    /**vaadin component.*/
074    private CheckBox m_enabledAfter;
075
076    /**vaadin component.*/
077    private CheckBox m_enabledBefore;
078
079    /**date field.*/
080    private CmsDateField m_endTimeAfter;
081
082    /** The form field binder. */
083    private Binder<CmsLoginMessage> m_formBinderAfter;
084
085    /** The form field binder. */
086    private Binder<CmsLoginMessage> m_formBinderBefore;
087
088    /**Vaadin component. */
089    private TabSheet m_tab;
090
091    /**vaadin component.*/
092    private CheckBox m_logoutAfter;
093
094    /**vaadin component.*/
095    protected CmsRichTextArea m_messageAfter;
096
097    /**vaadin component.*/
098    protected CmsRichTextArea m_messageBefore;
099
100    /**vaadin component.*/
101    private Button m_ok;
102
103    /**date field.*/
104    private CmsDateField m_startTimeAfter;
105
106    /**
107     * Public constructor.<p>
108     *
109     * @param window to be closed
110     */
111    public CmsEditLoginView(final Window window) {
112
113        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
114        setHeight("100%");
115        bindFields();
116
117        CmsLoginMessage message = OpenCms.getLoginManager().getLoginMessage();
118        if (message == null) {
119            message = new CmsLoginMessage();
120        }
121
122        m_formBinderAfter.readBean(message);
123        if (!message.isEnabled()) {
124            m_endTimeAfter.setValue(null);
125            m_startTimeAfter.setValue(null);
126        }
127        CmsLoginMessage messageBefore = OpenCms.getLoginManager().getBeforeLoginMessage();
128        if (messageBefore == null) {
129            messageBefore = new CmsLoginMessage();
130        }
131        m_formBinderBefore.readBean(messageBefore);
132
133        m_cancel.addClickListener(new ClickListener() {
134
135            private static final long serialVersionUID = 4425001638229366505L;
136
137            public void buttonClick(ClickEvent event) {
138
139                window.close();
140            }
141
142        });
143
144        m_ok.addClickListener(new Button.ClickListener() {
145
146            private static final long serialVersionUID = 5512397920545155478L;
147
148            public void buttonClick(ClickEvent event) {
149
150                if (isFormValid()) {
151                    submit();
152                    window.close();
153                }
154
155            }
156        });
157        m_deleteBefore.addClickListener(event -> m_messageBefore.clear());
158        m_deleteAfter.addClickListener(event -> m_messageAfter.clear());
159    }
160
161    /**
162     * Gets end time from formular.<p>
163     *
164     * @return time as long
165     */
166    protected long getEnd() {
167
168        if (m_endTimeAfter.getValue() == null) {
169            return 0;
170        }
171        return m_endTimeAfter.getDate().getTime();
172    }
173
174    /**
175     * Gets start time from formular.<p>
176     *
177     * @return time as long
178     */
179    protected long getStart() {
180
181        if (m_startTimeAfter.getValue() == null) {
182            return 0;
183        }
184        return m_startTimeAfter.getDate().getTime();
185    }
186
187    /**
188     * Checks if formular is valid.<p>
189     *
190     * @return true if all fields are ok
191     */
192    protected boolean isFormValid() {
193
194        // return m_startTimeAfter.isValid() & m_endTimeAfter.isValid() & m_messageAfter.isValid();
195        return !m_formBinderAfter.validate().hasErrors();
196    }
197
198    /**
199     * Saves the settings.<p>
200     */
201    protected void submit() {
202
203        CmsLoginMessage loginMessage = new CmsLoginMessage();
204        CmsLoginMessage beforeLoginMessage = new CmsLoginMessage();
205        try {
206            m_formBinderAfter.writeBean(loginMessage);
207            m_formBinderBefore.writeBean(beforeLoginMessage);
208            OpenCms.getLoginManager().setLoginMessage(A_CmsUI.getCmsObject(), loginMessage);
209            OpenCms.getLoginManager().setBeforeLoginMessage(A_CmsUI.getCmsObject(), beforeLoginMessage);
210            // update the system configuration
211            OpenCms.writeConfiguration(CmsVariablesConfiguration.class);
212        } catch (Exception e) {
213            LOG.error("Unable to save Login Message", e);
214        }
215    }
216
217    /**
218     * Checks whether the entered start end end times are valid.<p>
219     *
220     * @return <code>true</code> in case the times are valid
221     */
222    boolean hasValidTimes() {
223
224        if ((getEnd() > 0L) && (getEnd() < System.currentTimeMillis())) {
225            return false;
226        }
227
228        return ((getEnd() == 0) | (getStart() == 0)) || (getEnd() >= getStart());
229    }
230
231    /**
232     * Binds the form fields.<p>
233     */
234    private void bindFields() {
235
236        m_formBinderAfter = new Binder<>();
237        m_formBinderAfter.bind(m_messageAfter, CmsLoginMessage::getMessage, CmsLoginMessage::setMessage);
238
239        m_formBinderAfter.bind(
240            m_enabledAfter,
241            loginMessage -> Boolean.valueOf(loginMessage.isEnabled()),
242            (loginMessage, enabled) -> loginMessage.setEnabled(enabled.booleanValue()));
243
244        m_formBinderAfter.bind(
245            m_logoutAfter,
246            loginMessage -> Boolean.valueOf(loginMessage.isLoginForbidden()),
247            (loginMessage, forbidden) -> loginMessage.setLoginForbidden(forbidden.booleanValue()));
248
249        m_formBinderAfter.forField(m_endTimeAfter).withValidator(
250            endTime -> hasValidTimes(),
251            CmsVaadinUtils.getMessageText(Messages.GUI_MESSAGES_LOGINMESSAGE_VAL_DATE_0)).bind(
252                loginMessage -> loginMessage.getTimeEnd() != CmsLoginMessage.DEFAULT_TIME_END
253                ? CmsDateField.dateToLocalDateTime(new Date(loginMessage.getTimeEnd()))
254                : null,
255                (loginMessage, endTime) -> loginMessage.setTimeEnd(
256                    endTime != null
257                    ? CmsDateField.localDateTimeToDate(endTime).getTime()
258                    : CmsLoginMessage.DEFAULT_TIME_END));
259        m_formBinderAfter.forField(m_startTimeAfter).withValidator(
260            startTime -> hasValidTimes(),
261            CmsVaadinUtils.getMessageText(Messages.GUI_MESSAGES_LOGINMESSAGE_VAL_DATE_0)).bind(
262                loginMessage -> loginMessage.getTimeStart() != CmsLoginMessage.DEFAULT_TIME_START
263                ? CmsDateField.dateToLocalDateTime(new Date(loginMessage.getTimeStart()))
264                : null,
265                (loginMessage, startTime) -> loginMessage.setTimeStart(
266                    startTime != null
267                    ? CmsDateField.localDateTimeToDate(startTime).getTime()
268                    : CmsLoginMessage.DEFAULT_TIME_START));
269        m_formBinderBefore = new Binder<>();
270        m_formBinderBefore.bind(m_messageBefore, CmsLoginMessage::getMessage, CmsLoginMessage::setMessage);
271
272        m_formBinderBefore.bind(
273            m_enabledBefore,
274            loginMessage -> Boolean.valueOf(loginMessage.isEnabled()),
275            (loginMessage, enabled) -> loginMessage.setEnabled(enabled.booleanValue()));
276    }
277}