001/*
002 *  Copyright 2020 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.odf.schedulable;
017
018import java.nio.file.Path;
019import java.time.Instant;
020import java.time.temporal.ChronoUnit;
021
022import org.apache.avalon.framework.activity.Initializable;
023import org.quartz.JobDataMap;
024import org.quartz.JobExecutionContext;
025
026import org.ametys.cms.schedule.AbstractDeleteFilesSchedulable;
027import org.ametys.plugins.core.schedule.Scheduler;
028import org.ametys.runtime.util.AmetysHomeHelper;
029
030/**
031 * Schedulable to clean educational booklet generated files.
032 */
033public class DeleteEducationalBookletSchedulable extends AbstractDeleteFilesSchedulable implements Initializable
034{
035    /** Parameter for workspace */
036    public static final String LIFETIME_KEY = "lifetime";
037    
038    private static final String __JOBDATAMAP_LIFETIME_KEY = Scheduler.PARAM_VALUES_PREFIX + LIFETIME_KEY;
039
040    private Path _rootPath;
041    
042    @Override
043    public void initialize() throws Exception
044    {
045        _rootPath = AmetysHomeHelper.getAmetysHomeData().toPath().resolve(EducationalBookletSchedulable.EDUCATIONAL_BOOKLET_DIR_NAME);
046    }
047    
048    @Override
049    protected DeleteFilesConfiguration _getConfiguration(JobExecutionContext context)
050    {
051        DeleteFilesConfiguration configuration = new DeleteFilesConfiguration(_rootPath);
052        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
053        long lifetime = jobDataMap.getLong(__JOBDATAMAP_LIFETIME_KEY);
054        configuration.setAgeLimit(Instant.now().minus(lifetime, ChronoUnit.DAYS));
055        return configuration;
056    }
057}