001/*
002 *  Copyright 2016 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.indexing.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.observation.ObserverHelper;
026import org.ametys.core.ObservationConstants;
027import org.ametys.core.observation.AsyncObserver;
028import org.ametys.core.observation.Event;
029import org.ametys.core.right.RightManager;
030import org.ametys.plugins.repository.AmetysObject;
031import org.ametys.plugins.repository.AmetysObjectResolver;
032import org.ametys.plugins.repository.AmetysRepositoryException;
033import org.ametys.plugins.repository.RepositoryConstants;
034import org.ametys.runtime.plugin.component.AbstractLogEnabled;
035import org.ametys.web.WebConstants;
036import org.ametys.web.indexing.solr.SolrPageIndexer;
037import org.ametys.web.repository.page.Page;
038import org.ametys.web.repository.sitemap.Sitemap;
039
040/**
041 * This observer reindexes the page on which the READER profile affectation has been modified.
042 */
043public class IndexPageReadAclObserver extends AbstractLogEnabled implements AsyncObserver, Serviceable
044{
045    private SolrPageIndexer _solrPageIndexer;
046    private AmetysObjectResolver _resolver;
047    @Override
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        _solrPageIndexer = (SolrPageIndexer) manager.lookup(SolrPageIndexer.ROLE);
051        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
052    }
053    
054    @Override
055    public boolean supports(Event event)
056    {
057        Map<String, Object> arguments = event.getArguments();
058        @SuppressWarnings("unchecked")
059        Collection<String> profileIds = (Collection<String>) arguments.get(ObservationConstants.ARGS_ACL_PROFILES);
060        String contextId = (String) arguments.get(ObservationConstants.ARGS_ACL_CONTEXT_IDENTIFIER);
061        
062        try
063        {
064            return ObservationConstants.EVENT_ACL_UPDATED.equals(event.getId()) 
065                    && profileIds.contains(RightManager.READER_PROFILE_ID)
066                    && contextId != null 
067                    && _resolver.hasAmetysObjectForId(contextId);
068        }
069        catch (AmetysRepositoryException e)
070        {
071            return false;
072        }
073    }
074    
075    @Override
076    public int getPriority(Event event)
077    {
078        return MIN_PRIORITY;
079    }
080    
081    @Override
082    public void observe(Event event, Map<String, Object> transientVars) throws Exception
083    {
084        if (ObserverHelper.isNotSuspendedObservationForIndexation())
085        {
086            Map<String, Object> arguments = event.getArguments();
087            String contextId = (String) arguments.get(ObservationConstants.ARGS_ACL_CONTEXT_IDENTIFIER);
088            AmetysObject context = _resolver.resolveById(contextId);
089            if (context instanceof Page)
090            {
091                Page page = (Page) context;
092                _reindexPage(page);
093            }
094            else if (context instanceof Sitemap)
095            {
096                // Reindex all the pages
097                Sitemap sitemap = (Sitemap) context;
098                for (AmetysObject child : sitemap.getChildren())
099                {
100                    if (child instanceof Page)
101                    {
102                        _reindexPage((Page) child);
103                    }
104                }
105            }
106        }
107    }
108    
109    private void _reindexPage(Page page) throws Exception
110    {
111        _solrPageIndexer.reindexPage(page.getId(), RepositoryConstants.DEFAULT_WORKSPACE, true, false, true);
112        _solrPageIndexer.reindexPage(page.getId(), WebConstants.LIVE_WORKSPACE, true, false, true);
113    }
114}