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