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.odf.schedulable; 017 018import java.util.ArrayList; 019import java.util.List; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.quartz.JobExecutionContext; 024 025import org.ametys.cms.repository.Content; 026import org.ametys.cms.repository.ContentDAO; 027import org.ametys.cms.repository.ContentQueryHelper; 028import org.ametys.cms.repository.ContentTypeExpression; 029import org.ametys.core.schedule.progression.ContainerProgressionTracker; 030import org.ametys.odf.skill.workflow.SkillEditionFunction; 031import org.ametys.plugins.core.impl.schedule.AbstractStaticSchedulable; 032import org.ametys.plugins.repository.AmetysObjectResolver; 033import org.ametys.plugins.repository.query.expression.AndExpression; 034import org.ametys.plugins.repository.query.expression.Expression; 035import org.ametys.plugins.repository.query.expression.Expression.Operator; 036import org.ametys.plugins.repository.query.expression.MetadataExpression; 037import org.ametys.plugins.repository.query.expression.NotExpression; 038 039/** 040 * Scheduler to purge orphan skills. 041 */ 042public class SkillsPurgeSchedulable extends AbstractStaticSchedulable 043{ 044 private AmetysObjectResolver _resolver; 045 private ContentDAO _contentDAO; 046 047 @Override 048 public void service(ServiceManager manager) throws ServiceException 049 { 050 super.service(manager); 051 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 052 _contentDAO = (ContentDAO) manager.lookup(ContentDAO.ROLE); 053 } 054 055 @Override 056 public void execute(JobExecutionContext context, ContainerProgressionTracker progressionTracker) throws Exception 057 { 058 List<Content> contentIdsToDelete = new ArrayList<>(); 059 060 contentIdsToDelete.addAll(_getOrphansMacroSkills()); 061 contentIdsToDelete.addAll(_getOrphansMicroSkills()); 062 063 // Remove the skills 064 _contentDAO.forceDeleteContentsObj(contentIdsToDelete, null); 065 } 066 067 private List<Content> _getOrphansSkills(Expression expression) 068 { 069 String query = ContentQueryHelper.getContentXPathQuery(expression); 070 071 return _resolver.<Content>query(query).stream() 072 // Log 073 .peek(skill -> getLogger().info("The purge will delete the content {}.", skill.getId().toString())) 074 .toList(); 075 } 076 077 private List<Content> _getOrphansMacroSkills() 078 { 079 AndExpression expression = new AndExpression(); 080 081 // The orphans macroskills are the program ones that don't have a parent 082 expression.add(new ContentTypeExpression(Operator.EQ, SkillEditionFunction.DISCIPLINARY_MACRO_SKILL_TYPE)); 083 expression.add(new NotExpression(new MetadataExpression("parentProgram"))); 084 085 return _getOrphansSkills(expression); 086 } 087 088 private List<Content> _getOrphansMicroSkills() 089 { 090 AndExpression expression = new AndExpression(); 091 expression.add(new ContentTypeExpression(Operator.EQ, SkillEditionFunction.MICRO_SKILL_TYPE)); 092 expression.add(new NotExpression(new MetadataExpression("parentMacroSkill"))); 093 094 return _getOrphansSkills(expression); 095 } 096}