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.util.ArrayList;
019import java.util.Date;
020import java.util.List;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.commons.lang.time.DateUtils;
025import org.quartz.JobDataMap;
026import org.quartz.JobExecutionContext;
027
028import org.ametys.cms.repository.Content;
029import org.ametys.cms.repository.ContentDAO;
030import org.ametys.cms.repository.ContentQueryHelper;
031import org.ametys.cms.repository.ContentTypeExpression;
032import org.ametys.cms.repository.WorkflowStepExpression;
033import org.ametys.core.schedule.Schedulable;
034import org.ametys.plugins.core.impl.schedule.AbstractStaticSchedulable;
035import org.ametys.plugins.core.schedule.Scheduler;
036import org.ametys.plugins.repository.AmetysObjectIterable;
037import org.ametys.plugins.repository.AmetysObjectResolver;
038import org.ametys.plugins.repository.query.expression.AndExpression;
039import org.ametys.plugins.repository.query.expression.Expression;
040import org.ametys.plugins.repository.query.expression.Expression.Operator;
041import org.ametys.web.repository.content.jcr.ModifiableDefaultWebContent;
042
043/**
044 * A {@link Schedulable} job which delete classified ads after an defined number of days
045 */
046public class DeleteClassifiedAdsSchedulable extends AbstractStaticSchedulable
047{
048    /** The key for the number of day */
049    public static final String JOBDATAMAP_NB_DAY = "nbDay";
050    
051    /** The content type id */
052    public static final String CLASSIFIED_ADS_CONTENT_TYPE = "org.ametys.plugins.classified.ads.Content.ads";
053    
054    /** The Ametys object resolver */
055    protected AmetysObjectResolver _resolver;
056    
057    /** The content DAO */
058    protected ContentDAO _contentDAO;
059    
060    @Override
061    public void service(ServiceManager manager) throws ServiceException
062    {
063        super.service(manager);
064        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
065        _contentDAO = (ContentDAO) manager.lookup(ContentDAO.ROLE);
066    }
067    
068    @Override
069    public void execute(JobExecutionContext context) throws Exception
070    {
071        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
072        long nbDay = (Long) jobDataMap.get(Scheduler.PARAM_VALUES_PREFIX + JOBDATAMAP_NB_DAY);
073        
074        // Delete Validated contents
075        Expression contentTypeExpression = new ContentTypeExpression(Operator.EQ, CLASSIFIED_ADS_CONTENT_TYPE);
076        Expression workflowExpression = new WorkflowStepExpression(Operator.EQ, 3);
077        Expression expression = new AndExpression(contentTypeExpression, workflowExpression);
078        String query = ContentQueryHelper.getContentXPathQuery(expression);
079
080        AmetysObjectIterable<Content> contents = _resolver.query(query);
081
082        // Today + delay
083        Date compareDate = DateUtils.addDays(new Date(), -(int) nbDay);
084        List<String> contentIds = new ArrayList<>();
085        for (Content content : contents)
086        {
087            assert content instanceof ModifiableDefaultWebContent;
088            Date validationDate = ((ModifiableDefaultWebContent) content).getLastValidationDate();
089            if (compareDate.compareTo(validationDate) >= 0)
090            {
091                contentIds.add(content.getId());
092            }
093        }
094        
095        _contentDAO.deleteContents(contentIds, true);
096    }
097}