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 org.apache.avalon.framework.logger.AbstractLogEnabled;
019import org.apache.avalon.framework.service.ServiceException;
020import org.apache.avalon.framework.service.ServiceManager;
021import org.apache.avalon.framework.service.Serviceable;
022
023import java.util.Map;
024
025import org.ametys.cms.ObservationConstants;
026import org.ametys.core.observation.Event;
027import org.ametys.core.observation.Observer;
028import org.ametys.odf.orgunit.OrgUnit;
029import org.ametys.plugins.odfweb.repository.OdfPageHandler;
030import org.ametys.web.cache.CacheInvalidationPolicy;
031import org.ametys.web.repository.site.Site;
032import org.ametys.web.repository.site.SiteManager;
033
034/**
035 * {@link Observer} for observing OrgUnit validation, unpublishing, deletion or moving 
036 * (with event {@link ObservationConstants#EVENT_CONTENT_MODIFIED}, which will lead to sometimes invalidating cache whereas it is not necessary)
037 *  in order to invalidate cache on front-office.
038 */
039public class InvalidateCacheOnOrgunitChangedObserver extends AbstractLogEnabled implements Observer, Serviceable
040{
041    /** The site manager. */
042    protected SiteManager _siteManager;
043    
044    /** The ODF page handler. */
045    protected OdfPageHandler _odfPageHandler;
046    
047    /** Cache invalidation policy */
048    protected CacheInvalidationPolicy _cachePolicy;
049    
050    @Override
051    public void service(ServiceManager serviceManager) throws ServiceException
052    {
053        _siteManager = (SiteManager) serviceManager.lookup(SiteManager.ROLE);
054        _odfPageHandler = (OdfPageHandler) serviceManager.lookup(OdfPageHandler.ROLE);
055        _cachePolicy = (CacheInvalidationPolicy) serviceManager.lookup(CacheInvalidationPolicy.ROLE);
056    }
057
058    @Override
059    public boolean supports(Event event)
060    {
061        return event.getArguments().get(ObservationConstants.ARGS_CONTENT) instanceof OrgUnit
062                && (event.getId().equals(ObservationConstants.EVENT_CONTENT_VALIDATED)
063                || event.getId().equals(ObservationConstants.EVENT_CONTENT_DELETING)
064                || event.getId().equals(org.ametys.web.ObservationConstants.EVENT_CONTENT_UNPUBLISHED)
065                || event.getId().equals(ObservationConstants.EVENT_CONTENT_MODIFIED /* for handling moved orgUnit */));
066    }
067
068    @Override
069    public int getPriority(Event event)
070    {
071        return MAX_PRIORITY + 4000;
072    }
073
074    @Override
075    public void observe(Event event, Map<String, Object> transientVars) throws Exception
076    {
077        OrgUnit orgUnit = (OrgUnit) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
078        
079        for (Site site : _siteManager.getSites())
080        {
081            if (_odfPageHandler.hasOdfRootPage(site))
082            {
083                try
084                {
085                    _cachePolicy.invalidateCacheOnContentModification(site, orgUnit);
086                }
087                catch (Exception e)
088                {
089                    getLogger().error("Unable to invalidate cache for OrgUnit " + orgUnit, e);
090                }
091            }
092        }
093    }
094}