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.PropertyIterator;
020import javax.jcr.Session;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.cms.ObservationConstants;
026import org.ametys.cms.repository.Content;
027import org.ametys.core.observation.Event;
028import org.ametys.core.observation.Observer;
029import org.ametys.plugins.repository.AmetysObject;
030import org.ametys.plugins.repository.AmetysObjectResolver;
031import org.ametys.plugins.repository.jcr.JCRAmetysObject;
032import org.ametys.web.repository.page.Page;
033import org.ametys.web.repository.page.jcr.DefaultZone;
034import org.ametys.web.repository.site.Site;
035
036/**
037 * {@link Observer} for observing content deletion in order to 
038 * invalidate cache on front-office.
039 */
040public class InvalidateCacheOnContentCommentedObserver extends AbstractSiteCacheObserver
041{
042    private AmetysObjectResolver _ametysObjectResolver;
043    
044    @Override
045    public boolean supports(Event event)
046    {
047        return event.getId().equals(ObservationConstants.EVENT_CONTENT_COMMENT_VALIDATED)
048            || event.getId().equals(ObservationConstants.EVENT_CONTENT_COMMENT_UNVALIDATED);
049    }
050    
051    @Override
052    public void service(ServiceManager manager) throws ServiceException
053    {
054        super.service(manager);
055        _ametysObjectResolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
056    }
057    
058    @Override
059    protected Site _getSite(Event event)
060    {
061        Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
062        AmetysObject ao = content.getParent().getParent();
063        return ao instanceof Site ? (Site) ao : null;
064
065    }
066
067    @Override
068    protected void _internalObserve(Event event, Site site, Session liveSession) throws Exception
069    {
070        if (getLogger().isInfoEnabled())
071        {
072            getLogger().info("Comment validated or unvalidated: " + event + ", invalidating cache");
073        }
074        
075        Content contentTarget = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
076        
077        if (contentTarget instanceof JCRAmetysObject)
078        {
079            JCRAmetysObject jcrContent = (JCRAmetysObject) contentTarget;
080            Node node = jcrContent.getNode();
081            
082            // test if in live
083            if (liveSession.itemExists(node.getPath()))
084            {
085                // publish pages if necessary
086                PropertyIterator itReferences = jcrContent.getNode().getReferences();
087                
088                while (itReferences.hasNext())
089                {
090                    Node refererNode = itReferences.nextProperty().getParent();
091                    if (refererNode.getPrimaryNodeType().getName().equals(DefaultZone.ZONEITEM_NODE_NAME))
092                    {
093                        Node pageNode = refererNode.getParent().getParent().getParent().getParent();
094                        Page page = _ametysObjectResolver.resolve(pageNode, false);
095
096                        _cachePolicy.invalidateCacheOnContentCommented(page, contentTarget);
097                    }
098                }
099            }
100        }
101    }
102}