001/*
002 *  Copyright 2014 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.tags.observers;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.context.Context;
021import org.apache.avalon.framework.context.ContextException;
022import org.apache.avalon.framework.context.Contextualizable;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.apache.cocoon.components.ContextHelper;
027import org.apache.cocoon.environment.Request;
028
029import org.ametys.core.observation.Event;
030import org.ametys.core.observation.Observer;
031import org.ametys.runtime.plugin.component.AbstractLogEnabled;
032import org.ametys.web.ObservationConstants;
033import org.ametys.web.WebConstants;
034import org.ametys.web.cache.CacheHelper;
035import org.ametys.web.cache.pageelement.PageElementCache;
036import org.ametys.web.indexing.SiteIndexer;
037import org.ametys.web.repository.site.Site;
038
039/**
040 * Re-index site when tag autoposting parameter have changed.
041 */
042public class TagAutopostingSiteParameterObserverPart2 extends AbstractLogEnabled implements Observer, Contextualizable, Serviceable
043{
044    /** The avalon context */
045    protected Context _context;
046    
047    /** The site indexer */
048    protected SiteIndexer _siteIndexer;
049    
050    /** The input data cache */
051    protected PageElementCache _inputDataCache;
052    
053    /** The zone item cache */
054    protected PageElementCache _zoneItemCache;
055
056    @Override
057    public void contextualize(Context context) throws ContextException
058    {
059        _context = context;
060    }
061    
062    @Override
063    public void service(ServiceManager manager) throws ServiceException
064    {
065        _siteIndexer = (SiteIndexer) manager.lookup(SiteIndexer.ROLE);
066        _inputDataCache = (PageElementCache) manager.lookup(PageElementCache.ROLE + "/inputData");
067        _zoneItemCache = (PageElementCache) manager.lookup(PageElementCache.ROLE + "/zoneItem");
068    }
069    
070    @Override
071    public boolean supports(Event event)
072    {
073        return event.getId().equals(ObservationConstants.EVENT_SITE_UPDATED);
074    }
075    
076    @Override
077    public int getPriority(Event event)
078    {
079        return MAX_PRIORITY + 3000;
080    }
081    
082    @Override
083    public void observe(Event event, Map<String, Object> transientVars) throws Exception
084    {
085        Site site = (Site) event.getArguments().get(ObservationConstants.ARGS_SITE);
086        Boolean newAutoposting = site.getValue("tags-autoposting");
087        
088        // Saving previous "tags-autoposting" parameter value as a request attribute.
089        Request request = ContextHelper.getRequest(_context);
090        Boolean oldAutoposting = (Boolean) request.getAttribute(TagAutopostingSiteParameterObserverPart1.SITE_TAGS_AUTOPOSTING_REQUEST_ATTR);
091        
092        if (oldAutoposting != null && !oldAutoposting.equals(newAutoposting))
093        {
094            _reindexAndClearCache(site);
095        }
096    }
097    
098    /**
099     * Reindex and clear the caches for a given site
100     * @param site the site
101     */
102    protected void _reindexAndClearCache(Site site)
103    {
104        if (getLogger().isInfoEnabled())
105        {
106            getLogger().info("Indexing site " + site.getName() + ".");
107        }
108        
109        long t0 = System.currentTimeMillis();
110        
111        try
112        {
113            // Reindex the site in the live workspace.
114            _siteIndexer.indexSite(site.getName(), WebConstants.LIVE_WORKSPACE);
115        }
116        catch (Exception e)
117        {
118            getLogger().error("Unable to index site '" + site.getName() + "'.");
119        }
120        
121        if (getLogger().isInfoEnabled())
122        {
123            getLogger().info("Site " + site.getName() + " indexed in " + (System.currentTimeMillis() - t0) + " ms");
124        }
125        
126        if (getLogger().isInfoEnabled())
127        {
128            getLogger().info("Clearing cache for site " + site.getName());
129        }
130        
131        try
132        {
133            CacheHelper.invalidateCache(site, getLogger());
134        }
135        catch (Exception e)
136        {
137            getLogger().error("Unable ot invalidate cache for site : " + site.getName());
138        }
139        
140        _inputDataCache.clear(null, site.getName());
141        _zoneItemCache.clear(null, site.getName());
142    }
143}