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.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.cms.repository.Content;
024import org.ametys.core.observation.Observer;
025import org.ametys.odf.ProgramItem;
026import org.ametys.plugins.odfweb.repository.OdfPageResolver;
027import org.ametys.plugins.repository.RepositoryConstants;
028import org.ametys.web.WebConstants;
029import org.ametys.web.indexing.observation.SolrPageContentModifiedObserver;
030import org.ametys.web.repository.page.Page;
031
032/**
033 * This {@link Observer} observes ODF content modification
034 * in order to synchronize the solr index of referencing pages
035 */
036public class SolrOdfContentModifiedObserver extends SolrPageContentModifiedObserver
037{
038    private OdfPageResolver _odfPageResolver;
039
040    @Override
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        super.service(manager);
044        _odfPageResolver = (OdfPageResolver) manager.lookup(OdfPageResolver.ROLE);
045    }
046    
047    @Override
048    protected void _updateIndexReferencingPages (String contentId, boolean recursively) throws Exception
049    {
050        Content content = _resolver.resolveById(contentId);
051        
052        if (content instanceof ProgramItem)
053        {
054            Collection<Page> referencingPages = _odfPageResolver.getReferencingPages((ProgramItem) content);
055            
056            for (Page page : referencingPages)
057            {
058                // Reindex page in default workspace (its attachments are still indexed, so no need to index them)
059                _solrPageIndexer.reindexPage(page.getId(), RepositoryConstants.DEFAULT_WORKSPACE, recursively, false);
060                // Reindex page in live workspace if exists (if page was not in live before, its attachments were not either, so index them)
061                _solrPageIndexer.reindexPage(page.getId(), WebConstants.LIVE_WORKSPACE, recursively, true);
062            }
063        }
064    }
065
066}