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