001/*
002 *  Copyright 2018 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.classified.ads;
017
018import java.time.Period;
019import java.time.ZonedDateTime;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.quartz.JobDataMap;
025import org.quartz.JobExecutionContext;
026
027import org.ametys.cms.repository.Content;
028import org.ametys.cms.repository.ContentDAO;
029import org.ametys.cms.repository.ContentQueryHelper;
030import org.ametys.cms.repository.ContentTypeExpression;
031import org.ametys.cms.repository.DefaultContent;
032import org.ametys.core.schedule.Schedulable;
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.Operator;
044import org.ametys.runtime.config.Config;
045
046/**
047 * A {@link Schedulable} job which delete classified ads after an defined number of days
048 */
049public class DeleteClassifiedAdsSchedulable extends AbstractStaticSchedulable
050{
051    /** The key for the number of month */
052    public static final String JOBDATAMAP_NB_MONTH = "nbMonth";
053    
054    /** The content type id */
055    public static final String CLASSIFIED_ADS_CONTENT_TYPE = "org.ametys.plugins.classified.ads.Content.ads";
056    
057    /** The Ametys object resolver */
058    protected AmetysObjectResolver _resolver;
059    
060    /** The content DAO */
061    protected ContentDAO _contentDAO;
062    
063    @Override
064    public void service(ServiceManager manager) throws ServiceException
065    {
066        super.service(manager);
067        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
068        _contentDAO = (ContentDAO) manager.lookup(ContentDAO.ROLE);
069    }
070    
071    @Override
072    public void execute(JobExecutionContext context, ContainerProgressionTracker progressionTracker) throws Exception
073    {
074        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
075        Long delay = (Long) jobDataMap.get(Scheduler.PARAM_VALUES_PREFIX + JOBDATAMAP_NB_MONTH);
076        if (delay == null)
077        {
078            delay = Config.getInstance().<Long>getValue("classified-ads.conservation.delay", false, null);
079        }
080        
081        if (delay == null || delay < 0)
082        {
083            throw new IllegalArgumentException("The expiration delay must be positive");
084        }
085        
086        ZonedDateTime expirationDate = ZonedDateTime.now().minus(Period.ofMonths(delay.intValue()));
087        AndExpression filter = new AndExpression();
088        filter.add(new DateExpression(DefaultContent.METADATA_CREATION, Operator.LE, DateUtils.asDate(expirationDate)));
089        filter.add(new ContentTypeExpression(Operator.EQ, CLASSIFIED_ADS_CONTENT_TYPE));
090        
091        String query = ContentQueryHelper.getContentXPathQuery(filter);
092        
093        try (AmetysObjectIterable<Content> contents = _resolver.query(query))
094        {
095            _contentDAO.forceDeleteContentsObj(contents.stream().toList(), null);
096            ForensicLogger.info("data.policy.classified-ads", Map.of("handled", Long.toString(contents.getSize())), UserPopulationDAO.SYSTEM_USER_IDENTITY);
097        }
098    }
099}