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.file.CmsObject;
031import org.opencms.main.CmsException;
032import org.opencms.report.A_CmsReportThread;
033import org.opencms.report.I_CmsReport;
034
035/**
036 * Clears the file history of the OpenCms database.<p>
037 *
038 * @since 6.0.0
039 */
040public class CmsHistoryClearThread extends A_CmsReportThread {
041
042    private CmsHistoryClear m_historyClear;
043
044    /**
045     * Creates the history clear Thread.<p>
046     *
047     * @param cms the current OpenCms context object
048     * @param historyClear the settings to clear the history
049     */
050    public CmsHistoryClearThread(CmsObject cms, CmsHistoryClear historyClear) {
051
052        super(
053            cms,
054            Messages.get().getBundle().key(
055                Messages.GUI_HISTORY_CLEAR_THREAD_NAME_1,
056                cms.getRequestContext().getCurrentProject().getName()));
057        m_historyClear = historyClear;
058        initHtmlReport(cms.getRequestContext().getLocale());
059    }
060
061    /**
062     * @see org.opencms.report.A_CmsReportThread#getReportUpdate()
063     */
064    @Override
065    public String getReportUpdate() {
066
067        return getReport().getReportUpdate();
068    }
069
070    /**
071     * @see java.lang.Runnable#run()
072     */
073    @Override
074    public void run() {
075
076        getReport().println(Messages.get().container(Messages.RPT_DELETE_HISTORY_BEGIN_0), I_CmsReport.FORMAT_HEADLINE);
077
078        // get the necessary parameters from the map
079        int versions = m_historyClear.getKeepVersions();
080        int versionsDeleted;
081        long timeDeleted = m_historyClear.getClearOlderThan();
082
083        if (timeDeleted == 0) {
084            timeDeleted = -1;
085        }
086
087        if (m_historyClear.getClearDeletedMode().equals(CmsHistoryClearDialog.MODE_CLEANDELETED_DELETE_NONE)) {
088            versionsDeleted = -1;
089        } else if (m_historyClear.getClearDeletedMode().equals(CmsHistoryClearDialog.MODE_CLEANDELETED_DELETE_ALL)) {
090            versionsDeleted = 0;
091        } else {
092            versionsDeleted = 1;
093        }
094
095        // delete the historical files
096        try {
097            getCms().deleteHistoricalVersions(versions, versionsDeleted, timeDeleted, getReport());
098        } catch (CmsException e) {
099            getReport().println(e);
100        }
101        getReport().println(Messages.get().container(Messages.RPT_DELETE_HISTORY_END_0), I_CmsReport.FORMAT_HEADLINE);
102    }
103}