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