001/*
002 *  Copyright 2014 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.web.indexing.observation;
017
018import java.util.Map;
019
020import org.apache.cocoon.components.ContextHelper;
021import org.apache.cocoon.environment.Request;
022
023import org.ametys.cms.ObservationConstants;
024import org.ametys.cms.repository.Content;
025import org.ametys.core.observation.Event;
026import org.ametys.plugins.repository.provider.RequestAttributeWorkspaceSelector;
027import org.ametys.web.WebConstants;
028import org.ametys.web.repository.content.WebContent;
029import org.ametys.web.repository.page.Page;
030
031/**
032 * Observes content depublication in order to synchronize the solr index.
033 */
034public class SolrContentUnpublishedPart2Observer extends AbstractLiveSolrObserver
035{
036    @Override
037    public boolean supports(Event event)
038    {
039        return event.getId().equals(ObservationConstants.EVENT_CONTENT_UNTAG_LIVE);
040    }
041    
042    @Override
043    protected void _updateIndex(Event event, Map<String, Object> transientVars) throws Exception
044    {
045        Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
046        _updatePageDocumentsForUnpublishedContent(content);
047    }
048
049    /**
050     * Update the page documents for an unpublished content (the content is no more in live workspace).
051     * @param content the unpublished content.
052     * @throws Exception if an error occurs.
053     */
054    protected void _updatePageDocumentsForUnpublishedContent(Content content) throws Exception
055    {
056        if (content instanceof WebContent)
057        {
058            Request request = ContextHelper.getRequest(_context);
059            
060            // Retrieve current workspace
061            String currentWsp = RequestAttributeWorkspaceSelector.getForcedWorkspace(request);
062            
063            try
064            {
065                // Use live workspace
066                RequestAttributeWorkspaceSelector.setForcedWorkspace(request, WebConstants.LIVE_WORKSPACE);
067                
068                try
069                {
070                    for (Page page : ((WebContent) content).getReferencingPages())
071                    {
072                        String pageId = page.getId();
073                        
074                        if (_resolver.hasAmetysObjectForId(pageId))
075                        {
076                            // Updates the document by first deleting it
077                            _solrPageIndexer.indexPage(pageId, WebConstants.LIVE_WORKSPACE, false, true);
078                        }
079                    }
080                }
081                catch (Exception e)
082                {
083                    getLogger().error("An exception occured during page indexing", e);
084                }
085            }
086            finally
087            {
088                // Restore context
089                RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWsp);
090            }
091        }
092    }
093}