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;
019
020import javax.jcr.Repository;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.components.ContextHelper;
025import org.apache.cocoon.environment.Request;
026
027import org.ametys.cms.ObservationConstants;
028import org.ametys.core.observation.Event;
029import org.ametys.core.observation.Observer;
030import org.ametys.odf.course.Course;
031import org.ametys.odf.program.Program;
032import org.ametys.odf.program.SubProgram;
033import org.ametys.plugins.odfweb.repository.OdfPageHandler;
034import org.ametys.plugins.repository.RepositoryConstants;
035import org.ametys.plugins.repository.provider.AbstractRepository;
036import org.ametys.plugins.repository.provider.RequestAttributeWorkspaceSelector;
037import org.ametys.web.WebConstants;
038import org.ametys.web.repository.page.Page;
039import org.ametys.web.site.SiteConfigurationExtensionPoint;
040
041/**
042 * {@link Observer} unindexing pages corresponding to {@link Program}s that are not displayed by the site anymore
043 * given current ODF restrictions.
044 */
045public class SolrContentValidatedPart1Observer extends AbstractSolrODFObserver
046{
047    /** The site configuration extension point. */
048    protected SiteConfigurationExtensionPoint _siteConf;
049    /** The JCR repository */
050    protected Repository _repository;
051    /** The ODF page handler */
052    protected OdfPageHandler _odfPageHandler;
053    
054    @Override
055    public void service(ServiceManager manager) throws ServiceException
056    {
057        super.service(manager);
058        _siteConf = (SiteConfigurationExtensionPoint) manager.lookup(SiteConfigurationExtensionPoint.ROLE);
059        _repository = (Repository) manager.lookup(AbstractRepository.ROLE);
060        _odfPageHandler = (OdfPageHandler) manager.lookup(OdfPageHandler.ROLE);
061    }
062    
063    @Override
064    public boolean supports(Event event)
065    {
066        return event.getId().equals(ObservationConstants.EVENT_CONTENT_VALIDATED);
067    }
068    
069    @Override
070    public int getPriority(Event event)
071    {
072        // Before live synchronization.
073        return MAX_PRIORITY + 500;
074    }
075    
076    @Override
077    protected String _workspaceToUse()
078    {
079        return WebConstants.LIVE_WORKSPACE;
080    }
081    
082    @Override
083    protected void _updateIndex(Event event, Map<String, Object> transientVars, Page odfRootPage, Program program, SubProgram subProgram, Course course) throws Exception
084    {
085        Page liveOdfRootPage = _resolver.resolveById(odfRootPage.getId());
086        
087        // Test the program in its default version (with the new orgunit reference,
088        // as we are before live synchronization) but use the OrgUnit structure
089        // from the live workspace (forced above), as we want the structure
090        // which is present in Solr indexes.
091        
092        Request request = ContextHelper.getRequest(_context);
093        
094        // Retrieve current workspace
095        String currentWsp = RequestAttributeWorkspaceSelector.getForcedWorkspace(request);
096        
097        boolean isValid;
098        try
099        {
100            // is part of restrictions in default workspace ?
101            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, RepositoryConstants.DEFAULT_WORKSPACE);
102            isValid = _odfPageHandler.isValidRestriction(liveOdfRootPage, program);
103        }
104        finally
105        {
106            // Restore context
107            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWsp);
108        }
109        
110        String programId = program.getId();
111        if (isValid || !_resolver.hasAmetysObjectForId(programId))
112        {
113            return;
114        }
115        
116        // Retrieve program in the live workspace
117        Program liveProgram = _resolver.resolveById(programId);
118        
119        Page liveAbstractProgramPage = null;
120        if (subProgram != null)
121        {
122            // Retrieve subprogram in the live workspace
123            SubProgram liveSubProgram = _resolver.resolveById(subProgram.getId());
124            // Pages of subprogram
125            liveAbstractProgramPage = _odfPageResolver.getSubProgramPage(liveOdfRootPage, liveSubProgram, liveProgram);
126        }
127        else // program is the concerned content by the event
128        {
129            liveAbstractProgramPage = _odfPageResolver.getProgramPage(liveOdfRootPage, liveProgram);
130        }
131        
132        // If the page exists in the live whereas the program is outside the restrictions, unindex it.
133        // (the program won't be displayed in this site anymore after live synchronization).
134        if (liveAbstractProgramPage != null)
135        {
136            String abstractProgramPageId = liveAbstractProgramPage.getId();
137            getLogger().info("Removing Solr page document with id: {}", abstractProgramPageId);
138            
139            // Unindex recursively
140            _solrPageIndexer.unindexPage(abstractProgramPageId, WebConstants.LIVE_WORKSPACE, true, true);
141        }
142    }
143}