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.cms.content.indexing.solr.observation;
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;
024import org.apache.solr.client.solrj.SolrClient;
025
026import org.ametys.cms.ObservationConstants;
027import org.ametys.cms.content.indexing.solr.SolrIndexer;
028import org.ametys.cms.search.solr.SolrClientProvider;
029import org.ametys.core.observation.Event;
030import org.ametys.core.observation.Observer;
031import org.ametys.plugins.repository.provider.AbstractRepository;
032import org.ametys.plugins.repository.provider.JackrabbitRepository;
033
034/**
035 * Observer in charge for removing deleted contents from the Solr server.
036 */
037public class UnindexContentObserver extends AbstractLogEnabled implements Observer, Serviceable
038{
039    /** The provider of solr clients */
040    protected SolrClientProvider _solrClientProvider;
041    /** The Solr indexer. */
042    protected SolrIndexer _solrIndexer;
043    
044    /** The JCR repository */
045    protected JackrabbitRepository _repository;
046    
047    @Override
048    public void service(ServiceManager serviceManager) throws ServiceException
049    {
050        _solrClientProvider = (SolrClientProvider) serviceManager.lookup(SolrClientProvider.ROLE);
051        _solrIndexer = (SolrIndexer) serviceManager.lookup(SolrIndexer.ROLE);
052        _repository = (JackrabbitRepository) serviceManager.lookup(AbstractRepository.ROLE);
053    }
054    
055    @Override
056    public boolean supports(Event event)
057    {
058        return event.getId().equals(ObservationConstants.EVENT_CONTENT_DELETED);
059    }
060    
061    @Override
062    public int getPriority(Event event)
063    {
064        return 0;
065    }
066    
067    @Override
068    public void observe(Event event, Map<String, Object> transientVars) throws Exception
069    {
070        if (ObserverHelper.isNotSuspendedObservationForIndexation())
071        {
072            Map<String, Object> args = event.getArguments();
073            Boolean commit = (Boolean) args.get(ObservationConstants.ARGS_CONTENT_COMMIT);
074            if (commit == null)
075            {
076                // If not specified, let commit argument be 'true' by default, in order to be sure to commit changes.
077                commit = true;
078            }
079            
080            String contentId = (String) event.getArguments().get(ObservationConstants.ARGS_CONTENT_ID);
081            if (contentId != null)
082            {
083                String[] workspaceNames = _repository.getWorkspaces();
084                for (String workspaceName : workspaceNames)
085                {
086                    SolrClient solrClient = _solrClientProvider.getUpdateClient(workspaceName, commit);
087                    _solrIndexer.unindexContent(contentId, workspaceName, true, solrClient);
088                }
089            }
090        }
091    }
092    
093}