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