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