001/*
002 *  Copyright 2020 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;
019import java.util.Optional;
020
021import org.apache.avalon.framework.context.Context;
022import org.apache.avalon.framework.context.ContextException;
023import org.apache.avalon.framework.context.Contextualizable;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027import org.apache.cocoon.components.ContextHelper;
028import org.apache.cocoon.environment.Request;
029import org.apache.commons.lang.StringUtils;
030
031import org.ametys.cms.content.indexing.solr.observation.ObserverHelper;
032import org.ametys.core.observation.Event;
033import org.ametys.core.observation.Observer;
034import org.ametys.plugins.repository.AmetysObjectResolver;
035import org.ametys.plugins.repository.provider.RequestAttributeWorkspaceSelector;
036import org.ametys.runtime.plugin.component.AbstractLogEnabled;
037import org.ametys.web.ObservationConstants;
038import org.ametys.web.WebConstants;
039import org.ametys.web.repository.page.Page;
040
041/**
042 * Observes when a page changed, moved or deleted and test if it's in the live
043 */
044public class ContentOrphanStatusPart1Observer extends AbstractLogEnabled implements Observer, Serviceable, Contextualizable
045{
046    /** Key to know if the page was in live */
047    public static final String PAGE_WAS_IN_LIVE_KEY = "pageWasInLive";
048    
049    /** The Ametys object resolver */
050    protected AmetysObjectResolver _resolver;
051    /** The avalon context */
052    protected Context _context;
053    
054    @Override
055    public void service(ServiceManager manager) throws ServiceException
056    {
057        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
058    }
059    
060    @Override
061    public void contextualize(Context context) throws ContextException
062    {
063        _context = context;
064    }
065    
066    @Override
067    public boolean supports(Event event)
068    {
069        return event.getId().equals(ObservationConstants.EVENT_PAGE_CHANGED)
070            || event.getId().equals(ObservationConstants.EVENT_PAGE_MOVED)
071            || event.getId().equals(ObservationConstants.EVENT_PAGE_DELETED);
072    }
073    
074    @Override
075    public int getPriority(Event event)
076    {
077        // Will be processed BEFORE live synchronization observers, since pages have to be present in live to be unindexed.
078        return Observer.MAX_PRIORITY + 500;
079    }
080    
081    @Override
082    public void observe(Event event, Map<String, Object> transientVars) throws Exception
083    {
084        if (ObserverHelper.isNotSuspendedObservationForIndexation())
085        {
086            Optional<String> pageId = _getPageId(event);
087            transientVars.put(PAGE_WAS_IN_LIVE_KEY, pageId.isPresent() ? _pageWasInLive(pageId.get()) : false);
088        }
089    }
090
091    private Optional<String> _getPageId(Event event)
092    {
093        String pageId = (String) event.getArguments().get(ObservationConstants.ARGS_PAGE_ID);
094        if (StringUtils.isBlank(pageId))
095        {
096            Page page = (Page) event.getArguments().get(ObservationConstants.ARGS_PAGE);
097            pageId = page != null ? page.getId() : null;
098        }
099        return Optional.ofNullable(pageId);
100    }
101    
102    private boolean _pageWasInLive(String pageId)
103    {
104        // live workspace
105        Request request = ContextHelper.getRequest(_context);
106        String currentWsp = RequestAttributeWorkspaceSelector.getForcedWorkspace(request);
107        try
108        {
109            // Force the workspace.
110            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, WebConstants.LIVE_WORKSPACE);
111            return _resolver.hasAmetysObjectForId(pageId);
112        }
113        finally
114        {
115            // Restore workspace
116            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWsp);
117        }
118    }
119}