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.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 javax.servlet.ServletException;
050import javax.servlet.http.HttpServletRequest;
051import javax.servlet.http.HttpServletResponse;
052import javax.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    /** The history clear object that is edited on this dialog. */
071    protected CmsHistoryClear m_historyClear;
072
073    /** Widget value. */
074    private String m_clearDeletedMode = MODE_CLEANDELETED_KEEP_RESTORE_VERSION;
075
076    /** Cleanup deleted history files setting. */
077    public static final String MODE_CLEANDELETED_KEEP_RESTORE_VERSION = "keeprestore";
078
079    /** Cleanup deleted history files setting. */
080    public static final String MODE_CLEANDELETED_DELETE_ALL = "deleteall";
081
082    /** Cleanup deleted history files setting. */
083    public static final String MODE_CLEANDELETED_DELETE_NONE = "deletenone";
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(new CmsWidgetDialogParameter(
210            m_historyClear,
211            "clearDeletedMode",
212            PAGES[0],
213            new CmsRadioSelectWidget(getClearDeletedModes())));
214        addWidget(new CmsWidgetDialogParameter(m_historyClear, "clearOlderThan", PAGES[0], new CmsCalendarWidget()));
215    }
216
217    /**
218     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
219     */
220    @Override
221    protected String[] getPageArray() {
222
223        return PAGES;
224    }
225
226    /**
227     * Initializes this widget dialog's object.<p>
228     */
229    protected void initHistoryClearObject() {
230
231        Object o;
232
233        if (CmsStringUtil.isEmpty(getParamAction())) {
234            o = new CmsHistoryClear();
235        } else {
236            // this is not the initial call, get the job object from session
237            o = getDialogObject();
238        }
239
240        if (!(o instanceof CmsHistoryClear)) {
241
242            // create a new history settings handler object
243            m_historyClear = new CmsHistoryClear();
244        } else {
245
246            // reuse html import handler object stored in session
247            m_historyClear = (CmsHistoryClear)o;
248        }
249    }
250
251    /**
252     * @see org.opencms.workplace.CmsWorkplace#initMessages()
253     */
254    @Override
255    protected void initMessages() {
256
257        // add specific dialog resource bundle
258        addMessages(Messages.get().getBundleName());
259
260        // add default resource bundles
261        super.initMessages();
262    }
263
264    /**
265     * Returns a list with the possible modes for the clean deleted action.<p>
266     *
267     * @return a list with the possible modes for the clean deleted action
268     */
269    private List getClearDeletedModes() {
270
271        ArrayList ret = new ArrayList();
272
273        ret.add(
274            new CmsSelectWidgetOption(
275                MODE_CLEANDELETED_KEEP_RESTORE_VERSION,
276                getClearDeletedMode().equals(MODE_CLEANDELETED_KEEP_RESTORE_VERSION),
277                key(Messages.GUI_HISTORY_CLEAR_DELETED_KEEPRESTORE_0)));
278        ret.add(new CmsSelectWidgetOption(
279            MODE_CLEANDELETED_DELETE_ALL,
280            getClearDeletedMode().equals(MODE_CLEANDELETED_DELETE_ALL),
281            key(Messages.GUI_HISTORY_CLEAR_DELETED_DELETEALL_0)));
282        ret.add(new CmsSelectWidgetOption(
283            MODE_CLEANDELETED_DELETE_NONE,
284            getClearDeletedMode().equals(MODE_CLEANDELETED_DELETE_NONE),
285            key(Messages.GUI_HISTORY_CLEAR_DELETED_DELETENONE_0)));
286
287        return ret;
288    }
289
290    /**
291     * Returns a list with the possible versions to choose from.<p>
292     *
293     * @return a list with the possible versions to choose from
294     */
295    private List getVersions() {
296
297        ArrayList ret = new ArrayList();
298
299        int defaultHistoryVersions = OpenCms.getSystemInfo().getHistoryVersions();
300        int historyVersions = 0;
301
302        // Add the option for disabled version history
303        ret.add(
304            new CmsSelectWidgetOption(
305                "-1",
306                true,
307                Messages.get().getBundle().key(Messages.GUI_HISTORY_CLEAR_VERSION_SELECT_0)));
308
309        // 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
310        while (historyVersions < 50) {
311
312            // increment the history version
313            historyVersions++;
314
315            if (((historyVersions % 5) == 0) || (historyVersions <= 10)) {
316
317                ret.add(
318                    new CmsSelectWidgetOption(String.valueOf(historyVersions), false, String.valueOf(historyVersions)));
319            }
320        }
321
322        // If the default setting for the version history is more than 50
323        if (defaultHistoryVersions > historyVersions) {
324            ret.add(
325                new CmsSelectWidgetOption(
326                    String.valueOf(defaultHistoryVersions),
327                    false,
328                    String.valueOf(defaultHistoryVersions)));
329        }
330
331        return ret;
332    }
333
334}