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