001/*
002 *  Copyright 2020 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.ugc.observation;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020
021import org.ametys.cms.repository.Content;
022import org.ametys.core.observation.Event;
023import org.ametys.core.observation.Observer;
024import org.ametys.web.cache.CacheInvalidationPolicy;
025import org.ametys.web.repository.page.Page;
026import org.ametys.web.repository.site.Site;
027
028/**
029 * {@link Observer} for observing content validation in order to 
030 * invalidate cache on front-office.
031 */
032public class InvalidateCacheOnContentValidationObserver extends AbstractContentObserver
033{
034    /** Cache invalidation policy */
035    protected CacheInvalidationPolicy _cachePolicy;
036    
037    @Override
038    public void service(ServiceManager serviceManager) throws ServiceException
039    {
040        super.service(serviceManager);
041        _cachePolicy = (CacheInvalidationPolicy) serviceManager.lookup(CacheInvalidationPolicy.class.getName());
042    }
043    
044    
045    @Override
046    public boolean supports(Event event)
047    {
048        return event.getId().equals(org.ametys.cms.ObservationConstants.EVENT_CONTENT_VALIDATED);
049    }
050    
051    @Override
052    public int getPriority(Event event)
053    {
054        // Will be processed after indexation
055        return MAX_PRIORITY + 4000;
056    }
057    
058    @Override
059    protected void _internalObserve(Event event, Page rootPage, Content content)
060    {
061        Site site = rootPage.getSite();
062        
063        try
064        {
065            _cachePolicy.invalidateCacheOnContentModification(site, content);
066        }
067        catch (Exception e)
068        {
069            getLogger().error("Unable to invalidate cache for user content " + content, e);
070        }
071    }
072}