001/*
002 *  Copyright 2022 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.odfweb.observation;
017
018import java.util.Map;
019import java.util.Set;
020
021import org.ametys.cms.repository.Content;
022import org.ametys.core.observation.Event;
023import org.ametys.core.observation.Observer;
024import org.ametys.odf.ProgramItem;
025import org.ametys.odf.course.Course;
026import org.ametys.odf.program.Program;
027import org.ametys.odf.program.SubProgram;
028import org.ametys.web.repository.page.Page;
029import org.ametys.web.repository.site.Site;
030
031/**
032 * {@link Observer} for observing skills exclusion update in order to invalidate cache on front-office.
033 */
034public class InvalidateCacheOnSkillsExclusionChangedObserver extends InvalidateCacheOnContentValidationOrUnpublishingObserver
035{
036    @Override
037    public boolean supports(Event event)
038    {
039        return event.getId().equals(org.ametys.odf.observation.OdfObservationConstants.ODF_CONTENT_SKILLS_EXCLUSION_CHANGED);
040    }
041    
042    @Override
043    protected void _internalObserve(Event event, Map<String, Object> transientVars, Page odfRootPage, Set<Program> rootPrograms, SubProgram subProgram, Course course) throws Exception
044    {
045        Content content = _getContentArgument(event);
046        
047        if (content instanceof ProgramItem)
048        {
049            Site site = odfRootPage.getSite();
050            _invalidate(site, content);
051            
052            // IMPORTANT NOTE
053            // Changing visibility can change rendering of child subprogram/course and parent program/subprogram
054            // In theory we should compute and invalidate the cache of parent and child pages
055            // But given the current implementation of invalidate cache policy, it is unecessary to call invalidate of children and parents
056            // Useless computings are avoided here, as long as the cache policy cannot be more precise
057        }
058        
059    }
060}