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.web.attachments;
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.web.indexing.solr.SolrPageIndexer;
027import org.ametys.web.repository.page.Page;
028
029/**
030 * Listener on page attachments events
031 */
032public class PageAttachmentsSolrObserver extends AbstractSolrIndexResourceObserver
033{
034    /** The Solr page indexer */
035    protected SolrPageIndexer _solrPageIndexer;
036    
037    private Page _page;
038
039    @Override
040    public void service(ServiceManager serviceManager) throws ServiceException
041    {
042        super.service(serviceManager);
043        _solrPageIndexer = (SolrPageIndexer) serviceManager.lookup(SolrPageIndexer.ROLE);
044    }
045    
046    @Override
047    protected String[] getWorkspacesToIndex()
048    {
049        return new String[] {RepositoryConstants.DEFAULT_WORKSPACE};
050    }
051    
052    @Override
053    protected boolean isHandledResource(AmetysObject object)
054    {
055        AmetysObject parent = object.getParent();
056        while (parent != null)
057        {
058            if (parent instanceof Page)
059            {
060                _page = (Page) parent;
061                return true;
062            }
063            parent = parent.getParent();
064        }
065        
066        return false;
067    }
068    
069    @Override
070    protected void onResourceCreated(Resource resource, String workspaceName) throws Exception
071    {
072        if (_page != null)
073        {
074            // _page must be != null because #isHandledResource is called just before
075            _solrPageIndexer.indexPageAttachment(resource, _page);
076            _page = null;
077        }
078    }
079    
080    @Override
081    protected void onResourceUpdated(Resource resource, String workspaceName) throws Exception
082    {
083        if (_page != null)
084        {
085            // _page must be != null because #isHandledResource is called just before
086            _solrPageIndexer.indexPageAttachment(resource, _page);
087            _page = null;
088        }
089    }
090    
091    @Override
092    protected void onCollectionRenamedOrMoved(ResourceCollection resourceCollection, String workspaceName) throws Exception
093    {
094        if (_page != null)
095        {
096            // _page must be != null because #isHandledResource is called just before
097            _solrPageIndexer.indexPageAttachments(resourceCollection, _page);
098            _page = null;
099        }
100    }
101}