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