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.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.cms.content.indexing.solr.observation.ObserverHelper;
024import org.ametys.core.observation.Event;
025import org.ametys.core.observation.Observer;
026import org.ametys.plugins.odfweb.repository.OdfPageHandler;
027import org.ametys.web.ObservationConstants;
028import org.ametys.web.indexing.observation.AbstractSolrObserver;
029import org.ametys.web.repository.page.Page;
030import org.ametys.web.repository.site.Site;
031import org.ametys.web.repository.sitemap.Sitemap;
032
033/**
034 * {@link Observer} for observing site configuration modifications
035 * in order to synchronize Solr index.
036 */
037public abstract class AbstractSolrOnSiteConfModifiedObserver extends AbstractSolrObserver
038{
039    private OdfPageHandler _odfPageHandler;
040
041    @Override
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        super.service(manager);
045        _odfPageHandler = (OdfPageHandler) manager.lookup(OdfPageHandler.ROLE);
046    }
047
048    @Override
049    public boolean supports(Event event)
050    {
051        return event.getId().equals(ObservationConstants.EVENT_SITE_UPDATED);
052    }
053
054    @Override
055    public void observe(Event event, Map<String, Object> transientVars) throws Exception
056    {
057        if (ObserverHelper.isNotSuspendedObservationForIndexation())
058        {
059            Site site = (Site) event.getArguments().get(ObservationConstants.ARGS_SITE);
060            if (site != null)
061            {
062                _updateOdfRootIndex(site);
063            }
064        }
065    }
066
067    private void _updateOdfRootIndex(Site site) throws Exception
068    {
069        String siteName = site.getName();
070        for (Sitemap sitemap : site.getSitemaps())
071        {
072            for (Page odfRootPage : _odfPageHandler.getOdfRootPages(siteName, sitemap.getSitemapName()))
073            {
074                _updateIndex(odfRootPage);
075            }
076        }
077    }
078
079    /**
080     * Update the index from the observed event. 
081     * @param odfRootPage the odf root page
082     * @throws Exception if an error occurs.
083     */
084    protected abstract void _updateIndex(Page odfRootPage) throws Exception;
085}