001/*
002 *  Copyright 2017 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.plugins.workspaces.indexing.solr;
017
018import java.io.IOException;
019
020import org.ametys.cms.content.indexing.solr.SolrIndexer;
021import org.ametys.cms.content.indexing.solr.SolrResourceIndexer;
022import org.ametys.cms.search.solr.SolrClientProvider;
023import org.ametys.plugins.explorer.resources.Resource;
024import org.ametys.plugins.explorer.resources.ResourceCollection;
025import org.ametys.plugins.repository.AmetysObject;
026import org.ametys.plugins.workspaces.project.objects.Project;
027import org.ametys.runtime.plugin.component.AbstractLogEnabled;
028import org.ametys.web.indexing.solr.SolrWebFieldNames;
029import org.apache.avalon.framework.component.Component;
030import org.apache.avalon.framework.service.ServiceException;
031import org.apache.avalon.framework.service.ServiceManager;
032import org.apache.avalon.framework.service.Serviceable;
033import org.apache.solr.client.solrj.SolrClient;
034import org.apache.solr.client.solrj.SolrServerException;
035import org.apache.solr.client.solrj.response.UpdateResponse;
036import org.apache.solr.common.SolrInputDocument;
037
038/**
039 * Component responsible for indexing a project resource (document)
040 */
041public class SolrProjectResourceIndexer extends AbstractLogEnabled implements Component, Serviceable, SolrWorkspacesConstants
042{
043    /** The avalon role. */
044    public static final String ROLE = SolrProjectResourceIndexer.class.getName();
045    
046    /** The Solr client provider */
047    protected SolrClientProvider _solrClientProvider;
048    /** Solr resource indexer */
049    protected SolrResourceIndexer _solrResourceIndexer;
050    /** Solr indexer */
051    protected SolrIndexer _solrIndexer;
052    
053    @Override
054    public void service(ServiceManager manager) throws ServiceException
055    {
056        _solrClientProvider = (SolrClientProvider) manager.lookup(SolrClientProvider.ROLE);
057        _solrResourceIndexer = (SolrResourceIndexer) manager.lookup(SolrResourceIndexer.ROLE);
058        _solrIndexer = (SolrIndexer) manager.lookup(SolrIndexer.ROLE);
059    }
060    
061    /**
062     * Index the children project resources of the given resource collection
063     * @param collection The collection of project resources
064     * @param project The project whose resources are attached
065     * @param workspaceName The workspace name
066     * @throws Exception if an error occurs when processing the indexation of the project resources
067     */
068    public void indexProjectResources(ResourceCollection collection, Project project, String workspaceName) throws Exception
069    {
070        SolrClient solrClient = _solrClientProvider.getUpdateClient(workspaceName, true);
071        indexProjectResources(collection, project, solrClient);
072    }
073    
074    /**
075     * Index the children project resources of the given resource collection
076     * @param collection The collection of project resources
077     * @param project The project whose resources are attached
078     * @param solrClient The solr client to use
079     * @throws Exception if an error occurs when processing the indexation of the project resources
080     */
081    public void indexProjectResources(ResourceCollection collection, Project project, SolrClient solrClient) throws Exception
082    {
083        if (collection == null)
084        {
085            return;
086        }
087        
088        for (AmetysObject object : collection.getChildren())
089        {
090            if (object instanceof ResourceCollection)
091            {
092                indexProjectResources((ResourceCollection) object, project, solrClient);
093            }
094            else if (object instanceof Resource)
095            {
096                _indexProjectResource((Resource) object, project, solrClient);
097            }
098        }
099    }
100    
101    /**
102     * Index a project resource
103     * @param resource The project resource (document)
104     * @param project The project whose the resource is attached
105     * @param workspaceName The workspace name
106     * @throws Exception if an error occurs when processing the indexation of the project resource
107     */
108    public void indexProjectResource(Resource resource, Project project, String workspaceName) throws Exception
109    {
110        SolrClient solrClient = _solrClientProvider.getUpdateClient(workspaceName, true);
111        _indexProjectResource(resource, project, solrClient);
112    }
113    
114    private void _indexProjectResource(Resource resource, Project project, SolrClient solrClient) throws Exception
115    {
116        SolrInputDocument solrDocument = new SolrInputDocument();
117        
118        // Prepare resource doc
119        _indexProjectResource(resource, solrDocument, project);
120        
121        // Indexation of the Solr document
122        _indexResourceSolrDocument(resource, solrDocument, solrClient);
123    }
124    
125    private void _indexProjectResource(Resource resource, SolrInputDocument document, Project project) throws Exception
126    {
127        _solrResourceIndexer.indexResource(resource, document, TYPE_PROJECT_RESOURCE);
128        
129        project.getSites().forEach(site -> 
130        {
131            // site name - Store.YES, Index.NOT_ANALYZED
132            document.addField(SolrWebFieldNames.SITE_NAME, site.getName());
133            
134            // site type - Store.YES, Index.NOT_ANALYZED
135            document.addField(SolrWebFieldNames.SITE_TYPE, site.getType());
136        });
137        
138        document.addField(PROJECT_ID, project.getId());
139        document.setField(KEYWORDS, resource.getKeywords());
140    }
141    
142    private void _indexResourceSolrDocument(Resource resource, SolrInputDocument document, SolrClient solrClient) throws SolrServerException, IOException
143    {
144        UpdateResponse solrResponse = solrClient.add(_solrClientProvider.getCollectionName(), document);
145        int status = solrResponse.getStatus();
146        
147        if (status != 0)
148        {
149            throw new IOException("Ametys resource indexing - Expecting status code of '0' in the Solr response but got : '" + status + "'. Resource id : " + resource.getId());
150        }
151        
152        getLogger().debug("Successful resource indexing. Resource identifier : {}", resource.getId());
153    }
154}