001/*
002 *  Copyright 2017 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.plugins.externaldata.cache;
017
018import javax.jcr.RepositoryException;
019import javax.jcr.Session;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023
024import org.ametys.core.observation.Event;
025import org.ametys.plugins.repository.AmetysObjectIterable;
026import org.ametys.web.cache.CacheHelper;
027import org.ametys.web.cache.pageelement.PageElementCache;
028import org.ametys.web.repository.site.Site;
029import org.ametys.web.repository.site.SiteManager;
030import org.ametys.web.synchronization.AbstractSynchronizeObserver;
031
032/**
033 * Invalidate the site cache and page element cache when 
034 */
035public class InvalidateCacheOnQueryChangesObserver extends AbstractSynchronizeObserver
036{
037    private PageElementCache _inputDataCache;
038    private PageElementCache _zoneItemCache;
039    private SiteManager _siteManager;
040    
041    @Override
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        super.service(manager);
045        
046        _inputDataCache = (PageElementCache) manager.lookup(PageElementCache.ROLE + "/inputData");
047        _zoneItemCache = (PageElementCache) manager.lookup(PageElementCache.ROLE + "/zoneItem");
048        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
049    }
050
051    public boolean supports(Event event)
052    {
053        return event.getId().equals(ObservationConstants.EVENT_QUERY_UPDATED) 
054            || event.getId().equals(ObservationConstants.EVENT_QUERY_DELETED);
055    }
056
057    @Override
058    protected void _internalObserve(Event event, Session liveSession) throws RepositoryException
059    {
060        if (getLogger().isInfoEnabled())
061        {
062            getLogger().info("Database changes: " + event + ", invalidating cache");
063        }
064        
065        // clear the whole FO cache
066        AmetysObjectIterable<Site> sites = _siteManager.getSites();
067        for (Site site : sites)
068        {
069            try
070            {
071                CacheHelper.invalidateCache(site, getLogger());
072            }
073            catch (Exception e)
074            {
075                getLogger().error("Unable to clear cache of site " + site.getName(), e);
076            }
077        }
078        
079        // clear the page element cache
080        _inputDataCache.clear();
081        _zoneItemCache.clear();
082    }
083
084}