001/*
002 *  Copyright 2025 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.joboffer;
017
018import java.time.Period;
019import java.time.ZonedDateTime;
020import java.util.Date;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.quartz.JobExecutionContext;
026
027import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
028import org.ametys.cms.repository.Content;
029import org.ametys.cms.repository.ContentDAO;
030import org.ametys.cms.repository.ContentQueryHelper;
031import org.ametys.cms.repository.ContentVersionHistoryHelper;
032import org.ametys.cms.repository.DefaultContent;
033import org.ametys.core.schedule.progression.ContainerProgressionTracker;
034import org.ametys.core.trace.ForensicLogger;
035import org.ametys.core.user.population.UserPopulationDAO;
036import org.ametys.core.util.DateUtils;
037import org.ametys.plugins.core.impl.schedule.AbstractStaticSchedulable;
038import org.ametys.plugins.core.schedule.Scheduler;
039import org.ametys.plugins.repository.AmetysObjectIterable;
040import org.ametys.plugins.repository.AmetysObjectResolver;
041import org.ametys.plugins.repository.query.expression.AndExpression;
042import org.ametys.plugins.repository.query.expression.DateExpression;
043import org.ametys.plugins.repository.query.expression.Expression;
044import org.ametys.plugins.repository.query.expression.Expression.Operator;
045import org.ametys.plugins.repository.query.expression.ExpressionContext;
046import org.ametys.runtime.config.Config;
047
048/**
049 * Schedulable to remove all the applications older than a given delay
050 */
051public class CleanExpiredApplicationSchedulable extends AbstractStaticSchedulable
052{
053    /** The content DAO */
054    protected ContentDAO _contentDAO;
055    /** The content type extension point */
056    protected ContentTypeExtensionPoint _cTypeEP;
057    /** The ametys object resolver */
058    protected AmetysObjectResolver _resolver;
059    /** The version history helper */
060    protected ContentVersionHistoryHelper _versionHistoryHelper;
061
062    @Override
063    public void service(ServiceManager manager) throws ServiceException
064    {
065        super.service(manager);
066        _contentDAO = (ContentDAO) manager.lookup(ContentDAO.ROLE);
067        _cTypeEP = (ContentTypeExtensionPoint) manager.lookup(ContentTypeExtensionPoint.ROLE);
068        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
069        _versionHistoryHelper = (ContentVersionHistoryHelper) manager.lookup(ContentVersionHistoryHelper.ROLE);
070    }
071    
072    public void execute(JobExecutionContext context, ContainerProgressionTracker progressionTracker) throws Exception
073    {
074        Long delay = (Long) context.getMergedJobDataMap().get(Scheduler.PARAM_VALUES_PREFIX + "delay");
075        if (delay == null)
076        {
077            delay = Config.getInstance().getValue("job-offer.clean-expired-application.delay", false, null);
078        }
079        if (delay == null || delay < 0)
080        {
081            throw new IllegalArgumentException("The expiration delay must be positive");
082        }
083        
084        ZonedDateTime expirationDateTime = ZonedDateTime.now().minus(Period.ofMonths(delay.intValue()));
085        Date expirationDate = DateUtils.asDate(expirationDateTime);
086        Expression dateExpression = new DateExpression(DefaultContent.METADATA_MODIFIED, Operator.LE, expirationDate, ExpressionContext.newInstance().withInternal(true));
087        
088        Expression contentTypeExpression = _cTypeEP.createHierarchicalCTExpression(JobOfferConstants.JOB_APPLICATION_CONTENT_TYPE);
089        AndExpression expiredApplicationExpression = new AndExpression(contentTypeExpression, dateExpression);
090        String query = ContentQueryHelper.getContentXPathQuery(expiredApplicationExpression);
091        try (AmetysObjectIterable<Content> applications = _resolver.query(query))
092        {
093            _contentDAO.forceDeleteContentsObj(applications, null);
094            ForensicLogger.info("data.policy.gdpr.remove.job-offer.applications", Map.of("handled", Long.toString(applications.getSize())), UserPopulationDAO.SYSTEM_USER_IDENTITY);
095        }
096        
097        _versionHistoryHelper.clearUnusedHistory(JobOfferConstants.JOB_APPLICATION_CONTENT_TYPE);
098    }
099}