001/*
002 *  Copyright 2016 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.cms.indexing.explorer;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.logger.AbstractLogEnabled;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024
025import org.ametys.cms.content.indexing.solr.SolrIndexer;
026import org.ametys.core.observation.Event;
027import org.ametys.core.observation.Observer;
028import org.ametys.plugins.explorer.ObservationConstants;
029import org.ametys.plugins.explorer.resources.ResourceCollection;
030import org.ametys.plugins.explorer.resources.ResourceHelper;
031import org.ametys.plugins.repository.AmetysObjectResolver;
032
033/**
034 * Observer in charge for removing deleted contents from the Solr server.
035 */
036public class SolrUnindexResourceObserver extends AbstractLogEnabled implements Observer, Serviceable
037{
038    
039    /** The Solr indexer. */
040    protected SolrIndexer _solrIndexer;
041    
042    /** The AmetysObject resolver. */
043    protected AmetysObjectResolver _resolver;
044    
045    @Override
046    public void service(ServiceManager serviceManager) throws ServiceException
047    {
048        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
049        _solrIndexer = (SolrIndexer) serviceManager.lookup(SolrIndexer.ROLE);
050    }
051    
052    @Override
053    public boolean supports(Event event)
054    {
055        return event.getId().equals(ObservationConstants.EVENT_RESOURCE_DELETED)
056            || event.getId().equals(ObservationConstants.EVENT_COLLECTION_DELETED);
057    }
058    
059    @Override
060    public int getPriority(Event event)
061    {
062        return 0;
063    }
064    
065    @Override
066    public void observe(Event event, Map<String, Object> transientVars) throws Exception
067    {
068        if (event.getId().equals(ObservationConstants.EVENT_RESOURCE_DELETED))
069        {
070            String resourceId = (String) event.getArguments().get(ObservationConstants.ARGS_ID);
071            if (resourceId != null)
072            {
073                _solrIndexer.unindexResource(resourceId);
074            }
075        }
076        else if (event.getId().equals(ObservationConstants.EVENT_COLLECTION_DELETED))
077        {
078            String parentId = (String) event.getArguments().get(ObservationConstants.ARGS_PARENT_ID);
079            String collectionPath = (String) event.getArguments().get(ObservationConstants.ARGS_EXPLORER_PATH);
080            
081            ResourceCollection parent = _resolver.resolveById(parentId);
082            
083            String rootId = ResourceHelper.getResourceRoot(parent).getId();
084            
085            _solrIndexer.unindexResourcesByPath(rootId, collectionPath);
086        }
087    }
088    
089}