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.indexing.solr.SolrIndexer;
026import org.ametys.cms.indexing.solr.SolrAclCacheInfluence;
027import org.ametys.cms.indexing.solr.SolrAclCacheUninfluentialObject;
028import org.ametys.core.ObservationConstants;
029import org.ametys.core.observation.AsyncObserver;
030import org.ametys.core.observation.Event;
031import org.ametys.core.right.RightManager;
032import org.ametys.runtime.plugin.component.AbstractLogEnabled;
033
034/**
035 * Reload the ACL Solr cache when a right assignment on READER profile changed.
036 * 
037 * <br>The {@link ObservationConstants#ARGS_ACL_CONTEXT context object} can be an uninfluential one
038 * on the Solr Acl cache, and avoiding this reloading is possible by marking it with the {@link SolrAclCacheUninfluentialObject} interface.
039 */
040public class ReloadSolrCacheForAclUpdatedObserver extends AbstractLogEnabled implements AsyncObserver, Serviceable
041{
042    /** The solr indexer */
043    protected SolrIndexer _solrIndexer;
044    
045    @Override
046    public void service(ServiceManager manager) throws ServiceException
047    {
048        _solrIndexer = (SolrIndexer) manager.lookup(SolrIndexer.ROLE);
049    }
050    
051    @Override
052    public boolean supports(Event event)
053    {
054        Map<String, Object> arguments = event.getArguments();
055        @SuppressWarnings("unchecked")
056        Collection<String> profileIds = (Collection<String>) arguments.get(ObservationConstants.ARGS_ACL_PROFILES);
057        
058        return ObservationConstants.EVENT_ACL_UPDATED.equals(event.getId()) 
059               && profileIds.contains(RightManager.READER_PROFILE_ID);
060    }
061    
062    @Override
063    public int getPriority(Event event)
064    {
065        return MAX_PRIORITY;
066    }
067    
068    @Override
069    public void observe(Event event, Map<String, Object> transientVars) throws Exception
070    {
071        Map<String, Object> eventArgs = event.getArguments();
072        Object aclContext = eventArgs.get(ObservationConstants.ARGS_ACL_CONTEXT);
073        Boolean solrAclCacheUninfluential = (Boolean) eventArgs.get(org.ametys.cms.ObservationConstants.ARGS_ACL_SOLR_CACHE_UNINFLUENTIAL);
074        if (solrAclCacheUninfluential == null)
075        {
076            // If not specified, let be 'false' by default
077            solrAclCacheUninfluential = false;
078        }
079        
080        getLogger().info("ReloadSolrCacheForAclUpdatedObserver called for context \"{}\" and profile \"READER\"", aclContext);
081        
082        if (solrAclCacheUninfluential)
083        {
084            getLogger().info("Acl cache will not be reloaded for \"{}\" because it was notified as uninfluential", aclContext);
085        }
086        else
087        {
088            if (SolrAclCacheInfluence.isUninfluent(aclContext))
089            {
090                getLogger().info("Acl cache will not be reloaded because \"{}\" is uninfluent on it", aclContext);
091            }
092            else
093            {
094                _solrIndexer.reloadAclCache();
095            }
096        }
097    }
098}