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;
019import java.util.Set;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.components.ContextHelper;
024import org.apache.cocoon.environment.Request;
025import org.apache.commons.lang.StringUtils;
026
027import org.ametys.cms.content.indexing.solr.observation.ObserverHelper;
028import org.ametys.cms.indexing.IndexingObserver;
029import org.ametys.core.observation.Event;
030import org.ametys.core.observation.Observer;
031import org.ametys.odf.course.Course;
032import org.ametys.odf.program.Program;
033import org.ametys.odf.program.SubProgram;
034import org.ametys.plugins.odfweb.observation.AbstractODFObserver;
035import org.ametys.plugins.odfweb.repository.OdfPageResolver;
036import org.ametys.web.indexing.solr.SolrPageIndexer;
037import org.ametys.web.repository.page.Page;
038import org.ametys.web.repository.sitemap.Sitemap;
039
040/**
041 * Abstract {@link Observer} for synchronizing the Solr indexes.
042 */
043public abstract class AbstractSolrODFObserver extends AbstractODFObserver implements IndexingObserver
044{
045    /** Attribute request indicating an ongoing indexation */
046    public static final String REQUEST_ATTRIBUTE_INDEXING = "odfweb.solr.indexing";
047    
048    /** The page indexer. */
049    protected SolrPageIndexer _solrPageIndexer;
050    /** ODF page resolver */
051    protected OdfPageResolver _odfPageResolver;
052    
053    @Override
054    public void service(ServiceManager manager) throws ServiceException
055    {
056        super.service(manager);
057        _solrPageIndexer = (SolrPageIndexer) manager.lookup(SolrPageIndexer.ROLE);
058        _odfPageResolver = (OdfPageResolver) manager.lookup(OdfPageResolver.ROLE);
059    }
060    
061    @Override
062    public int getPriority()
063    {
064        // Will be processed after live synchronization observers
065        return MAX_PRIORITY + 3000;
066    }
067    
068    @Override
069    protected final void _internalObserve(Event event, Map<String, Object> transientVars, Page odfRootPage, Set<Program> programs, SubProgram subProgram, Course course) throws Exception
070    {
071        if (ObserverHelper.isNotSuspendedObservationForIndexation())
072        {
073            // indicate that we are currently indexing, to allow further optimizations
074            Request request = ContextHelper.getRequest(_context);
075            request.setAttribute(REQUEST_ATTRIBUTE_INDEXING, true);
076            
077            Sitemap sitemap = odfRootPage.getSitemap();
078            String sitemapName = sitemap.getSitemapName();
079            
080            for (Program program : programs)
081            {
082                if (StringUtils.equals(sitemapName, program.getLanguage()))
083                {
084                    _updateIndex(event, transientVars, odfRootPage, program, subProgram, course);
085                }
086            }
087        }
088    }
089    
090    /**
091     * Update the index from the observed event.
092     * @param event the event.
093     * @param transientVars transientVars passed from one Observer to another when processing a single Event.
094     * This may allow optimizations between observers.
095     * @param odfRootPage the odf root page
096     * @param program the target program
097     * @param subProgram The subprogram. Can be null
098     * @param course the course. Can be null.
099     * @throws Exception if an error occurs.
100     */
101    protected abstract void _updateIndex(Event event, Map<String, Object> transientVars, Page odfRootPage, Program program, SubProgram subProgram, Course course) throws Exception;
102    
103}