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.web.indexing.solr;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020
021import org.ametys.cms.content.indexing.solr.SolrFieldNames;
022import org.ametys.cms.indexing.IndexingException;
023import org.ametys.cms.indexing.solr.SolrWorkspaceIndexer;
024import org.ametys.plugins.repository.AmetysObject;
025import org.ametys.plugins.repository.AmetysObjectIterable;
026import org.ametys.plugins.repository.AmetysObjectResolver;
027import org.ametys.plugins.repository.RepositoryConstants;
028import org.ametys.plugins.repository.TraversableAmetysObject;
029import org.ametys.plugins.repository.UnknownAmetysObjectException;
030import org.ametys.runtime.config.Config;
031import org.ametys.web.explorer.ResourceHelper;
032import org.ametys.web.indexing.SiteIndexer;
033import org.ametys.web.repository.site.Site;
034import org.ametys.web.repository.site.SiteManager;
035
036/**
037 * Web-specific component indexing a workspace in a Solr server.
038 */
039public class SolrWebWorkspaceIndexer extends SolrWorkspaceIndexer
040{
041    /** The site manager. */
042    protected SiteManager _siteManager;
043    
044    /** The site indexer. */
045    protected SiteIndexer _siteIndexer;
046    
047    /** The Ametys object resolver. */
048    protected AmetysObjectResolver _resolver;
049    
050    @Override
051    public void service(ServiceManager manager) throws ServiceException
052    {
053        super.service(manager);
054        
055        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
056        _siteIndexer = (SiteIndexer) manager.lookup(SiteIndexer.ROLE);
057        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
058    }
059    
060    @Override
061    protected void doIndex(String workspaceName) throws IndexingException
062    {
063        try (AmetysObjectIterable<Site> sites = _siteManager.getSites())
064        {
065            // First, unindex all documents.
066            _solrIndexer.unindexAllDocuments(workspaceName, false);
067            
068            // Index each site.
069            for (Site site : sites)
070            {
071                _siteIndexer.indexSite(site.getName(), workspaceName);
072            }
073            
074            // Index contents in the out-of-site content root.
075            indexContentRoot(workspaceName);
076            indexPluginsContentRoot(workspaceName);
077            
078            // Index shared resources.
079            indexSharedResources(workspaceName);
080            
081            // Optionally index additional documents.
082            indexAdditionalDocuments(workspaceName);
083            
084            _solrIndexer.commit(workspaceName);
085            // Optimize once when done.
086            _solrIndexer.optimize(workspaceName);
087        }
088        catch (Exception e)
089        {
090            getLogger().error("Error indexing the workspace '" + workspaceName + "'.", e);
091            throw new IndexingException("Error indexing the workspace '" + workspaceName + "'.", e);
092        }
093    }
094    
095    /**
096     * Index the out-of-site contents.
097     * @param workspaceName The workspace name
098     * @throws Exception If an error occurs.
099     */
100    protected void indexContentRoot(String workspaceName) throws Exception
101    {
102        try
103        {
104            TraversableAmetysObject contentRoot = _resolver.resolveByPath("/" + RepositoryConstants.NAMESPACE_PREFIX + ":contents");
105            _solrIndexer.indexContents(contentRoot.getChildren(), workspaceName, true, false);
106        }
107        catch (UnknownAmetysObjectException e)
108        {
109            // Ignore if the content root doesn't exist in a workspace.
110        }
111    }
112    
113    /**
114     * Index the contents stored by plugins
115     * @param workspaceName The workspace name
116     * @throws Exception If an error occurs.
117     */
118    protected void indexPluginsContentRoot(String workspaceName) throws Exception
119    {
120        try
121        {
122            TraversableAmetysObject pluginsNode = _resolver.resolveByPath("/" + RepositoryConstants.NAMESPACE_PREFIX + ":plugins");
123            
124            for (AmetysObject pluginNode : pluginsNode.getChildren())
125            {
126                if (pluginNode instanceof TraversableAmetysObject)
127                {
128                    try
129                    {
130                        AmetysObject rootContents = ((TraversableAmetysObject) pluginNode).getChild(RepositoryConstants.NAMESPACE_PREFIX + ":contents");
131                        if (rootContents instanceof TraversableAmetysObject)
132                        {
133                            _solrIndexer.indexContents(((TraversableAmetysObject) rootContents).getChildren(), workspaceName, true, false);
134                        }
135                    }
136                    catch (UnknownAmetysObjectException e)
137                    {
138                        // Ignore if the content root doesn't exist in a plugin's node.
139                    }
140                    
141                }
142            }
143        }
144        catch (UnknownAmetysObjectException e)
145        {
146            // Ignore if the plugin's node doesn't exist in a workspace.
147        }
148    }
149    
150    
151    /**
152     * Index the shared resources.
153     * @param workspaceName The workspace name
154     * @throws Exception If an error occurs.
155     */
156    protected void indexSharedResources(String workspaceName) throws Exception
157    {
158        boolean useShared = Config.getInstance().getValueAsBoolean("resources.shared.folder");
159        if (useShared)
160        {
161            try
162            {
163                TraversableAmetysObject sharedResources = _resolver.resolveByPath(ResourceHelper.SHARED_RESOURCE_PATH + "/all");
164                _solrIndexer.indexResources(sharedResources.getChildren(), SolrFieldNames.TYPE_RESOURCE, sharedResources, workspaceName, false);
165            }
166            catch (UnknownAmetysObjectException e)
167            {
168                // Ignore if the shared resource root doesn't exist in a workspace.
169            }
170        }
171    }
172    
173}