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 org.ametys.cms.ObservationConstants;
021import org.ametys.core.observation.Event;
022import org.ametys.odf.course.Course;
023import org.ametys.odf.program.Program;
024import org.ametys.odf.program.SubProgram;
025import org.ametys.plugins.repository.RepositoryConstants;
026import org.ametys.web.WebConstants;
027import org.ametys.web.repository.page.Page;
028
029/**
030 * Observes ODF {@link Program} deletion or unpublishing in order to synchronize the Solr index.
031 */
032public class UnindexProgramObserver extends AbstractSolrODFObserver
033{
034    @Override
035    public boolean supports(Event event)
036    {
037        return (event.getId().equals(ObservationConstants.EVENT_CONTENT_DELETING) || event.getId().equals(org.ametys.web.ObservationConstants.EVENT_CONTENT_UNPUBLISHED))
038                && event.getArguments().get(ObservationConstants.ARGS_CONTENT) instanceof Program;
039    }
040    
041    @Override
042    public int getPriority(Event event)
043    {
044        // Will be processed before live synchronization observers
045        return MAX_PRIORITY;
046    }
047    
048    @Override
049    protected void _updateIndex(Event event, Map<String, Object> transientVars, Page odfRootPage, Program program, SubProgram subProgram, Course course) throws Exception
050    {
051        Page programPage = _odfPageResolver.getProgramPage(odfRootPage, program);
052        if (programPage != null)
053        {
054            String pageId = programPage.getId();
055            getLogger().info("Updating Solr document with id: {}", pageId);
056            
057            // Delete the page document.
058            if (event.getId().equals(ObservationConstants.EVENT_CONTENT_DELETING)) // otherwise it is an unpublishing, so keep page in 'default'
059            {
060                _solrPageIndexer.unindexPage(pageId, RepositoryConstants.DEFAULT_WORKSPACE, true, true);
061            }
062            _solrPageIndexer.unindexPage(pageId, WebConstants.LIVE_WORKSPACE, true, true);
063        }
064    }
065}