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 org.apache.avalon.framework.context.Context; 019import org.apache.avalon.framework.context.ContextException; 020import org.apache.avalon.framework.context.Contextualizable; 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.avalon.framework.service.Serviceable; 024import org.apache.cocoon.components.ContextHelper; 025import org.apache.cocoon.environment.Request; 026import org.apache.solr.client.solrj.SolrClient; 027 028import org.ametys.cms.indexing.IndexingException; 029import org.ametys.cms.indexing.solr.DocumentProvider; 030import org.ametys.plugins.explorer.resources.ModifiableResourceCollection; 031import org.ametys.plugins.repository.AmetysObjectIterable; 032import org.ametys.plugins.repository.provider.RequestAttributeWorkspaceSelector; 033import org.ametys.plugins.workspaces.documents.DocumentWorkspaceModule; 034import org.ametys.plugins.workspaces.project.ProjectManager; 035import org.ametys.plugins.workspaces.project.modules.WorkspaceModuleExtensionPoint; 036import org.ametys.plugins.workspaces.project.objects.Project; 037import org.ametys.runtime.plugin.component.AbstractLogEnabled; 038 039/** 040 * {@link DocumentProvider} for indexing documents (project resources...) during workspace indexation 041 */ 042public class ProjectDocumentProvider extends AbstractLogEnabled implements DocumentProvider, Serviceable, Contextualizable 043{ 044 /** The manager for {@link Project}s */ 045 protected ProjectManager _projectManager; 046 /** The Solr indexer for project resources */ 047 protected SolrProjectResourceIndexer _solrProjectResourceIndexer; 048 /** The manager for documents */ 049 protected DocumentWorkspaceModule _documentModule; 050 051 /** The component context. */ 052 protected Context _context; 053 054 @Override 055 public void contextualize(Context context) throws ContextException 056 { 057 _context = context; 058 } 059 060 @Override 061 public void service(ServiceManager manager) throws ServiceException 062 { 063 _projectManager = (ProjectManager) manager.lookup(ProjectManager.ROLE); 064 _solrProjectResourceIndexer = (SolrProjectResourceIndexer) manager.lookup(SolrProjectResourceIndexer.ROLE); 065 WorkspaceModuleExtensionPoint moduleManager = (WorkspaceModuleExtensionPoint) manager.lookup(WorkspaceModuleExtensionPoint.ROLE); 066 _documentModule = moduleManager.getModule(DocumentWorkspaceModule.DODUMENT_MODULE_ID); 067 } 068 069 @Override 070 public void indexDocuments(String workspaceName, SolrClient solrClient) throws IndexingException 071 { 072 Request request = ContextHelper.getRequest(_context); 073 String currentWsp = RequestAttributeWorkspaceSelector.getForcedWorkspace(request); 074 075 try 076 { 077 // Force the workspace. 078 RequestAttributeWorkspaceSelector.setForcedWorkspace(request, workspaceName); 079 _indexProjects(workspaceName, solrClient); 080 } 081 finally 082 { 083 // Restore context 084 RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWsp); 085 } 086 } 087 088 private void _indexProjects(String workspaceName, SolrClient solrClient) throws IndexingException 089 { 090 AmetysObjectIterable<Project> projects = _projectManager.getProjects(); 091 for (Project project : projects) 092 { 093 _indexProject(workspaceName, project, solrClient); 094 } 095 } 096 097 private void _indexProject(String workspaceName, Project project, SolrClient solrClient) throws IndexingException 098 { 099 try 100 { 101 // Index the documents 102 ModifiableResourceCollection documentRoot = _documentModule.getModuleRoot(project, false); 103 getLogger().info("Starting indexation of project resources for project {} ('{}') in workspace {}", project.getName(), project.getId(), workspaceName); 104 _solrProjectResourceIndexer.indexProjectResources(documentRoot, project, solrClient); 105 } 106 catch (Exception e) 107 { 108 String error = String.format("Failed to index project %s in workspace %s", project.getName(), workspaceName); 109 getLogger().error(error, e); 110 throw new IndexingException(error, e); 111 } 112 } 113}