001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.jsp.CmsJspActionElement;
031import org.opencms.main.CmsIllegalArgumentException;
032import org.opencms.main.OpenCms;
033import org.opencms.util.CmsStringUtil;
034import org.opencms.widgets.CmsCalendarWidget;
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;
040import org.opencms.workplace.tools.CmsToolDialog;
041import org.opencms.workplace.tools.CmsToolManager;
042
043import java.io.IOException;
044import java.util.ArrayList;
045import java.util.HashMap;
046import java.util.List;
047import java.util.Map;
048
049import jakarta.servlet.ServletException;
050import jakarta.servlet.http.HttpServletRequest;
051import jakarta.servlet.http.HttpServletResponse;
052import jakarta.servlet.jsp.PageContext;
053
054/**
055 * Dialog to enter the settings to clear the history in the administration view.<p>
056 *
057 * @since 6.9.1
058 */
059public class CmsHistoryClearDialog extends CmsWidgetDialog {
060
061    /** localized messages Keys prefix. */
062    public static final String KEY_PREFIX = "histclear";
063
064    /** Defines which pages are valid for this dialog. */
065    public static final String[] PAGES = {"page1"};
066
067    /** The import JSP report workplace URI. */
068    protected static final String CLEAR_ACTION_REPORT = PATH_WORKPLACE + "admin/history/reports/clearhistory.jsp";
069
070    /** Cleanup deleted history files setting. */
071    public static final String MODE_CLEANDELETED_KEEP_RESTORE_VERSION = "keeprestore";
072
073    /** Cleanup deleted history files setting. */
074    public static final String MODE_CLEANDELETED_DELETE_ALL = "deleteall";
075
076    /** Cleanup deleted history files setting. */
077    public static final String MODE_CLEANDELETED_DELETE_NONE = "deletenone";
078
079    /** The history clear object that is edited on this dialog. */
080    protected CmsHistoryClear m_historyClear;
081
082    /** Widget value. */
083    private String m_clearDeletedMode = MODE_CLEANDELETED_KEEP_RESTORE_VERSION;
084
085    /**
086     * Public constructor with JSP action element.<p>
087     *
088     * @param jsp an initialized JSP action element
089     */
090    public CmsHistoryClearDialog(CmsJspActionElement jsp) {
091
092        super(jsp);
093    }
094
095    /**
096     * Public constructor with JSP variables.<p>
097     *
098     * @param context the JSP page context
099     * @param req the JSP request
100     * @param res the JSP response
101     */
102    public CmsHistoryClearDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
103
104        this(new CmsJspActionElement(context, req, res));
105    }
106
107    /**
108     * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
109     */
110    @Override
111    public void actionCommit() throws IOException, ServletException {
112
113        List errors = new ArrayList();
114        setDialogObject(m_historyClear);
115
116        if (m_historyClear.getKeepVersions() < 0) {
117            errors.add(
118                new CmsIllegalArgumentException(
119                    Messages.get().container(Messages.GUI_HISTORY_CLEAR_INVALID_SETTINGS_0)));
120        } else {
121
122            Map params = new HashMap();
123
124            // set the name of this class to get dialog object in report
125            params.put(CmsHistoryClearReport.PARAM_CLASSNAME, this.getClass().getName());
126
127            // set style to display report in correct layout
128            params.put(PARAM_STYLE, CmsToolDialog.STYLE_NEW);
129
130            // set close link to get back to overview after finishing the import
131            params.put(PARAM_CLOSELINK, CmsToolManager.linkForToolPath(getJsp(), "/history"));
132
133            // redirect to the report output JSP
134            getToolManager().jspForwardPage(this, CLEAR_ACTION_REPORT, params);
135        }
136
137        // set the list of errors to display when saving failed
138        setCommitErrors(errors);
139    }
140
141    /**
142     * Returns the cleanDeletedmode.<p>
143     *
144     * @return the clearDeletedmode
145     */
146    public final String getClearDeletedMode() {
147
148        // m_clearDeletedMode will be null in initial display because constructor triggers this before member initialization
149        String result = m_clearDeletedMode;
150        if (result == null) {
151            result = MODE_CLEANDELETED_KEEP_RESTORE_VERSION;
152        }
153        return result;
154    }
155
156    /**
157     * Sets the cleanDeletedmode.<p>
158     *
159     * @param clearDeletedmode the cleanDeletedmode to set
160     */
161    public final void setClearDeletedMode(String clearDeletedmode) {
162
163        m_clearDeletedMode = clearDeletedmode;
164    }
165
166    /**
167     * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
168     */
169    @Override
170    protected String createDialogHtml(String dialog) {
171
172        StringBuffer result = new StringBuffer(1024);
173
174        result.append(createWidgetTableStart());
175
176        // show error header once if there were validation errors
177        result.append(createWidgetErrorHeader());
178
179        if (dialog.equals(PAGES[0])) {
180
181            // create the widgets for the first dialog page
182            result.append(createWidgetBlockStart(key(Messages.GUI_HISTORY_CLEAR_BLOCK_LABEL_0)));
183
184            Object versionStr = (OpenCms.getSystemInfo().getHistoryVersions() == -1)
185            ? key(Messages.GUI_HISTORY_SETTINGS_VERSIONS_UNLIMITED_0)
186            : String.valueOf(OpenCms.getSystemInfo().getHistoryVersions());
187            result.append(key(Messages.GUI_HISTORY_CLEAR_VERSIONINFO_1, new Object[] {versionStr}));
188            result.append("<p>");
189
190            result.append(createDialogRowsHtml(0, 2));
191            result.append(createWidgetBlockEnd());
192        }
193
194        result.append(createWidgetTableEnd());
195        return result.toString();
196    }
197
198    /**
199     * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
200     */
201    @Override
202    protected void defineWidgets() {
203
204        initHistoryClearObject();
205        setKeyPrefix(KEY_PREFIX);
206
207        addWidget(
208            new CmsWidgetDialogParameter(m_historyClear, "keepVersions", PAGES[0], new CmsSelectWidget(getVersions())));
209        addWidget(
210            new CmsWidgetDialogParameter(
211                m_historyClear,
212                "clearDeletedMode",
213                PAGES[0],
214                new CmsRadioSelectWidget(getClearDeletedModes())));
215        addWidget(new CmsWidgetDialogParameter(m_historyClear, "clearOlderThan", PAGES[0], new CmsCalendarWidget()));
216    }
217
218    /**
219     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
220     */
221    @Override
222    protected String[] getPageArray() {
223
224        return PAGES;
225    }
226
227    /**
228     * Initializes this widget dialog's object.<p>
229     */
230    protected void initHistoryClearObject() {
231
232        Object o;
233
234        if (CmsStringUtil.isEmpty(getParamAction())) {
235            o = new CmsHistoryClear();
236        } else {
237            // this is not the initial call, get the job object from session
238            o = getDialogObject();
239        }
240
241        if (!(o instanceof CmsHistoryClear)) {
242
243            // create a new history settings handler object
244            m_historyClear = new CmsHistoryClear();
245        } else {
246
247            // reuse html import handler object stored in session
248            m_historyClear = (CmsHistoryClear)o;
249        }
250    }
251
252    /**
253     * @see org.opencms.workplace.CmsWorkplace#initMessages()
254     */
255    @Override
256    protected void initMessages() {
257
258        // add specific dialog resource bundle
259        addMessages(Messages.get().getBundleName());
260
261        // add default resource bundles
262        super.initMessages();
263    }
264
265    /**
266     * Returns a list with the possible modes for the clean deleted action.<p>
267     *
268     * @return a list with the possible modes for the clean deleted action
269     */
270    private List getClearDeletedModes() {
271
272        ArrayList ret = new ArrayList();
273
274        ret.add(
275            new CmsSelectWidgetOption(
276                MODE_CLEANDELETED_KEEP_RESTORE_VERSION,
277                getClearDeletedMode().equals(MODE_CLEANDELETED_KEEP_RESTORE_VERSION),
278                key(Messages.GUI_HISTORY_CLEAR_DELETED_KEEPRESTORE_0)));
279        ret.add(
280            new CmsSelectWidgetOption(
281                MODE_CLEANDELETED_DELETE_ALL,
282                getClearDeletedMode().equals(MODE_CLEANDELETED_DELETE_ALL),
283                key(Messages.GUI_HISTORY_CLEAR_DELETED_DELETEALL_0)));
284        ret.add(
285            new CmsSelectWidgetOption(
286                MODE_CLEANDELETED_DELETE_NONE,
287                getClearDeletedMode().equals(MODE_CLEANDELETED_DELETE_NONE),
288                key(Messages.GUI_HISTORY_CLEAR_DELETED_DELETENONE_0)));
289
290        return ret;
291    }
292
293    /**
294     * Returns a list with the possible versions to choose from.<p>
295     *
296     * @return a list with the possible versions to choose from
297     */
298    private List getVersions() {
299
300        ArrayList ret = new ArrayList();
301
302        int defaultHistoryVersions = OpenCms.getSystemInfo().getHistoryVersions();
303        int historyVersions = 0;
304
305        // Add the option for disabled version history
306        ret.add(
307            new CmsSelectWidgetOption(
308                "-1",
309                true,
310                Messages.get().getBundle().key(Messages.GUI_HISTORY_CLEAR_VERSION_SELECT_0)));
311
312        // 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
313        while (historyVersions < 50) {
314
315            // increment the history version
316            historyVersions++;
317
318            if (((historyVersions % 5) == 0) || (historyVersions <= 10)) {
319
320                ret.add(
321                    new CmsSelectWidgetOption(String.valueOf(historyVersions), false, String.valueOf(historyVersions)));
322            }
323        }
324
325        // If the default setting for the version history is more than 50
326        if (defaultHistoryVersions > historyVersions) {
327            ret.add(
328                new CmsSelectWidgetOption(
329                    String.valueOf(defaultHistoryVersions),
330                    false,
331                    String.valueOf(defaultHistoryVersions)));
332        }
333
334        return ret;
335    }
336
337}