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