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.cms.content.attachments;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020
021import org.ametys.cms.content.indexing.solr.SolrIndexer;
022import org.ametys.cms.indexing.explorer.AbstractSolrIndexResourceObserver;
023import org.ametys.cms.repository.Content;
024import org.ametys.plugins.explorer.resources.Resource;
025import org.ametys.plugins.explorer.resources.ResourceCollection;
026import org.ametys.plugins.repository.AmetysObject;
027import org.ametys.plugins.repository.RepositoryConstants;
028
029/**
030 * Listener on content attachments events
031 */
032public class ContentAttachmentsSolrObserver extends AbstractSolrIndexResourceObserver
033{
034    private Content _content;
035
036    @Override
037    public void service(ServiceManager serviceManager) throws ServiceException
038    {
039        super.service(serviceManager);
040        _solrIndexer = (SolrIndexer) serviceManager.lookup(SolrIndexer.ROLE);
041    }
042    
043    @Override
044    protected String[] getWorkspacesToIndex()
045    {
046        return new String[] {RepositoryConstants.DEFAULT_WORKSPACE};
047    }
048    
049    @Override
050    protected boolean isHandledResource(AmetysObject object)
051    {
052        AmetysObject parent = object.getParent();
053        while (parent != null)
054        {
055            if (parent instanceof Content)
056            {
057                _content = (Content) parent;
058                return true;
059            }
060            parent = parent.getParent();
061        }
062        
063        return false;
064    }
065    
066    @Override
067    protected void onResourceCreated(Resource resource, String workspaceName) throws Exception
068    {
069        if (_content != null)
070        {
071            // _content must be != null because #isHandledResource is called just before
072            _solrIndexer.indexContentAttachment(resource, _content);
073            _content = null;
074        }
075    }
076    
077    @Override
078    protected void onResourceUpdated(Resource resource, String workspaceName) throws Exception
079    {
080        if (_content != null)
081        {
082            // _content must be != null because #isHandledResource is called just before
083            _solrIndexer.indexContentAttachment(resource, _content);
084            _content = null;
085        }
086    }
087    
088    @Override
089    protected void onCollectionRenamedOrMoved(ResourceCollection resourceCollection, String workspaceName) throws Exception
090    {
091        if (_content != null)
092        {
093            // _content must be != null because #isHandledResource is called just before
094            _solrIndexer.indexContentAttachments(resourceCollection, _content);
095            _content = null;
096        }
097    }
098}