001/*
002 *  Copyright 2019 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.inputdata;
017
018import java.util.Arrays;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025
026import org.ametys.core.observation.Event;
027import org.ametys.web.ObservationConstants;
028import org.ametys.web.WebHelper;
029import org.ametys.web.cache.AbstractCacheObserver;
030import org.ametys.web.repository.site.Site;
031import org.ametys.web.repository.site.SiteManager;
032
033/**
034 * Invalidates the {@link SitemapSaxer}'s cache used by the {@link SitemapInputData}.
035 */
036public class InvalidateSitemapSaxerCacheObserver extends AbstractCacheObserver implements Serviceable
037{
038    private SiteManager _siteManager;
039    private SitemapSaxer _sitemapSaxer;
040    private List<String> _defaultWorkspaceEvents = Arrays.asList(ObservationConstants.EVENT_PAGE_ADDED,
041                                                                ObservationConstants.EVENT_PAGE_CHANGED,
042                                                                ObservationConstants.EVENT_PAGE_DELETED,
043                                                                ObservationConstants.EVENT_PAGE_MOVED,
044                                                                ObservationConstants.EVENT_PAGE_RENAMED, 
045                                                                ObservationConstants.EVENT_PAGE_UPDATED,
046                                                                org.ametys.core.ObservationConstants.EVENT_ACL_UPDATED);
047    private List<String> _liveWorkspaceEvents = Arrays.asList(org.ametys.cms.ObservationConstants.EVENT_CONTENT_ADDED,
048                                                                org.ametys.cms.ObservationConstants.EVENT_CONTENT_VALIDATED,
049                                                                org.ametys.cms.ObservationConstants.EVENT_CONTENT_DELETED,
050                                                                ObservationConstants.EVENT_CONTENT_UNPUBLISHED,
051                                                                ObservationConstants.EVENT_ZONEITEM_ADDED,
052                                                                ObservationConstants.EVENT_ZONEITEM_DELETED,
053                                                                ObservationConstants.EVENT_ZONEITEM_MODIFIED);
054    
055    @Override
056    public void service(ServiceManager manager) throws ServiceException
057    {
058        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
059        _sitemapSaxer = (SitemapSaxer) manager.lookup(SitemapSaxer.ROLE);
060    }
061
062    public boolean supports(Event event)
063    {
064        return _defaultWorkspaceEvents.contains(event.getId()) 
065                || _liveWorkspaceEvents.contains(event.getId());
066    }
067
068    public void observe(Event event, Map<String, Object> transientVars) throws Exception
069    {
070        boolean defaultWorkspace = _defaultWorkspaceEvents.contains(event.getId());
071        boolean liveWorkspace = defaultWorkspace || _liveWorkspaceEvents.contains(event.getId());
072                
073        Site site = WebHelper.findSite(event);
074
075        if (site != null)
076        {
077            _clearCache(site, defaultWorkspace, liveWorkspace);
078        }
079        else
080        {
081            for (Site s : _siteManager.getSites())
082            {
083                _clearCache(s, defaultWorkspace, liveWorkspace);
084            }
085        }
086    }
087    
088    private void _clearCache(Site site, boolean defaultWorkspace, boolean liveWorkspace)
089    {
090        if (defaultWorkspace)
091        {
092            _clearCache("default", site);
093        }
094        
095        if (liveWorkspace)
096        {
097            _clearCache("live", site);
098        }
099    }
100    
101    private void _clearCache(String workspace, Site site)
102    {
103        getLogger().info("Clearing SitemapSaxer cache for site {} and workspace {}.", site.getName(), workspace);
104        _sitemapSaxer.clearCache(site.getName(), workspace);
105    }
106}