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.odf.schedulable; 017 018import java.util.List; 019import java.util.stream.Collectors; 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.core.schedule.progression.ContainerProgressionTracker; 028import org.ametys.odf.course.Course; 029import org.ametys.odf.course.CourseFactory; 030import org.ametys.odf.coursepart.CoursePart; 031import org.ametys.odf.coursepart.CoursePartFactory; 032import org.ametys.plugins.core.impl.schedule.AbstractStaticSchedulable; 033import org.ametys.plugins.repository.AmetysObjectResolver; 034import org.ametys.plugins.repository.query.QueryHelper; 035import org.ametys.plugins.repository.query.expression.Expression.Operator; 036import org.ametys.plugins.repository.query.expression.StringExpression; 037 038/** 039 * Scheduler to purge orphan course parts. 040 */ 041public class CoursePartPurgeSchedulable extends AbstractStaticSchedulable 042{ 043 private AmetysObjectResolver _resolver; 044 private ContentDAO _contentDAO; 045 046 @Override 047 public void service(ServiceManager manager) throws ServiceException 048 { 049 super.service(manager); 050 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 051 _contentDAO = (ContentDAO) manager.lookup(ContentDAO.ROLE); 052 } 053 054 @Override 055 public void execute(JobExecutionContext context, ContainerProgressionTracker progressionTracker) throws Exception 056 { 057 // Getting all orphan course parts (linked to any course) 058 String xPathQuery = QueryHelper.getXPathQuery(null, CoursePartFactory.COURSE_PART_NODETYPE, null); 059 // Search course parts only 060 List<String> contentIdsToDelete = _resolver.<CoursePart>query(xPathQuery) 061 .stream() 062 // Verify if the content has no parent courses 063 .filter(coursePart -> coursePart.getCourses().isEmpty()) 064 // Twice... 065 .filter(coursePart -> { 066 String query = QueryHelper.getXPathQuery(null, CourseFactory.COURSE_NODETYPE, new StringExpression(Course.CHILD_COURSE_PARTS, Operator.EQ, coursePart.getId())); 067 return !_resolver.query(query).iterator().hasNext(); 068 }) 069 // Log 070 .peek(coursePart -> getLogger().info("The purge will delete the content {}.", coursePart.toString())) 071 // Get the identifier 072 .map(Content::getId) 073 // Collect 074 .collect(Collectors.toList()); 075 076 // Remove the course parts 077 _contentDAO.deleteContents(contentIdsToDelete, true); 078 } 079}