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 java.util.Map;
019
020import javax.jcr.Node;
021import javax.jcr.Session;
022
023import org.ametys.core.observation.Event;
024import org.ametys.core.observation.Observer;
025import org.ametys.plugins.repository.jcr.JCRAmetysObject;
026import org.ametys.web.ObservationConstants;
027import org.ametys.web.repository.page.Page;
028import org.ametys.web.repository.page.ZoneItem;
029import org.ametys.web.repository.site.Site;
030
031/**
032 * {@link Observer} for observing page creation or modification in order to
033 * invalidate cache on front-office.
034 */
035public class InvalidateCacheOnPageModificationObserver extends AbstractSiteCacheObserver
036{
037    @Override
038    public boolean supports(Event event)
039    {
040        return event.getId().equals(ObservationConstants.EVENT_PAGE_ADDED)
041            || event.getId().equals(ObservationConstants.EVENT_PAGE_CHANGED)
042            || event.getId().equals(ObservationConstants.EVENT_PAGE_UPDATED)
043            || event.getId().equals(ObservationConstants.EVENT_PAGE_RENAMED)
044            || event.getId().equals(ObservationConstants.EVENT_VIEW_PARAMETERS_MODIFIED);
045    }
046    
047    @Override
048    protected Site _getSite(Event event)
049    {
050        Page page = _getPage(event);
051        return page != null ? page.getSite() : null;
052    }
053    
054    private Page _getPage (Event event)
055    {
056        Map<String, Object> args = event.getArguments();
057        if (args.containsKey(ObservationConstants.ARGS_ZONE_ITEM))
058        {
059            ZoneItem zoneItem = (ZoneItem) args.get(ObservationConstants.ARGS_ZONE_ITEM);
060            if (zoneItem.getZone().getSitemapElement() instanceof Page page)
061            {
062                return page;
063            }
064            return null;
065        }
066        else
067        {
068            return (Page) args.get(ObservationConstants.ARGS_PAGE);
069        }
070    }
071    
072    @Override
073    protected void _internalObserve(Event event, Site site, Session liveSession) throws Exception
074    {
075        Page page = _getPage (event);
076        
077        if (page instanceof JCRAmetysObject)
078        {
079            JCRAmetysObject jcrPage = (JCRAmetysObject) page;
080            Node pageNode = jcrPage.getNode();
081            
082            if (liveSession.itemExists(pageNode.getPath()) || event.getId().equals(ObservationConstants.EVENT_PAGE_CHANGED))
083            {
084                if (getLogger().isInfoEnabled())
085                {
086                    getLogger().info("Page modification: " + event + ", invalidating cache");
087                }
088                
089                _cachePolicy.invalidateCacheOnPageModification(page);
090            }
091        }
092    }
093}