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.ui.apps.filehistory;
029
030import org.opencms.file.CmsObject;
031import org.opencms.main.CmsException;
032import org.opencms.main.CmsLog;
033import org.opencms.report.A_CmsReportThread;
034import org.opencms.report.I_CmsReport;
035import org.opencms.ui.CmsVaadinUtils;
036import org.opencms.ui.apps.Messages;
037
038import org.apache.commons.logging.Log;
039
040/**
041 * Clears the file history of the OpenCms database.<p>
042 *
043 * @since 6.0.0
044 */
045public class CmsHistoryClearThread extends A_CmsReportThread {
046
047    /** The logger for this class. */
048    static Log LOG = CmsLog.getLog(CmsHistoryClearThread.class.getName());
049
050    /**Clear all deleted versions older than this date.*/
051    private long m_dateClearDeletedOlder;
052
053    /**amount of versions to keep for deleted resources. */
054    private int m_keepDeletedVersions;
055
056    /**amount of versions to keep.*/
057    private int m_keepVersions;
058
059    /**
060     * Creates the history clear Thread.<p>
061     *
062     * @param cms the current OpenCms context object
063     * @param keepV count of Versions to keep
064     * @param keepD count of Versions to keep for deleted resources
065     * @param date Clear all deleted versions older than this date
066     */
067    public CmsHistoryClearThread(CmsObject cms, int keepV, int keepD, long date) {
068
069        super(
070            cms,
071            CmsVaadinUtils.getMessageText(
072                Messages.GUI_FILEHISTORY_DELETE_THREAD_NAME_1,
073                cms.getRequestContext().getCurrentProject().getName()));
074
075        m_keepVersions = keepV;
076        m_keepDeletedVersions = keepD;
077        m_dateClearDeletedOlder = date;
078        initHtmlReport(cms.getRequestContext().getLocale());
079    }
080
081    /**
082     * @see org.opencms.report.A_CmsReportThread#getReportUpdate()
083     */
084    @Override
085    public String getReportUpdate() {
086
087        return getReport().getReportUpdate();
088    }
089
090    /**
091     * @see java.lang.Runnable#run()
092     */
093    @Override
094    public void run() {
095
096        LOG.info("Start delete history thread from user " + getCms().getRequestContext().getCurrentUser().getName());
097        LOG.info(
098            "Parameter: m_keepVersions="
099                + m_keepVersions
100                + ", m_keepDeletedVersions="
101                + m_keepDeletedVersions
102                + ", m_dateClearDeletedOlder="
103                + m_dateClearDeletedOlder);
104
105        getReport().println(
106            Messages.get().container(Messages.RPT_DELETE_FILEHISTORY_BEGIN_0),
107            I_CmsReport.FORMAT_HEADLINE);
108
109        if (m_dateClearDeletedOlder == 0) {
110            m_dateClearDeletedOlder = -1;
111        }
112
113        // delete the historical files
114        try {
115            getCms().deleteHistoricalVersions(m_keepVersions, m_keepDeletedVersions, m_dateClearDeletedOlder, getReport());
116            LOG.info("Delete history thread successfully finished.");
117        } catch (CmsException e) {
118            getReport().println(e);
119            LOG.error("Delete history thread stoped because of exceptions", e);
120        }
121        LOG.info("Delete history thread closed.");
122        getReport().println(
123            Messages.get().container(Messages.RPT_DELETE_FILEHISTORY_END_0),
124            I_CmsReport.FORMAT_HEADLINE);
125    }
126}