001/*
002 *  Copyright 2017 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.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023
024import org.ametys.cms.ObservationConstants;
025import org.ametys.cms.repository.Content;
026import org.ametys.core.observation.Event;
027import org.ametys.core.observation.Observer;
028import org.ametys.odf.course.Course;
029import org.ametys.odf.program.Program;
030import org.ametys.odf.program.SubProgram;
031import org.ametys.web.WebConstants;
032import org.ametys.web.cache.CacheInvalidationPolicy;
033import org.ametys.web.repository.page.Page;
034import org.ametys.web.repository.site.Site;
035
036/**
037 * {@link Observer} for observing content validation or tagging in order to 
038 * invalidate cache on front-office.
039 */
040public class InvalidateCacheOnContentValidationOrUnpublishingObserver extends AbstractODFObserver
041{
042    /** Cache invalidation policy */
043    protected CacheInvalidationPolicy _cachePolicy;
044    
045    @Override
046    public void service(ServiceManager serviceManager) throws ServiceException
047    {
048        super.service(serviceManager);
049        _cachePolicy = (CacheInvalidationPolicy) serviceManager.lookup(CacheInvalidationPolicy.class.getName());
050    }
051    
052    
053    @Override
054    public boolean supports(Event event)
055    {
056        return event.getId().equals(ObservationConstants.EVENT_CONTENT_VALIDATED) 
057            || event.getId().equals(org.ametys.web.ObservationConstants.EVENT_CONTENT_UNPUBLISHED);
058    }
059    
060    @Override
061    public int getPriority(Event event)
062    {
063        // Will be processed after Solr update
064        return MAX_PRIORITY + 4000;
065    }
066    
067    @Override
068    protected String _workspaceToUse()
069    {
070        return WebConstants.LIVE_WORKSPACE;
071    }
072    
073    @Override
074    protected void _internalObserve(Event event, Map<String, Object> transientVars, Page odfRootPage, Set<Program> rootPrograms, SubProgram subProgram, Course course)
075    {
076        Site site = odfRootPage.getSite();
077        
078        for (Program program : rootPrograms)
079        {
080            _invalidate(site, program);
081        }
082        
083        if (subProgram != null)
084        {
085            _invalidate(site, subProgram);
086        }
087        
088        if (course != null)
089        {
090            _invalidate(site, course);
091        }
092    }
093    
094    private void _invalidate(Site site, Content content)
095    {
096        try
097        {
098            _cachePolicy.invalidateCacheOnContentModification(site, content);
099        }
100        catch (Exception e)
101        {
102            getLogger().error("Unable to invalidate cache for progam " + content, e);
103        }
104    }
105}