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