001/*
002 *  Copyright 2012 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.ZoneType;
030import org.ametys.web.repository.site.Site;
031
032/**
033 * {@link Observer} for observing zone item addition in order to invalidate cache on front-office.
034 */
035public class InvalidateCacheOnZoneItemAdditionObserver extends AbstractSiteCacheObserver
036{
037    @Override
038    public boolean supports(Event event)
039    {
040        return event.getId().equals(ObservationConstants.EVENT_ZONEITEM_ADDED);
041    }
042    
043    @Override
044    protected Site _getSite(Event event)
045    {
046        return _getPage(event).getSite();
047    }
048    
049    private SitemapElement _getPage(Event event)
050    {
051        return (SitemapElement) event.getArguments().get(ObservationConstants.ARGS_SITEMAP_ELEMENT);
052    }
053    
054    @Override
055    protected void _internalObserve(Event event, Site site, Session liveSession) throws Exception
056    {
057        SitemapElement sitemapElement = _getPage(event);
058        if (sitemapElement instanceof Page page)
059        {
060            Map<String, Object> args = event.getArguments();
061            
062            ZoneType type = (ZoneType) args.get(ObservationConstants.ARGS_ZONE_TYPE);
063            
064            if (ZoneType.CONTENT.equals(type))
065            {
066                _cachePolicy.invalidateCacheOnPageModification(page);
067            }
068            else
069            {
070                if (page instanceof JCRAmetysObject)
071                {
072                    JCRAmetysObject jcrPage = (JCRAmetysObject) page;
073                    Node pageNode = jcrPage.getNode();
074                    
075                    if (liveSession.itemExists(pageNode.getPath()))
076                    {
077                        _cachePolicy.invalidateCacheOnPageMinorChange(page);
078                    }
079                }
080            }
081        }
082    }
083    
084}