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.indexing.solr.observation;
017
018import java.util.Collection;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024
025import org.ametys.cms.content.ContentHelper;
026import org.ametys.cms.content.indexing.solr.SolrIndexer;
027import org.ametys.cms.repository.Content;
028import org.ametys.cms.rights.SimpleContentAccessController;
029import org.ametys.core.ObservationConstants;
030import org.ametys.core.observation.AsyncObserver;
031import org.ametys.core.observation.Event;
032import org.ametys.core.right.RightManager;
033import org.ametys.plugins.repository.AmetysObjectResolver;
034import org.ametys.plugins.repository.RepositoryConstants;
035import org.ametys.plugins.repository.collection.AmetysObjectCollection;
036import org.ametys.runtime.plugin.component.AbstractLogEnabled;
037
038/**
039 * This observer reindexes all the simple content on which the READER profile affectation has been modified.
040 */
041public class IndexSimpleContentReadAclObserver extends AbstractLogEnabled implements AsyncObserver, Serviceable
042{
043    /** The solr indexer */
044    protected SolrIndexer _solrIndexer;
045    /** The ametys object resolver */
046    protected AmetysObjectResolver _resolver;
047    /** The content helper */
048    protected ContentHelper _contentHelper;
049
050    public void service(ServiceManager manager) throws ServiceException
051    {
052        _solrIndexer = (SolrIndexer) manager.lookup(SolrIndexer.ROLE);
053        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
054        _contentHelper = (ContentHelper) manager.lookup(ContentHelper.ROLE);
055    }
056    
057    @Override
058    public boolean supports(Event event)
059    {
060        Map<String, Object> arguments = event.getArguments();
061        @SuppressWarnings("unchecked")
062        Collection<String> profileIds = (Collection<String>) arguments.get(ObservationConstants.ARGS_ACL_PROFILES);
063        String contextId = (String) arguments.get(ObservationConstants.ARGS_ACL_CONTEXT_IDENTIFIER);
064        
065        return ObservationConstants.EVENT_ACL_UPDATED.equals(event.getId()) 
066                && profileIds.contains(RightManager.READER_PROFILE_ID) 
067                && SimpleContentAccessController.CONTEXT.equals(contextId);
068    }
069    
070    @Override
071    public int getPriority(Event event)
072    {
073        return MIN_PRIORITY;
074    }
075    
076    @Override
077    public void observe(Event event, Map<String, Object> transientVars) throws Exception
078    {
079        if (ObserverHelper.isNotSuspendedObservationForIndexation())
080        {
081            for (Object ametysObject : ((AmetysObjectCollection) _resolver.resolveByPath("/" + RepositoryConstants.NAMESPACE_PREFIX + ":contents")).getChildren())
082            {
083                if (ametysObject instanceof Content && _contentHelper.isSimple((Content) ametysObject))
084                {
085                    _solrIndexer.indexContent(((Content) ametysObject).getId());
086                }
087            }
088        }
089    }
090}