001/*
002 *  Copyright 2015 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.avalon.framework.context.Context;
021import org.apache.avalon.framework.context.ContextException;
022import org.apache.avalon.framework.context.Contextualizable;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.apache.cocoon.components.ContextHelper;
027import org.apache.cocoon.environment.Request;
028
029import org.ametys.cms.content.indexing.solr.SolrIndexer;
030import org.ametys.cms.content.indexing.solr.observation.ObserverHelper;
031import org.ametys.cms.repository.Content;
032import org.ametys.core.observation.Event;
033import org.ametys.core.observation.Observer;
034import org.ametys.plugins.repository.AmetysObjectResolver;
035import org.ametys.plugins.repository.RepositoryConstants;
036import org.ametys.plugins.repository.provider.RequestAttributeWorkspaceSelector;
037import org.ametys.runtime.plugin.component.AbstractLogEnabled;
038import org.ametys.web.ObservationConstants;
039import org.ametys.web.WebConstants;
040import org.ametys.web.repository.content.WebContent;
041import org.ametys.web.repository.page.ZoneItem.ZoneType;
042
043/**
044 * Observes when a content is made orphan or reaffected to a page and update the index accordingly.
045 */
046public class ContentOrphanStatusObserver extends AbstractLogEnabled implements Observer, Serviceable, Contextualizable
047{
048    /** The Solr indexer. */
049    protected SolrIndexer _solrIndexer;
050    /** The Ametys object resolver */
051    protected AmetysObjectResolver _resolver;
052    /** The avalon context */
053    protected Context _context;
054    
055    @Override
056    public void service(ServiceManager manager) throws ServiceException
057    {
058        _solrIndexer = (SolrIndexer) manager.lookup(SolrIndexer.ROLE);
059        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
060    }
061    
062    @Override
063    public void contextualize(Context context) throws ContextException
064    {
065        _context = context;
066    }
067    
068    @Override
069    public boolean supports(Event event)
070    {
071        return event.getId().equals(ObservationConstants.EVENT_ZONEITEM_DELETED)
072            || event.getId().equals(ObservationConstants.EVENT_ZONEITEM_ADDED)
073            || event.getId().equals(ObservationConstants.EVENT_ZONEITEM_MODIFIED);
074    }
075    
076    @Override
077    public int getPriority(Event event)
078    {
079        // Will be processed AFTER live synchronization observers, to have up-to-date pages in live, in order to have correct indexation data
080        return Observer.MAX_PRIORITY + 4000;
081    }
082    
083    @Override
084    public void observe(Event event, Map<String, Object> transientVars) throws Exception
085    {
086        if (ObserverHelper.isNotSuspendedObservationForIndexation())
087        {
088            ZoneType zoneType = (ZoneType) event.getArguments().get(ObservationConstants.ARGS_ZONE_TYPE);
089            if (zoneType == ZoneType.CONTENT)
090            {
091                Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_ZONE_ITEM_CONTENT);
092                
093                if (content != null)
094                {
095                    // default workspace
096                    _updateProperties(content, RepositoryConstants.DEFAULT_WORKSPACE);
097                    
098                    // live workspace
099                    Request request = ContextHelper.getRequest(_context);
100                    String currentWsp = RequestAttributeWorkspaceSelector.getForcedWorkspace(request);
101                    try
102                    {
103                        // Force the workspace.
104                        RequestAttributeWorkspaceSelector.setForcedWorkspace(request, WebConstants.LIVE_WORKSPACE);
105                        if (_resolver.hasAmetysObjectForId(content.getId()))
106                        {
107                            Content liveContent = _resolver.resolveById(content.getId(), null);
108                            _updateProperties(liveContent, WebConstants.LIVE_WORKSPACE);
109                        }
110                    }
111                    finally
112                    {
113                        // Restore workspace
114                        RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWsp);
115                    }
116                }
117            }
118        }
119    }
120    
121    private void _updateProperties(Content content, String workspace) throws Exception
122    {
123        if (content instanceof WebContent)
124        {
125            // Update orphan status
126            _solrIndexer.updateSystemProperty(content, "orphan", workspace);
127            // Update pageIds
128            _solrIndexer.updateSystemProperty(content, "pages", workspace);
129        }
130    }
131}