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.apache.avalon.framework.component.Component; 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.avalon.framework.service.Serviceable; 024import org.apache.solr.client.solrj.SolrClient; 025import org.apache.solr.client.solrj.SolrServerException; 026import org.apache.solr.client.solrj.response.UpdateResponse; 027 028import org.ametys.cms.content.indexing.solr.SolrIndexer; 029import org.ametys.cms.search.query.Query; 030import org.ametys.cms.search.query.QuerySyntaxException; 031import org.ametys.cms.search.solr.SolrClientProvider; 032import org.ametys.plugins.repository.RepositoryConstants; 033import org.ametys.plugins.workspaces.search.query.ProjectQuery; 034import org.ametys.runtime.plugin.component.AbstractLogEnabled; 035import org.ametys.web.WebConstants; 036 037/** 038 * Component responsible for indexing workspaces' objects 039 */ 040public class SolrProjectIndexer extends AbstractLogEnabled implements Component, Serviceable, SolrWorkspacesConstants 041{ 042 /** The avalon role. */ 043 public static final String ROLE = SolrProjectIndexer.class.getName(); 044 045 /** The Solr client provider */ 046 protected SolrClientProvider _solrClientProvider; 047 /** The Solr indexer */ 048 protected SolrIndexer _solrIndexer; 049 050 @Override 051 public void service(ServiceManager manager) throws ServiceException 052 { 053 _solrClientProvider = (SolrClientProvider) manager.lookup(SolrClientProvider.ROLE); 054 _solrIndexer = (SolrIndexer) manager.lookup(SolrIndexer.ROLE); 055 } 056 057 /** 058 * Unindex all Solr documents related to the given project in all workspaces and commit. 059 * @param projectId The id of the project 060 * @throws SolrServerException if there is an error on the solr server 061 * @throws IOException If there is a low-level I/O error. 062 * @throws QuerySyntaxException If there is a low-level I/O error. 063 */ 064 public void unindexProject(String projectId) throws SolrServerException, IOException, QuerySyntaxException 065 { 066 unindexProject(projectId, RepositoryConstants.DEFAULT_WORKSPACE); 067 unindexProject(projectId, WebConstants.LIVE_WORKSPACE); 068 } 069 /** 070 * Unindex all Solr documents related to the given project 071 * @param projectId The id of the project 072 * @param workspaceName The workspace name 073 * @throws SolrServerException if there is an error on the solr server 074 * @throws IOException If there is a low-level I/O error. 075 * @throws QuerySyntaxException If there is a low-level I/O error. 076 */ 077 public void unindexProject(String projectId, String workspaceName) throws SolrServerException, IOException, QuerySyntaxException 078 { 079 Query query = new ProjectQuery(projectId); 080 SolrClient solrClient = _solrClientProvider.getUpdateClient(workspaceName); 081 UpdateResponse solrResponse = solrClient.deleteByQuery(_solrClientProvider.getCollectionName(workspaceName), query.build()); 082 int status = solrResponse.getStatus(); 083 084 if (status != 0) 085 { 086 throw new IOException("Ametys unindexing all Solr documents related to a Project- Expecting status code of '0' in the Solr response but got : '" + status + "'. Project id : " + projectId); 087 } 088 089 getLogger().debug("Successful unindexing of all Solr documents with field {} equals to {} in workspace {}", SolrWorkspacesConstants.PROJECT_ID, projectId, workspaceName); 090 } 091}