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.SitemapElement;
029import org.ametys.web.repository.page.ZoneItem;
030import org.ametys.web.repository.site.Site;
031
032/**
033 * {@link Observer} for observing page content changes (which do not impact other pages) in order to invalidate cache on front-office.
034 */
035public class InvalidateCacheOnPageMinorChangeObserver extends AbstractSiteCacheObserver
036{
037    
038    @Override
039    public boolean supports(Event event)
040    {
041        return _getSitemapElement(event) instanceof Page
042            && (event.getId().equals(ObservationConstants.EVENT_ZONEITEM_MOVED)
043            || event.getId().equals(ObservationConstants.EVENT_SERVICE_MODIFIED));
044    }
045    
046    @Override
047    protected Site _getSite(Event event)
048    {
049        return _getSitemapElement(event).getSite();
050    }
051    
052    private SitemapElement _getSitemapElement (Event event)
053    {
054        Map<String, Object> args = event.getArguments();
055        if (args.containsKey(ObservationConstants.ARGS_ZONE_ITEM))
056        {
057            ZoneItem zoneItem = (ZoneItem) args.get(ObservationConstants.ARGS_ZONE_ITEM);
058            SitemapElement sitemapElement = zoneItem.getZone().getSitemapElement();
059            return sitemapElement;
060        }
061        else
062        {
063            return (SitemapElement) args.get(ObservationConstants.ARGS_SITEMAP_ELEMENT);
064        }
065    }
066    
067    @Override
068    protected void _internalObserve(Event event, Site site, Session liveSession) throws Exception
069    {
070        SitemapElement sitemapElement = _getSitemapElement (event);
071        
072        if (sitemapElement instanceof JCRAmetysObject
073            && sitemapElement instanceof Page page)
074        {
075            Node pageNode = ((JCRAmetysObject) page).getNode();
076            if (liveSession.itemExists(pageNode.getPath()))
077            {
078                // If the page is in the live workspace, invalidate the cache.
079                _cachePolicy.invalidateCacheOnPageMinorChange(page);
080            }
081        }
082    }
083}