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