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.Collection;
019
020import org.apache.avalon.framework.context.Context;
021import org.apache.avalon.framework.context.ContextException;
022import org.apache.avalon.framework.context.Contextualizable;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.cocoon.components.ContextHelper;
026import org.apache.cocoon.environment.Request;
027
028import org.ametys.cms.repository.Content;
029import org.ametys.core.observation.Observer;
030import org.ametys.odf.ProgramItem;
031import org.ametys.plugins.odfweb.repository.OdfPageResolver;
032import org.ametys.plugins.repository.RepositoryConstants;
033import org.ametys.web.WebConstants;
034import org.ametys.web.indexing.observation.SolrPageContentModifiedObserver;
035import org.ametys.web.repository.page.Page;
036
037/**
038 * This {@link Observer} observes ODF content modification
039 * in order to synchronize the solr index of referencing pages
040 */
041public class SolrOdfContentModifiedObserver extends SolrPageContentModifiedObserver implements Contextualizable
042{
043    private OdfPageResolver _odfPageResolver;
044    
045    private Context _context;
046    
047    public void contextualize(Context context) throws ContextException
048    {
049        _context = context;
050    }
051    
052    @Override
053    public void service(ServiceManager manager) throws ServiceException
054    {
055        super.service(manager);
056        _odfPageResolver = (OdfPageResolver) manager.lookup(OdfPageResolver.ROLE);
057    }
058    
059    @Override
060    protected void _updateIndexReferencingPages (String contentId, boolean recursively) throws Exception
061    {
062        // indicate that we are currently indexing, to allow further optimizations
063        Request request = ContextHelper.getRequest(_context);
064        request.setAttribute(AbstractSolrODFObserver.REQUEST_ATTRIBUTE_INDEXING, true);
065
066        Content content = _resolver.resolveById(contentId);
067        
068        if (content instanceof ProgramItem)
069        {
070            Collection<Page> referencingPages = _odfPageResolver.getReferencingPages((ProgramItem) content);
071            
072            for (Page page : referencingPages)
073            {
074                // Reindex page in default workspace (its attachments are still indexed, so no need to index them)
075                _solrPageIndexer.reindexPage(page.getId(), RepositoryConstants.DEFAULT_WORKSPACE, recursively, false);
076                // Reindex page in live workspace if exists (if page was not in live before, its attachments were not either, so index them)
077                _solrPageIndexer.reindexPage(page.getId(), WebConstants.LIVE_WORKSPACE, recursively, true);
078            }
079        }
080    }
081}