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