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.BooleanExpression;
035import org.ametys.plugins.repository.query.expression.Expression;
036import org.ametys.plugins.repository.query.expression.Expression.Operator;
037import org.ametys.plugins.repository.query.expression.MetadataExpression;
038import org.ametys.plugins.repository.query.expression.NotExpression;
039
040/**
041 * Scheduler to purge orphan skills.
042 */
043public class SkillsPurgeSchedulable extends AbstractStaticSchedulable
044{
045    private AmetysObjectResolver _resolver;
046    private ContentDAO _contentDAO;
047
048    @Override
049    public void service(ServiceManager manager) throws ServiceException
050    {
051        super.service(manager);
052        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
053        _contentDAO = (ContentDAO) manager.lookup(ContentDAO.ROLE);
054    }
055    
056    @Override
057    public void execute(JobExecutionContext context, ContainerProgressionTracker progressionTracker) throws Exception
058    {
059        List<Content> contentIdsToDelete = new ArrayList<>();
060                
061        contentIdsToDelete.addAll(_getOrphansMacroSkills());
062        contentIdsToDelete.addAll(_getOrphansMicroSkills());
063        
064        // Remove the skills
065        _contentDAO.forceDeleteContentsObj(contentIdsToDelete, null);
066    }
067    
068    private List<Content> _getOrphansSkills(Expression expression)
069    {
070        String query = ContentQueryHelper.getContentXPathQuery(expression);
071        
072        return _resolver.<Content>query(query).stream()
073                // Log
074                .peek(skill -> getLogger().info("The purge will delete the content {}.", skill.getId().toString()))
075                .toList();
076    }
077    
078    private List<Content> _getOrphansMacroSkills()
079    {
080        AndExpression expression = new AndExpression();
081        expression.add(new ContentTypeExpression(Operator.EQ, SkillEditionFunction.MACRO_SKILL_TYPE));
082        expression.add(new NotExpression(new MetadataExpression("parentProgram")));
083        
084        // The orphans macroskills are the one that don't have a parent and that are also not transversal
085        expression.add(new BooleanExpression("transversal", false));
086        
087        return _getOrphansSkills(expression);
088    }
089    
090    private List<Content> _getOrphansMicroSkills()
091    {
092        AndExpression expression = new AndExpression();
093        expression.add(new ContentTypeExpression(Operator.EQ, SkillEditionFunction.MICRO_SKILL_TYPE));
094        expression.add(new NotExpression(new MetadataExpression("parentMacroSkill")));
095        
096        return _getOrphansSkills(expression);
097    }
098}