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