001/*
002 *  Copyright 2010 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.web.cache;
017
018import javax.jcr.Node;
019import javax.jcr.Session;
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.plugins.repository.AmetysObject;
029import org.ametys.plugins.repository.AmetysObjectResolver;
030import org.ametys.plugins.repository.jcr.JCRAmetysObject;
031import org.ametys.web.repository.site.Site;
032
033/**
034 * {@link Observer} for observing content validation or tagging in order to 
035 * invalidate cache on front-office.
036 */
037public class InvalidateCacheOnContentValidationOrTaggingObserver extends AbstractSiteCacheObserver
038{
039    /** Ametys object resolver. */
040    protected AmetysObjectResolver _ametysObjectResolver;
041    
042    @Override
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        super.service(manager);
046        _ametysObjectResolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
047    }
048
049    @Override
050    public boolean supports(Event event)
051    {
052        return event.getId().equals(ObservationConstants.EVENT_CONTENT_VALIDATED)
053            || event.getId().equals(ObservationConstants.EVENT_CONTENT_ADDED)
054            || event.getId().equals(org.ametys.cms.ObservationConstants.EVENT_CONTENT_TAGGED);
055    }
056    
057    @Override
058    protected Site _getSite(Event event)
059    {
060        Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
061        AmetysObject ao = content.getParent().getParent();
062        return ao instanceof Site ? (Site) ao : null;
063    }
064    
065    @Override
066    protected void _internalObserve(Event event, Site site, Session liveSession) throws Exception
067    {
068        if (getLogger().isInfoEnabled())
069        {
070            getLogger().info("Content modified: " + event + ", invalidating cache");
071        }
072        
073        Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
074        
075        if (content instanceof JCRAmetysObject)
076        {
077            JCRAmetysObject jcrContent = (JCRAmetysObject) content;
078            Node node = jcrContent.getNode();
079            
080            // test if in live
081            if (liveSession.itemExists(node.getPath()))
082            {
083                _cachePolicy.invalidateCacheOnContentModification(site, content);
084            }
085        }
086    }
087}