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.cms.repository.Content;
022import org.ametys.core.observation.AsyncObserver;
023import org.ametys.core.observation.Event;
024import org.ametys.core.observation.Observer;
025import org.ametys.odf.course.Course;
026import org.ametys.odf.program.Program;
027import org.ametys.odf.program.SubProgram;
028import org.ametys.plugins.repository.UnknownAmetysObjectException;
029import org.ametys.web.WebConstants;
030import org.ametys.web.repository.page.Page;
031
032/**
033 * <p>
034 * {@link Observer} for observing odf content validation in order to synchronize
035 * lucene index associated.
036 * </p>
037 */
038public class SolrContentValidatedPart2Observer extends AbstractSolrODFObserver implements AsyncObserver
039{
040    @Override
041    public boolean supports(Event event)
042    {
043        return event.getId().equals(ObservationConstants.EVENT_CONTENT_VALIDATED);
044    }
045
046    @Override
047    protected String _workspaceToUse()
048    {
049        return WebConstants.LIVE_WORKSPACE;
050    }
051    
052    @Override
053    protected Content _getContentArgument(Event event) throws Exception
054    {
055        // Async observer cannot directly use an AmetysObject as argument because its session might not be alive anymore.
056        Content content = super._getContentArgument(event);
057        if (content == null)
058        {
059            return null;
060        }
061        
062        // content is resolved in the current workspace (ie. live in this case)
063        return _resolver.resolveById(content.getId());
064    }
065    
066    @Override
067    protected void _updateIndex(Event event, Map<String, Object> transientVars, Page odfRootPage, Program program, SubProgram subProgram, Course course) throws Exception
068    {
069        Page liveOdfRootPage = _resolver.resolveById(odfRootPage.getId());
070        
071        try
072        {
073            Page abstractProgramPage = null;
074            if (subProgram != null)
075            {
076                // Page of subprogram
077                abstractProgramPage = _odfPageResolver.getSubProgramPage(liveOdfRootPage, subProgram, program);
078            }
079            else
080            {
081                abstractProgramPage = _odfPageResolver.getProgramPage(liveOdfRootPage, program);
082            }
083            
084            if (abstractProgramPage != null)
085            {
086                String abstractProgramPageId = abstractProgramPage.getId();
087                getLogger().info("Updating Solr page document with id: {}", abstractProgramPageId);
088                
089                // Index recursively
090                _solrPageIndexer.indexPage(abstractProgramPageId, WebConstants.LIVE_WORKSPACE, true, true, false);
091                
092                // No need to index course page(s) as they are already indexed in line above as we index recursively 
093//                if (course != null)
094//                {
095//                    // Retrieve course in the live workspace
096//                    Page liveCoursePage = _odfPageResolver.getCoursePage(liveOdfRootPage, course, program);
097//                    if (liveCoursePage != null)
098//                    {
099//                        // Index course page recursively
100//                        _solrPageIndexer.indexPage(liveCoursePage.getId(), WebConstants.LIVE_WORKSPACE, true, true, false);
101//                    }
102//                    else
103//                    {
104//                        getLogger().warn("Can not find live course page for course '{}' ({}) and parent program '{}' ({})", course.getTitle(), course.getId(), program.getTitle(), program.getId());
105//                    }
106//                }
107            }
108        }
109        catch (UnknownAmetysObjectException e)
110        {
111            // Page is not synchronized
112        }
113    }
114}