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.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;
027import org.apache.solr.client.solrj.SolrClient;
028
029import org.ametys.cms.ObservationConstants;
030import org.ametys.cms.content.indexing.solr.SolrIndexer;
031import org.ametys.cms.content.indexing.solr.SolrWorkflowIndexer;
032import org.ametys.cms.repository.Content;
033import org.ametys.cms.repository.WorkflowAwareContent;
034import org.ametys.cms.search.solr.SolrClientProvider;
035import org.ametys.core.observation.AsyncObserver;
036import org.ametys.core.observation.Event;
037import org.ametys.plugins.repository.AmetysObjectResolver;
038import org.ametys.plugins.repository.RepositoryConstants;
039
040/**
041 * Observer in charge for indexing contents when created or modified.
042 */
043public class IndexContentObserver extends AbstractLogEnabled implements AsyncObserver, Contextualizable, Serviceable
044{
045    /** The provider of solr clients */
046    protected SolrClientProvider _solrClientProvider;
047    
048    /** The Solr indexer. */
049    protected SolrIndexer _solrIndexer;
050    
051    /** The solr workflow indexer. */
052    protected SolrWorkflowIndexer _solrWfIndexer;
053    
054    /** The component context. */
055    protected Context _context;
056
057    private AmetysObjectResolver _resolver;
058
059    @Override
060    public void contextualize(Context context) throws ContextException
061    {
062        _context = context;
063    }
064    
065    @Override
066    public void service(ServiceManager serviceManager) throws ServiceException
067    {
068        _solrClientProvider = (SolrClientProvider) serviceManager.lookup(SolrClientProvider.ROLE);
069        _solrIndexer = (SolrIndexer) serviceManager.lookup(SolrIndexer.ROLE);
070        _solrWfIndexer = (SolrWorkflowIndexer) serviceManager.lookup(SolrWorkflowIndexer.ROLE);
071        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
072    }
073    
074    @Override
075    public boolean supports(Event event)
076    {
077        return event.getId().equals(ObservationConstants.EVENT_CONTENT_MODIFIED)
078            || event.getId().equals(ObservationConstants.EVENT_CONTENT_ADDED)
079            || event.getId().equals(ObservationConstants.EVENT_CONTENT_WORKFLOW_CHANGED)
080            || event.getId().equals(ObservationConstants.EVENT_CONTENT_TAGGED);
081    }
082    
083    @Override
084    public int getPriority(Event event)
085    {
086        return MAX_PRIORITY + 3000;
087    }
088    
089    @Override
090    public void observe(Event event, Map<String, Object> transientVars) throws Exception
091    {
092        if (ObserverHelper.isNotSuspendedObservationForIndexation())
093        {
094            Map<String, Object> args = event.getArguments();
095            String contentId = (String) args.get(ObservationConstants.ARGS_CONTENT_ID);
096            Boolean commit = (Boolean) args.get(ObservationConstants.ARGS_CONTENT_COMMIT);
097            if (commit == null)
098            {
099                // If not specified, let commit argument be 'true' by default, in order to be sure to commit changes.
100                commit = true;
101            }
102            String workspaceName = RepositoryConstants.DEFAULT_WORKSPACE;
103            SolrClient solrClient = _solrClientProvider.getUpdateClient(workspaceName, commit);
104            
105            Content content = _resolver.resolveById(contentId);
106            
107            if (content != null && index(content))
108            {
109                if (event.getId().equals(ObservationConstants.EVENT_CONTENT_WORKFLOW_CHANGED))
110                {
111                    _solrIndexer.updateSystemProperty(content, "workflowStep", workspaceName, solrClient);
112                    if (content instanceof WorkflowAwareContent)
113                    {
114                        _solrWfIndexer.indexAmetysObjectWorkflow((WorkflowAwareContent) content, workspaceName, solrClient);
115                    }
116                }
117                else
118                {
119                    _solrIndexer.indexContent(content.getId(), workspaceName, false, solrClient);
120                }
121            }
122        }
123    }
124    
125    /**
126     * Test whether to index the given content or not.
127     * @param content the content to test.
128     * @return true to index the content, false otherwise.
129     */
130    protected boolean index(Content content)
131    {
132        return true;
133    }
134    
135}