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.context.Context;
021import org.apache.avalon.framework.context.ContextException;
022import org.apache.avalon.framework.context.Contextualizable;
023import org.apache.avalon.framework.logger.AbstractLogEnabled;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027
028import org.ametys.cms.ObservationConstants;
029import org.ametys.cms.content.indexing.solr.SolrIndexer;
030import org.ametys.cms.content.indexing.solr.SolrWorkflowIndexer;
031import org.ametys.cms.content.indexing.solr.observation.ObserverHelper;
032import org.ametys.cms.indexing.IndexingObserver;
033import org.ametys.core.observation.AsyncObserver;
034import org.ametys.core.observation.Event;
035
036/**
037 * Observer in charge for indexing contents when archived to the archived workspace.
038 */
039public class IndexArchivedContentObserver extends AbstractLogEnabled implements AsyncObserver, IndexingObserver, Contextualizable, Serviceable
040{
041    /** The Solr indexer. */
042    protected SolrIndexer _solrIndexer;
043    
044    /** The solr workflow indexer. */
045    protected SolrWorkflowIndexer _solrWfIndexer;
046    
047    /** The component context. */
048    protected Context _context;
049
050    @Override
051    public void contextualize(Context context) throws ContextException
052    {
053        _context = context;
054    }
055    
056    @Override
057    public void service(ServiceManager serviceManager) throws ServiceException
058    {
059        _solrIndexer = (SolrIndexer) serviceManager.lookup(SolrIndexer.ROLE);
060        _solrWfIndexer = (SolrWorkflowIndexer) serviceManager.lookup(SolrWorkflowIndexer.ROLE);
061    }
062    
063    @Override
064    public boolean supports(Event event)
065    {
066        return event.getId().equals(ObservationConstants.EVENT_CONTENT_ARCHIVED);
067    }
068    
069    @Override
070    public int getPriority()
071    {
072        return MAX_PRIORITY + 3000;
073    }
074    
075    @Override
076    public void observe(Event event, Map<String, Object> transientVars) throws Exception
077    {
078        if (ObserverHelper.isNotSuspendedObservationForIndexation())
079        {
080            String contentId = (String) event.getArguments().get(ObservationConstants.ARGS_CONTENT_ID);
081            if (contentId != null)
082            {
083                _solrIndexer.indexContent(contentId, ArchiveConstants.ARCHIVE_WORKSPACE, true);
084            }
085        }
086    }
087    
088}