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.plugins.extraction.execution;
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.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.quartz.JobDataMap;
026import org.quartz.JobExecutionContext;
027
028import org.ametys.cms.schedule.AbstractDeleteFilesSchedulable;
029import org.ametys.core.right.RightManager;
030import org.ametys.core.right.RightManager.RightResult;
031import org.ametys.core.schedule.Schedulable;
032import org.ametys.core.user.CurrentUserProvider;
033import org.ametys.core.user.UserIdentity;
034import org.ametys.plugins.core.schedule.Scheduler;
035import org.ametys.plugins.extraction.ExtractionConstants;
036import org.ametys.runtime.util.AmetysHomeHelper;
037
038/**
039 * A {@link Schedulable} job for deleting old extractions
040 */
041public class DeleteExtractionsSchedulable extends AbstractDeleteFilesSchedulable implements Initializable
042{
043    /** Parameter for workspace */
044    public static final String LIFETIME_KEY = "lifetime";
045    
046    private static final String __JOBDATAMAP_LIFETIME_KEY = Scheduler.PARAM_VALUES_PREFIX + LIFETIME_KEY;
047    
048    /** Right Manager */
049    protected RightManager _rightManager;
050    
051    /** Current user provider */
052    protected CurrentUserProvider _currentUserProvider;
053    
054    private Path _rootPath;
055
056    @Override
057    public void service(ServiceManager manager) throws ServiceException
058    {
059        super.service(manager);
060        _rightManager = (RightManager) manager.lookup(RightManager.ROLE);
061        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
062    }
063    
064    @Override
065    public void initialize() throws Exception
066    {
067        _rootPath = AmetysHomeHelper.getAmetysHomeData().toPath().resolve(ExtractionConstants.RESULT_EXTRACTION_DIR_NAME);
068    }
069    
070    @Override
071    public void execute(JobExecutionContext context) throws Exception
072    {
073        UserIdentity user = _currentUserProvider.getUser();
074        if (_rightManager.hasRight(user, ExtractionConstants.MODIFY_EXTRACTION_RIGHT_ID, "/${WorkspaceName}") != RightResult.RIGHT_ALLOW)
075        {
076            throw new IllegalStateException("User " + user + " try to delete extractions with no sufficient rights");
077        }
078        
079        super.execute(context);
080    }
081    
082    @Override
083    protected DeleteFilesConfiguration _getConfiguration(JobExecutionContext context)
084    {
085        DeleteFilesConfiguration configuration = new DeleteFilesConfiguration(_rootPath);
086        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
087        long lifetime = jobDataMap.getLong(__JOBDATAMAP_LIFETIME_KEY);
088        configuration.setAgeLimit(Instant.now().minus(lifetime, ChronoUnit.DAYS));
089        return configuration;
090    }
091}