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.project;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020
021import org.ametys.cms.indexing.explorer.AbstractSolrIndexResourceObserver;
022import org.ametys.plugins.explorer.resources.Resource;
023import org.ametys.plugins.explorer.resources.ResourceCollection;
024import org.ametys.plugins.repository.AmetysObject;
025import org.ametys.plugins.repository.RepositoryConstants;
026import org.ametys.plugins.workspaces.documents.DocumentWorkspaceModule;
027import org.ametys.plugins.workspaces.indexing.solr.SolrProjectResourceIndexer;
028import org.ametys.plugins.workspaces.project.objects.Project;
029import org.ametys.web.WebConstants;
030
031/**
032 * Component for indexing {@link Project} resources.
033 * Project are indexed in default and live systematically since there is currently no workflow on project resources.
034 */
035public class SolrIndexProjectResourceObserver extends AbstractSolrIndexResourceObserver
036{
037    /** The Solr indexer for project resources */
038    protected SolrProjectResourceIndexer _solrProjectResourceIndexer;
039    
040    private Project _project;
041
042    @Override
043    public void service(ServiceManager serviceManager) throws ServiceException
044    {
045        super.service(serviceManager);
046        _solrProjectResourceIndexer = (SolrProjectResourceIndexer) serviceManager.lookup(SolrProjectResourceIndexer.ROLE);
047    }
048    
049    @Override
050    protected String[] getWorkspacesToIndex()
051    {
052        return new String[] {RepositoryConstants.DEFAULT_WORKSPACE, WebConstants.LIVE_WORKSPACE};
053    }
054    
055    @Override
056    protected boolean isHandledResource(AmetysObject object)
057    {
058        AmetysObject current = object.getParent();
059        while (current != null)
060        {
061            AmetysObject parent = current.getParent();
062            if (DocumentWorkspaceModule.WORKSPACES_DOCUMENTS_NODE_NAME.equals(current.getName()) 
063                    && parent != null 
064                    && (RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":resources").equals(parent.getName())
065                    && parent.getParent() instanceof Project)
066            {
067                _project = parent.getParent();
068                return true;
069            }
070            current = current.getParent();
071        }
072        
073        return false;
074    }
075
076    @Override
077    protected void onResourceCreated(Resource resource, String workspaceName) throws Exception
078    {
079        if (_project != null)
080        {
081            // _project must be != null because #isHandledResource is called just before
082            _solrProjectResourceIndexer.indexProjectResource(resource, _project, workspaceName, false);
083            _project = null;
084        }
085    }
086
087    @Override
088    protected void onResourceUpdated(Resource resource, String workspaceName) throws Exception
089    {
090        if (_project != null)
091        {
092            
093            // _project must be != null because #isHandledResource is called just before
094            _solrProjectResourceIndexer.indexProjectResource(resource, _project, workspaceName, false);
095            _project = null;
096        }
097    }
098
099    @Override
100    protected void onCollectionRenamedOrMoved(ResourceCollection resourceCollection, String workspaceName) throws Exception
101    {
102        if (_project != null)
103        {
104            // _project must be != null because #isHandledResource is called just before
105            _solrProjectResourceIndexer.indexProjectResources(resourceCollection, _project, workspaceName, false);
106            _project = null;
107        }
108    }
109}