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.workplace.tools.workplace;
029
030import org.opencms.configuration.CmsVariablesConfiguration;
031import org.opencms.db.CmsLoginMessage;
032import org.opencms.jsp.CmsJspActionElement;
033import org.opencms.main.OpenCms;
034import org.opencms.widgets.CmsCalendarWidget;
035import org.opencms.widgets.CmsCheckboxWidget;
036import org.opencms.widgets.CmsTextareaWidget;
037import org.opencms.workplace.CmsWidgetDialog;
038import org.opencms.workplace.CmsWidgetDialogParameter;
039import org.opencms.workplace.CmsWorkplaceSettings;
040
041import java.util.ArrayList;
042import java.util.List;
043
044import javax.servlet.http.HttpServletRequest;
045import javax.servlet.http.HttpServletResponse;
046import javax.servlet.jsp.PageContext;
047
048/**
049 * Dialog to edit the login message of the OpenCms Workplace.<p>
050 *
051 * @since 6.0.0
052 */
053public class CmsEditLoginMessageDialog extends CmsWidgetDialog {
054
055    /** localized messages Keys prefix. */
056    public static final String KEY_PREFIX = "loginmsg";
057
058    /** Defines which pages are valid for this dialog. */
059    public static final String[] PAGES = {"page1"};
060
061    /** The login message that is edited with this dialog. */
062    private CmsLoginMessage m_loginMessage;
063
064    /**
065     * Public constructor with JSP action element.<p>
066     *
067     * @param jsp an initialized JSP action element
068     */
069    public CmsEditLoginMessageDialog(CmsJspActionElement jsp) {
070
071        super(jsp);
072    }
073
074    /**
075     * Public constructor with JSP variables.<p>
076     *
077     * @param context the JSP page context
078     * @param req the JSP request
079     * @param res the JSP response
080     */
081    public CmsEditLoginMessageDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
082
083        this(new CmsJspActionElement(context, req, res));
084    }
085
086    /**
087     * Commits the edited login message to the login manager.<p>
088     */
089    @Override
090    public void actionCommit() {
091
092        List<Throwable> errors = new ArrayList<Throwable>();
093
094        try {
095            // set the edited message
096            OpenCms.getLoginManager().setLoginMessage(getCms(), m_loginMessage);
097            // update the system configuration
098            OpenCms.writeConfiguration(CmsVariablesConfiguration.class);
099            // clear the message object
100            setDialogObject(null);
101        } catch (Throwable t) {
102            errors.add(t);
103        }
104
105        // set the list of errors to display when saving failed
106        setCommitErrors(errors);
107    }
108
109    /**
110     * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
111     *
112     * This overwrites the method from the super class to create a layout variation for the widgets.<p>
113     *
114     * @param dialog the dialog (page) to get the HTML for
115     * @return the dialog HTML for all defined widgets of the named dialog (page)
116     */
117    @Override
118    protected String createDialogHtml(String dialog) {
119
120        StringBuffer result = new StringBuffer(1024);
121
122        // create widget table
123        result.append(createWidgetTableStart());
124
125        // show error header once if there were validation errors
126        result.append(createWidgetErrorHeader());
127
128        // create the widgets for the first dialog page
129        result.append(dialogBlockStart(key(Messages.GUI_EDITOR_LABEL_ACTIVATE_BLOCK_0)));
130        result.append(createWidgetTableStart());
131        result.append(createDialogRowsHtml(0, 0));
132        result.append(createWidgetTableEnd());
133        result.append(dialogBlockEnd());
134        result.append(dialogBlockStart(key(Messages.GUI_EDITOR_LABEL_CONFIGURATION_BLOCK_0)));
135        result.append(createWidgetTableStart());
136        result.append(createDialogRowsHtml(1, 4));
137        result.append(createWidgetTableEnd());
138        result.append(dialogBlockEnd());
139
140        // close widget table
141        result.append(createWidgetTableEnd());
142
143        return result.toString();
144    }
145
146    /**
147     * Creates the list of widgets for this dialog.<p>
148     */
149    @Override
150    protected void defineWidgets() {
151
152        // initialize the object to use for the dialog
153        initLoginMessageObject();
154
155        setKeyPrefix(KEY_PREFIX);
156
157        // required to read the default values for the optional context parameters for the widgets
158        CmsLoginMessage def = new CmsLoginMessage();
159
160        addWidget(new CmsWidgetDialogParameter(m_loginMessage, "enabled", PAGES[0], new CmsCheckboxWidget()));
161        addWidget(new CmsWidgetDialogParameter(m_loginMessage, "message", PAGES[0], new CmsTextareaWidget()));
162        addWidget(new CmsWidgetDialogParameter(m_loginMessage, "loginForbidden", PAGES[0], new CmsCheckboxWidget()));
163        addWidget(
164            new CmsWidgetDialogParameter(
165                m_loginMessage,
166                "timeStart",
167                String.valueOf(def.getTimeStart()),
168                PAGES[0],
169                new CmsCalendarWidget(),
170                0,
171                1));
172        addWidget(
173            new CmsWidgetDialogParameter(
174                m_loginMessage,
175                "timeEnd",
176                String.valueOf(def.getTimeEnd()),
177                PAGES[0],
178                new CmsCalendarWidget(),
179                0,
180                1));
181    }
182
183    /**
184     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
185     */
186    @Override
187    protected String[] getPageArray() {
188
189        return PAGES;
190    }
191
192    /**
193     * Initializes the login message object for this dialog.<p>
194     */
195    protected void initLoginMessageObject() {
196
197        Object o = getDialogObject();
198
199        if ((o == null) || !(o instanceof CmsLoginMessage)) {
200            o = OpenCms.getLoginManager().getLoginMessage();
201        }
202
203        if (o != null) {
204            m_loginMessage = (CmsLoginMessage)((CmsLoginMessage)o).clone();
205        } else {
206            m_loginMessage = new CmsLoginMessage();
207        }
208    }
209
210    /**
211     * @see org.opencms.workplace.CmsWorkplace#initMessages()
212     */
213    @Override
214    protected void initMessages() {
215
216        // add specific dialog resource bundle
217        addMessages(Messages.get().getBundleName());
218        // add default resource bundles
219        super.initMessages();
220    }
221
222    /**
223     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
224     */
225    @Override
226    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
227
228        // initialize parameters and dialog actions in super implementation
229        super.initWorkplaceRequestValues(settings, request);
230
231        // save the current login message (may be changed because of the widget values)
232        setDialogObject(m_loginMessage);
233    }
234
235}