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.odfweb.observation.solr;
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.cocoon.components.ContextHelper;
026import org.apache.cocoon.environment.Request;
027
028import org.ametys.cms.content.indexing.solr.observation.ObserverHelper;
029import org.ametys.core.observation.Event;
030import org.ametys.core.observation.Observer;
031import org.ametys.plugins.odfweb.repository.OdfPageHandler;
032import org.ametys.web.ObservationConstants;
033import org.ametys.web.indexing.observation.AbstractSolrObserver;
034import org.ametys.web.repository.page.Page;
035import org.ametys.web.repository.site.Site;
036import org.ametys.web.repository.sitemap.Sitemap;
037
038/**
039 * {@link Observer} for observing site configuration modifications
040 * in order to synchronize Solr index.
041 */
042public abstract class AbstractSolrOnSiteConfModifiedObserver extends AbstractSolrObserver implements Contextualizable
043{
044    private OdfPageHandler _odfPageHandler;
045
046    private Context _context;
047    
048    public void contextualize(Context context) throws ContextException
049    {
050        _context = context;
051    }
052    
053    @Override
054    public void service(ServiceManager manager) throws ServiceException
055    {
056        super.service(manager);
057        _odfPageHandler = (OdfPageHandler) manager.lookup(OdfPageHandler.ROLE);
058    }
059
060    @Override
061    public boolean supports(Event event)
062    {
063        return event.getId().equals(ObservationConstants.EVENT_SITE_UPDATED);
064    }
065
066    @Override
067    public void observe(Event event, Map<String, Object> transientVars) throws Exception
068    {
069        if (ObserverHelper.isNotSuspendedObservationForIndexation())
070        {
071            // indicate that we are currently indexing, to allow further optimizations
072            Request request = ContextHelper.getRequest(_context);
073            request.setAttribute(AbstractSolrODFObserver.REQUEST_ATTRIBUTE_INDEXING, true);
074
075            Site site = (Site) event.getArguments().get(ObservationConstants.ARGS_SITE);
076            if (site != null)
077            {
078                _updateOdfRootIndex(site);
079            }
080        }
081    }
082
083    private void _updateOdfRootIndex(Site site) throws Exception
084    {
085        String siteName = site.getName();
086        for (Sitemap sitemap : site.getSitemaps())
087        {
088            for (Page odfRootPage : _odfPageHandler.getOdfRootPages(siteName, sitemap.getSitemapName()))
089            {
090                _updateIndex(odfRootPage);
091            }
092        }
093    }
094
095    /**
096     * Update the index from the observed event. 
097     * @param odfRootPage the odf root page
098     * @throws Exception if an error occurs.
099     */
100    protected abstract void _updateIndex(Page odfRootPage) throws Exception;
101}