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