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.history;
029
030import org.opencms.configuration.CmsSystemConfiguration;
031import org.opencms.jsp.CmsJspActionElement;
032import org.opencms.main.CmsIllegalArgumentException;
033import org.opencms.main.OpenCms;
034import org.opencms.util.CmsStringUtil;
035import org.opencms.widgets.CmsRadioSelectWidget;
036import org.opencms.widgets.CmsSelectWidget;
037import org.opencms.widgets.CmsSelectWidgetOption;
038import org.opencms.workplace.CmsWidgetDialog;
039import org.opencms.workplace.CmsWidgetDialogParameter;
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 enter the settings for the history in the administration view.<p>
050 *
051 * @since 6.9.1
052 */
053public class CmsHistorySettingsDialog extends CmsWidgetDialog {
054
055    /** localized messages Keys prefix. */
056    public static final String KEY_PREFIX = "histsettings";
057
058    /** Defines which pages are valid for this dialog. */
059    public static final String[] PAGES = {"page1"};
060
061    /** The history settings object that is edited on this dialog. */
062    protected CmsHistorySettings m_historySettings;
063
064    /**
065     * Public constructor with JSP action element.<p>
066     *
067     * @param jsp an initialized JSP action element
068     */
069    public CmsHistorySettingsDialog(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 CmsHistorySettingsDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
082
083        this(new CmsJspActionElement(context, req, res));
084    }
085
086    /**
087     * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
088     */
089    @Override
090    public void actionCommit() {
091
092        List errors = new ArrayList();
093        setDialogObject(m_historySettings);
094
095        boolean enabled = m_historySettings.getVersions() > -2;
096        int versions = m_historySettings.getVersions();
097
098        int versionsDeleted = 0;
099        switch (m_historySettings.getMode()) {
100            case CmsHistorySettings.MODE_DELETED_HISTORY_DISABLED:
101                versionsDeleted = 0;
102                break;
103            case CmsHistorySettings.MODE_DELETED_HISTORY_KEEP_NO_VERSIONS:
104                versionsDeleted = 1;
105                break;
106            case CmsHistorySettings.MODE_DELETED_HISTORY_KEEP_WITH_VERSIONS:
107                if (enabled) {
108                    versionsDeleted = versions;
109                } else {
110                    errors.add(
111                        new CmsIllegalArgumentException(
112                            Messages.get().container(Messages.GUI_HISTORY_SETTINGS_INVALID_0)));
113                }
114                break;
115            default:
116                versionsDeleted = 0;
117        }
118
119        OpenCms.getSystemInfo().setVersionHistorySettings(enabled, versions, versionsDeleted);
120        OpenCms.writeConfiguration(CmsSystemConfiguration.class);
121
122        // set the list of errors to display when saving failed
123        setCommitErrors(errors);
124    }
125
126    /**
127     * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
128     */
129    @Override
130    protected String createDialogHtml(String dialog) {
131
132        StringBuffer result = new StringBuffer(1024);
133
134        result.append(createWidgetTableStart());
135
136        // show error header once if there were validation errors
137        result.append(createWidgetErrorHeader());
138
139        if (dialog.equals(PAGES[0])) {
140
141            // create the widgets for the first dialog page
142            result.append(createWidgetBlockStart(key(Messages.GUI_HISTORY_SETTINGS_BLOCK_LABEL_0)));
143            result.append(createDialogRowsHtml(0, 1));
144            result.append(createWidgetBlockEnd());
145        }
146
147        result.append(createWidgetTableEnd());
148        return result.toString();
149    }
150
151    /**
152     * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
153     */
154    @Override
155    protected void defineWidgets() {
156
157        initHistorySettingsObject();
158        setKeyPrefix(KEY_PREFIX);
159
160        addWidget(
161            new CmsWidgetDialogParameter(m_historySettings, "versions", PAGES[0], new CmsSelectWidget(getVersions())));
162        addWidget(
163            new CmsWidgetDialogParameter(m_historySettings, "mode", PAGES[0], new CmsRadioSelectWidget(getModes())));
164    }
165
166    /**
167     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
168     */
169    @Override
170    protected String[] getPageArray() {
171
172        return PAGES;
173    }
174
175    /**
176     * Initializes this widget dialog's object.<p>
177     */
178    protected void initHistorySettingsObject() {
179
180        Object o;
181
182        if (CmsStringUtil.isEmpty(getParamAction())) {
183            o = new CmsHistorySettings();
184        } else {
185            // this is not the initial call, get the job object from session
186            o = getDialogObject();
187        }
188
189        if (!(o instanceof CmsHistorySettings)) {
190
191            // create a new history settings handler object
192            m_historySettings = new CmsHistorySettings();
193        } else {
194
195            // reuse html import handler object stored in session
196            m_historySettings = (CmsHistorySettings)o;
197        }
198    }
199
200    /**
201     * @see org.opencms.workplace.CmsWorkplace#initMessages()
202     */
203    @Override
204    protected void initMessages() {
205
206        // add specific dialog resource bundle
207        addMessages(Messages.get().getBundleName());
208
209        // add default resource bundles
210        super.initMessages();
211    }
212
213    /**
214     * Returns a list with the possible modes for the history to keep.<p>
215     *
216     * @return a list with the possible modes for the history to keep
217     */
218    private List getModes() {
219
220        ArrayList ret = new ArrayList();
221
222        ret.add(
223            new CmsSelectWidgetOption(
224                String.valueOf(CmsHistorySettings.MODE_DELETED_HISTORY_DISABLED),
225                m_historySettings.getMode() == CmsHistorySettings.MODE_DELETED_HISTORY_DISABLED,
226                key(Messages.GUI_HISTORY_SETTINGS_MODE_DISABLED_0)));
227        ret.add(new CmsSelectWidgetOption(
228            String.valueOf(CmsHistorySettings.MODE_DELETED_HISTORY_KEEP_NO_VERSIONS),
229            m_historySettings.getMode() == CmsHistorySettings.MODE_DELETED_HISTORY_KEEP_NO_VERSIONS,
230            key(Messages.GUI_HISTORY_SETTINGS_MODE_KEEP_NO_VERSIONS_0)));
231        ret.add(new CmsSelectWidgetOption(
232            String.valueOf(CmsHistorySettings.MODE_DELETED_HISTORY_KEEP_WITH_VERSIONS),
233            m_historySettings.getMode() == CmsHistorySettings.MODE_DELETED_HISTORY_KEEP_WITH_VERSIONS,
234            key(Messages.GUI_HISTORY_SETTINGS_MODE_KEEP_WITH_VERSIONS_0)));
235
236        return ret;
237    }
238
239    /**
240     * Returns a list with the possible versions to choose from.<p>
241     *
242     * @return a list with the possible versions to choose from
243     */
244    private List getVersions() {
245
246        ArrayList ret = new ArrayList();
247
248        int defaultHistoryVersions = OpenCms.getSystemInfo().getHistoryVersions();
249        int historyVersions = 0;
250
251        // Add the option for disabled version history
252        ret.add(
253            new CmsSelectWidgetOption(
254                String.valueOf(-2),
255                defaultHistoryVersions == -2,
256                key(Messages.GUI_HISTORY_SETTINGS_VERSIONS_DISABLED_0)));
257
258        // Iterate from 1 to 50 with a stepping of 1 for the first 10 entries and a stepping of five for the entries from 10 to 50
259        while (historyVersions < 50) {
260
261            // increment the history version
262            historyVersions++;
263
264            if (((historyVersions % 5) == 0) || (historyVersions <= 10)) {
265
266                boolean defaultValue = defaultHistoryVersions == historyVersions;
267                ret.add(
268                    new CmsSelectWidgetOption(
269                        String.valueOf(historyVersions),
270                        defaultValue,
271                        String.valueOf(historyVersions)));
272            }
273        }
274
275        // If the default setting for the version history is more than 50
276        if (defaultHistoryVersions > historyVersions) {
277            ret.add(
278                new CmsSelectWidgetOption(
279                    String.valueOf(defaultHistoryVersions),
280                    true,
281                    String.valueOf(defaultHistoryVersions)));
282        }
283
284        // Add the option for unlimited version history
285        ret.add(
286            new CmsSelectWidgetOption(
287                String.valueOf(-1),
288                defaultHistoryVersions == -1,
289                key(Messages.GUI_HISTORY_SETTINGS_VERSIONS_UNLIMITED_0)));
290
291        return ret;
292    }
293
294}