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.scheduler.jobs;
029
030import org.opencms.file.CmsObject;
031import org.opencms.report.CmsLogReport;
032import org.opencms.scheduler.I_CmsScheduledJob;
033import org.opencms.util.CmsStringUtil;
034
035import java.util.Calendar;
036import java.util.GregorianCalendar;
037import java.util.Map;
038
039/**
040 * A schedulable OpenCms job to clear the history.<p>
041 *
042 * The user to execute the process should have have access to the required "Workplace manager" role.<p>
043 *
044 * If there is an Uri set for the scheduled job, which should only be folders, it will be used
045 * for clearing the history only in there (and the subfolders).<p>
046 *
047 * Job parameters:<p>
048 * <dl>
049 * <dt><code>keepVersions={Number/Integer}</code></dt>
050 * <dd>Number/Integer to control how many versions will be kept.</dd>
051 * <dt><code>clearDeleted=true|false</code></dt>
052 * <dd>Boolean to configure if the versions of deleted resources should be cleared.
053 * The default is false.</dd>
054 * <dt><code>keepTimeRange</code></dt>
055 * <dd>Number/Integer to configure the number of days the versions of deleted resources will
056 * be kept. That means that all versions wich are older than the specified number will be deleted.
057 * This parameter is optional and only makes sense if the clearDeleted parameter is set to true.</dd>
058 * </dl>
059 *
060 * @since 7.0.0
061 */
062public class CmsHistoryClearJob implements I_CmsScheduledJob {
063
064    /** Name of the parameter where to configure how many versions are kept. */
065    public static final String PARAM_KEEPVERSIONS = "keepVersions";
066
067    /** Name of the parameter where to configure if versions of deleted resources are cleared. */
068    public static final String PARAM_CLEARDELETED = "clearDeleted";
069
070    /** Name of the parameter where to configure the number of days the versions will be kept. */
071    public static final String PARAM_KEEPTIMERANGE = "keepTimeRange";
072
073    /**
074     * @see org.opencms.scheduler.I_CmsScheduledJob#launch(org.opencms.file.CmsObject, java.util.Map)
075     */
076    public String launch(CmsObject cms, Map<String, String> parameters) throws Exception {
077
078        // read the parameter for the versions to keep
079        int keepVersions = Integer.parseInt(parameters.get(PARAM_KEEPVERSIONS));
080
081        // read the parameter if to clear versions of deleted resources
082        boolean clearDeleted = Boolean.valueOf(parameters.get(PARAM_CLEARDELETED)).booleanValue();
083
084        // read the optional parameter for the time range to keep versions
085        String keepTimeRangeStr = parameters.get(PARAM_KEEPTIMERANGE);
086        int keepTimeRange = -1;
087        if (!CmsStringUtil.isEmptyOrWhitespaceOnly(keepTimeRangeStr)) {
088            keepTimeRange = Integer.parseInt(keepTimeRangeStr);
089        }
090
091        // calculate the date from where to clear deleted versions
092        long timeDeleted = -1;
093        int keepDeletedVersions;
094        if (clearDeleted) {
095            keepDeletedVersions = 1;
096            GregorianCalendar cal = new GregorianCalendar();
097            cal.add(Calendar.DAY_OF_YEAR, (keepTimeRange) * -1);
098            timeDeleted = cal.getTimeInMillis();
099        } else {
100            keepDeletedVersions = -1;
101        }
102
103        // create a new report
104        CmsLogReport report = new CmsLogReport(cms.getRequestContext().getLocale(), CmsHistoryClearJob.class);
105
106        // delete the versions
107        cms.deleteHistoricalVersions(keepVersions, keepDeletedVersions, timeDeleted, report);
108
109        return null;
110    }
111
112}