001/*
002 *  Copyright 2010 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.plugins.userdirectory.observation;
017
018import java.util.Map;
019import java.util.Set;
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.contenttype.ContentTypesHelper;
026import org.ametys.core.observation.Event;
027import org.ametys.core.observation.Observer;
028import org.ametys.plugins.contentio.synchronize.observation.ObservationConstants;
029import org.ametys.plugins.userdirectory.UserDirectoryPageHandler;
030import org.ametys.web.indexing.solr.SolrPageIndexer;
031import org.ametys.web.repository.page.Page;
032
033/**
034 * {@link Observer} after synchronization of the user collection
035 *
036 */
037public class UserContentCollectionSynchronizedObserver implements Observer, Serviceable
038{
039    private UserDirectoryPageHandler _userPageHandler;
040    private ContentTypesHelper _contentTypeHelper;
041    private SolrPageIndexer _solrPageIndexer;
042    
043    @Override
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        _userPageHandler = (UserDirectoryPageHandler) manager.lookup(UserDirectoryPageHandler.ROLE);
047        _contentTypeHelper = (ContentTypesHelper) manager.lookup(ContentTypesHelper.ROLE);
048        _solrPageIndexer = (SolrPageIndexer) manager.lookup(SolrPageIndexer.ROLE);
049    }
050    
051    @Override
052    public boolean supports(Event event)
053    {
054        return event.getId().equals(ObservationConstants.EVENT_CONTENT_COLLECTION_SYNCHRONIZED);
055    }
056
057    public int getPriority(Event event)
058    {
059        return MAX_PRIORITY;
060    }
061
062    public void observe(Event event, Map<String, Object> transientVars) throws Exception
063    {
064        String cTypeId = (String) event.getArguments().get(ObservationConstants.ARGS_COLLECTION_CONTENT_TYPE);
065        
066        if (_contentTypeHelper.getAncestors(cTypeId).contains(UserDirectoryPageHandler.ABSTRACT_USER_CONTENT_TYPE))
067        {
068            Set<Page> rootPages = _userPageHandler.getUserDirectoryRootPages(cTypeId);
069            
070            for (Page rootPage : rootPages)
071            {
072                _userPageHandler.clearCache(rootPage);
073                _solrPageIndexer.reindexPage(rootPage.getId(), true, false);
074            }
075            
076        }
077    }
078
079}