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.archive;
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.cms.content.indexing.solr.observation.ObserverHelper;
028import org.ametys.cms.indexing.IndexingObserver;
029import org.ametys.core.observation.AsyncObserver;
030import org.ametys.core.observation.Event;
031
032/**
033 * Observer in charge for indexing contents when archived to the archived workspace.
034 */
035public class IndexArchivedContentObserver extends AbstractLogEnabled implements AsyncObserver, IndexingObserver, Serviceable
036{
037    /** The Solr indexer. */
038    protected SolrIndexer _solrIndexer;
039    
040    @Override
041    public void service(ServiceManager serviceManager) throws ServiceException
042    {
043        _solrIndexer = (SolrIndexer) serviceManager.lookup(SolrIndexer.ROLE);
044    }
045    
046    @Override
047    public boolean supports(Event event)
048    {
049        return event.getId().equals(ObservationConstants.EVENT_CONTENT_ARCHIVED);
050    }
051    
052    @Override
053    public int getPriority()
054    {
055        return MAX_PRIORITY + 3000;
056    }
057    
058    @Override
059    public void observe(Event event, Map<String, Object> transientVars) throws Exception
060    {
061        if (ObserverHelper.isNotSuspendedObservationForIndexation())
062        {
063            String contentId = (String) event.getArguments().get(ObservationConstants.ARGS_CONTENT_ID);
064            if (contentId != null)
065            {
066                _solrIndexer.indexContent(contentId, ArchiveConstants.ARCHIVE_WORKSPACE, true);
067            }
068        }
069    }
070    
071}