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