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.plugins.userdirectory.observation;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020
021import org.ametys.cms.ObservationConstants;
022import org.ametys.cms.repository.Content;
023import org.ametys.core.observation.Event;
024import org.ametys.core.observation.Observer;
025import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionDAO;
026import org.ametys.plugins.userdirectory.UserDirectoryHelper;
027import org.ametys.plugins.userdirectory.UserDirectoryPageHandler;
028import org.ametys.web.repository.page.Page;
029
030/**
031 * {@link Observer} when a user content is modified, deleted or validated.
032 * Clear the cache of virtual user pages and reindex all virtual pages on modification.
033 */
034public class UserContentCacheObserver extends AbstractContentObserver
035{
036    /** The user directory page handler. */
037    protected UserDirectoryPageHandler _userPageHandler;
038    /** The DAO for synchronizable contents collections */
039    protected SynchronizableContentsCollectionDAO _synchronizableContentsCollectionDAO;
040    /** The user directory helper */
041    protected UserDirectoryHelper _userDirectoryHelper;
042    
043    @Override
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        super.service(manager);
047        _userPageHandler = (UserDirectoryPageHandler) manager.lookup(UserDirectoryPageHandler.ROLE);
048        _synchronizableContentsCollectionDAO = (SynchronizableContentsCollectionDAO) manager.lookup(SynchronizableContentsCollectionDAO.ROLE);
049        _userDirectoryHelper = (UserDirectoryHelper) manager.lookup(UserDirectoryHelper.ROLE);
050    }
051
052    @Override
053    public boolean supports(Event event)
054    {
055        return event.getId().equals(ObservationConstants.EVENT_CONTENT_DELETING)  
056                || event.getId().equals(org.ametys.web.ObservationConstants.EVENT_CONTENT_UNPUBLISHED) 
057                || event.getId().equals(ObservationConstants.EVENT_CONTENT_MODIFIED) 
058                || event.getId().equals(ObservationConstants.EVENT_CONTENT_ADDED) 
059                || event.getId().equals(ObservationConstants.EVENT_CONTENT_VALIDATED);
060    }
061
062    @Override
063    public int getPriority(Event event)
064    {
065        // Will be processed AFTER live synchronization observers and AFTER page unindexing observer
066        return MAX_PRIORITY + 5000;
067    }
068
069    @Override
070    protected void _internalObserve(Event event, Page rootUsersPage, Content userContent)
071    {
072        if (_isUserContent(userContent))
073        {
074            if (!event.getId().equals(ObservationConstants.EVENT_CONTENT_ADDED))
075            {
076                _userPageHandler.clearCache(rootUsersPage);
077                // Invalidate user page cache
078                _userDirectoryPageResolver.invalidateUserPageCache(userContent.getLanguage());
079            }
080            
081            if (!event.getId().equals(org.ametys.web.ObservationConstants.EVENT_CONTENT_UNPUBLISHED) && !event.getId().equals(ObservationConstants.EVENT_CONTENT_VALIDATED))
082            {
083                // Invalidate user content cache
084                _userDirectoryHelper.invalidateUserContentCache(userContent.getLanguage());
085            }
086        }
087    }
088}