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