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