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     * @param commit true to commit
067     * @throws Exception if an error occurs when processing the indexation of the project resources
068     */
069    public void indexProjectResources(ResourceCollection collection, Project project, String workspaceName, boolean commit) throws Exception
070    {
071        if (collection == null)
072        {
073            return;
074        }
075        
076        for (AmetysObject object : collection.getChildren())
077        {
078            if (object instanceof ResourceCollection)
079            {
080                indexProjectResources((ResourceCollection) object, project, workspaceName, false);
081            }
082            else if (object instanceof Resource)
083            {
084                indexProjectResource((Resource) object, project, workspaceName, false);
085            }
086        }
087        
088        if (commit)
089        {
090            _solrIndexer.commit(workspaceName);
091        }
092    }
093    
094    /**
095     * Index a project resource
096     * @param resource The project resource (document)
097     * @param project The project whose the resource is attached
098     * @param workspaceName The workspace name
099     * @param commit true to commit
100     * @throws Exception if an error occurs when processing the indexation of the project resource
101     */
102    public void indexProjectResource(Resource resource, Project project, String workspaceName, boolean commit) throws Exception
103    {
104        SolrInputDocument solrDocument = new SolrInputDocument();
105        
106        // Prepare resource doc
107        _indexProjectResource(resource, solrDocument, project);
108        
109        // Indexation of the Solr document
110        _indexResourceSolrDocument(resource, solrDocument, workspaceName, commit);
111    }
112    
113    private void _indexProjectResource(Resource resource, SolrInputDocument document, Project project) throws Exception
114    {
115        _solrResourceIndexer.indexResource(resource, document, TYPE_PROJECT_RESOURCE);
116        
117        project.getSites().forEach(site -> 
118        {
119            // site name - Store.YES, Index.NOT_ANALYZED
120            document.addField(SolrWebFieldNames.SITE_NAME, site.getName());
121            
122            // site type - Store.YES, Index.NOT_ANALYZED
123            document.addField(SolrWebFieldNames.SITE_TYPE, site.getType());
124        });
125        
126        document.addField(PROJECT_ID, project.getId());
127        document.setField(KEYWORDS, resource.getKeywords());
128    }
129    
130    private void _indexResourceSolrDocument(Resource resource, SolrInputDocument document, String workspaceName, boolean commit) throws SolrServerException, IOException
131    {
132        SolrClient solrClient = _solrClientProvider.getUpdateClient(workspaceName);
133        UpdateResponse solrResponse = solrClient.add(_solrClientProvider.getCollectionName(), document);
134        int status = solrResponse.getStatus();
135        
136        if (status != 0)
137        {
138            throw new IOException("Ametys resource indexing - Expecting status code of '0' in the Solr response but got : '" + status + "'. Resource id : " + resource.getId());
139        }
140        
141        if (commit)
142        {
143            _solrIndexer.commit(workspaceName);
144        }
145        
146        getLogger().debug("Successful resource indexing. Resource identifier : {}", resource.getId());
147    }
148}