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.runtime.config.Config; 046 047/** 048 * Schedulable to remove all the applications older than a given delay 049 */ 050public class CleanExpiredApplicationSchedulable extends AbstractStaticSchedulable 051{ 052 /** The content DAO */ 053 protected ContentDAO _contentDAO; 054 /** The content type extension point */ 055 protected ContentTypeExtensionPoint _cTypeEP; 056 /** The ametys object resolver */ 057 protected AmetysObjectResolver _resolver; 058 /** The version history helper */ 059 protected ContentVersionHistoryHelper _versionHistoryHelper; 060 061 @Override 062 public void service(ServiceManager manager) throws ServiceException 063 { 064 super.service(manager); 065 _contentDAO = (ContentDAO) manager.lookup(ContentDAO.ROLE); 066 _cTypeEP = (ContentTypeExtensionPoint) manager.lookup(ContentTypeExtensionPoint.ROLE); 067 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 068 _versionHistoryHelper = (ContentVersionHistoryHelper) manager.lookup(ContentVersionHistoryHelper.ROLE); 069 } 070 071 public void execute(JobExecutionContext context, ContainerProgressionTracker progressionTracker) throws Exception 072 { 073 Long delay = (Long) context.getMergedJobDataMap().get(Scheduler.PARAM_VALUES_PREFIX + "delay"); 074 if (delay == null) 075 { 076 delay = Config.getInstance().getValue("job-offer.clean-expired-application.delay", false, null); 077 } 078 if (delay == null || delay < 0) 079 { 080 throw new IllegalArgumentException("The expiration delay must be positive"); 081 } 082 083 ZonedDateTime expirationDateTime = ZonedDateTime.now().minus(Period.ofMonths(delay.intValue())); 084 Date expirationDate = DateUtils.asDate(expirationDateTime); 085 Expression dateExpression = new DateExpression(DefaultContent.METADATA_MODIFIED, Operator.LE, expirationDate); 086 087 Expression contentTypeExpression = _cTypeEP.createHierarchicalCTExpression(JobOfferConstants.JOB_APPLICATION_CONTENT_TYPE); 088 AndExpression expiredApplicationExpression = new AndExpression(contentTypeExpression, dateExpression); 089 String query = ContentQueryHelper.getContentXPathQuery(expiredApplicationExpression); 090 try (AmetysObjectIterable<Content> applications = _resolver.query(query)) 091 { 092 _contentDAO.forceDeleteContentsObj(applications, null); 093 ForensicLogger.info("data.policy.gdpr.remove.job-offer.applications", Map.of("handled", Long.toString(applications.getSize())), UserPopulationDAO.SYSTEM_USER_IDENTITY); 094 } 095 096 _versionHistoryHelper.clearUnusedHistory(JobOfferConstants.JOB_APPLICATION_CONTENT_TYPE); 097 } 098}